Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 93 additions & 4 deletions test/e2e/common_test.go
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
)



Copy link
Collaborator

@lstocchi lstocchi Nov 26, 2025

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

_ = BeforeEach(func() {
	macadamTest = MacadamTestCreate()
})

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 👍

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()
})
78 changes: 78 additions & 0 deletions test/e2e/multipleVM_test.go
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"))
})

})
81 changes: 3 additions & 78 deletions test/e2e/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,23 @@ package e2e
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/crc-org/macadam/test/osprovider"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)

var tempDir string
var machineResponses []ListReporter
var err error
var image string
var keypath string
var cloudinitPath string

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 _ = Describe("Macadam init setup test", Label("init"), func() {
BeforeEach(func() {
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))
noVMcheck()
})

AfterEach(func() {
report := CurrentSpecReport()
labels := report.Labels()
stopCmd := "stop"
rmCmd := "rm -f"
for _, l := range labels {
if l == "name" {
stopCmd = "stop myVM"
rmCmd = "rm myVM -f"
break
}
}

// 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))
removeAllVM()
})

It("init CentOS VM with cpu, disk and memory setup", Label("cpu"), func() {
Expand All @@ -112,6 +36,7 @@ var _ = Describe("Macadam init setup test", Label("init"), func() {
Expect(err).NotTo(HaveOccurred())
Expect(len(machineResponses)).Should(Equal(1))


// start the CentOS VM
session = macadamTest.Macadam([]string{"start"})
session.WaitWithDefaultTimeout()
Expand Down
17 changes: 3 additions & 14 deletions test/e2e/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/json"
"os"

"github.com/crc-org/macadam/test/osprovider"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)

type ListReporter struct {
Name string
Image string
Created string
Running bool
Expand Down Expand Up @@ -41,21 +41,10 @@ var _ = Describe("Macadam", func() {

It("creates a new CentOS VM, starts it, ssh in and cleans", func() {
// verify there is no vm
var machineResponses []ListReporter
session := macadamTest.Macadam([]string{"list", "--format", "json"})
session.WaitWithDefaultTimeout()
Expect(session).Should(gexec.Exit())
err := json.Unmarshal(session.Out.Contents(), &machineResponses)
Expect(err).NotTo(HaveOccurred())
Expect(len(machineResponses)).Should(Equal(0))

// download CentOS image
centosProvider := osprovider.NewCentosProvider()
image, err := centosProvider.Fetch(tempDir)
Expect(err).NotTo(HaveOccurred())
noVMcheck()

// init a CentOS VM
session = macadamTest.Macadam([]string{"init", image})
session := macadamTest.Macadam([]string{"init", image})
session.WaitWithDefaultTimeout()
Expect(session).Should(gexec.Exit())

Expand Down
Loading