-
Notifications
You must be signed in to change notification settings - Fork 11
add test cases for multiple vm #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,110 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/crc-org/macadam/test/osprovider" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/onsi/gomega/gexec" | ||
| ) | ||
|
|
||
| var ( | ||
| macadamTest *MacadamTestIntegration | ||
|
|
||
| _ = BeforeEach(func() { | ||
| macadamTest = MacadamTestCreate() | ||
| }) | ||
| machineResponses []ListReporter | ||
| err error | ||
| tempDir string | ||
| image string | ||
| keypath string | ||
| cloudinitPath string | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| func TestSuite(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Macadam suite") | ||
| } | ||
|
|
||
| func noVMcheck() { | ||
| session := macadamTest.Macadam([]string{"list", "--format", "json"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| err := json.Unmarshal(session.Out.Contents(), &machineResponses) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(len(machineResponses)).Should(Equal(0)) | ||
| } | ||
|
|
||
| func removeAllVM() { | ||
| session := macadamTest.Macadam([]string{"list", "--format", "json"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| err = json.Unmarshal(session.Out.Contents(), &machineResponses) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| for _, m := range machineResponses { | ||
| stopCmd := "stop " + string(m.Name) | ||
| rmCmd := "rm -f " + string(m.Name) | ||
|
|
||
| // stop the CentOS VM | ||
| session = macadamTest.Macadam(strings.Fields(stopCmd)) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(ContainSubstring("stopped successfully")) | ||
|
|
||
| // rm the CentOS VM and verify that "list" does not return any vm | ||
| session = macadamTest.Macadam(strings.Fields(rmCmd)) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| } | ||
|
|
||
| session = macadamTest.Macadam([]string{"list", "--format", "json"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| err = json.Unmarshal(session.Out.Contents(), &machineResponses) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(len(machineResponses)).Should(Equal(0)) | ||
| } | ||
|
|
||
| var _ = BeforeSuite(func() { | ||
| tempDir, err = os.MkdirTemp("", "test-") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| // download CentOS image | ||
| centosProvider := osprovider.NewCentosProvider() | ||
| image, err = centosProvider.Fetch(tempDir) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| keypath = filepath.Join(tempDir, "id_rsa") | ||
| cloudinitPath = filepath.Join(tempDir, "user-data") | ||
| //generate ssh key | ||
| cmd := exec.Command("ssh-keygen", "-t", "rsa", "-f", keypath, "-N", "") | ||
| err = cmd.Run() | ||
| Expect(err).ShouldNot(HaveOccurred()) | ||
| //copy user-data | ||
| wd, err := os.Getwd() | ||
| Expect(err).ShouldNot(HaveOccurred()) | ||
| cloudinit := wd + "/../testdata/user-data" | ||
| content, err := os.ReadFile(cloudinit) | ||
| Expect(err).ShouldNot(HaveOccurred()) | ||
| //replace ssh pub key to user-data file | ||
| pubkeypath := keypath + ".pub" | ||
| pubkey, err := os.ReadFile(pubkeypath) | ||
| Expect(err).ShouldNot(HaveOccurred()) | ||
| newContent := strings.ReplaceAll(string(content), "[sshkey]", string(pubkey)) | ||
| err = os.WriteFile(cloudinitPath, []byte(newContent), 0644) | ||
| Expect(err).ShouldNot(HaveOccurred()) | ||
| }) | ||
|
|
||
| var _ = AfterSuite(func() { | ||
| os.RemoveAll(tempDir) | ||
| }) | ||
|
|
||
| var _ = BeforeEach(func() { | ||
| macadamTest = MacadamTestCreate() | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/onsi/gomega/gexec" | ||
| ) | ||
|
|
||
| var _ = Describe("Macadam init setup test", Label("multiple"), func() { | ||
| BeforeEach(func() { | ||
| noVMcheck() | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| removeAllVM() | ||
| }) | ||
|
|
||
| It("create multiple CentOS VM", Label("mul-centos"), func() { | ||
| // init a CentOS VM with name vm1 | ||
| session := macadamTest.Macadam([]string{"init", "--name", "vm1", image}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
|
|
||
| // check the list command returns one item | ||
| session = macadamTest.Macadam([]string{"list", "--format", "json"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| err = json.Unmarshal(session.Out.Contents(), &machineResponses) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(len(machineResponses)).Should(Equal(1)) | ||
|
|
||
| // start the CentOS VM1 | ||
| session = macadamTest.Macadam([]string{"start", "vm1"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) | ||
|
|
||
| // ssh into the VM1 and prints user | ||
| session = macadamTest.Macadam([]string{"ssh", "vm1", "whoami"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(Equal("core")) | ||
|
Comment on lines
+40
to
+44
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is also done at the end of the file. Is it a duplicate to be removed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lstocchi This check ensures the first VM functions properly. The check at the end ensures that the first VM still works and is not affected by the newly created VM. |
||
|
|
||
| //init another centos VM with name vm2 | ||
| session = macadamTest.Macadam([]string{"init", "--name", "vm2", image}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
|
|
||
| // check the list command returns two items | ||
| session = macadamTest.Macadam([]string{"list", "--format", "json"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| err = json.Unmarshal(session.Out.Contents(), &machineResponses) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(len(machineResponses)).Should(Equal(2)) | ||
|
|
||
| // start the CentOS VM2 | ||
| session = macadamTest.Macadam([]string{"start", "vm2"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(ContainSubstring("started successfully")) | ||
|
|
||
| // ssh into the VM2 and prints user | ||
| session = macadamTest.Macadam([]string{"ssh", "vm2", "whoami"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(Equal("core")) | ||
|
|
||
| //check again VM1 still running | ||
| session = macadamTest.Macadam([]string{"ssh", "vm1", "whoami"}) | ||
| session.WaitWithDefaultTimeout() | ||
| Expect(session).Should(gexec.Exit(0)) | ||
| Expect(session.OutputToString()).Should(Equal("core")) | ||
| }) | ||
|
|
||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd propose to unify all var under the same var clause above. Having like
var (
macadamTest *MacadamTestIntegration
machineResponses []ListReporter
err error
tempDir string
image string
keypath string
cloudinitPath string
)
and move
outside as you did for BeforeSuite and AfterSuite. I read it is a best practice and I did it wrong when I created it there 👍