Skip to content

Commit 63764d6

Browse files
committed
Adding instascale e2e test
1 parent 6f7d6b1 commit 63764d6

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
. "github.com/onsi/gomega"
6+
. "github.com/project-codeflare/codeflare-operator/test/support"
7+
mcadv1beta1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
8+
corev1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/api/resource"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"testing"
12+
)
13+
14+
var (
15+
ocmToken string
16+
machinePoolsExist bool
17+
numInitialNodePools int
18+
numInitialMachineSets int
19+
)
20+
21+
func TestInstascale(t *testing.T) {
22+
23+
test := With(t)
24+
test.T().Parallel()
25+
26+
namespace := test.NewTestNamespace()
27+
28+
connection, err := CreateOCMConnection()
29+
if err != nil {
30+
test.T().Errorf("Unable to create ocm connection - Error : %v", err)
31+
}
32+
defer connection.Close()
33+
34+
machinePoolsExist = true
35+
// check existing machine pools
36+
numInitialMachinePools, err := MachinePoolsCount(connection)
37+
if err != nil {
38+
test.T().Errorf("Unable to count machine pools - Error : %v", err)
39+
}
40+
41+
if numInitialMachinePools == 0 {
42+
machinePoolsExist = false
43+
numInitialNodePools, err = NodePoolsCount(connection)
44+
if err != nil {
45+
test.T().Errorf("Unable to count node pools - Error : %v", err)
46+
}
47+
if numInitialNodePools == 0 {
48+
numInitialMachineSets, err = MachineSetsCount(connection)
49+
if err != nil {
50+
test.T().Errorf("Unable to count machine sets - Error : %v", err)
51+
}
52+
}
53+
}
54+
55+
// create an appwrapper
56+
aw := &mcadv1beta1.AppWrapper{
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: "test-instascale",
59+
Namespace: namespace.Name,
60+
Labels: map[string]string{
61+
"orderedinstance": "m5.xlarge_g4dn.xlarge",
62+
},
63+
},
64+
Spec: mcadv1beta1.AppWrapperSpec{
65+
AggrResources: mcadv1beta1.AppWrapperResourceList{
66+
GenericItems: []mcadv1beta1.AppWrapperGenericResource{
67+
{
68+
CustomPodResources: []mcadv1beta1.CustomPodResourceTemplate{
69+
{
70+
Replicas: 1,
71+
Requests: corev1.ResourceList{
72+
corev1.ResourceCPU: resource.MustParse("250m"),
73+
corev1.ResourceMemory: resource.MustParse("512Mi"),
74+
},
75+
Limits: corev1.ResourceList{
76+
corev1.ResourceCPU: resource.MustParse("500m"),
77+
corev1.ResourceMemory: resource.MustParse("1G"),
78+
},
79+
},
80+
{
81+
Replicas: 1,
82+
Requests: corev1.ResourceList{
83+
corev1.ResourceCPU: resource.MustParse("250m"),
84+
corev1.ResourceMemory: resource.MustParse("512Mi"),
85+
},
86+
Limits: corev1.ResourceList{
87+
corev1.ResourceCPU: resource.MustParse("500m"),
88+
corev1.ResourceMemory: resource.MustParse("1G"),
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
95+
},
96+
}
97+
98+
aw, err = test.Client().MCAD().McadV1beta1().AppWrappers(namespace.Name).Create(test.Ctx(), aw, metav1.CreateOptions{})
99+
test.Expect(err).NotTo(HaveOccurred())
100+
test.T().Logf("AppWrapper created successfully %s/%s", aw.Namespace, aw.Name)
101+
102+
test.Eventually(AppWrapper(test, namespace, aw.Name), TestTimeoutMedium).
103+
Should(WithTransform(AppWrapperState, Equal(mcadv1beta1.AppWrapperStateActive)))
104+
105+
if !machinePoolsExist {
106+
numNodePools, err := NodePoolsCount(connection)
107+
if err != nil {
108+
test.T().Errorf("Unable to count node pools - Error : %v", err)
109+
}
110+
fmt.Println(numNodePools)
111+
test.Expect(numNodePools).To(BeNumerically(">", numInitialNodePools))
112+
test.T().Logf("number of machine pools increased")
113+
114+
} else if machinePoolsExist {
115+
numMachinePools, err := MachinePoolsCount(connection)
116+
if err != nil {
117+
test.T().Errorf("Unable to count machine pools - Error : %v", err)
118+
}
119+
fmt.Println(numMachinePools)
120+
test.Expect(numMachinePools).To(BeNumerically(">", numInitialMachinePools))
121+
test.T().Logf("number of machine pools increased")
122+
} else {
123+
numMachineSets, err := MachineSetsCount(connection)
124+
if err != nil {
125+
test.T().Errorf("Unable to count machine sets - Error : %v", err)
126+
}
127+
fmt.Println(numMachineSets)
128+
test.Expect(numMachineSets).To(BeNumerically(">", numInitialMachineSets))
129+
test.T().Logf("number of machine sets increased")
130+
}
131+
132+
// TODO submit and check that the job has completed and that resources are released/scaled down
133+
}

test/support/clusterpools.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package support
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
ocmsdk "github.com/openshift-online/ocm-sdk-go"
9+
mapiclientset "github.com/openshift/client-go/machine/clientset/versioned"
10+
"github.com/openshift/client-go/machine/listers/machine/v1beta1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
var (
15+
ocmToken string = os.Getenv("OCMTOKEN")
16+
ClusterID string = os.Getenv("CLUSTERID")
17+
machinePoolsExist bool
18+
machineClient mapiclientset.Interface
19+
msLister v1beta1.MachineSetLister
20+
)
21+
22+
const (
23+
namespaceToList = "openshift-machine-api"
24+
)
25+
26+
func CreateOCMConnection() (*ocmsdk.Connection, error) {
27+
28+
logger, err := ocmsdk.NewGoLoggerBuilder().
29+
Debug(false).
30+
Build()
31+
if err != nil {
32+
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err)
33+
os.Exit(1)
34+
}
35+
36+
connection, err := ocmsdk.NewConnectionBuilder().
37+
Logger(logger).
38+
Tokens(ocmToken).
39+
Build()
40+
fmt.Println("connection", connection, err)
41+
if err != nil || connection == nil {
42+
fmt.Println("something went wrong", connection, err)
43+
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
44+
os.Exit(1)
45+
}
46+
47+
return connection, nil
48+
}
49+
50+
51+
func MachinePoolsCount(connection *ocmsdk.Connection) (numMachinePools int, err error) {
52+
fmt.Println("clusterID %v", ClusterID)
53+
machinePoolsConnection := connection.ClustersMgmt().V1().Clusters().Cluster(ClusterID).MachinePools().List()
54+
fmt.Println("machine pools connection %v", machinePoolsConnection)
55+
56+
machinePoolsListResponse, err := machinePoolsConnection.Send()
57+
if err != nil {
58+
fmt.Println("machine pools list response, %v error, %v", machinePoolsListResponse, err)
59+
return 0, fmt.Errorf("unable to send request, error: %v", err)
60+
}
61+
machinePoolsList := machinePoolsListResponse.Items()
62+
fmt.Println("machine pool list %v", machinePoolsList)
63+
//check the current number of machine pools
64+
// TODO to be more precise could we check the machineTypes?
65+
numMachinePools = machinePoolsList.Len()
66+
fmt.Println(numMachinePools)
67+
68+
return numMachinePools, nil
69+
}
70+
71+
func NodePoolsCount(connection *ocmsdk.Connection) (numNodePools int, err error) {
72+
nodePoolsConnection := connection.ClustersMgmt().V1().Clusters().Cluster(ClusterID).NodePools().List()
73+
nodePoolsListResponse, err := nodePoolsConnection.SendContext(context.Background())
74+
if err != nil {
75+
return 0, fmt.Errorf("unable to send request, error: %v", err)
76+
}
77+
nodePoolsList := nodePoolsListResponse.Items()
78+
numNodePools = nodePoolsList.Len()
79+
fmt.Println(numNodePools)
80+
81+
return numNodePools, nil
82+
}
83+
84+
func MachineSetsCount(connection *ocmsdk.Connection) (numMachineSets int, err error) {
85+
machineSets, err := machineClient.MachineV1beta1().MachineSets(namespaceToList).List(context.Background(), metav1.ListOptions{})
86+
if err != nil {
87+
return 0, fmt.Errorf("error while listing machine sets, error: %v", err)
88+
}
89+
machineSetsSize := machineSets.ListMeta.Size()
90+
91+
return machineSetsSize, nil
92+
}

0 commit comments

Comments
 (0)