Merge pull request #15010 from Luap99/machine-e2e

enable linter for pkg/machine/e2e
This commit is contained in:
OpenShift Merge Robot
2022-07-22 12:51:49 +02:00
committed by GitHub
26 changed files with 91 additions and 102 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/machine" "github.com/containers/podman/v4/pkg/machine"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -40,26 +41,6 @@ var (
inFormat string inFormat string
) )
// Info contains info on the machine host and version info
type Info struct {
Host *HostInfo `json:"Host"`
Version define.Version `json:"Version"`
}
// HostInfo contains info on the machine host
type HostInfo struct {
Arch string `json:"Arch"`
CurrentMachine string `json:"CurrentMachine"`
DefaultMachine string `json:"DefaultMachine"`
EventsDir string `json:"EventsDir"`
MachineConfigDir string `json:"MachineConfigDir"`
MachineImageDir string `json:"MachineImageDir"`
MachineState string `json:"MachineState"`
NumberOfMachines int `json:"NumberOfMachines"`
OS string `json:"OS"`
VMType string `json:"VMType"`
}
func init() { func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{ registry.Commands = append(registry.Commands, registry.CliCommand{
Command: infoCmd, Command: infoCmd,
@ -69,11 +50,11 @@ func init() {
flags := infoCmd.Flags() flags := infoCmd.Flags()
formatFlagName := "format" formatFlagName := "format"
flags.StringVarP(&inFormat, formatFlagName, "f", "", "Change the output format to JSON or a Go template") flags.StringVarP(&inFormat, formatFlagName, "f", "", "Change the output format to JSON or a Go template")
_ = infoCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&define.Info{})) _ = infoCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.MachineInfo{}))
} }
func info(cmd *cobra.Command, args []string) error { func info(cmd *cobra.Command, args []string) error {
info := Info{} info := entities.MachineInfo{}
version, err := define.GetVersion() version, err := define.GetVersion()
if err != nil { if err != nil {
return fmt.Errorf("error getting version info %w", err) return fmt.Errorf("error getting version info %w", err)
@ -112,8 +93,8 @@ func info(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func hostInfo() (*HostInfo, error) { func hostInfo() (*entities.MachineHostInfo, error) {
host := HostInfo{} host := entities.MachineHostInfo{}
host.Arch = runtime.GOARCH host.Arch = runtime.GOARCH
host.OS = runtime.GOOS host.OS = runtime.GOOS

View File

@ -80,6 +80,11 @@ func ssh(cmd *cobra.Command, args []string) error {
} }
} }
vm, err = provider.LoadVMByName(vmName)
if err != nil {
return fmt.Errorf("vm %s not found: %w", vmName, err)
}
if !validVM && sshOpts.Username == "" { if !validVM && sshOpts.Username == "" {
sshOpts.Username, err = remoteConnectionUsername() sshOpts.Username, err = remoteConnectionUsername()
if err != nil { if err != nil {
@ -87,10 +92,6 @@ func ssh(cmd *cobra.Command, args []string) error {
} }
} }
vm, err = provider.LoadVMByName(vmName)
if err != nil {
return fmt.Errorf("vm %s not found: %w", vmName, err)
}
err = vm.SSH(vmName, sshOpts) err = vm.SSH(vmName, sshOpts)
return utils.HandleOSExecError(err) return utils.HandleOSExecError(err)
} }

View File

@ -9,9 +9,9 @@ BUILD_TAGS[abi]="${BUILD_TAGS[default]},systemd"
BUILD_TAGS[tunnel]="${BUILD_TAGS[default]},remote" BUILD_TAGS[tunnel]="${BUILD_TAGS[default]},remote"
declare -A SKIP_DIRS declare -A SKIP_DIRS
SKIP_DIRS[abi]="pkg/machine/e2e" SKIP_DIRS[abi]=""
# TODO: add "remote" build tag to pkg/api # TODO: add "remote" build tag to pkg/api
SKIP_DIRS[tunnel]="pkg/api,pkg/machine/e2e" SKIP_DIRS[tunnel]="pkg/api"
[[ $1 == run ]] && shift [[ $1 == run ]] && shift

View File

@ -1,5 +1,7 @@
package entities package entities
import "github.com/containers/podman/v4/libpod/define"
type ListReporter struct { type ListReporter struct {
Name string Name string
Default bool Default bool
@ -16,3 +18,23 @@ type ListReporter struct {
RemoteUsername string RemoteUsername string
IdentityPath string IdentityPath string
} }
// MachineInfo contains info on the machine host and version info
type MachineInfo struct {
Host *MachineHostInfo `json:"Host"`
Version define.Version `json:"Version"`
}
// MachineHostInfo contains info on the machine host
type MachineHostInfo struct {
Arch string `json:"Arch"`
CurrentMachine string `json:"CurrentMachine"`
DefaultMachine string `json:"DefaultMachine"`
EventsDir string `json:"EventsDir"`
MachineConfigDir string `json:"MachineConfigDir"`
MachineImageDir string `json:"MachineImageDir"`
MachineState string `json:"MachineState"`
NumberOfMachines int `json:"NumberOfMachines"`
OS string `json:"OS"`
VMType string `json:"VMType"`
}

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -20,7 +20,7 @@ var _ = Describe("run basic podman commands", func() {
}) })
It("Basic ops", func() { It("Basic ops", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())

View File

@ -1,8 +1,7 @@
package e2e package e2e_test
type basicMachine struct { type basicMachine struct {
args []string args []string
cmd []string
} }
func (s basicMachine) buildCmd(m *machineTestBuilder) []string { func (s basicMachine) buildCmd(m *machineTestBuilder) []string {

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
type infoMachine struct { type infoMachine struct {
format string format string

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"strconv" "strconv"
@ -25,7 +25,7 @@ type initMachine struct {
memory *uint memory *uint
now bool now bool
timezone string timezone string
rootful bool rootful bool //nolint:unused,structcheck
volumes []string volumes []string
cmd []string cmd []string
@ -71,7 +71,7 @@ func (i *initMachine) withDiskSize(size uint) *initMachine {
return i return i
} }
func (i *initMachine) withIgnitionPath(path string) *initMachine { func (i *initMachine) withIgnitionPath(path string) *initMachine { //nolint:unused
i.ignitionPath = path i.ignitionPath = path
return i return i
} }

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
type inspectMachine struct { type inspectMachine struct {
/* /*

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
type listMachine struct { type listMachine struct {
/* /*

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
type rmMachine struct { type rmMachine struct {
/* /*
@ -40,17 +40,17 @@ func (i *rmMachine) withForce() *rmMachine {
return i return i
} }
func (i *rmMachine) withSaveIgnition() *rmMachine { func (i *rmMachine) withSaveIgnition() *rmMachine { //nolint:unused
i.saveIgnition = true i.saveIgnition = true
return i return i
} }
func (i *rmMachine) withSaveImage() *rmMachine { func (i *rmMachine) withSaveImage() *rmMachine { //nolint:unused
i.saveImage = true i.saveImage = true
return i return i
} }
func (i *rmMachine) withSaveKeys() *rmMachine { func (i *rmMachine) withSaveKeys() *rmMachine { //nolint:unused
i.saveKeys = true i.saveKeys = true
return i return i
} }

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"strconv" "strconv"

View File

@ -1,14 +1,12 @@
package e2e package e2e_test
type sshMachine struct { type sshMachine struct {
/* /*
--username string Username to use when ssh-ing into the VM. --username string Username to use when ssh-ing into the VM.
*/ */
username string username string //nolint:unused
sshCommand []string sshCommand []string
cmd []string
} }
func (s sshMachine) buildCmd(m *machineTestBuilder) []string { func (s sshMachine) buildCmd(m *machineTestBuilder) []string {
@ -22,7 +20,7 @@ func (s sshMachine) buildCmd(m *machineTestBuilder) []string {
return cmd return cmd
} }
func (s *sshMachine) withUsername(name string) *sshMachine { func (s *sshMachine) withUsername(name string) *sshMachine { //nolint:unused
s.username = name s.username = name
return s return s
} }

View File

@ -1,10 +1,9 @@
package e2e package e2e_test
type startMachine struct { type startMachine struct {
/* /*
No command line args other than a machine vm name (also not required) No command line args other than a machine vm name (also not required)
*/ */
cmd []string
} }
func (s startMachine) buildCmd(m *machineTestBuilder) []string { func (s startMachine) buildCmd(m *machineTestBuilder) []string {

View File

@ -1,10 +1,9 @@
package e2e package e2e_test
type stopMachine struct { type stopMachine struct {
/* /*
No command line args other than a machine vm name (also not required) No command line args other than a machine vm name (also not required)
*/ */
cmd []string
} }
func (s stopMachine) buildCmd(m *machineTestBuilder) []string { func (s stopMachine) buildCmd(m *machineTestBuilder) []string {

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"encoding/json" "encoding/json"
@ -10,13 +10,11 @@ import (
"time" "time"
"github.com/containers/podman/v4/pkg/machine" "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/podman/v4/pkg/util"
"github.com/containers/storage/pkg/stringid" "github.com/containers/storage/pkg/stringid"
. "github.com/onsi/ginkgo" //nolint:golint,stylecheck . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec" . "github.com/onsi/gomega/gexec"
. "github.com/onsi/gomega/gexec" //nolint:golint,stylecheck
) )
var originalHomeDir = os.Getenv("HOME") var originalHomeDir = os.Getenv("HOME")
@ -36,7 +34,7 @@ type MachineTestBuilder interface {
run() (*machineSession, error) run() (*machineSession, error)
} }
type machineSession struct { type machineSession struct {
*gexec.Session *Session
} }
type machineTestBuilder struct { type machineTestBuilder struct {
@ -47,10 +45,6 @@ type machineTestBuilder struct {
podmanBinary string podmanBinary string
timeout time.Duration timeout time.Duration
} }
type qemuMachineInspectInfo struct {
State machine.Status
VM qemu.MachineVM
}
// waitWithTimeout waits for a command to complete for a given // waitWithTimeout waits for a command to complete for a given
// number of seconds // number of seconds
@ -121,7 +115,7 @@ func (m *machineTestBuilder) setCmd(mc machineCommand) *machineTestBuilder {
// If no name for the machine exists, we set a random name. // If no name for the machine exists, we set a random name.
if !util.StringInSlice(m.name, m.names) { if !util.StringInSlice(m.name, m.names) {
if len(m.name) < 1 { if len(m.name) < 1 {
m.name = randomString(12) m.name = randomString()
} }
m.names = append(m.names, m.name) m.names = append(m.names, m.name)
} }
@ -136,10 +130,10 @@ func (m *machineTestBuilder) setTimeout(timeout time.Duration) *machineTestBuild
// toQemuInspectInfo is only for inspecting qemu machines. Other providers will need // toQemuInspectInfo is only for inspecting qemu machines. Other providers will need
// to make their own. // to make their own.
func (mb *machineTestBuilder) toQemuInspectInfo() ([]machine.InspectInfo, int, error) { func (m *machineTestBuilder) toQemuInspectInfo() ([]machine.InspectInfo, int, error) {
args := []string{"machine", "inspect"} args := []string{"machine", "inspect"}
args = append(args, mb.names...) args = append(args, m.names...)
session, err := runWrapper(mb.podmanBinary, args, defaultTimeout, true) session, err := runWrapper(m.podmanBinary, args, defaultTimeout, true)
if err != nil { if err != nil {
return nil, -1, err return nil, -1, err
} }
@ -175,9 +169,7 @@ func runWrapper(podmanBinary string, cmdArgs []string, timeout time.Duration, wa
return &ms, nil return &ms, nil
} }
func (m *machineTestBuilder) init() {}
// randomString returns a string of given length composed of random characters // randomString returns a string of given length composed of random characters
func randomString(n int) string { func randomString() string {
return stringid.GenerateRandomID()[0:12] return stringid.GenerateRandomID()[0:12]
} }

View File

@ -1,7 +1,7 @@
package e2e package e2e_test
import ( import (
"github.com/containers/podman/v4/cmd/podman/machine" "github.com/containers/podman/v4/pkg/domain/entities"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -51,7 +51,7 @@ var _ = Describe("podman machine info", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(infoSession).Should(Exit(0)) Expect(infoSession).Should(Exit(0))
infoReport := &machine.Info{} infoReport := &entities.MachineInfo{}
err = jsoniter.Unmarshal(infoSession.Bytes(), infoReport) err = jsoniter.Unmarshal(infoSession.Bytes(), infoReport)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
}) })

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"io/ioutil" "io/ioutil"
@ -78,7 +78,7 @@ var _ = Describe("podman machine init", func() {
}) })
It("machine init with cpus, disk size, memory, timezone", func() { It("machine init with cpus, disk size, memory, timezone", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withCPUs(2).withDiskSize(102).withMemory(4000).withTimezone("Pacific/Honolulu")).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withCPUs(2).withDiskSize(102).withMemory(4000).withTimezone("Pacific/Honolulu")).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -108,7 +108,7 @@ var _ = Describe("podman machine init", func() {
switch runtime.GOOS { switch runtime.GOOS {
// os's handle memory differently // os's handle memory differently
case "linux": case "linux":
Expect(memorySession.outputToString()).To(ContainSubstring("3821")) Expect(memorySession.outputToString()).To(ContainSubstring("3822"))
case "darwin": case "darwin":
Expect(memorySession.outputToString()).To(ContainSubstring("3824")) Expect(memorySession.outputToString()).To(ContainSubstring("3824"))
default: default:
@ -130,7 +130,7 @@ var _ = Describe("podman machine init", func() {
mount := tmpDir + ":/testmountdir" mount := tmpDir + ":/testmountdir"
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withVolume(mount)).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withVolume(mount)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"strings" "strings"
@ -52,15 +52,15 @@ var _ = Describe("podman machine stop", func() {
}) })
It("inspect with go format", func() { It("inspect with go format", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(session).To(Exit(0)) Expect(session).To(Exit(0))
// regular inspect should // regular inspect should
inspectJson := new(inspectMachine) inspectJSON := new(inspectMachine)
inspectSession, err := mb.setName(name).setCmd(inspectJson).run() inspectSession, err := mb.setName(name).setCmd(inspectJSON).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(inspectSession).To(Exit(0)) Expect(inspectSession).To(Exit(0))

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"strings" "strings"
@ -45,8 +45,8 @@ var _ = Describe("podman machine list", func() {
It("list machines with quiet or noheading", func() { It("list machines with quiet or noheading", func() {
// Random names for machines to test list // Random names for machines to test list
name1 := randomString(12) name1 := randomString()
name2 := randomString(12) name2 := randomString()
list := new(listMachine) list := new(listMachine)
firstList, err := mb.setCmd(list.withQuiet()).run() firstList, err := mb.setCmd(list.withQuiet()).run()
@ -109,7 +109,7 @@ var _ = Describe("podman machine list", func() {
It("list with --format", func() { It("list with --format", func() {
// Random names for machines to test list // Random names for machines to test list
name1 := randomString(12) name1 := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run() session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run()

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"runtime" "runtime"
@ -22,7 +22,7 @@ var _ = Describe("podman machine set", func() {
}) })
It("set machine cpus, disk, memory", func() { It("set machine cpus, disk, memory", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -62,7 +62,7 @@ var _ = Describe("podman machine set", func() {
switch runtime.GOOS { switch runtime.GOOS {
// it seems macos and linux handle memory differently // it seems macos and linux handle memory differently
case "linux": case "linux":
Expect(memorySession.outputToString()).To(ContainSubstring("3821")) Expect(memorySession.outputToString()).To(ContainSubstring("3822"))
case "darwin": case "darwin":
Expect(memorySession.outputToString()).To(ContainSubstring("3824")) Expect(memorySession.outputToString()).To(ContainSubstring("3824"))
default: default:
@ -75,7 +75,7 @@ var _ = Describe("podman machine set", func() {
}) })
It("no settings should change if no flags", func() { It("no settings should change if no flags", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -20,17 +20,16 @@ var _ = Describe("podman machine ssh", func() {
}) })
It("bad machine name", func() { It("bad machine name", func() {
name := randomString(12) name := randomString()
ssh := sshMachine{} ssh := sshMachine{}
session, err := mb.setName(name).setCmd(ssh).run() session, err := mb.setName(name).setCmd(ssh).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(session).To(Exit(125)) Expect(session).To(Exit(125))
// TODO seems like stderr is not being returned; re-enabled when fixed Expect(session.errorToString()).To(ContainSubstring("not exist"))
//Expect(session.outputToString()).To(ContainSubstring("not exist"))
}) })
It("ssh to non-running machine", func() { It("ssh to non-running machine", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -39,13 +38,12 @@ var _ = Describe("podman machine ssh", func() {
ssh := sshMachine{} ssh := sshMachine{}
sshSession, err := mb.setName(name).setCmd(ssh).run() sshSession, err := mb.setName(name).setCmd(ssh).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
// TODO seems like stderr is not being returned; re-enabled when fixed Expect(sshSession.errorToString()).To(ContainSubstring("is not running"))
//Expect(sshSession.outputToString()).To(ContainSubstring("is not running"))
Expect(sshSession).To(Exit(125)) Expect(sshSession).To(Exit(125))
}) })
It("ssh to running machine and check os-type", func() { It("ssh to running machine and check os-type", func() {
name := randomString(12) name := randomString()
i := new(initMachine) i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
"github.com/containers/podman/v4/pkg/machine" "github.com/containers/podman/v4/pkg/machine"

View File

@ -1,4 +1,4 @@
package e2e package e2e_test
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"