Skip to content

Commit

Permalink
ci: add GetInternalMachine function
Browse files Browse the repository at this point in the history
Signed-off-by: Loic Devulder <[email protected]>
  • Loading branch information
ldevulder committed Sep 29, 2023
1 parent 9222903 commit 4c6986e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
77 changes: 47 additions & 30 deletions tests/e2e/helpers/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,50 @@ import (
)

/**
* Get MachineInventory name (aka. server id)
* @remarks The repository is added to the local cluster
* Add node selector
* @remarks A nodeSelector field is added
* @param key key to add in YAML
* @param value value to set the key to
* @returns The YAML structure or an error
*/
func AddSelector(key, value string) ([]byte, error) {
type selectorYaml struct {
MatchLabels map[string]string `yaml:"matchLabels,omitempty"`
}

type selector struct {
SelectorYaml selectorYaml `yaml:"nodeSelector,omitempty"`
}

v := selectorYaml{map[string]string{key: value}}
s := selector{v}
out, err := yaml.Marshal(s)
if err != nil {
return nil, err
}

// Add indent at the beginning
out = append([]byte(" "), out...)

return out, nil
}

/**
* Get Machine from MachineInventory
* @remarks This is useful to link machine name from Elemental to the Rancher Manager one
* @param clusterNS Name of the repository
* @param index URL of the repository
* @returns The name/id of the server or an error
* @param machineInventory Machine name as seen by Elemental
* @returns Corresponding internal machine name
*/
func GetServerID(clusterNS string, index int) (string, error) {
serverID, err := kubectl.Run("get", "MachineInventories",
func GetInternalMachine(clusterNS, machineInventory string) (string, error) {
machine, err := kubectl.Run("get", "Machine",
"--namespace", clusterNS,
"-o", "jsonpath={.items["+fmt.Sprint(index-1)+"].metadata.name}")
"-o", "jsonpath={.items[?(@.status.nodeRef.name==\""+machineInventory+"\")].metadata.name}")
if err != nil {
return "", err
}

return serverID, nil
return machine, nil
}

/**
Expand Down Expand Up @@ -74,32 +103,20 @@ func GetOperatorVersion() (string, error) {
}

/**
* Add node selector
* @remarks A nodeSelector field is added
* @param key key to add in YAML
* @param value value to set the key to
* @returns The YAML structure or an error
* Get MachineInventory name (aka. server id)
* @param clusterNS Name of the repository
* @param index URL of the repository
* @returns The name/id of the server or an error
*/
func AddSelector(key, value string) ([]byte, error) {
type selectorYaml struct {
MatchLabels map[string]string `yaml:"matchLabels,omitempty"`
}

type selector struct {
SelectorYaml selectorYaml `yaml:"nodeSelector,omitempty"`
}

v := selectorYaml{map[string]string{key: value}}
s := selector{v}
out, err := yaml.Marshal(s)
func GetServerID(clusterNS string, index int) (string, error) {
serverID, err := kubectl.Run("get", "MachineInventories",
"--namespace", clusterNS,
"-o", "jsonpath={.items["+fmt.Sprint(index-1)+"].metadata.name}")
if err != nil {
return nil, err
return "", err
}

// Add indent at the beginning
out = append([]byte(" "), out...)

return out, nil
return serverID, nil
}

/**
Expand Down
22 changes: 13 additions & 9 deletions tests/e2e/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,42 @@ limitations under the License.
package e2e_test

import (
"os/exec"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher-sandbox/ele-testhelpers/kubectl"
"github.com/rancher-sandbox/ele-testhelpers/tools"
"github.com/rancher/elemental/tests/e2e/helpers/elemental"
)

var _ = Describe("E2E - Test the reset feature", Label("reset"), func() {
It("Reset one node in the cluster", func() {
// Get the machine inventory name list
machineInventory, err := kubectl.Run("get", "machineinventory", "-A", "-o", "jsonpath='{.items[*].metadata.name}'")
machineInventory, err := kubectl.Run("get", "MachineInventory",
"--namespace", clusterNS,
"-o", "jsonpath='{.items[*].metadata.name}'")
Expect(err).To(Not(HaveOccurred()))
firstMachineInventory := strings.Split(machineInventory, " ")[1]

By("Configuring reset at machine inventory level", func() {
By("Configuring reset at MachineInventory level", func() {
// Patch the first machine inventory to enable reset
_, err = kubectl.Run("patch", "machineinventory", firstMachineInventory, "--namespace", clusterNS, "--type", "merge", "--patch-file", resetMachineInv)
_, err = kubectl.Run("patch", "MachineInventory", firstMachineInventory,
"--namespace", clusterNS, "--type", "merge",
"--patch-file", resetMachineInv)
Expect(err).To(Not(HaveOccurred()))
})

By("Deleting and removing the node from the cluster", func() {
out, err := exec.Command("bash", "-c", "kubectl get machines -A | awk '/"+firstMachineInventory+"/ {print $2}'").CombinedOutput()
machineToRemove, err := elemental.GetInternalMachine(clusterNS, firstMachineInventory)
Expect(err).To(Not(HaveOccurred()))
machineToRemove := strings.Trim(string(out), "\n")
_, err = kubectl.Run("delete", "machines", machineToRemove, "-n", "fleet-default")
_, err = kubectl.Run("delete", "machines", machineToRemove,
"--namespace", clusterNS)
Expect(err).To(Not(HaveOccurred()))
})

By("Checking that machine inventory is deleted", func() {
By("Checking that MachineInventory is deleted", func() {
Eventually(func() string {
out, _ := kubectl.Run("get", "MachineInventory",
"--namespace", clusterNS,
Expand All @@ -55,7 +59,7 @@ var _ = Describe("E2E - Test the reset feature", Label("reset"), func() {
}, tools.SetTimeout(5*time.Minute), 5*time.Second).ShouldNot(ContainSubstring(firstMachineInventory))
})

By("Checking that machine inventory is back after the reset", func() {
By("Checking that MachineInventory is back after the reset", func() {
Eventually(func() string {
out, _ := kubectl.Run("get", "MachineInventory",
"--namespace", clusterNS,
Expand Down

0 comments on commit 4c6986e

Please sign in to comment.