github.com/pluralsh/plural-cli@v0.9.5/pkg/test/e2eclusterapi/e2e_api_test.go (about) 1 //go:build e2e 2 3 package e2e_test 4 5 import ( 6 "context" 7 "fmt" 8 "os" 9 "os/exec" 10 "path" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/pluralsh/plural-cli/pkg/kubernetes" 16 "github.com/pluralsh/plural-cli/pkg/machinepool" 17 "github.com/pluralsh/plural-cli/pkg/utils" 18 "github.com/pluralsh/polly/containers" 19 "github.com/stretchr/testify/assert" 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 ) 22 23 func TestApiListInstallations(t *testing.T) { 24 25 cmd := exec.Command("plural", "api", "list", "installations") 26 cmdOutput, err := cmd.CombinedOutput() 27 if err != nil { 28 t.Fatal(err) 29 } 30 installations := make([]string, 0) 31 rows := strings.Split(string(cmdOutput[:]), "\n") 32 for _, row := range rows[1:] { // Skip the heading row and iterate through the remaining rows 33 row = strings.ReplaceAll(row, "|", "") 34 cols := strings.Fields(row) // Extract each column from the row. 35 if len(cols) == 3 { 36 installations = append(installations, cols[0]) 37 } 38 } 39 expected := []string{"bootstrap"} 40 expects := containers.ToSet(expected) 41 assert.True(t, expects.Equal(containers.ToSet(installations)), fmt.Sprintf("the expected %s is different then %s", expected, installations)) 42 } 43 44 func TestPackagesList(t *testing.T) { 45 homeDir, err := os.UserHomeDir() 46 assert.NoError(t, err) 47 48 testDir := path.Join(homeDir, "test") 49 50 err = os.Chdir(testDir) 51 assert.NoError(t, err) 52 cmd := exec.Command("plural", "packages", "list", "bootstrap") 53 cmdOutput, err := cmd.CombinedOutput() 54 if err != nil { 55 t.Fatal(err) 56 } 57 packages := make([]string, 0) 58 rows := strings.Split(string(cmdOutput[:]), "\n") 59 for _, row := range rows[3:] { // Skip the heading row and iterate through the remaining rows 60 row = strings.ReplaceAll(row, "|", "") 61 cols := strings.Fields(row) // Extract each column from the row. 62 if len(cols) == 3 { 63 packages = append(packages, cols[1]) 64 } 65 } 66 expected := []string{"bootstrap", "kind-bootstrap-cluster-api", "cluster-api-control-plane", "cluster-api-bootstrap", "plural-certmanager-webhook", "cluster-api-cluster", "cluster-api-core", "cluster-api-provider-docker"} 67 expects := containers.ToSet(expected) 68 assert.True(t, expects.Equal(containers.ToSet(packages)), fmt.Sprintf("the expected %s is different then %s", expected, packages)) 69 } 70 71 func TestUpdateNodePools(t *testing.T) { 72 cmd := exec.Command("plural", "ops", "cluster") 73 cmdOutput, err := cmd.CombinedOutput() 74 if err != nil { 75 t.Fatal(err) 76 } 77 nodes := make([]string, 0) 78 rows := strings.Split(string(cmdOutput[:]), "\n") 79 for _, row := range rows[3:] { // Skip the heading row and iterate through the remaining rows 80 row = strings.ReplaceAll(row, "|", "") 81 cols := strings.Fields(row) // Extract each column from the row. 82 if len(cols) == 3 { 83 nodes = append(nodes, cols[0]) 84 } 85 } 86 87 assert.Equal(t, 3, len(nodes), fmt.Sprintf("expected %d nodes got %d", 3, len(nodes))) 88 kubeConf, err := kubernetes.KubeConfig() 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 mpools, err := machinepool.ListAll(kubeConf) 94 if err != nil { 95 t.Fatal(err) 96 } 97 if len(mpools) != 1 { 98 t.Fatal("expected one machine pool") 99 } 100 mp := mpools[0] 101 102 mps, err := machinepool.NewForConfig(kubeConf) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 client := mps.MachinePools("bootstrap") 108 machinePool, err := client.Get(context.Background(), mp.Name, metav1.GetOptions{}) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 replicas := *machinePool.Spec.Replicas 114 115 if replicas != 2 { 116 t.Fatal("expected 2 replicas") 117 } 118 replicas = 3 119 machinePool.Spec.Replicas = &replicas 120 121 machinePool, err = client.Update(context.Background(), machinePool) 122 if err != nil { 123 t.Fatal(err) 124 } 125 126 kube, err := kubernetes.Kubernetes() 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 if err := utils.WaitFor(5*time.Minute, 5*time.Second, func() (bool, error) { 132 nodeList, err := kube.Nodes() 133 if err != nil { 134 t.Fatal(err) 135 } 136 if len(nodeList.Items) == 4 { 137 return true, nil 138 } 139 return false, nil 140 }); err != nil { 141 t.Fatal(err) 142 } 143 144 }