|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + "github.com/onsi/gomega/gexec" |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +var _ = Describe("provider libkrun test", Label("libkrun", "darwin"), func() { |
| 16 | + BeforeEach(func() { |
| 17 | + session := macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 18 | + session.WaitWithDefaultTimeout() |
| 19 | + Expect(session).Should(gexec.Exit(0)) |
| 20 | + err := json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 21 | + Expect(err).NotTo(HaveOccurred()) |
| 22 | + Expect(len(machineResponses)).Should(Equal(0)) |
| 23 | + }) |
| 24 | + |
| 25 | + AfterEach(func() { |
| 26 | + report := CurrentSpecReport() |
| 27 | + labels := report.Labels() |
| 28 | + stopCmd := "stop --provider libkrun" |
| 29 | + rmCmd := "rm --provider libkrun -f" |
| 30 | + for _, l := range labels { |
| 31 | + if l == "name" { |
| 32 | + stopCmd = "stop --provider libkrun myVM" |
| 33 | + rmCmd = "rm --provider libkrun myVM -f" |
| 34 | + break |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // stop the CentOS VM |
| 39 | + session := macadamTest.Macadam(strings.Fields(stopCmd)) |
| 40 | + session.WaitWithDefaultTimeout() |
| 41 | + Expect(session).Should(gexec.Exit(0)) |
| 42 | + Expect(session.OutputToString()).Should(ContainSubstring("stopped successfully")) |
| 43 | + |
| 44 | + // rm the CentOS VM and verify that "list" does not return any vm |
| 45 | + session = macadamTest.Macadam(strings.Fields(rmCmd)) |
| 46 | + session.WaitWithDefaultTimeout() |
| 47 | + Expect(session).Should(gexec.Exit(0)) |
| 48 | + |
| 49 | + session = macadamTest.Macadam([]string{"list", "--provider", "libkrun", "--format", "json"}) |
| 50 | + session.WaitWithDefaultTimeout() |
| 51 | + Expect(session).Should(gexec.Exit(0)) |
| 52 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 53 | + Expect(err).NotTo(HaveOccurred()) |
| 54 | + Expect(len(machineResponses)).Should(Equal(0)) |
| 55 | + }) |
| 56 | + |
| 57 | + It("init CentOS VM with cpu, disk and memory setup", Label("cpu"), func() { |
| 58 | + // init a CentOS VM with cpu and disk-size setup |
| 59 | + session := macadamTest.Macadam([]string{"init", "--provider", "libkrun", "--cpus", "3", "--disk-size", "30", "--memory", "2048", image}) |
| 60 | + session.WaitWithDefaultTimeout() |
| 61 | + Expect(session).Should(gexec.Exit(0)) |
| 62 | + |
| 63 | + // check the list command returns one item |
| 64 | + session = macadamTest.Macadam([]string{"list", "--provider", "libkrun", "--format", "json"}) |
| 65 | + session.WaitWithDefaultTimeout() |
| 66 | + Expect(session).Should(gexec.Exit(0)) |
| 67 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 68 | + Expect(err).NotTo(HaveOccurred()) |
| 69 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 70 | + |
| 71 | + // start the CentOS VM |
| 72 | + session = macadamTest.Macadam([]string{"start", "--provider", "libkrun"}) |
| 73 | + session.WaitWithDefaultTimeout() |
| 74 | + Expect(session).Should(gexec.Exit(0)) |
| 75 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 76 | + |
| 77 | + // ssh into the VM and prints user |
| 78 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "nproc"}) |
| 79 | + session.WaitWithDefaultTimeout() |
| 80 | + Expect(session).Should(gexec.Exit(0)) |
| 81 | + Expect(session.OutputToString()).Should(Equal("3")) |
| 82 | + |
| 83 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "lsblk"}) |
| 84 | + session.WaitWithDefaultTimeout() |
| 85 | + Expect(session).Should(gexec.Exit(0)) |
| 86 | + Expect(session.OutputToString()).Should(ContainSubstring("30G")) |
| 87 | + |
| 88 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "free", "-h"}) |
| 89 | + session.WaitWithDefaultTimeout() |
| 90 | + Expect(session).Should(gexec.Exit(0)) |
| 91 | + // Verify memory is close to 2048MB (allow for system overhead) |
| 92 | + output := session.OutputToString() |
| 93 | + Expect(output).Should(Or(ContainSubstring("1.7G"), ContainSubstring("1.8G"), ContainSubstring("1.9G"), ContainSubstring("2.0G"))) |
| 94 | + }) |
| 95 | + |
| 96 | + It("init CentOS VM with username and sshkey setup", Label("sshkey"), func() { |
| 97 | + // init a CentOS VM with cpu and disk-size setup |
| 98 | + session := macadamTest.Macadam([]string{"init", "--provider", "libkrun", "--username", "test", "--ssh-identity-path", keypath, image}) |
| 99 | + session.WaitWithDefaultTimeout() |
| 100 | + Expect(session).Should(gexec.Exit(0)) |
| 101 | + |
| 102 | + // check the list command returns one item |
| 103 | + session = macadamTest.Macadam([]string{"list", "--provider", "libkrun", "--format", "json"}) |
| 104 | + session.WaitWithDefaultTimeout() |
| 105 | + Expect(session).Should(gexec.Exit(0)) |
| 106 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 109 | + |
| 110 | + // start the CentOS VM |
| 111 | + session = macadamTest.Macadam([]string{"start", "--provider", "libkrun"}) |
| 112 | + session.WaitWithDefaultTimeout() |
| 113 | + Expect(session).Should(gexec.Exit(0)) |
| 114 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 115 | + |
| 116 | + // ssh into the VM and prints user |
| 117 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "--username", "test", "whoami"}) |
| 118 | + session.WaitWithDefaultTimeout() |
| 119 | + Expect(session).Should(gexec.Exit(0)) |
| 120 | + Expect(session.OutputToString()).Should(Equal("test")) |
| 121 | + }) |
| 122 | + |
| 123 | + It("init CentOS VM with cloud-init setup", Label("cloudinit"), func() { |
| 124 | + // init a CentOS VM with cpu and disk-size setup |
| 125 | + session := macadamTest.Macadam([]string{"init", "--provider", "libkrun", "--cloud-init", cloudinitPath, "--username", "macadamtest", "--ssh-identity-path", keypath, image}) |
| 126 | + session.WaitWithDefaultTimeout() |
| 127 | + Expect(session).Should(gexec.Exit(0)) |
| 128 | + |
| 129 | + // check the list command returns one item |
| 130 | + session = macadamTest.Macadam([]string{"list", "--provider", "libkrun", "--format", "json"}) |
| 131 | + session.WaitWithDefaultTimeout() |
| 132 | + Expect(session).Should(gexec.Exit(0)) |
| 133 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 134 | + Expect(err).NotTo(HaveOccurred()) |
| 135 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 136 | + |
| 137 | + // start the CentOS VM |
| 138 | + session = macadamTest.Macadam([]string{"start", "--provider", "libkrun"}) |
| 139 | + session.WaitWithDefaultTimeout() |
| 140 | + Expect(session).Should(gexec.Exit(0)) |
| 141 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 142 | + |
| 143 | + // ssh into the VM and prints user |
| 144 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "whoami"}) |
| 145 | + session.WaitWithDefaultTimeout() |
| 146 | + Expect(session).Should(gexec.Exit(0)) |
| 147 | + Expect(session.OutputToString()).Should(Equal("macadamtest")) |
| 148 | + |
| 149 | + // wait until cloud-init finish |
| 150 | + Eventually(func() string { |
| 151 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "systemctl", "status", "cloud-final"}) |
| 152 | + session.WaitWithDefaultTimeout() |
| 153 | + |
| 154 | + if session.ExitCode() != 0 { |
| 155 | + return "" |
| 156 | + } |
| 157 | + return session.OutputToString() |
| 158 | + }, 20*time.Minute, 30*time.Second).Should(ContainSubstring("Active: active (exited)")) |
| 159 | + |
| 160 | + fmt.Println("cloud-init has finished") |
| 161 | + |
| 162 | + // ssh into the VM and check installed app |
| 163 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "git", "--version"}) |
| 164 | + session.WaitWithDefaultTimeout() |
| 165 | + Expect(session).Should(gexec.Exit(0)) |
| 166 | + Expect(session.OutputToString()).Should(ContainSubstring("git version")) |
| 167 | + |
| 168 | + // ssh into the VM and check file created |
| 169 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "ls", "/home/macadamtest/hello.txt"}) |
| 170 | + session.WaitWithDefaultTimeout() |
| 171 | + Expect(session).Should(gexec.Exit(0)) |
| 172 | + }) |
| 173 | + |
| 174 | + It("init CentOS VM with name", Label("name"), func() { |
| 175 | + // init a CentOS VM with name setup |
| 176 | + session := macadamTest.Macadam([]string{"init", "--provider", "libkrun", "--name", "myVM", image}) |
| 177 | + session.WaitWithDefaultTimeout() |
| 178 | + Expect(session).Should(gexec.Exit(0)) |
| 179 | + |
| 180 | + // check the list command returns one item |
| 181 | + session = macadamTest.Macadam([]string{"list", "--provider", "libkrun", "--format", "json"}) |
| 182 | + session.WaitWithDefaultTimeout() |
| 183 | + Expect(session).Should(gexec.Exit(0)) |
| 184 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 185 | + Expect(err).NotTo(HaveOccurred()) |
| 186 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 187 | + |
| 188 | + // start the CentOS VM with set name |
| 189 | + session = macadamTest.Macadam([]string{"start", "--provider", "libkrun", "myVM"}) |
| 190 | + session.WaitWithDefaultTimeout() |
| 191 | + Expect(session).Should(gexec.Exit(0)) |
| 192 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 193 | + |
| 194 | + // ssh into the VM and prints user |
| 195 | + session = macadamTest.Macadam([]string{"ssh", "--provider", "libkrun", "myVM", "whoami"}) |
| 196 | + session.WaitWithDefaultTimeout() |
| 197 | + Expect(session).Should(gexec.Exit(0)) |
| 198 | + Expect(session.OutputToString()).Should(Equal("core")) |
| 199 | + }) |
| 200 | + |
| 201 | +}) |
0 commit comments