From ce790e61b1a0a983a1900b76b1ad9864d15b1496 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 21 Jul 2022 11:36:32 +0200 Subject: enable linter for pkg/machine/e2e Rename all files to _test.go and rename the package to e2e_test. This makes the linter less strict about things like dot imports. Add some unused nolint directives to silence some warnings, these can be used to find untested options so someone could add tests for them. Fixes #14996 Signed-off-by: Paul Holzinger --- pkg/machine/e2e/basic_test.go | 4 +- pkg/machine/e2e/config.go | 183 --------------------------------- pkg/machine/e2e/config_basic.go | 19 ---- pkg/machine/e2e/config_basic_test.go | 18 ++++ pkg/machine/e2e/config_info.go | 20 ---- pkg/machine/e2e/config_info_test.go | 20 ++++ pkg/machine/e2e/config_init.go | 102 ------------------ pkg/machine/e2e/config_init_test.go | 102 ++++++++++++++++++ pkg/machine/e2e/config_inspect.go | 24 ----- pkg/machine/e2e/config_inspect_test.go | 24 +++++ pkg/machine/e2e/config_list.go | 45 -------- pkg/machine/e2e/config_list_test.go | 45 ++++++++ pkg/machine/e2e/config_rm.go | 56 ---------- pkg/machine/e2e/config_rm_test.go | 56 ++++++++++ pkg/machine/e2e/config_set.go | 43 -------- pkg/machine/e2e/config_set_test.go | 43 ++++++++ pkg/machine/e2e/config_ssh.go | 33 ------ pkg/machine/e2e/config_ssh_test.go | 31 ++++++ pkg/machine/e2e/config_start.go | 16 --- pkg/machine/e2e/config_start_test.go | 15 +++ pkg/machine/e2e/config_stop.go | 16 --- pkg/machine/e2e/config_stop_test.go | 15 +++ pkg/machine/e2e/config_test.go | 175 +++++++++++++++++++++++++++++++ pkg/machine/e2e/info_test.go | 2 +- pkg/machine/e2e/init_test.go | 6 +- pkg/machine/e2e/inspect_test.go | 8 +- pkg/machine/e2e/list_test.go | 8 +- pkg/machine/e2e/machine_test.go | 2 +- pkg/machine/e2e/rm_test.go | 2 +- pkg/machine/e2e/set_test.go | 6 +- pkg/machine/e2e/ssh_test.go | 12 +-- pkg/machine/e2e/start_test.go | 2 +- pkg/machine/e2e/stop_test.go | 2 +- 33 files changed, 571 insertions(+), 584 deletions(-) delete mode 100644 pkg/machine/e2e/config.go delete mode 100644 pkg/machine/e2e/config_basic.go create mode 100644 pkg/machine/e2e/config_basic_test.go delete mode 100644 pkg/machine/e2e/config_info.go create mode 100644 pkg/machine/e2e/config_info_test.go delete mode 100644 pkg/machine/e2e/config_init.go create mode 100644 pkg/machine/e2e/config_init_test.go delete mode 100644 pkg/machine/e2e/config_inspect.go create mode 100644 pkg/machine/e2e/config_inspect_test.go delete mode 100644 pkg/machine/e2e/config_list.go create mode 100644 pkg/machine/e2e/config_list_test.go delete mode 100644 pkg/machine/e2e/config_rm.go create mode 100644 pkg/machine/e2e/config_rm_test.go delete mode 100644 pkg/machine/e2e/config_set.go create mode 100644 pkg/machine/e2e/config_set_test.go delete mode 100644 pkg/machine/e2e/config_ssh.go create mode 100644 pkg/machine/e2e/config_ssh_test.go delete mode 100644 pkg/machine/e2e/config_start.go create mode 100644 pkg/machine/e2e/config_start_test.go delete mode 100644 pkg/machine/e2e/config_stop.go create mode 100644 pkg/machine/e2e/config_stop_test.go create mode 100644 pkg/machine/e2e/config_test.go (limited to 'pkg/machine') diff --git a/pkg/machine/e2e/basic_test.go b/pkg/machine/e2e/basic_test.go index f67fb4c67..da0310485 100644 --- a/pkg/machine/e2e/basic_test.go +++ b/pkg/machine/e2e/basic_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( . "github.com/onsi/ginkgo" @@ -20,7 +20,7 @@ var _ = Describe("run basic podman commands", func() { }) It("Basic ops", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).To(BeNil()) diff --git a/pkg/machine/e2e/config.go b/pkg/machine/e2e/config.go deleted file mode 100644 index b3fe74b0c..000000000 --- a/pkg/machine/e2e/config.go +++ /dev/null @@ -1,183 +0,0 @@ -package e2e - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" - "time" - - "github.com/containers/podman/v4/pkg/machine" - "github.com/containers/podman/v4/pkg/machine/qemu" - "github.com/containers/podman/v4/pkg/util" - "github.com/containers/storage/pkg/stringid" - . "github.com/onsi/ginkgo" //nolint:golint,stylecheck - . "github.com/onsi/gomega" - "github.com/onsi/gomega/gexec" - . "github.com/onsi/gomega/gexec" //nolint:golint,stylecheck -) - -var originalHomeDir = os.Getenv("HOME") - -const ( - defaultTimeout time.Duration = 90 * time.Second -) - -type machineCommand interface { - buildCmd(m *machineTestBuilder) []string -} - -type MachineTestBuilder interface { - setName(string) *MachineTestBuilder - setCmd(mc machineCommand) *MachineTestBuilder - setTimeout(duration time.Duration) *MachineTestBuilder - run() (*machineSession, error) -} -type machineSession struct { - *gexec.Session -} - -type machineTestBuilder struct { - cmd []string - imagePath string - name string - names []string - podmanBinary string - timeout time.Duration -} -type qemuMachineInspectInfo struct { - State machine.Status - VM qemu.MachineVM -} - -// waitWithTimeout waits for a command to complete for a given -// number of seconds -func (ms *machineSession) waitWithTimeout(timeout time.Duration) { - Eventually(ms, timeout).Should(Exit()) - os.Stdout.Sync() - os.Stderr.Sync() -} - -func (ms *machineSession) Bytes() []byte { - return []byte(ms.outputToString()) -} - -func (ms *machineSession) outputToStringSlice() []string { - var results []string - output := string(ms.Out.Contents()) - for _, line := range strings.Split(output, "\n") { - if line != "" { - results = append(results, line) - } - } - return results -} - -// outputToString returns the output from a session in string form -func (ms *machineSession) outputToString() string { - if ms == nil || ms.Out == nil || ms.Out.Contents() == nil { - return "" - } - - fields := strings.Fields(string(ms.Out.Contents())) - return strings.Join(fields, " ") -} - -// errorToString returns the error output from a session in string form -func (ms *machineSession) errorToString() string { - if ms == nil || ms.Err == nil || ms.Err.Contents() == nil { - return "" - } - return string(ms.Err.Contents()) -} - -// newMB constructor for machine test builders -func newMB() (*machineTestBuilder, error) { - mb := machineTestBuilder{ - timeout: defaultTimeout, - } - cwd, err := os.Getwd() - if err != nil { - return nil, err - } - mb.podmanBinary = filepath.Join(cwd, "../../../bin/podman-remote") - if os.Getenv("PODMAN_BINARY") != "" { - mb.podmanBinary = os.Getenv("PODMAN_BINARY") - } - return &mb, nil -} - -// setName sets the name of the virtuaql machine for the command -func (m *machineTestBuilder) setName(name string) *machineTestBuilder { - m.name = name - return m -} - -// setCmd takes a machineCommand struct and assembles a cmd line -// representation of the podman machine command -func (m *machineTestBuilder) setCmd(mc machineCommand) *machineTestBuilder { - // If no name for the machine exists, we set a random name. - if !util.StringInSlice(m.name, m.names) { - if len(m.name) < 1 { - m.name = randomString(12) - } - m.names = append(m.names, m.name) - } - m.cmd = mc.buildCmd(m) - return m -} - -func (m *machineTestBuilder) setTimeout(timeout time.Duration) *machineTestBuilder { - m.timeout = timeout - return m -} - -// toQemuInspectInfo is only for inspecting qemu machines. Other providers will need -// to make their own. -func (mb *machineTestBuilder) toQemuInspectInfo() ([]machine.InspectInfo, int, error) { - args := []string{"machine", "inspect"} - args = append(args, mb.names...) - session, err := runWrapper(mb.podmanBinary, args, defaultTimeout, true) - if err != nil { - return nil, -1, err - } - mii := []machine.InspectInfo{} - err = json.Unmarshal(session.Bytes(), &mii) - return mii, session.ExitCode(), err -} - -func (m *machineTestBuilder) runWithoutWait() (*machineSession, error) { - return runWrapper(m.podmanBinary, m.cmd, m.timeout, false) -} - -func (m *machineTestBuilder) run() (*machineSession, error) { - return runWrapper(m.podmanBinary, m.cmd, m.timeout, true) -} - -func runWrapper(podmanBinary string, cmdArgs []string, timeout time.Duration, wait bool) (*machineSession, error) { - if len(os.Getenv("DEBUG")) > 0 { - cmdArgs = append([]string{"--log-level=debug"}, cmdArgs...) - } - fmt.Println(podmanBinary + " " + strings.Join(cmdArgs, " ")) - c := exec.Command(podmanBinary, cmdArgs...) - session, err := Start(c, GinkgoWriter, GinkgoWriter) - if err != nil { - Fail(fmt.Sprintf("Unable to start session: %q", err)) - return nil, err - } - ms := machineSession{session} - if wait { - ms.waitWithTimeout(timeout) - fmt.Println("output:", ms.outputToString()) - } - return &ms, nil -} - -func (m *machineTestBuilder) init() {} - -// randomString returns a string of given length composed of random characters -func randomString(n int) string { - return stringid.GenerateRandomID()[0:12] -} diff --git a/pkg/machine/e2e/config_basic.go b/pkg/machine/e2e/config_basic.go deleted file mode 100644 index be0896156..000000000 --- a/pkg/machine/e2e/config_basic.go +++ /dev/null @@ -1,19 +0,0 @@ -package e2e - -type basicMachine struct { - args []string - cmd []string -} - -func (s basicMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"-r"} - if len(s.args) > 0 { - cmd = append(cmd, s.args...) - } - return cmd -} - -func (s *basicMachine) withPodmanCommand(args []string) *basicMachine { - s.args = args - return s -} diff --git a/pkg/machine/e2e/config_basic_test.go b/pkg/machine/e2e/config_basic_test.go new file mode 100644 index 000000000..d1cb24174 --- /dev/null +++ b/pkg/machine/e2e/config_basic_test.go @@ -0,0 +1,18 @@ +package e2e_test + +type basicMachine struct { + args []string +} + +func (s basicMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"-r"} + if len(s.args) > 0 { + cmd = append(cmd, s.args...) + } + return cmd +} + +func (s *basicMachine) withPodmanCommand(args []string) *basicMachine { + s.args = args + return s +} diff --git a/pkg/machine/e2e/config_info.go b/pkg/machine/e2e/config_info.go deleted file mode 100644 index 410c7e518..000000000 --- a/pkg/machine/e2e/config_info.go +++ /dev/null @@ -1,20 +0,0 @@ -package e2e - -type infoMachine struct { - format string - cmd []string -} - -func (i *infoMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "info"} - if len(i.format) > 0 { - cmd = append(cmd, "--format", i.format) - } - i.cmd = cmd - return cmd -} - -func (i *infoMachine) withFormat(format string) *infoMachine { - i.format = format - return i -} diff --git a/pkg/machine/e2e/config_info_test.go b/pkg/machine/e2e/config_info_test.go new file mode 100644 index 000000000..4da40ab99 --- /dev/null +++ b/pkg/machine/e2e/config_info_test.go @@ -0,0 +1,20 @@ +package e2e_test + +type infoMachine struct { + format string + cmd []string +} + +func (i *infoMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "info"} + if len(i.format) > 0 { + cmd = append(cmd, "--format", i.format) + } + i.cmd = cmd + return cmd +} + +func (i *infoMachine) withFormat(format string) *infoMachine { + i.format = format + return i +} diff --git a/pkg/machine/e2e/config_init.go b/pkg/machine/e2e/config_init.go deleted file mode 100644 index 7f18cce7d..000000000 --- a/pkg/machine/e2e/config_init.go +++ /dev/null @@ -1,102 +0,0 @@ -package e2e - -import ( - "strconv" -) - -type initMachine struct { - /* - --cpus uint Number of CPUs (default 1) - --disk-size uint Disk size in GB (default 100) - --ignition-path string Path to ignition file - --image-path string Path to qcow image (default "testing") - -m, --memory uint Memory in MB (default 2048) - --now Start machine now - --rootful Whether this machine should prefer rootful container execution - --timezone string Set timezone (default "local") - -v, --volume stringArray Volumes to mount, source:target - --volume-driver string Optional volume driver - - */ - cpus *uint - diskSize *uint - ignitionPath string - imagePath string - memory *uint - now bool - timezone string - rootful bool - volumes []string - - cmd []string -} - -func (i *initMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "init"} - if i.cpus != nil { - cmd = append(cmd, "--cpus", strconv.Itoa(int(*i.cpus))) - } - if i.diskSize != nil { - cmd = append(cmd, "--disk-size", strconv.Itoa(int(*i.diskSize))) - } - if l := len(i.ignitionPath); l > 0 { - cmd = append(cmd, "--ignition-path", i.ignitionPath) - } - if l := len(i.imagePath); l > 0 { - cmd = append(cmd, "--image-path", i.imagePath) - } - if i.memory != nil { - cmd = append(cmd, "--memory", strconv.Itoa(int(*i.memory))) - } - if l := len(i.timezone); l > 0 { - cmd = append(cmd, "--timezone", i.timezone) - } - for _, v := range i.volumes { - cmd = append(cmd, "--volume", v) - } - if i.now { - cmd = append(cmd, "--now") - } - cmd = append(cmd, m.name) - i.cmd = cmd - return cmd -} - -func (i *initMachine) withCPUs(num uint) *initMachine { - i.cpus = &num - return i -} -func (i *initMachine) withDiskSize(size uint) *initMachine { - i.diskSize = &size - return i -} - -func (i *initMachine) withIgnitionPath(path string) *initMachine { - i.ignitionPath = path - return i -} - -func (i *initMachine) withImagePath(path string) *initMachine { - i.imagePath = path - return i -} - -func (i *initMachine) withMemory(num uint) *initMachine { - i.memory = &num - return i -} - -func (i *initMachine) withNow() *initMachine { - i.now = true - return i -} - -func (i *initMachine) withTimezone(tz string) *initMachine { - i.timezone = tz - return i -} - -func (i *initMachine) withVolume(v string) *initMachine { - i.volumes = append(i.volumes, v) - return i -} diff --git a/pkg/machine/e2e/config_init_test.go b/pkg/machine/e2e/config_init_test.go new file mode 100644 index 000000000..d6c7990b0 --- /dev/null +++ b/pkg/machine/e2e/config_init_test.go @@ -0,0 +1,102 @@ +package e2e_test + +import ( + "strconv" +) + +type initMachine struct { + /* + --cpus uint Number of CPUs (default 1) + --disk-size uint Disk size in GB (default 100) + --ignition-path string Path to ignition file + --image-path string Path to qcow image (default "testing") + -m, --memory uint Memory in MB (default 2048) + --now Start machine now + --rootful Whether this machine should prefer rootful container execution + --timezone string Set timezone (default "local") + -v, --volume stringArray Volumes to mount, source:target + --volume-driver string Optional volume driver + + */ + cpus *uint + diskSize *uint + ignitionPath string + imagePath string + memory *uint + now bool + timezone string + rootful bool //nolint:unused,structcheck + volumes []string + + cmd []string +} + +func (i *initMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "init"} + if i.cpus != nil { + cmd = append(cmd, "--cpus", strconv.Itoa(int(*i.cpus))) + } + if i.diskSize != nil { + cmd = append(cmd, "--disk-size", strconv.Itoa(int(*i.diskSize))) + } + if l := len(i.ignitionPath); l > 0 { + cmd = append(cmd, "--ignition-path", i.ignitionPath) + } + if l := len(i.imagePath); l > 0 { + cmd = append(cmd, "--image-path", i.imagePath) + } + if i.memory != nil { + cmd = append(cmd, "--memory", strconv.Itoa(int(*i.memory))) + } + if l := len(i.timezone); l > 0 { + cmd = append(cmd, "--timezone", i.timezone) + } + for _, v := range i.volumes { + cmd = append(cmd, "--volume", v) + } + if i.now { + cmd = append(cmd, "--now") + } + cmd = append(cmd, m.name) + i.cmd = cmd + return cmd +} + +func (i *initMachine) withCPUs(num uint) *initMachine { + i.cpus = &num + return i +} +func (i *initMachine) withDiskSize(size uint) *initMachine { + i.diskSize = &size + return i +} + +func (i *initMachine) withIgnitionPath(path string) *initMachine { //nolint:unused + i.ignitionPath = path + return i +} + +func (i *initMachine) withImagePath(path string) *initMachine { + i.imagePath = path + return i +} + +func (i *initMachine) withMemory(num uint) *initMachine { + i.memory = &num + return i +} + +func (i *initMachine) withNow() *initMachine { + i.now = true + return i +} + +func (i *initMachine) withTimezone(tz string) *initMachine { + i.timezone = tz + return i +} + +func (i *initMachine) withVolume(v string) *initMachine { + i.volumes = append(i.volumes, v) + return i +} diff --git a/pkg/machine/e2e/config_inspect.go b/pkg/machine/e2e/config_inspect.go deleted file mode 100644 index 74c9a5d9c..000000000 --- a/pkg/machine/e2e/config_inspect.go +++ /dev/null @@ -1,24 +0,0 @@ -package e2e - -type inspectMachine struct { - /* - --format string Format volume output using JSON or a Go template - */ - cmd []string - format string -} - -func (i *inspectMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "inspect"} - if len(i.format) > 0 { - cmd = append(cmd, "--format", i.format) - } - cmd = append(cmd, m.names...) - i.cmd = cmd - return cmd -} - -func (i *inspectMachine) withFormat(format string) *inspectMachine { - i.format = format - return i -} diff --git a/pkg/machine/e2e/config_inspect_test.go b/pkg/machine/e2e/config_inspect_test.go new file mode 100644 index 000000000..ffd74220f --- /dev/null +++ b/pkg/machine/e2e/config_inspect_test.go @@ -0,0 +1,24 @@ +package e2e_test + +type inspectMachine struct { + /* + --format string Format volume output using JSON or a Go template + */ + cmd []string + format string +} + +func (i *inspectMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "inspect"} + if len(i.format) > 0 { + cmd = append(cmd, "--format", i.format) + } + cmd = append(cmd, m.names...) + i.cmd = cmd + return cmd +} + +func (i *inspectMachine) withFormat(format string) *inspectMachine { + i.format = format + return i +} diff --git a/pkg/machine/e2e/config_list.go b/pkg/machine/e2e/config_list.go deleted file mode 100644 index 150f984bc..000000000 --- a/pkg/machine/e2e/config_list.go +++ /dev/null @@ -1,45 +0,0 @@ -package e2e - -type listMachine struct { - /* - --format string Format volume output using JSON or a Go template (default "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n") - --noheading Do not print headers - -q, --quiet Show only machine names - */ - - format string - noHeading bool - quiet bool - - cmd []string -} - -func (i *listMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "list"} - if len(i.format) > 0 { - cmd = append(cmd, "--format", i.format) - } - if i.noHeading { - cmd = append(cmd, "--noheading") - } - if i.quiet { - cmd = append(cmd, "--quiet") - } - i.cmd = cmd - return cmd -} - -func (i *listMachine) withNoHeading() *listMachine { - i.noHeading = true - return i -} - -func (i *listMachine) withQuiet() *listMachine { - i.quiet = true - return i -} - -func (i *listMachine) withFormat(format string) *listMachine { - i.format = format - return i -} diff --git a/pkg/machine/e2e/config_list_test.go b/pkg/machine/e2e/config_list_test.go new file mode 100644 index 000000000..78f9edc62 --- /dev/null +++ b/pkg/machine/e2e/config_list_test.go @@ -0,0 +1,45 @@ +package e2e_test + +type listMachine struct { + /* + --format string Format volume output using JSON or a Go template (default "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n") + --noheading Do not print headers + -q, --quiet Show only machine names + */ + + format string + noHeading bool + quiet bool + + cmd []string +} + +func (i *listMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "list"} + if len(i.format) > 0 { + cmd = append(cmd, "--format", i.format) + } + if i.noHeading { + cmd = append(cmd, "--noheading") + } + if i.quiet { + cmd = append(cmd, "--quiet") + } + i.cmd = cmd + return cmd +} + +func (i *listMachine) withNoHeading() *listMachine { + i.noHeading = true + return i +} + +func (i *listMachine) withQuiet() *listMachine { + i.quiet = true + return i +} + +func (i *listMachine) withFormat(format string) *listMachine { + i.format = format + return i +} diff --git a/pkg/machine/e2e/config_rm.go b/pkg/machine/e2e/config_rm.go deleted file mode 100644 index 6cf262a22..000000000 --- a/pkg/machine/e2e/config_rm.go +++ /dev/null @@ -1,56 +0,0 @@ -package e2e - -type rmMachine struct { - /* - -f, --force Stop and do not prompt before rming - --save-ignition Do not delete ignition file - --save-image Do not delete the image file - --save-keys Do not delete SSH keys - - */ - force bool - saveIgnition bool - saveImage bool - saveKeys bool - - cmd []string -} - -func (i *rmMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "rm"} - if i.force { - cmd = append(cmd, "--force") - } - if i.saveIgnition { - cmd = append(cmd, "--save-ignition") - } - if i.saveImage { - cmd = append(cmd, "--save-image") - } - if i.saveKeys { - cmd = append(cmd, "--save-keys") - } - cmd = append(cmd, m.name) - i.cmd = cmd - return cmd -} - -func (i *rmMachine) withForce() *rmMachine { - i.force = true - return i -} - -func (i *rmMachine) withSaveIgnition() *rmMachine { - i.saveIgnition = true - return i -} - -func (i *rmMachine) withSaveImage() *rmMachine { - i.saveImage = true - return i -} - -func (i *rmMachine) withSaveKeys() *rmMachine { - i.saveKeys = true - return i -} diff --git a/pkg/machine/e2e/config_rm_test.go b/pkg/machine/e2e/config_rm_test.go new file mode 100644 index 000000000..1f9c9b4ec --- /dev/null +++ b/pkg/machine/e2e/config_rm_test.go @@ -0,0 +1,56 @@ +package e2e_test + +type rmMachine struct { + /* + -f, --force Stop and do not prompt before rming + --save-ignition Do not delete ignition file + --save-image Do not delete the image file + --save-keys Do not delete SSH keys + + */ + force bool + saveIgnition bool + saveImage bool + saveKeys bool + + cmd []string +} + +func (i *rmMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "rm"} + if i.force { + cmd = append(cmd, "--force") + } + if i.saveIgnition { + cmd = append(cmd, "--save-ignition") + } + if i.saveImage { + cmd = append(cmd, "--save-image") + } + if i.saveKeys { + cmd = append(cmd, "--save-keys") + } + cmd = append(cmd, m.name) + i.cmd = cmd + return cmd +} + +func (i *rmMachine) withForce() *rmMachine { + i.force = true + return i +} + +func (i *rmMachine) withSaveIgnition() *rmMachine { //nolint:unused + i.saveIgnition = true + return i +} + +func (i *rmMachine) withSaveImage() *rmMachine { //nolint:unused + i.saveImage = true + return i +} + +func (i *rmMachine) withSaveKeys() *rmMachine { //nolint:unused + i.saveKeys = true + return i +} diff --git a/pkg/machine/e2e/config_set.go b/pkg/machine/e2e/config_set.go deleted file mode 100644 index b310ab1b9..000000000 --- a/pkg/machine/e2e/config_set.go +++ /dev/null @@ -1,43 +0,0 @@ -package e2e - -import ( - "strconv" -) - -type setMachine struct { - cpus *uint - diskSize *uint - memory *uint - - cmd []string -} - -func (i *setMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "set"} - if i.cpus != nil { - cmd = append(cmd, "--cpus", strconv.Itoa(int(*i.cpus))) - } - if i.diskSize != nil { - cmd = append(cmd, "--disk-size", strconv.Itoa(int(*i.diskSize))) - } - if i.memory != nil { - cmd = append(cmd, "--memory", strconv.Itoa(int(*i.memory))) - } - cmd = append(cmd, m.name) - i.cmd = cmd - return cmd -} - -func (i *setMachine) withCPUs(num uint) *setMachine { - i.cpus = &num - return i -} -func (i *setMachine) withDiskSize(size uint) *setMachine { - i.diskSize = &size - return i -} - -func (i *setMachine) withMemory(num uint) *setMachine { - i.memory = &num - return i -} diff --git a/pkg/machine/e2e/config_set_test.go b/pkg/machine/e2e/config_set_test.go new file mode 100644 index 000000000..3c773b970 --- /dev/null +++ b/pkg/machine/e2e/config_set_test.go @@ -0,0 +1,43 @@ +package e2e_test + +import ( + "strconv" +) + +type setMachine struct { + cpus *uint + diskSize *uint + memory *uint + + cmd []string +} + +func (i *setMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "set"} + if i.cpus != nil { + cmd = append(cmd, "--cpus", strconv.Itoa(int(*i.cpus))) + } + if i.diskSize != nil { + cmd = append(cmd, "--disk-size", strconv.Itoa(int(*i.diskSize))) + } + if i.memory != nil { + cmd = append(cmd, "--memory", strconv.Itoa(int(*i.memory))) + } + cmd = append(cmd, m.name) + i.cmd = cmd + return cmd +} + +func (i *setMachine) withCPUs(num uint) *setMachine { + i.cpus = &num + return i +} +func (i *setMachine) withDiskSize(size uint) *setMachine { + i.diskSize = &size + return i +} + +func (i *setMachine) withMemory(num uint) *setMachine { + i.memory = &num + return i +} diff --git a/pkg/machine/e2e/config_ssh.go b/pkg/machine/e2e/config_ssh.go deleted file mode 100644 index b09eed47d..000000000 --- a/pkg/machine/e2e/config_ssh.go +++ /dev/null @@ -1,33 +0,0 @@ -package e2e - -type sshMachine struct { - /* - --username string Username to use when ssh-ing into the VM. - */ - - username string - sshCommand []string - - cmd []string -} - -func (s sshMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "ssh"} - if len(m.name) > 0 { - cmd = append(cmd, m.name) - } - if len(s.sshCommand) > 0 { - cmd = append(cmd, s.sshCommand...) - } - return cmd -} - -func (s *sshMachine) withUsername(name string) *sshMachine { - s.username = name - return s -} - -func (s *sshMachine) withSSHComand(sshCommand []string) *sshMachine { - s.sshCommand = sshCommand - return s -} diff --git a/pkg/machine/e2e/config_ssh_test.go b/pkg/machine/e2e/config_ssh_test.go new file mode 100644 index 000000000..f062625fa --- /dev/null +++ b/pkg/machine/e2e/config_ssh_test.go @@ -0,0 +1,31 @@ +package e2e_test + +type sshMachine struct { + /* + --username string Username to use when ssh-ing into the VM. + */ + + username string //nolint:unused + sshCommand []string +} + +func (s sshMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "ssh"} + if len(m.name) > 0 { + cmd = append(cmd, m.name) + } + if len(s.sshCommand) > 0 { + cmd = append(cmd, s.sshCommand...) + } + return cmd +} + +func (s *sshMachine) withUsername(name string) *sshMachine { //nolint:unused + s.username = name + return s +} + +func (s *sshMachine) withSSHComand(sshCommand []string) *sshMachine { + s.sshCommand = sshCommand + return s +} diff --git a/pkg/machine/e2e/config_start.go b/pkg/machine/e2e/config_start.go deleted file mode 100644 index 86b1721f8..000000000 --- a/pkg/machine/e2e/config_start.go +++ /dev/null @@ -1,16 +0,0 @@ -package e2e - -type startMachine struct { - /* - No command line args other than a machine vm name (also not required) - */ - cmd []string -} - -func (s startMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "start"} - if len(m.name) > 0 { - cmd = append(cmd, m.name) - } - return cmd -} diff --git a/pkg/machine/e2e/config_start_test.go b/pkg/machine/e2e/config_start_test.go new file mode 100644 index 000000000..d9efbf489 --- /dev/null +++ b/pkg/machine/e2e/config_start_test.go @@ -0,0 +1,15 @@ +package e2e_test + +type startMachine struct { + /* + No command line args other than a machine vm name (also not required) + */ +} + +func (s startMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "start"} + if len(m.name) > 0 { + cmd = append(cmd, m.name) + } + return cmd +} diff --git a/pkg/machine/e2e/config_stop.go b/pkg/machine/e2e/config_stop.go deleted file mode 100644 index 04dcfb524..000000000 --- a/pkg/machine/e2e/config_stop.go +++ /dev/null @@ -1,16 +0,0 @@ -package e2e - -type stopMachine struct { - /* - No command line args other than a machine vm name (also not required) - */ - cmd []string -} - -func (s stopMachine) buildCmd(m *machineTestBuilder) []string { - cmd := []string{"machine", "stop"} - if len(m.name) > 0 { - cmd = append(cmd, m.name) - } - return cmd -} diff --git a/pkg/machine/e2e/config_stop_test.go b/pkg/machine/e2e/config_stop_test.go new file mode 100644 index 000000000..41142ec7e --- /dev/null +++ b/pkg/machine/e2e/config_stop_test.go @@ -0,0 +1,15 @@ +package e2e_test + +type stopMachine struct { + /* + No command line args other than a machine vm name (also not required) + */ +} + +func (s stopMachine) buildCmd(m *machineTestBuilder) []string { + cmd := []string{"machine", "stop"} + if len(m.name) > 0 { + cmd = append(cmd, m.name) + } + return cmd +} diff --git a/pkg/machine/e2e/config_test.go b/pkg/machine/e2e/config_test.go new file mode 100644 index 000000000..9940e711b --- /dev/null +++ b/pkg/machine/e2e/config_test.go @@ -0,0 +1,175 @@ +package e2e_test + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + + "github.com/containers/podman/v4/pkg/machine" + "github.com/containers/podman/v4/pkg/util" + "github.com/containers/storage/pkg/stringid" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" +) + +var originalHomeDir = os.Getenv("HOME") + +const ( + defaultTimeout time.Duration = 90 * time.Second +) + +type machineCommand interface { + buildCmd(m *machineTestBuilder) []string +} + +type MachineTestBuilder interface { + setName(string) *MachineTestBuilder + setCmd(mc machineCommand) *MachineTestBuilder + setTimeout(duration time.Duration) *MachineTestBuilder + run() (*machineSession, error) +} +type machineSession struct { + *Session +} + +type machineTestBuilder struct { + cmd []string + imagePath string + name string + names []string + podmanBinary string + timeout time.Duration +} + +// waitWithTimeout waits for a command to complete for a given +// number of seconds +func (ms *machineSession) waitWithTimeout(timeout time.Duration) { + Eventually(ms, timeout).Should(Exit()) + os.Stdout.Sync() + os.Stderr.Sync() +} + +func (ms *machineSession) Bytes() []byte { + return []byte(ms.outputToString()) +} + +func (ms *machineSession) outputToStringSlice() []string { + var results []string + output := string(ms.Out.Contents()) + for _, line := range strings.Split(output, "\n") { + if line != "" { + results = append(results, line) + } + } + return results +} + +// outputToString returns the output from a session in string form +func (ms *machineSession) outputToString() string { + if ms == nil || ms.Out == nil || ms.Out.Contents() == nil { + return "" + } + + fields := strings.Fields(string(ms.Out.Contents())) + return strings.Join(fields, " ") +} + +// errorToString returns the error output from a session in string form +func (ms *machineSession) errorToString() string { + if ms == nil || ms.Err == nil || ms.Err.Contents() == nil { + return "" + } + return string(ms.Err.Contents()) +} + +// newMB constructor for machine test builders +func newMB() (*machineTestBuilder, error) { + mb := machineTestBuilder{ + timeout: defaultTimeout, + } + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + mb.podmanBinary = filepath.Join(cwd, "../../../bin/podman-remote") + if os.Getenv("PODMAN_BINARY") != "" { + mb.podmanBinary = os.Getenv("PODMAN_BINARY") + } + return &mb, nil +} + +// setName sets the name of the virtuaql machine for the command +func (m *machineTestBuilder) setName(name string) *machineTestBuilder { + m.name = name + return m +} + +// setCmd takes a machineCommand struct and assembles a cmd line +// representation of the podman machine command +func (m *machineTestBuilder) setCmd(mc machineCommand) *machineTestBuilder { + // If no name for the machine exists, we set a random name. + if !util.StringInSlice(m.name, m.names) { + if len(m.name) < 1 { + m.name = randomString() + } + m.names = append(m.names, m.name) + } + m.cmd = mc.buildCmd(m) + return m +} + +func (m *machineTestBuilder) setTimeout(timeout time.Duration) *machineTestBuilder { + m.timeout = timeout + return m +} + +// toQemuInspectInfo is only for inspecting qemu machines. Other providers will need +// to make their own. +func (m *machineTestBuilder) toQemuInspectInfo() ([]machine.InspectInfo, int, error) { + args := []string{"machine", "inspect"} + args = append(args, m.names...) + session, err := runWrapper(m.podmanBinary, args, defaultTimeout, true) + if err != nil { + return nil, -1, err + } + mii := []machine.InspectInfo{} + err = json.Unmarshal(session.Bytes(), &mii) + return mii, session.ExitCode(), err +} + +func (m *machineTestBuilder) runWithoutWait() (*machineSession, error) { + return runWrapper(m.podmanBinary, m.cmd, m.timeout, false) +} + +func (m *machineTestBuilder) run() (*machineSession, error) { + return runWrapper(m.podmanBinary, m.cmd, m.timeout, true) +} + +func runWrapper(podmanBinary string, cmdArgs []string, timeout time.Duration, wait bool) (*machineSession, error) { + if len(os.Getenv("DEBUG")) > 0 { + cmdArgs = append([]string{"--log-level=debug"}, cmdArgs...) + } + fmt.Println(podmanBinary + " " + strings.Join(cmdArgs, " ")) + c := exec.Command(podmanBinary, cmdArgs...) + session, err := Start(c, GinkgoWriter, GinkgoWriter) + if err != nil { + Fail(fmt.Sprintf("Unable to start session: %q", err)) + return nil, err + } + ms := machineSession{session} + if wait { + ms.waitWithTimeout(timeout) + fmt.Println("output:", ms.outputToString()) + } + return &ms, nil +} + +// randomString returns a string of given length composed of random characters +func randomString() string { + return stringid.GenerateRandomID()[0:12] +} diff --git a/pkg/machine/e2e/info_test.go b/pkg/machine/e2e/info_test.go index eeabb78af..759beecb5 100644 --- a/pkg/machine/e2e/info_test.go +++ b/pkg/machine/e2e/info_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "github.com/containers/podman/v4/cmd/podman/machine" diff --git a/pkg/machine/e2e/init_test.go b/pkg/machine/e2e/init_test.go index 40f140cae..52a62cee3 100644 --- a/pkg/machine/e2e/init_test.go +++ b/pkg/machine/e2e/init_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "io/ioutil" @@ -78,7 +78,7 @@ var _ = Describe("podman machine init", func() { }) It("machine init with cpus, disk size, memory, timezone", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withCPUs(2).withDiskSize(102).withMemory(4000).withTimezone("Pacific/Honolulu")).run() Expect(err).To(BeNil()) @@ -130,7 +130,7 @@ var _ = Describe("podman machine init", func() { mount := tmpDir + ":/testmountdir" defer os.RemoveAll(tmpDir) - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withVolume(mount)).run() Expect(err).To(BeNil()) diff --git a/pkg/machine/e2e/inspect_test.go b/pkg/machine/e2e/inspect_test.go index 93fb8cc2b..0ab928205 100644 --- a/pkg/machine/e2e/inspect_test.go +++ b/pkg/machine/e2e/inspect_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "strings" @@ -52,15 +52,15 @@ var _ = Describe("podman machine stop", func() { }) It("inspect with go format", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) Expect(session).To(Exit(0)) // regular inspect should - inspectJson := new(inspectMachine) - inspectSession, err := mb.setName(name).setCmd(inspectJson).run() + inspectJSON := new(inspectMachine) + inspectSession, err := mb.setName(name).setCmd(inspectJSON).run() Expect(err).To(BeNil()) Expect(inspectSession).To(Exit(0)) diff --git a/pkg/machine/e2e/list_test.go b/pkg/machine/e2e/list_test.go index 8b7443d47..5c7ae6c5e 100644 --- a/pkg/machine/e2e/list_test.go +++ b/pkg/machine/e2e/list_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "strings" @@ -45,8 +45,8 @@ var _ = Describe("podman machine list", func() { It("list machines with quiet or noheading", func() { // Random names for machines to test list - name1 := randomString(12) - name2 := randomString(12) + name1 := randomString() + name2 := randomString() list := new(listMachine) firstList, err := mb.setCmd(list.withQuiet()).run() @@ -109,7 +109,7 @@ var _ = Describe("podman machine list", func() { It("list with --format", func() { // Random names for machines to test list - name1 := randomString(12) + name1 := randomString() i := new(initMachine) session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run() diff --git a/pkg/machine/e2e/machine_test.go b/pkg/machine/e2e/machine_test.go index 93eabdad3..5de04b9f7 100644 --- a/pkg/machine/e2e/machine_test.go +++ b/pkg/machine/e2e/machine_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "fmt" diff --git a/pkg/machine/e2e/rm_test.go b/pkg/machine/e2e/rm_test.go index 43b8c594c..e33eaf702 100644 --- a/pkg/machine/e2e/rm_test.go +++ b/pkg/machine/e2e/rm_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( . "github.com/onsi/ginkgo" diff --git a/pkg/machine/e2e/set_test.go b/pkg/machine/e2e/set_test.go index 80cb89488..df4136a82 100644 --- a/pkg/machine/e2e/set_test.go +++ b/pkg/machine/e2e/set_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "runtime" @@ -22,7 +22,7 @@ var _ = Describe("podman machine set", func() { }) It("set machine cpus, disk, memory", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) @@ -75,7 +75,7 @@ var _ = Describe("podman machine set", func() { }) It("no settings should change if no flags", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) diff --git a/pkg/machine/e2e/ssh_test.go b/pkg/machine/e2e/ssh_test.go index 9ee31ac26..6d23a024e 100644 --- a/pkg/machine/e2e/ssh_test.go +++ b/pkg/machine/e2e/ssh_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( . "github.com/onsi/ginkgo" @@ -20,17 +20,17 @@ var _ = Describe("podman machine ssh", func() { }) It("bad machine name", func() { - name := randomString(12) + name := randomString() ssh := sshMachine{} session, err := mb.setName(name).setCmd(ssh).run() Expect(err).To(BeNil()) Expect(session).To(Exit(125)) // TODO seems like stderr is not being returned; re-enabled when fixed - //Expect(session.outputToString()).To(ContainSubstring("not exist")) + // Expect(session.outputToString()).To(ContainSubstring("not exist")) }) It("ssh to non-running machine", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) @@ -40,12 +40,12 @@ var _ = Describe("podman machine ssh", func() { sshSession, err := mb.setName(name).setCmd(ssh).run() Expect(err).To(BeNil()) // TODO seems like stderr is not being returned; re-enabled when fixed - //Expect(sshSession.outputToString()).To(ContainSubstring("is not running")) + // Expect(sshSession.outputToString()).To(ContainSubstring("is not running")) Expect(sshSession).To(Exit(125)) }) It("ssh to running machine and check os-type", func() { - name := randomString(12) + name := randomString() i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).To(BeNil()) diff --git a/pkg/machine/e2e/start_test.go b/pkg/machine/e2e/start_test.go index 1de66eb9a..1f9405569 100644 --- a/pkg/machine/e2e/start_test.go +++ b/pkg/machine/e2e/start_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "github.com/containers/podman/v4/pkg/machine" diff --git a/pkg/machine/e2e/stop_test.go b/pkg/machine/e2e/stop_test.go index 0c27045a6..621bbdb16 100644 --- a/pkg/machine/e2e/stop_test.go +++ b/pkg/machine/e2e/stop_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( . "github.com/onsi/ginkgo" -- cgit v1.2.3-54-g00ecf