Skip to content

Commit

Permalink
oasis-test-runner: move TestInstanceInfo to Env
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Feb 28, 2020
1 parent ef9e4f4 commit e974566
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go/oasis-net-runner/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func runRoot(cmd *cobra.Command, args []string) error {
defer rootEnv.Cleanup()
logger := logging.GetLogger("net-runner")

childEnv, err := rootEnv.NewChild("net-runner")
childEnv, err := rootEnv.NewChild("net-runner", env.TestInstanceInfo{})
if err != nil {
logger.Error("failed to setup child environment",
"err", err,
Expand Down
30 changes: 7 additions & 23 deletions go/oasis-test-runner/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -200,25 +198,6 @@ func computeParamSets(zp map[string][]string, ps map[string]string) []map[string
return rps
}

// writeParamSetToFile dumps test instance parameter set to test_instance_info.json file for debugging afterwards.
func writeTestInstanceInfo(dirName string, s scenario.Scenario, run int) error {
testInfo := scenario.TestInstanceInfo{
Name: s.Name(),
ParameterSet: scenario.ParametersToStringMap(s.Parameters()),
Run: run,
}

b, err := json.Marshal(testInfo)
if err != nil {
return err
}
if err = ioutil.WriteFile(dirName+"/test_instance_info.json", b, 0644); err != nil {
return err
}

return nil
}

// Register adds a scenario to the runner and the default scenarios list.
func Register(scenario scenario.Scenario) error {
if err := RegisterNondefault(scenario); err != nil {
Expand Down Expand Up @@ -347,7 +326,12 @@ func runRoot(cmd *cobra.Command, args []string) error {
"test", n,
)

childEnv, err := rootEnv.NewChild(n)
childEnv, err := rootEnv.NewChild(n, env.TestInstanceInfo{
Name: v.Name(),
Instance: filepath.Base(rootEnv.Dir()),
ParameterSet: scenario.ParametersToStringMap(v.Parameters()),
Run: run,
})
if err != nil {
logger.Error("failed to setup child environment",
"err", err,
Expand All @@ -357,7 +341,7 @@ func runRoot(cmd *cobra.Command, args []string) error {
}

// Dump current parameter set to file.
if err = writeTestInstanceInfo(childEnv.Dir(), v, run); err != nil {
if err = childEnv.WriteTestInstanceInfo(); err != nil {
return err
}

Expand Down
39 changes: 38 additions & 1 deletion go/oasis-test-runner/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package env

import (
"container/list"
"encoding/json"
"errors"
"io/ioutil"
"os/exec"
"sync"
"syscall"
Expand All @@ -17,6 +19,21 @@ var ErrEarlyTerm = errors.New("env: sub-process exited early")
// CleanupFn is the cleanup hook function prototype.
type CleanupFn func()

// TestInstanceInfo contains information of the current test run.
type TestInstanceInfo struct {
// Name is the name of the test.
Name string `json:"name"`

// Instance is the instance name of the test. e.g. oasis-test-runner123456
Instance string `json:"instance"`

// ParameterSet is the paramater set the test was run with.
ParameterSet map[string]string `json:"parameter_set"`

// Run is the number of run.
Run int `json:"run"`
}

// Env is a (nested) test environment.
type Env struct {
name string
Expand All @@ -26,6 +43,7 @@ type Env struct {
children *list.List

dir *Dir
testInfo TestInstanceInfo
cleanupFns []CleanupFn
cleanupCmds []*cmdMonitor
cleanupLock sync.Mutex
Expand Down Expand Up @@ -53,6 +71,11 @@ func (env *Env) NewSubDir(subDirName string) (*Dir, error) {
return env.dir.NewSubDir(subDirName)
}

// TestInfo returns the test instance information.
func (env *Env) TestInfo() TestInstanceInfo {
return env.testInfo
}

// AddOnCleanup adds a cleanup routine to be called durring the environment's
// cleanup. Routines will be called in reverse order that they were
// registered.
Expand Down Expand Up @@ -130,7 +153,7 @@ func (env *Env) Cleanup() {
}

// NewChild returns a new child test environment.
func (env *Env) NewChild(childName string) (*Env, error) {
func (env *Env) NewChild(childName string, testInfo TestInstanceInfo) (*Env, error) {
var parentDir *Dir
if env.parent != nil {
parentDir = env.parent.dir
Expand All @@ -148,12 +171,26 @@ func (env *Env) NewChild(childName string) (*Env, error) {
parent: env,
children: list.New(),
dir: subDir,
testInfo: testInfo,
}
child.parentElem = env.children.PushBack(child)

return child, nil
}

// WriteParamSetToFile dumps test instance parameter set to test_instance_info.json file for debugging afterwards.
func (env *Env) WriteTestInstanceInfo() error {
b, err := json.Marshal(env.testInfo)
if err != nil {
return err
}
if err = ioutil.WriteFile(env.Dir()+"/test_instance_info.json", b, 0644); err != nil {
return err
}

return nil
}

// New creates a new root test environment.
func New(dir *Dir) *Env {
return &Env{
Expand Down
10 changes: 5 additions & 5 deletions go/oasis-test-runner/oasis/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,16 @@ func (args *argBuilder) appendNodeMetrics(node *Node) *argBuilder {
"--" + metrics.CfgMetricsAddr, viper.GetString(metrics.CfgMetricsAddr),
"--" + metrics.CfgMetricsPushInterval, viper.GetString(metrics.CfgMetricsPushInterval),
// TODO Matevz: Should job_name also be moved as a simple label?
"--" + metrics.CfgMetricsPushJobName, filepath.Base(node.dir.String())}...)
"--" + metrics.CfgMetricsPushJobName, node.Name}...)

// Append labels.
args.vec = append(args.vec, "--"+metrics.CfgMetricsPushLabels)

// TODO Matevz: Populate with test-parameters, startdate, git_branch?
dirs := strings.Split(node.dir.String(), "/")
l := []string{"instance=" + dirs[len(dirs)-4],
"run=" + dirs[len(dirs)-3],
"test=" + dirs[len(dirs)-4], // TODO: What if test name contains / like "gas-fees/runtimes"?
ti := node.net.env.TestInfo()
l := []string{"instance=" + ti.Instance,
"run=" + strconv.Itoa(ti.Run),
"test=" + ti.Name, // TODO: Escape slashes, for e.g. "gas-fees/runtimes".
"software_version=" + version.SoftwareVersion}

args.vec = append(args.vec, strings.Join(l, ","))
Expand Down
12 changes: 0 additions & 12 deletions go/oasis-test-runner/scenario/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import (
"github.com/oasislabs/oasis-core/go/oasis-test-runner/oasis"
)

// TestInstanceInfo contains information of the current test run.
type TestInstanceInfo struct {
// Name is the name of the test.
Name string `json:"name"`

// ParameterSet is the paramater set the test was run with.
ParameterSet map[string]string `json:"parameter_set"`

// Run is the number of run.
Run int `json:"run"`
}

// ParametersToStringMap convert scenario-specific key->value parameter set to string->string map.
func ParametersToStringMap(p map[string]interface{}) map[string]string {
sMap := make(map[string]string)
Expand Down

0 comments on commit e974566

Please sign in to comment.