Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add first golang written test #873

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ tests/workflow/workflow.large.txt
eve-config-dir/*

export

# vim swap files
*.swp
*.swo
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ install: build
CGO_ENABLED=0 go install .

build: $(BIN) $(EMPTY_DRIVE).raw $(EMPTY_DRIVE).qcow2 $(EMPTY_DRIVE).qcow $(EMPTY_DRIVE).vmdk $(EMPTY_DRIVE).vhdx $(LINUXKIT)
$(LOCALBIN): $(BINDIR) cmd/*.go pkg/*/*.go pkg/*/*/*.go
$(LOCALBIN): $(BINDIR) cmd/cli/*.go pkg/*/*.go pkg/*/*/*.go
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -ldflags "-s -w" -o $@ .
mkdir -p dist/scripts/shell
cp -r shell-scripts/* dist/scripts/shell/
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 0 additions & 15 deletions cmd/debug.go → cmd/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ func newDebugCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {
Use: "debug",
}

/*
if viperLoaded {
eveSSHKey = utils.ResolveAbsPath(viper.GetString("eden.ssh-key"))
extension := filepath.Ext(eveSSHKey)
eveSSHKey = strings.TrimRight(eveSSHKey, extension)
eveRemote = viper.GetBool("eve.remote")
eveRemoteAddr = viper.GetString("eve.remote-addr")
if eveRemote || eveRemoteAddr == "" {
if !cmd.Flags().Changed("eve-ssh-port") {
eveSSHPort = 22
}
}
}
*/

