Merge pull request #14170 from ashley-cui/machtests

Add more machine tests
This commit is contained in:
OpenShift Merge Robot 2022-05-11 03:03:31 -04:00 committed by GitHub
commit c379014ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 65 deletions

View File

@ -43,7 +43,7 @@ type listFlagType struct {
quiet bool quiet bool
} }
type machineReporter struct { type ListReporter struct {
Name string Name string
Default bool Default bool
Created string Created string
@ -68,7 +68,7 @@ func init() {
flags := lsCmd.Flags() flags := lsCmd.Flags()
formatFlagName := "format" formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template") flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&machineReporter{})) _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&ListReporter{}))
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers") flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names") flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names")
} }
@ -121,8 +121,8 @@ func list(cmd *cobra.Command, args []string) error {
return outputTemplate(cmd, machineReporter) return outputTemplate(cmd, machineReporter)
} }
func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error { func outputTemplate(cmd *cobra.Command, responses []*ListReporter) error {
headers := report.Headers(machineReporter{}, map[string]string{ headers := report.Headers(ListReporter{}, map[string]string{
"LastUp": "LAST UP", "LastUp": "LAST UP",
"VmType": "VM TYPE", "VmType": "VM TYPE",
"CPUs": "CPUS", "CPUs": "CPUS",
@ -181,15 +181,15 @@ func streamName(imageStream string) string {
return imageStream return imageStream
} }
func toMachineFormat(vms []*machine.ListResponse) ([]*machineReporter, error) { func toMachineFormat(vms []*machine.ListResponse) ([]*ListReporter, error) {
cfg, err := config.ReadCustomConfig() cfg, err := config.ReadCustomConfig()
if err != nil { if err != nil {
return nil, err return nil, err
} }
machineResponses := make([]*machineReporter, 0, len(vms)) machineResponses := make([]*ListReporter, 0, len(vms))
for _, vm := range vms { for _, vm := range vms {
response := new(machineReporter) response := new(ListReporter)
response.Default = vm.Name == cfg.Engine.ActiveService response.Default = vm.Name == cfg.Engine.ActiveService
response.Name = vm.Name response.Name = vm.Name
response.Running = vm.Running response.Running = vm.Running
@ -209,15 +209,15 @@ func toMachineFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
return machineResponses, nil return machineResponses, nil
} }
func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) { func toHumanFormat(vms []*machine.ListResponse) ([]*ListReporter, error) {
cfg, err := config.ReadCustomConfig() cfg, err := config.ReadCustomConfig()
if err != nil { if err != nil {
return nil, err return nil, err
} }
humanResponses := make([]*machineReporter, 0, len(vms)) humanResponses := make([]*ListReporter, 0, len(vms))
for _, vm := range vms { for _, vm := range vms {
response := new(machineReporter) response := new(ListReporter)
if vm.Name == cfg.Engine.ActiveService { if vm.Name == cfg.Engine.ActiveService {
response.Name = vm.Name + "*" response.Name = vm.Name + "*"
response.Default = true response.Default = true

View File

@ -25,6 +25,7 @@ type initMachine struct {
memory *uint memory *uint
now bool now bool
timezone string timezone string
rootful bool
volumes []string volumes []string
cmd []string cmd []string

View File

@ -1,6 +1,8 @@
package e2e package e2e
import ( import (
"io/ioutil"
"os"
"time" "time"
"github.com/containers/podman/v4/pkg/machine" "github.com/containers/podman/v4/pkg/machine"
@ -74,4 +76,67 @@ var _ = Describe("podman machine init", func() {
Expect(inspectAfter[0].State).To(Equal(machine.Running)) Expect(inspectAfter[0].State).To(Equal(machine.Running))
}) })
It("machine init with cpus, disk size, memory, timezone", func() {
name := randomString(12)
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())
Expect(session).To(Exit(0))
s := new(startMachine)
startSession, err := mb.setCmd(s).run()
Expect(err).To(BeNil())
Expect(startSession).To(Exit(0))
sshCPU := sshMachine{}
CPUsession, err := mb.setName(name).setCmd(sshCPU.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
Expect(err).To(BeNil())
Expect(CPUsession).To(Exit(0))
Expect(CPUsession.outputToString()).To(ContainSubstring("2"))
sshDisk := sshMachine{}
diskSession, err := mb.setName(name).setCmd(sshDisk.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run()
Expect(err).To(BeNil())
Expect(diskSession).To(Exit(0))
Expect(diskSession.outputToString()).To(ContainSubstring("102 GiB"))
sshMemory := sshMachine{}
memorySession, err := mb.setName(name).setCmd(sshMemory.withSSHComand([]string{"cat", "/proc/meminfo", "|", "numfmt", "--field", "2", "--from-unit=Ki", "--to-unit=Mi", "|", "sed", "'s/ kB/M/g'", "|", "grep", "MemTotal"})).run()
Expect(err).To(BeNil())
Expect(memorySession).To(Exit(0))
Expect(memorySession.outputToString()).To(ContainSubstring("3824"))
sshTimezone := sshMachine{}
timezoneSession, err := mb.setName(name).setCmd(sshTimezone.withSSHComand([]string{"date"})).run()
Expect(err).To(BeNil())
Expect(timezoneSession).To(Exit(0))
Expect(timezoneSession.outputToString()).To(ContainSubstring("HST"))
})
It("machine init with volume", func() {
tmpDir, err := ioutil.TempDir("", "")
Expect(err).To(BeNil())
_, err = ioutil.TempFile(tmpDir, "example")
Expect(err).To(BeNil())
mount := tmpDir + ":/testmountdir"
defer os.RemoveAll(tmpDir)
name := randomString(12)
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withVolume(mount)).run()
Expect(err).To(BeNil())
Expect(session).To(Exit(0))
s := new(startMachine)
startSession, err := mb.setCmd(s).run()
Expect(err).To(BeNil())
Expect(startSession).To(Exit(0))
ssh2 := sshMachine{}
sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"ls /testmountdir"})).run()
Expect(err).To(BeNil())
Expect(sshSession2).To(Exit(0))
Expect(sshSession2.outputToString()).To(ContainSubstring("example"))
})
}) })

View File

@ -3,7 +3,9 @@ package e2e
import ( import (
"encoding/json" "encoding/json"
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/qemu" "github.com/containers/podman/v4/pkg/machine/qemu"
jsoniter "github.com/json-iterator/go"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -67,4 +69,29 @@ var _ = Describe("podman machine stop", func() {
// mb.names = []string{} // mb.names = []string{}
}) })
It("inspect with go format", func() {
name := randomString(12)
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()
Expect(err).To(BeNil())
Expect(inspectSession).To(Exit(0))
var inspectInfo []machine.InspectInfo
err = jsoniter.Unmarshal(inspectSession.Bytes(), &inspectInfo)
Expect(err).To(BeNil())
inspect := new(inspectMachine)
inspect = inspect.withFormat("{{.Name}}")
inspectSession, err = mb.setName(name).setCmd(inspect).run()
Expect(err).To(BeNil())
Expect(inspectSession).To(Exit(0))
Expect(inspectSession.Bytes()).To(ContainSubstring(name))
})
}) })

View File

@ -4,6 +4,8 @@ import (
"strings" "strings"
"github.com/containers/buildah/util" "github.com/containers/buildah/util"
"github.com/containers/podman/v4/cmd/podman/machine"
jsoniter "github.com/json-iterator/go"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec" . "github.com/onsi/gomega/gexec"
@ -40,7 +42,7 @@ var _ = Describe("podman machine list", func() {
Expect(len(secondList.outputToStringSlice())).To(Equal(2)) // one machine and the header Expect(len(secondList.outputToStringSlice())).To(Equal(2)) // one machine and the header
}) })
It("list machines with quiet", 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(12)
name2 := randomString(12) name2 := randomString(12)
@ -51,6 +53,11 @@ var _ = Describe("podman machine list", func() {
Expect(firstList).Should(Exit(0)) Expect(firstList).Should(Exit(0))
Expect(len(firstList.outputToStringSlice())).To(Equal(0)) // No header with quiet Expect(len(firstList.outputToStringSlice())).To(Equal(0)) // No header with quiet
noheaderSession, err := mb.setCmd(list.withNoHeading()).run() // noheader
Expect(err).NotTo(HaveOccurred())
Expect(noheaderSession).Should(Exit(0))
Expect(len(noheaderSession.outputToStringSlice())).To(Equal(0))
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()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -97,6 +104,38 @@ var _ = Describe("podman machine list", func() {
Expect(listSession.outputToString()).To(ContainSubstring("Currently running")) Expect(listSession.outputToString()).To(ContainSubstring("Currently running"))
Expect(listSession.outputToString()).NotTo(ContainSubstring("Less than a second ago")) // check to make sure time created is accurate Expect(listSession.outputToString()).NotTo(ContainSubstring("Less than a second ago")) // check to make sure time created is accurate
}) })
It("list with --format", func() {
// Random names for machines to test list
name1 := randomString(12)
i := new(initMachine)
session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil())
Expect(session).To(Exit(0))
// go format
list := new(listMachine)
listSession, err := mb.setCmd(list.withFormat("{{.Name}}").withNoHeading()).run()
Expect(err).NotTo(HaveOccurred())
Expect(listSession).To(Exit(0))
Expect(len(listSession.outputToStringSlice())).To(Equal(1))
listNames := listSession.outputToStringSlice()
stripAsterisk(listNames)
Expect(util.StringInSlice(name1, listNames)).To(BeTrue())
// --format json
list2 := new(listMachine)
list2 = list2.withFormat("json")
listSession2, err := mb.setName("foo1").setCmd(list2).run()
Expect(err).To(BeNil())
Expect(listSession2).To(Exit(0))
var listResponse []*machine.ListReporter
err = jsoniter.Unmarshal(listSession.Bytes(), &listResponse)
Expect(err).To(BeNil())
})
}) })
func stripAsterisk(sl []string) { func stripAsterisk(sl []string) {

View File

@ -19,7 +19,7 @@ var _ = Describe("podman machine set", func() {
teardown(originalHomeDir, testDir, mb) teardown(originalHomeDir, testDir, mb)
}) })
It("set machine cpus", func() { It("set machine cpus, disk, memory", func() {
name := randomString(12) name := randomString(12)
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()
@ -27,40 +27,11 @@ var _ = Describe("podman machine set", func() {
Expect(session).To(Exit(0)) Expect(session).To(Exit(0))
set := setMachine{} set := setMachine{}
setSession, err := mb.setName(name).setCmd(set.withCPUs(2)).run() setSession, err := mb.setName(name).setCmd(set.withCPUs(2).withDiskSize(102).withMemory(4000)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(setSession).To(Exit(0)) Expect(setSession).To(Exit(0))
s := new(startMachine) // shrinking disk size is verboten
startSession, err := mb.setCmd(s).run()
Expect(err).To(BeNil())
Expect(startSession).To(Exit(0))
ssh2 := sshMachine{}
sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
Expect(err).To(BeNil())
Expect(sshSession2).To(Exit(0))
Expect(sshSession2.outputToString()).To(ContainSubstring("2"))
// Setting a running machine results in 125
runner, err := mb.setName(name).setCmd(set.withCPUs(4)).run()
Expect(err).To(BeNil())
Expect(runner).To(Exit(125))
})
It("increase machine disk size", func() {
name := randomString(12)
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).To(BeNil())
Expect(session).To(Exit(0))
set := setMachine{}
setSession, err := mb.setName(name).setCmd(set.withDiskSize(102)).run()
Expect(err).To(BeNil())
Expect(setSession).To(Exit(0))
// shrinking disk size iss verboten
shrink, err := mb.setName(name).setCmd(set.withDiskSize(5)).run() shrink, err := mb.setName(name).setCmd(set.withDiskSize(5)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(shrink).To(Exit(125)) Expect(shrink).To(Exit(125))
@ -70,35 +41,28 @@ var _ = Describe("podman machine set", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(startSession).To(Exit(0)) Expect(startSession).To(Exit(0))
ssh2 := sshMachine{} sshCPU := sshMachine{}
sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run() CPUsession, err := mb.setName(name).setCmd(sshCPU.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(sshSession2).To(Exit(0)) Expect(CPUsession).To(Exit(0))
Expect(sshSession2.outputToString()).To(ContainSubstring("102 GiB")) Expect(CPUsession.outputToString()).To(ContainSubstring("2"))
})
It("set machine ram", func() { sshDisk := sshMachine{}
name := randomString(12) diskSession, err := mb.setName(name).setCmd(sshDisk.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run()
i := new(initMachine)
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(diskSession).To(Exit(0))
Expect(diskSession.outputToString()).To(ContainSubstring("102 GiB"))
set := setMachine{} sshMemory := sshMachine{}
setSession, err := mb.setName(name).setCmd(set.withMemory(4000)).run() memorySession, err := mb.setName(name).setCmd(sshMemory.withSSHComand([]string{"cat", "/proc/meminfo", "|", "numfmt", "--field", "2", "--from-unit=Ki", "--to-unit=Mi", "|", "sed", "'s/ kB/M/g'", "|", "grep", "MemTotal"})).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(setSession).To(Exit(0)) Expect(memorySession).To(Exit(0))
Expect(memorySession.outputToString()).To(ContainSubstring("3824"))
s := new(startMachine) // Setting a running machine results in 125
startSession, err := mb.setCmd(s).run() runner, err := mb.setName(name).setCmd(set.withCPUs(4)).run()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(startSession).To(Exit(0)) Expect(runner).To(Exit(125))
ssh2 := sshMachine{}
sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"cat", "/proc/meminfo", "|", "numfmt", "--field", "2", "--from-unit=Ki", "--to-unit=Mi", "|", "sed", "'s/ kB/M/g'", "|", "grep", "MemTotal"})).run()
Expect(err).To(BeNil())
Expect(sshSession2).To(Exit(0))
Expect(sshSession2.outputToString()).To(ContainSubstring("3824"))
}) })
It("no settings should change if no flags", func() { It("no settings should change if no flags", func() {