Skip to content

Commit

Permalink
Make bulid-tests work with newer testing lib
Browse files Browse the repository at this point in the history
We are using stubs for tests in escript, this will be not needed
once NeoEden is merged

Signed-off-by: Pavel Abramov <uncle.decart@gmail.com>
  • Loading branch information
uncleDecart committed Oct 31, 2024
1 parent 96517df commit 7989fc7
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 72 deletions.
8 changes: 8 additions & 0 deletions .github/actions/setup-environment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
3 changes: 1 addition & 2 deletions cmd/edenTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -42,7 +41,7 @@ test <test_dir> -r <regexp> [-t <timewait>] [-v <level>]
}
}

vars, err := utils.InitVars()
vars, err := openevec.InitVarsFromConfig(cfg)

if err != nil {
return fmt.Errorf("error reading config: %s\n", err)
Expand Down
43 changes: 30 additions & 13 deletions pkg/openevec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {

Check failure on line 379 in pkg/openevec/config.go

View workflow job for this annotation

GitHub Actions / yetus

revive: exported function WriteConfig should have comment or be unexported https://revive.run/r#exported
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()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/openevec/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion pkg/openevec/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 1 addition & 30 deletions pkg/openevec/edenConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/openevec/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 26 additions & 19 deletions tests/escript/go-internal/testscript/testing_1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions tests/workflow/smoke.tests.txt
Original file line number Diff line number Diff line change
@@ -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"}}
Expand Down Expand Up @@ -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

0 comments on commit 7989fc7

Please sign in to comment.