groups := CommandGroups{
{
Message: "Basic Commands",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 16 additions & 11 deletions cmd/edenConfig.go → cmd/cli/edenConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"

"github.com/lf-edge/eden/pkg/defaults"
"github.com/lf-edge/eden/pkg/openevec"
Expand All @@ -10,7 +11,12 @@ import (
)

func newConfigCmd(configName, verbosity *string) *cobra.Command {
cfg := &openevec.EdenSetupArgs{}
currentPath, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

cfg := openevec.GetDefaultConfig(currentPath)
var configCmd = &cobra.Command{
Use: "config",
Short: "work with config",
Expand Down Expand Up @@ -69,17 +75,16 @@ func newConfigAddCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {
fmt.Sprintf("device model (%s/%s/%s/%s)",
defaults.DefaultQemuModel, defaults.DefaultRPIModel, defaults.DefaultGCPModel, defaults.DefaultGeneralModel))
configAddCmd.Flags().StringVar(&contextFile, "file", "", "file with config to add")
//not used in function
configAddCmd.Flags().StringVarP(&cfg.Eve.QemuFileToSave, "qemu-config", "", defaults.DefaultQemuFileToSave, "file to save config")
configAddCmd.Flags().IntVarP(&cfg.Eve.QemuCpus, "cpus", "", defaults.DefaultCpus, "cpus")
configAddCmd.Flags().IntVarP(&cfg.Eve.QemuMemory, "memory", "", defaults.DefaultMemory, "memory (MB)")
configAddCmd.Flags().StringSliceVarP(&cfg.Eve.QemuFirmware, "eve-firmware", "", nil, "firmware path")
configAddCmd.Flags().StringVarP(&cfg.Eve.QemuConfigPath, "config-part", "", "", "path for config drive")
configAddCmd.Flags().StringVarP(&cfg.Eve.QemuDTBPath, "dtb-part", "", "", "path for device tree drive (for arm)")
configAddCmd.Flags().StringToStringVarP(&cfg.Eve.HostFwd, "eve-hostfwd", "", defaults.DefaultQemuHostFwd, "port forward map")
configAddCmd.Flags().StringVar(&cfg.Eve.Ssid, "ssid", "", "set ssid of wifi for rpi")
configAddCmd.Flags().StringVar(&cfg.Eve.Arch, "arch", "", "arch of EVE (amd64 or arm64)")
configAddCmd.Flags().StringVar(&cfg.Eve.ModelFile, "devmodel-file", "", "File to use for overwrite of model defaults")
configAddCmd.Flags().IntVarP(&cfg.Eve.QemuCpus, "cpus", "", cfg.Eve.QemuCpus, "cpus")
configAddCmd.Flags().IntVarP(&cfg.Eve.QemuMemory, "memory", "", cfg.Eve.QemuMemory, "memory (MB)")
configAddCmd.Flags().StringSliceVarP(&cfg.Eve.QemuFirmware, "eve-firmware", "", cfg.Eve.QemuFirmware, "firmware path")
configAddCmd.Flags().StringVarP(&cfg.Eve.QemuConfigPath, "config-part", "", cfg.Eve.QemuConfigPath, "path for config drive")
configAddCmd.Flags().StringVarP(&cfg.Eve.QemuDTBPath, "dtb-part", "", cfg.Eve.QemuDTBPath, "path for device tree drive (for arm)")
configAddCmd.Flags().StringToStringVarP(&cfg.Eve.HostFwd, "eve-hostfwd", "", cfg.Eve.HostFwd, "port forward map")
configAddCmd.Flags().StringVar(&cfg.Eve.Ssid, "ssid", cfg.Eve.Ssid, "set ssid of wifi for rpi")
configAddCmd.Flags().StringVar(&cfg.Eve.Arch, "arch", cfg.Eve.Arch, "arch of EVE (amd64 or arm64)")
configAddCmd.Flags().StringVar(&cfg.Eve.ModelFile, "devmodel-file", cfg.Eve.ModelFile, "File to use for overwrite of model defaults")
configAddCmd.Flags().BoolVarP(&force, "force", "", false, "force overwrite config file")

return configAddCmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/edenController.go → cmd/cli/edenController.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func newEdgeNodeEVEImageUpdate(controllerMode string, cfg *openevec.EdenSetupArg
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
baseOSImage := args[0]
if err := openevec.EdgeNodeEVEImageUpdate(baseOSImage, baseOSVersion, registry, controllerMode, baseOSImageActivate, baseOSVDrive); err != nil {
if err := openevec.EdgeNodeEVEImageUpdate(baseOSImage, baseOSVersion, registry, cfg.Registry.IP, controllerMode, cfg.Registry.Port, baseOSImageActivate, baseOSVDrive); err != nil {
log.Fatal(err)
}
},
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions cmd/edenPacket.go → cmd/cli/edenPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func newPacketCmd(configName, verbosity *string) *cobra.Command {
var packetProjectName string

var packetCmd = &cobra.Command{
Use: "packet",
Short: `Manage VMs in Equinix Metal Platform`,
Use: "packet",
Short: `Manage VMs in Equinix Metal Platform`,
PersistentPreRunE: preRunViperLoadFunction(cfg, configName, verbosity),
}

packetCmd.AddCommand(newPacketVMCmd(cfg, &cfg.Packet.Key, &packetProjectName))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/edenStop.go → cmd/cli/edenStop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newStopCmd(configName, verbosity *string) *cobra.Command {
adamRm, redisRm,
registryRm, eServerRm,
cfg.Eve.Remote, cfg.Eve.Pid,
swtpmPidFile(cfg), cfg.Sdn.PidFile,
openevec.SwtpmPidFile(cfg), cfg.Sdn.PidFile,
cfg.Eve.DevModel, vmName,
)
},
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 9 additions & 6 deletions cmd/edenVolume.go → cmd/cli/edenVolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import (
"github.com/spf13/cobra"
)

func newVolumeCmd() *cobra.Command {
func newVolumeCmd(configName, verbosity *string) *cobra.Command {
cfg := &openevec.EdenSetupArgs{}

var volumeCmd = &cobra.Command{
Use: "volume",
Use: "volume",
PersistentPreRunE: preRunViperLoadFunction(cfg, configName, verbosity),
}

groups := CommandGroups{
{
Message: "Basic Commands",
Commands: []*cobra.Command{
newVolumeLsCmd(),
newVolumeCreateCmd(),
newVolumeCreateCmd(cfg),
newVolumeDeleteCmd(),
newVolumeDetachCmd(),
newVolumeAttachCmd(),
Expand All @@ -44,7 +47,7 @@ func newVolumeLsCmd() *cobra.Command {
return volumeLsCmd
}

func newVolumeCreateCmd() *cobra.Command {
func newVolumeCreateCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {
var registry, diskSize, volumeName, volumeType, datastoreOverride string
var sftpLoad, directLoad bool

Expand All @@ -55,8 +58,8 @@ func newVolumeCreateCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
appLink := args[0]
err := openevec.VolumeCreate(appLink, registry, diskSize, volumeName,
volumeType, datastoreOverride, sftpLoad, directLoad)
err := openevec.VolumeCreate(appLink, registry, cfg.Registry.IP, diskSize, volumeName,
volumeType, datastoreOverride, cfg.Registry.Port, sftpLoad, directLoad)
if err != nil {
log.Fatal(err)
}
Expand Down
File renamed without changes.
9 changes: 0 additions & 9 deletions cmd/eve.go → cmd/cli/eve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ func newEveCmd(configName, verbosity *string) *cobra.Command {
return eveCmd
}

func swtpmPidFile(cfg *openevec.EdenSetupArgs) string {
if cfg.Eve.TPM {
command := "swtpm"
return filepath.Join(filepath.Join(filepath.Dir(cfg.Eve.ImageFile), command),
fmt.Sprintf("%s.pid", command))
}
return ""
}

func newStartEveCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {
var vmName, tapInterface string

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/redis.go → cmd/cli/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newStartRedisCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {
log.Fatalf("cannot obtain executable path: %s", err)
}
log.Infof("Executable path: %s", command)
if err := eden.StartRedis(cfg.Redis.Port, cfg.Redis.Dist, cfg.Redis.Force, cfg.Redis.Tag); err != nil {
if err := openevec.StartRedis(*cfg); err != nil {
log.Errorf("cannot start redis: %s", err)
} else {
log.Infof("Redis is running and accessible on port %d", cfg.Redis.Port)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions cmd/root.go → cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewEdenCommand() *cobra.Command {
newUtilsCmd(&configName, &verbosity),
newControllerCmd(&configName, &verbosity),
newNetworkCmd(),
newVolumeCmd(),
newVolumeCmd(&configName, &verbosity),
newDisksCmd(),
newPacketCmd(&configName, &verbosity),
newRolCmd(&configName, &verbosity),
Expand All @@ -74,7 +74,7 @@ func preRunViperLoadFunction(cfg *openevec.EdenSetupArgs, configName, verbosity
if err != nil {
return err
}
openevec.Merge(reflect.ValueOf(viperCfg).Elem(), reflect.ValueOf(*cfg), cmd.Flags())
openevec.MergeFlagSet(reflect.ValueOf(viperCfg).Elem(), reflect.ValueOf(*cfg), cmd.Flags())
*cfg = *viperCfg
return nil
}
Expand Down
File renamed without changes.
82 changes: 82 additions & 0 deletions cmd/patchwork/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package patchwork

import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/lf-edge/eden/pkg/defaults"
"github.com/lf-edge/eden/pkg/eden"
ec "github.com/lf-edge/eden/pkg/openevec"
"github.com/lf-edge/eden/pkg/utils"
)

func setup(cfg *ec.EdenSetupArgs) error {

edenDir, err := utils.DefaultEdenDir()
if err != nil {
return err
}

// We need eden dir since we are storing certificates there
if _, err := os.Stat(edenDir); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(edenDir, os.ModePerm)
if err != nil {
return err
}
}

err = ec.SetupEden(cfg.ConfigName,
filepath.Join(cfg.Eden.Root, "eve-config-dir"),
"", "", "", []string{}, false, false, *cfg)

if err != nil {
return err
}

if err = ec.StartEden(cfg, defaults.DefaultVBoxVMName, "", ""); err != nil {
return err
}

if err = ec.OnboardEve(cfg.Eve.CertsUUID); err != nil {
return err
}
return nil
}

func teardown(cfg *ec.EdenSetupArgs) error {
eden.StopEden(false, false, false, false, cfg.Eve.Remote, cfg.Eve.Pid, ec.SwtpmPidFile(cfg), cfg.Sdn.PidFile, cfg.Eve.DevModel, defaults.DefaultVBoxVMName)

configDist, err := utils.DefaultEdenDir()
if err != nil {
return err
}

err = ec.EdenClean(*cfg, cfg.ConfigName, configDist, defaults.DefaultVBoxVMName, false)
if err != nil {
return err
}
return nil
}

func TestMain(m *testing.M) {
curPath, err := os.Getwd()
if err != nil {
// Getwd failed
os.Exit(1)
}
cfg := ec.GetDefaultConfig(curPath)

if err = setup(cfg); err != nil {
fmt.Printf("Error setup %v", err)
os.Exit(1)
}
code := m.Run()
if err = teardown(cfg); err != nil {
fmt.Printf("Error setup %v", err)
os.Exit(1)
}
os.Exit(code)
}
Loading