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