|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + |
| 10 | + "github.com/crc-org/macadam/test/osprovider" |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "github.com/onsi/gomega/gexec" |
| 14 | +) |
| 15 | + |
| 16 | +var tempDir string |
| 17 | +var machineResponses []ListReporter |
| 18 | +var err error |
| 19 | +var image string |
| 20 | +var keypath string |
| 21 | +var cloudinitPath string |
| 22 | + |
| 23 | +var _ = BeforeSuite(func() { |
| 24 | + tempDir, err = os.MkdirTemp("", "test-") |
| 25 | + Expect(err).NotTo(HaveOccurred()) |
| 26 | + |
| 27 | + // download CentOS image |
| 28 | + centosProvider := osprovider.NewCentosProvider() |
| 29 | + image, err = centosProvider.Fetch(tempDir) |
| 30 | + fmt.Println(image) |
| 31 | + Expect(err).NotTo(HaveOccurred()) |
| 32 | + |
| 33 | + keypath := filepath.Join(tempDir,"id_rsa") |
| 34 | + cloudinitPath := filepath.Join(tempDir,"user-data") |
| 35 | + //generate ssh key |
| 36 | + cmd := exec.Command("ssh-keygen", "-t", "rsa", "-f", keypath, "-N", "") |
| 37 | + err := cmd.Run() |
| 38 | + Expect(err).ShouldNot(HaveOccurred()) |
| 39 | + //copy user-data |
| 40 | + wd, err := os.Getwd() |
| 41 | + if err != nil { |
| 42 | + fmt.Printf("failed to get working directory: %v\n", err) |
| 43 | + } |
| 44 | + cloudinit := wd+"/../testdata/user-data" |
| 45 | + content, err := os.ReadFile(cloudinit) |
| 46 | + if err != nil { |
| 47 | + fmt.Printf("failed to read %s: %v\n", cloudinit, err) |
| 48 | + } |
| 49 | + err = os.WriteFile(cloudinitPath, []byte(content), 0644) |
| 50 | + if err != nil { |
| 51 | + fmt.Printf("failed to write to %s: %v\n", cloudinitPath, err) |
| 52 | + } |
| 53 | +}) |
| 54 | + |
| 55 | +var _ = AfterSuite(func() { |
| 56 | + os.RemoveAll(tempDir) |
| 57 | +}) |
| 58 | + |
| 59 | + |
| 60 | +var _ = Describe("Macadam init setup test", Label("init"), func() { |
| 61 | + BeforeEach(func() { |
| 62 | + session := macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 63 | + Eventually(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 | + // stop the CentOS VM |
| 71 | + session := macadamTest.Macadam([]string{"stop"}) |
| 72 | + session.WaitWithDefaultTimeout() |
| 73 | + Expect(session).Should(gexec.Exit()) |
| 74 | + Expect(session.OutputToString()).Should(ContainSubstring("stopped successfully")) |
| 75 | + |
| 76 | + // rm the CentOS VM and verify that "list" does not return any vm |
| 77 | + session = macadamTest.Macadam([]string{"rm", "-f"}) |
| 78 | + session.WaitWithDefaultTimeout() |
| 79 | + Expect(session).Should(gexec.Exit()) |
| 80 | + |
| 81 | + session = macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 82 | + session.WaitWithDefaultTimeout() |
| 83 | + Expect(session).Should(gexec.Exit()) |
| 84 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 85 | + Expect(err).NotTo(HaveOccurred()) |
| 86 | + Expect(len(machineResponses)).Should(Equal(0)) |
| 87 | + }) |
| 88 | + |
| 89 | + It("init CentOS VM with cpu, disk and memory setup", Label("cpu"), func() { |
| 90 | + // init a CentOS VM with cpu and disk-size setup |
| 91 | + session := macadamTest.Macadam([]string{"init", "--cpus", "3", "--disk-size", "30", "--memory","2048", image}) |
| 92 | + session.WaitWithDefaultTimeout() |
| 93 | + Expect(session).Should(gexec.Exit(0)) |
| 94 | + |
| 95 | + |
| 96 | + // check the list command returns one item |
| 97 | + session = macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 98 | + session.WaitWithDefaultTimeout() |
| 99 | + Expect(session).Should(gexec.Exit()) |
| 100 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 103 | + |
| 104 | + // start the CentOS VM |
| 105 | + session = macadamTest.Macadam([]string{"start"}) |
| 106 | + session.WaitWithDefaultTimeout() |
| 107 | + Expect(session).Should(gexec.Exit()) |
| 108 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 109 | + |
| 110 | + // ssh into the VM and prints user |
| 111 | + session = macadamTest.Macadam([]string{"ssh", "nproc"}) |
| 112 | + session.WaitWithDefaultTimeout() |
| 113 | + Expect(session).Should(gexec.Exit()) |
| 114 | + Expect(session.OutputToString()).Should(Equal("3")) |
| 115 | + |
| 116 | + session = macadamTest.Macadam([]string{"ssh", "lsblk"}) |
| 117 | + session.WaitWithDefaultTimeout() |
| 118 | + Expect(session).Should(gexec.Exit()) |
| 119 | + Expect(session.OutputToString()).Should(ContainSubstring("30G")) |
| 120 | + |
| 121 | + session = macadamTest.Macadam([]string{"ssh", "free", "-h"}) |
| 122 | + session.WaitWithDefaultTimeout() |
| 123 | + Expect(session).Should(gexec.Exit()) |
| 124 | + fmt.Println(session.OutputToString()) |
| 125 | + Expect(session.OutputToString()).Should(ContainSubstring("1.7")) |
| 126 | + }) |
| 127 | + |
| 128 | + It("init CentOS VM with username and sshkey setup", Label("test"), func() { |
| 129 | + // init a CentOS VM with cpu and disk-size setup |
| 130 | + session := macadamTest.Macadam([]string{"init", "--username", "test","--ssh-identity-path", keypath, image}) |
| 131 | + session.WaitWithDefaultTimeout() |
| 132 | + Expect(session).Should(gexec.Exit(0)) |
| 133 | + |
| 134 | + // check the list command returns one item |
| 135 | + session = macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 136 | + session.WaitWithDefaultTimeout() |
| 137 | + Expect(session).Should(gexec.Exit()) |
| 138 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 139 | + Expect(err).NotTo(HaveOccurred()) |
| 140 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 141 | + |
| 142 | + // start the CentOS VM |
| 143 | + session = macadamTest.Macadam([]string{"start"}) |
| 144 | + session.WaitWithDefaultTimeout() |
| 145 | + Expect(session).Should(gexec.Exit()) |
| 146 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 147 | + |
| 148 | + // ssh into the VM and prints user |
| 149 | + session = macadamTest.Macadam([]string{"ssh","--username", "test", "whoami"}) |
| 150 | + session.WaitWithDefaultTimeout() |
| 151 | + Expect(session).Should(gexec.Exit()) |
| 152 | + Expect(session.OutputToString()).Should(Equal("test")) |
| 153 | + }) |
| 154 | + |
| 155 | + It("init CentOS VM with cloud-init setup", Label("cloudinit"), func() { |
| 156 | + // init a CentOS VM with cpu and disk-size setup |
| 157 | + session := macadamTest.Macadam([]string{"init", "--cloud-init", cloudinitPath,"--username", "macadamtest", "--ssh-identity-path", keypath, image}) |
| 158 | + session.WaitWithDefaultTimeout() |
| 159 | + Expect(session).Should(gexec.Exit(0)) |
| 160 | + |
| 161 | + |
| 162 | + // check the list command returns one item |
| 163 | + session = macadamTest.Macadam([]string{"list", "--format", "json"}) |
| 164 | + session.WaitWithDefaultTimeout() |
| 165 | + Expect(session).Should(gexec.Exit()) |
| 166 | + err = json.Unmarshal(session.Out.Contents(), &machineResponses) |
| 167 | + Expect(err).NotTo(HaveOccurred()) |
| 168 | + Expect(len(machineResponses)).Should(Equal(1)) |
| 169 | + |
| 170 | + // start the CentOS VM |
| 171 | + session = macadamTest.Macadam([]string{"start"}) |
| 172 | + session.WaitWithDefaultTimeout() |
| 173 | + Expect(session).Should(gexec.Exit()) |
| 174 | + Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) |
| 175 | + |
| 176 | + // ssh into the VM and prints user |
| 177 | + session = macadamTest.Macadam([]string{"ssh", "whoami"}) |
| 178 | + session.WaitWithDefaultTimeout() |
| 179 | + Expect(session).Should(gexec.Exit()) |
| 180 | + Expect(session.OutputToString()).Should(Equal("macadamtest")) |
| 181 | + }) |
| 182 | + |
| 183 | +}) |
0 commit comments