github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/machine/e2e/init_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/hanks177/podman/v4/pkg/machine"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("podman machine init", func() {
    16  	var (
    17  		mb      *machineTestBuilder
    18  		testDir string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		testDir, mb = setup()
    23  	})
    24  	AfterEach(func() {
    25  		teardown(originalHomeDir, testDir, mb)
    26  	})
    27  
    28  	It("bad init name", func() {
    29  		i := initMachine{}
    30  		reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    31  		session, err := mb.setName(reallyLongName).setCmd(&i).run()
    32  		Expect(err).To(BeNil())
    33  		Expect(session).To(Exit(125))
    34  	})
    35  	It("simple init", func() {
    36  		i := new(initMachine)
    37  		session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run()
    38  		Expect(err).To(BeNil())
    39  		Expect(session).To(Exit(0))
    40  
    41  		inspectBefore, ec, err := mb.toQemuInspectInfo()
    42  		Expect(err).To(BeNil())
    43  		Expect(ec).To(BeZero())
    44  
    45  		Expect(len(inspectBefore)).To(BeNumerically(">", 0))
    46  		testMachine := inspectBefore[0]
    47  		Expect(testMachine.VM.Name).To(Equal(mb.names[0]))
    48  		Expect(testMachine.VM.CPUs).To(Equal(uint64(1)))
    49  		Expect(testMachine.VM.Memory).To(Equal(uint64(2048)))
    50  
    51  	})
    52  
    53  	It("simple init with start", func() {
    54  		i := initMachine{}
    55  		session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run()
    56  		Expect(err).To(BeNil())
    57  		Expect(session).To(Exit(0))
    58  
    59  		inspectBefore, ec, err := mb.toQemuInspectInfo()
    60  		Expect(ec).To(BeZero())
    61  		Expect(len(inspectBefore)).To(BeNumerically(">", 0))
    62  		Expect(err).To(BeNil())
    63  		Expect(len(inspectBefore)).To(BeNumerically(">", 0))
    64  		Expect(inspectBefore[0].VM.Name).To(Equal(mb.names[0]))
    65  
    66  		s := startMachine{}
    67  		ssession, err := mb.setCmd(s).setTimeout(time.Minute * 10).run()
    68  		Expect(err).To(BeNil())
    69  		Expect(ssession).Should(Exit(0))
    70  
    71  		inspectAfter, ec, err := mb.toQemuInspectInfo()
    72  		Expect(err).To(BeNil())
    73  		Expect(ec).To(BeZero())
    74  		Expect(len(inspectBefore)).To(BeNumerically(">", 0))
    75  		Expect(len(inspectAfter)).To(BeNumerically(">", 0))
    76  		Expect(inspectAfter[0].State).To(Equal(machine.Running))
    77  	})
    78  
    79  	It("machine init with cpus, disk size, memory, timezone", func() {
    80  		name := randomString(12)
    81  		i := new(initMachine)
    82  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withCPUs(2).withDiskSize(102).withMemory(4000).withTimezone("Pacific/Honolulu")).run()
    83  		Expect(err).To(BeNil())
    84  		Expect(session).To(Exit(0))
    85  
    86  		s := new(startMachine)
    87  		startSession, err := mb.setCmd(s).run()
    88  		Expect(err).To(BeNil())
    89  		Expect(startSession).To(Exit(0))
    90  
    91  		sshCPU := sshMachine{}
    92  		CPUsession, err := mb.setName(name).setCmd(sshCPU.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
    93  		Expect(err).To(BeNil())
    94  		Expect(CPUsession).To(Exit(0))
    95  		Expect(CPUsession.outputToString()).To(ContainSubstring("2"))
    96  
    97  		sshDisk := sshMachine{}
    98  		diskSession, err := mb.setName(name).setCmd(sshDisk.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run()
    99  		Expect(err).To(BeNil())
   100  		Expect(diskSession).To(Exit(0))
   101  		Expect(diskSession.outputToString()).To(ContainSubstring("102 GiB"))
   102  
   103  		sshMemory := sshMachine{}
   104  		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()
   105  		Expect(err).To(BeNil())
   106  		Expect(memorySession).To(Exit(0))
   107  		Expect(memorySession.outputToString()).To(ContainSubstring("3824"))
   108  
   109  		sshTimezone := sshMachine{}
   110  		timezoneSession, err := mb.setName(name).setCmd(sshTimezone.withSSHComand([]string{"date"})).run()
   111  		Expect(err).To(BeNil())
   112  		Expect(timezoneSession).To(Exit(0))
   113  		Expect(timezoneSession.outputToString()).To(ContainSubstring("HST"))
   114  	})
   115  
   116  	It("machine init with volume", func() {
   117  		tmpDir, err := ioutil.TempDir("", "")
   118  		Expect(err).To(BeNil())
   119  		_, err = ioutil.TempFile(tmpDir, "example")
   120  		Expect(err).To(BeNil())
   121  		mount := tmpDir + ":/testmountdir"
   122  		defer os.RemoveAll(tmpDir)
   123  
   124  		name := randomString(12)
   125  		i := new(initMachine)
   126  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withVolume(mount)).run()
   127  		Expect(err).To(BeNil())
   128  		Expect(session).To(Exit(0))
   129  
   130  		s := new(startMachine)
   131  		startSession, err := mb.setCmd(s).run()
   132  		Expect(err).To(BeNil())
   133  		Expect(startSession).To(Exit(0))
   134  
   135  		ssh2 := sshMachine{}
   136  		sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"ls /testmountdir"})).run()
   137  		Expect(err).To(BeNil())
   138  		Expect(sshSession2).To(Exit(0))
   139  		Expect(sshSession2.outputToString()).To(ContainSubstring("example"))
   140  	})
   141  
   142  })