From 7989fc7de34562609fb2634d429566d618a5546a Mon Sep 17 00:00:00 2001 From: Pavel Abramov Date: Thu, 24 Oct 2024 15:42:56 +0200 Subject: [PATCH] Make bulid-tests work with newer testing lib We are using stubs for tests in escript, this will be not needed once NeoEden is merged Signed-off-by: Pavel Abramov --- .github/actions/setup-environment/action.yml | 8 ++++ cmd/edenTest.go | 3 +- pkg/openevec/config.go | 43 ++++++++++++------ pkg/openevec/config_test.go | 4 +- pkg/openevec/defaults.go | 8 +++- pkg/openevec/edenConfig.go | 31 +------------ pkg/openevec/test.go | 2 +- .../go-internal/testscript/testing_1.18.go | 45 +++++++++++-------- tests/workflow/smoke.tests.txt | 5 +-- 9 files changed, 77 insertions(+), 72 deletions(-) diff --git a/.github/actions/setup-environment/action.yml b/.github/actions/setup-environment/action.yml index f89d27874..226245176 100644 --- a/.github/actions/setup-environment/action.yml +++ b/.github/actions/setup-environment/action.yml @@ -60,6 +60,7 @@ runs: else ./eden config set default --key=eve.accel --value=false fi + ./dist/bin/eden+ports.sh 2223:2223 2224:2224 5912:5902 5911:5901 8027:8027 8028:8028 8029:8029 8030:8030 8031:8031 ./eden config set default --key=eve.tpm --value=${{ inputs.tpm_enabled }} ./eden config set default --key=eve.cpu --value=2 shell: bash @@ -105,3 +106,10 @@ runs: ./eden setup -v debug --grub-options='set_global dom0_extra_args "$dom0_extra_args eve_install_zfs_with_raid_level "' shell: bash working-directory: "./eden" + + - name: Start and Onboard + run: | + ./eden start -v debug + ./eden eve onboard -v debug + shell: bash + working-directory: "./eden" diff --git a/cmd/edenTest.go b/cmd/edenTest.go index bcf94d154..172910de8 100644 --- a/cmd/edenTest.go +++ b/cmd/edenTest.go @@ -6,7 +6,6 @@ import ( "github.com/lf-edge/eden/pkg/defaults" "github.com/lf-edge/eden/pkg/openevec" - "github.com/lf-edge/eden/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -42,7 +41,7 @@ test -r [-t ] [-v ] } } - vars, err := utils.InitVars() + vars, err := openevec.InitVarsFromConfig(cfg) if err != nil { return fmt.Errorf("error reading config: %s\n", err) diff --git a/pkg/openevec/config.go b/pkg/openevec/config.go index 5fe0a9134..0c030bdd8 100644 --- a/pkg/openevec/config.go +++ b/pkg/openevec/config.go @@ -44,7 +44,7 @@ type EdenConfig struct { EdenBin string `mapstructure:"eden-bin"` TestBin string `mapstructure:"test-bin"` TestScenario string `mapstructure:"test-scenario"` - Tests string `mapstructure:"tests" resolvepath:""` + Tests string `mapstructure:"tests"` EServer EServerConfig `mapstructure:"eserver"` @@ -134,8 +134,8 @@ type EveConfig struct { Remote bool `mapstructure:"remote"` RemoteAddr string `mapstructure:"remote-addr"` ModelFile string `mapstructure:"devmodelfile" cobraflag:"devmodel-file"` - Cert string `mapstructure:"cert"` - DeviceCert string `mapstructure:"device-cert"` + Cert string `mapstructure:"cert" resolvepath:""` + DeviceCert string `mapstructure:"device-cert" resolvepath:""` Name string `mapstructure:"name"` AdamLogLevel string `mapstructure:"adam-log-level"` LogLevel string `mapstructure:"log-level"` @@ -376,35 +376,52 @@ func getValStrRepr(v reflect.Value) string { } } -func WriteConfig(dst reflect.Value, writer io.Writer, nestLevel int) { +func WriteConfig(dst reflect.Value, root string, writer io.Writer, nestLevel int) { + if dst.Kind() == reflect.Ptr { + dst = dst.Elem() + } + for i := 0; i < dst.NumField(); i++ { if structTag := dst.Type().Field(i).Tag.Get("mapstructure"); structTag != "" { io.WriteString(writer, strings.Repeat(" ", nestLevel)) - switch dst.Field(i).Kind() { + f := dst.Field(i) + fieldType := dst.Type().Field(i) + + switch f.Kind() { case reflect.Struct: io.WriteString(writer, structTag+":\n") - WriteConfig(dst.Field(i), writer, nestLevel+1) + // Pass the addressable value of the struct if it can be set, else create a pointer and pass + if f.CanAddr() { + WriteConfig(f.Addr(), root, writer, nestLevel+1) + } else { + WriteConfig(f, root, writer, nestLevel+1) + } case reflect.Map: io.WriteString(writer, structTag+":\n") - iter := dst.Field(i).MapRange() + iter := f.MapRange() for iter.Next() { k := iter.Key() v := iter.Value() io.WriteString(writer, strings.Repeat(" ", nestLevel+1)) - // We assume that map cannot have structure as value io.WriteString(writer, fmt.Sprintf("%v: %s\n", k.Interface(), getValStrRepr(v))) } case reflect.Slice: io.WriteString(writer, structTag+":\n") - for j := 0; j < dst.Field(i).Len(); j++ { + for j := 0; j < f.Len(); j++ { io.WriteString(writer, strings.Repeat(" ", nestLevel+1)) - elem := dst.Field(i).Index(j) + elem := f.Index(j) io.WriteString(writer, fmt.Sprintf("- %v\n", getValStrRepr(elem))) } - case reflect.String: // we need to wrap string in quotes - io.WriteString(writer, fmt.Sprintf("%s: '%v'\n", structTag, dst.Field(i))) + case reflect.String: + if _, ok := fieldType.Tag.Lookup("resolvepath"); ok { + val := f.String() + // Check if field is addressable and can be set + trimmed := strings.TrimPrefix(val, root+"/") + f.SetString(trimmed) // Update the field value + } + io.WriteString(writer, fmt.Sprintf("%s: '%v'\n", structTag, f.Interface())) default: - io.WriteString(writer, fmt.Sprintf("%s: %v\n", structTag, dst.Field(i))) + io.WriteString(writer, fmt.Sprintf("%s: %v\n", structTag, f.Interface())) } } } diff --git a/pkg/openevec/config_test.go b/pkg/openevec/config_test.go index 16baa76bc..315ecddce 100644 --- a/pkg/openevec/config_test.go +++ b/pkg/openevec/config_test.go @@ -54,7 +54,7 @@ func TestViperSerializeFromWriteConfig(t *testing.T) { } var buf bytes.Buffer - openevec.WriteConfig(reflect.ValueOf(cfg), &buf, 0) + openevec.WriteConfig(reflect.ValueOf(cfg), "", &buf, 0) v := viper.New() v.SetConfigType("yaml") @@ -80,7 +80,7 @@ func TestConfigSliceType(t *testing.T) { } var buf bytes.Buffer - openevec.WriteConfig(reflect.ValueOf(cfg), &buf, 0) + openevec.WriteConfig(reflect.ValueOf(cfg), "", &buf, 0) v := viper.New() v.SetConfigType("yaml") diff --git a/pkg/openevec/defaults.go b/pkg/openevec/defaults.go index 063b1138c..70d6aea2b 100644 --- a/pkg/openevec/defaults.go +++ b/pkg/openevec/defaults.go @@ -119,10 +119,16 @@ func GetDefaultConfig(currentPath string) *EdenSetupArgs { UefiTag: defaults.DefaultEVETag, HostFwd: map[string]string{ strconv.Itoa(defaults.DefaultSSHPort): "22", + "2223": "2223", + "2224": "2224", "5911": "5901", "5912": "5902", "8027": "8027", - "8028": "8028"}, + "8028": "8028", + "8029": "8029", + "8030": "8030", + "8031": "8031", + }, QemuFileToSave: filepath.Join(edenDir, fmt.Sprintf("%s-%s", defaults.DefaultContext, defaults.DefaultQemuFileToSave)), QemuCpus: defaults.DefaultCpus, QemuMemory: defaults.DefaultMemory, diff --git a/pkg/openevec/edenConfig.go b/pkg/openevec/edenConfig.go index 35bb49b73..444be6f40 100644 --- a/pkg/openevec/edenConfig.go +++ b/pkg/openevec/edenConfig.go @@ -76,26 +76,6 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo log.Debugf("current config already exists: %s", cfg.ConfigFile) } } - // if _, err = os.Stat(cfg.ConfigFile); os.IsNotExist(err) { - - // dir := filepath.Dir(cfg.ConfigFile) - // if err = os.MkdirAll(dir, os.ModePerm); err != nil { - // return fmt.Errorf("Error creating folders %v", err) - // } - - // file, err := os.Create(cfg.ConfigFile) - // if err != nil { - // return fmt.Errorf("Error creating file 111 %v", err) - // } - // defer file.Close() - - // WriteConfig(reflect.ValueOf(*cfg), file, 0) - - // log.Infof("Config file generated: %s", cfg.ConfigFile) - // } - // if err := ReloadConfigDetails(cfg); err != nil { - // return err - // } context, err := utils.ContextLoad() if err != nil { @@ -124,12 +104,6 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo } } context.SetContext(context.Current) - // if err := ReloadConfigDetails(cfg); err != nil { - // return err - // } - - // we prepare viper config here from EdenSetupArgs - // to feed into GenerateConfigFileFromViper if cfg.Eve.Arch != "" { viper.Set("eve.arch", cfg.Eve.Arch) @@ -161,11 +135,8 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo } defer file.Close() - WriteConfig(reflect.ValueOf(*cfg), file, 0) + WriteConfig(reflect.ValueOf(cfg), cfg.Eden.Root, file, 0) - // if err = utils.GenerateConfigFileFromViper(); err != nil { - // return fmt.Errorf("error writing config: %w", err) - // } context.SetContext(currentContextName) return nil diff --git a/pkg/openevec/test.go b/pkg/openevec/test.go index 224dc7a09..5ab979c21 100644 --- a/pkg/openevec/test.go +++ b/pkg/openevec/test.go @@ -95,7 +95,7 @@ func InitVarsFromConfig(cfg *EdenSetupArgs) (*utils.ConfigVars, error) { } func Test(tstCfg *TestArgs) error { - + fmt.Println("SOME TEST") switch { case tstCfg.TestList != "": tests.RunTest(tstCfg.TestProg, []string{"-test.list", tstCfg.TestList}, "", tstCfg.TestTimeout, tstCfg.FailScenario, tstCfg.ConfigFile, tstCfg.Verbosity) diff --git a/tests/escript/go-internal/testscript/testing_1.18.go b/tests/escript/go-internal/testscript/testing_1.18.go index f9d9a4a07..29a6214e9 100644 --- a/tests/escript/go-internal/testscript/testing_1.18.go +++ b/tests/escript/go-internal/testscript/testing_1.18.go @@ -24,10 +24,31 @@ type corpusEntry = struct { IsSeed bool } -func (d nopTestDeps) CoordinateFuzzing(_ time.Duration, _ int64, _ time.Duration, _ int64, _ int, _ []corpusEntry, _ []reflect.Type, _ string, _ string) error { +func (nopTestDeps) SetPanicOnExit0(_ bool) {} + +func (nopTestDeps) MatchString(_, _ string) (result bool, err error) { + return false, nil +} + +func (nopTestDeps) StartCPUProfile(_ io.Writer) error { return nil } +func (nopTestDeps) StopCPUProfile() {} + +func (nopTestDeps) StartTestLog(_ io.Writer) {} + +func (nopTestDeps) StopTestLog() error { + return nil +} + +func (nopTestDeps) WriteProfileTo(_ string, _ io.Writer, _ int) error { + return nil +} + +func (d nopTestDeps) CoordinateFuzzing(_ time.Duration, _ int64, _ time.Duration, _ int64, _ int, _ []corpusEntry, _ []reflect.Type, _ string, _ string) error { + return nil +} func (d nopTestDeps) RunFuzzWorker(_ func(corpusEntry) error) error { return nil } @@ -48,29 +69,15 @@ func (d nopTestDeps) SnapshotCoverage() { return } -func (nopTestDeps) SetPanicOnExit0(_ bool) {} - -func (nopTestDeps) MatchString(_, _ string) (result bool, err error) { - return false, nil -} - -func (nopTestDeps) StartCPUProfile(_ io.Writer) error { - return nil +func (d nopTestDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) { + tmp := func(_, _ string) (string, error) { return "", nil } + snapc := func() float64 { return 0 } + return "", tmp, snapc } -func (nopTestDeps) StopCPUProfile() {} - -func (nopTestDeps) WriteProfileTo(_ string, _ io.Writer, _ int) error { - return nil -} func (nopTestDeps) ImportPath() string { return "" } -func (nopTestDeps) StartTestLog(_ io.Writer) {} - -func (nopTestDeps) StopTestLog() error { - return nil -} func getTestingMain() *testing.M { return testing.MainStart(nopTestDeps{}, nil, nil, nil, nil) diff --git a/tests/workflow/smoke.tests.txt b/tests/workflow/smoke.tests.txt index 4e7790b10..a779af763 100644 --- a/tests/workflow/smoke.tests.txt +++ b/tests/workflow/smoke.tests.txt @@ -1,5 +1,5 @@ # Number of tests -{{$tests := 23}} +{{$tests := 22}} # EDEN_TEST_SETUP env. var. -- "y"(default) performs the EDEN setup steps {{$setup := "y"}} {{$setup_env := EdenGetEnv "EDEN_TEST_SETUP"}} @@ -78,6 +78,3 @@ eden.escript.test -testdata ../eclient/testdata/ -test.run TestEdenScripts/shutd /bin/echo EVE reset (22/{{$tests}}) eden.escript.test -test.run TestEdenScripts/eden_reset - -/bin/echo EVE security tests (23/{{$tests}}) -eden.escript.test -test.run TestEdenScripts/sec_eden