11package e2e
22
33import (
4- "sync"
54 "testing"
65 "time"
76
@@ -14,13 +13,6 @@ import (
1413 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1514)
1615
17- var (
18- machinePoolsExist bool
19- numInitialNodePools int
20- numInitialMachineSets int
21- wg = & sync.WaitGroup {}
22- )
23-
2416func TestInstascale (t * testing.T ) {
2517
2618 test := With (t )
@@ -65,25 +57,25 @@ func TestInstascale(t *testing.T) {
6557 }
6658 defer connection .Close ()
6759
68- machinePoolsExist = true
6960 // check existing cluster resources
70- numInitialMachinePools , err := MachinePoolsCount (connection )
71- if err != nil {
72- test . T (). Errorf ( "Unable to count machine pools - Error : %v" , err )
73- }
61+ machinePoolsExist , err := MachinePoolsExist (connection )
62+ test . Expect ( err ). NotTo ( HaveOccurred ())
63+ nodePoolsExist , err := NodePoolsExist ( connection )
64+ test . Expect ( err ). NotTo ( HaveOccurred ())
7465
75- if numInitialMachinePools == 0 {
76- machinePoolsExist = false
77- numInitialNodePools , err = NodePoolsCount (connection )
78- if err != nil {
79- test .T ().Errorf ("Unable to count node pools - Error : %v" , err )
80- }
81- if numInitialNodePools == 0 {
82- numInitialMachineSets , err = MachineSetsCount ()
83- if err != nil {
84- test .T ().Errorf ("Unable to count machine sets - Error : %v" , err )
85- }
86- }
66+ if machinePoolsExist {
67+ // look for machine pool with aw name - expect not to find it
68+ foundMachinePool , err := CheckMachinePools (connection , "test-instascale" )
69+ test .Expect (err ).NotTo (HaveOccurred ())
70+ test .Expect (foundMachinePool ).To (BeFalse ())
71+ } else if nodePoolsExist {
72+ // look for node pool with aw name - expect not to find it
73+ foundNodePool , err := CheckNodePools (connection , "test-instascale" )
74+ test .Expect (err ).NotTo (HaveOccurred ())
75+ test .Expect (foundNodePool ).To (BeFalse ())
76+ } else {
77+ // TODO update to foundMachineSet
78+
8779 }
8880
8981 // Batch Job
@@ -103,13 +95,13 @@ func TestInstascale(t *testing.T) {
10395 Spec : corev1.PodSpec {
10496 Containers : []corev1.Container {
10597 {
106- Name : "job" ,
107- Image : GetPyTorchImage (),
98+ Name : "job" ,
99+ Image : GetPyTorchImage (),
108100 Env : []corev1.EnvVar {
109101 corev1.EnvVar {Name : "PYTHONUSERBASE" , Value : "/test2" },
110102 },
111103 Command : []string {"/bin/sh" , "-c" , "pip install -r /test/requirements.txt && torchrun /test/mnist.py" },
112- Args : []string {"$PYTHONUSERBASE" },
104+ Args : []string {"$PYTHONUSERBASE" },
113105 VolumeMounts : []corev1.VolumeMount {
114106 {
115107 Name : "test" ,
@@ -184,46 +176,41 @@ func TestInstascale(t *testing.T) {
184176 },
185177 },
186178 },
187- GenericTemplate : Raw (test , job ),
179+ GenericTemplate : Raw (test , job ),
180+ CompletionStatus : "Complete" ,
188181 },
189182 },
190183 },
191184 },
192185 }
193186
194- _ , err = test .Client ().MCAD ().WorkloadV1beta1 ().AppWrappers (namespace .Name ).Create (test .Ctx (), aw , metav1.CreateOptions {})
187+ _ , err = test .Client ().MCAD ().WorkloadV1beta1 ().AppWrappers (namespace .Name ).Create (test .Ctx (), aw , metav1.CreateOptions {})
195188 test .Expect (err ).NotTo (HaveOccurred ())
196189 test .T ().Logf ("AppWrapper created successfully %s/%s" , aw .Namespace , aw .Name )
197190
198191 test .Eventually (AppWrapper (test , namespace , aw .Name ), TestTimeoutShort ).
199192 Should (WithTransform (AppWrapperState , Equal (mcadv1beta1 .AppWrapperStateActive )))
200193
201- // wait for required resources to be created before checking them again
202- time .Sleep (TestTimeoutShort )
203- if ! machinePoolsExist {
204- numNodePools , err := NodePoolsCount (connection )
205- if err != nil {
206- test .T ().Errorf ("Unable to count node pools - Error : %v" , err )
207- }
208- test .Expect (numNodePools ).To (BeNumerically (">" , numInitialNodePools ))
209- test .T ().Logf ("number of node pools increased from %d to %d" , numInitialNodePools , numNodePools )
210-
211- } else if machinePoolsExist {
212- numMachinePools , err := MachinePoolsCount (connection )
213- if err != nil {
214- test .T ().Errorf ("Unable to count machine pools - Error : %v" , err )
215- }
216- test .Expect (numMachinePools ).To (BeNumerically (">" , numInitialMachinePools ))
217- test .T ().Logf ("number of machine pools increased from %d to %d" , numInitialMachinePools , numMachinePools )
194+ // time.Sleep is used twice throughout the test, each for 30 seconds. Can look into using sync package waitGroup instead if that makes more sense
195+ // wait for required resources to scale up before checking them again
196+ time .Sleep (TestTimeoutThirtySeconds )
197+
198+ if machinePoolsExist {
199+ // look for machine pool with aw name - expect to find it
200+ foundMachinePool , err := CheckMachinePools (connection , "test-instascale" )
201+ test .Expect (err ).NotTo (HaveOccurred ())
202+ test .Expect (foundMachinePool ).To (BeTrue ())
203+ } else if nodePoolsExist {
204+ // look for node pool with aw name - expect to find it
205+ foundNodePool , err := CheckNodePools (connection , "test-instascale" )
206+ test .Expect (err ).NotTo (HaveOccurred ())
207+ test .Expect (foundNodePool ).To (BeTrue ())
218208 } else {
219- numMachineSets , err := MachineSetsCount ()
220- if err != nil {
221- test .T ().Errorf ("Unable to count machine sets - Error : %v" , err )
222- }
223- test .Expect (numMachineSets ).To (BeNumerically (">" , numInitialMachineSets ))
224- test .T ().Logf ("number of machine sets increased from %d to %d" , numInitialMachineSets , numMachineSets )
209+ // TODO update to foundMachineSet
210+
225211 }
226-
212+
213+ // Assert that the job has completed
227214 test .T ().Logf ("Waiting for Job %s/%s to complete" , job .Namespace , job .Name )
228215 test .Eventually (Job (test , job .Namespace , job .Name ), TestTimeoutLong ).Should (
229216 Or (
@@ -235,30 +222,24 @@ func TestInstascale(t *testing.T) {
235222 test .Expect (GetJob (test , job .Namespace , job .Name )).
236223 To (WithTransform (ConditionStatus (batchv1 .JobComplete ), Equal (corev1 .ConditionTrue )))
237224
238- // AppWrapper not being updated to complete once job is finished
239-
240- time .Sleep (TestTimeoutMedium )
241- if ! machinePoolsExist {
242- numNodePoolsFinal , err := NodePoolsCount (connection )
243- if err != nil {
244- test .T ().Errorf ("Unable to count node pools - Error : %v" , err )
245- }
246- test .Expect (numNodePoolsFinal ).To (BeNumerically ("==" , numInitialNodePools ))
247- test .T ().Logf ("number of machine pools decreased" )
248-
249- } else if machinePoolsExist {
250- numMachinePoolsFinal , err := MachinePoolsCount (connection )
251- if err != nil {
252- test .T ().Errorf ("Unable to count machine pools - Error : %v" , err )
253- }
254- test .Expect (numMachinePoolsFinal ).To (BeNumerically ("==" , numInitialMachinePools ))
255- test .T ().Logf ("number of machine pools decreased" )
225+ test .Eventually (AppWrapper (test , namespace , aw .Name ), TestTimeoutShort ).
226+ Should (WithTransform (AppWrapperState , Equal (mcadv1beta1 .AppWrapperStateCompleted )))
227+
228+ // allow time for the resources to scale down before checking them again
229+ time .Sleep (TestTimeoutThirtySeconds )
230+
231+ if machinePoolsExist {
232+ // look for machine pool with aw name - expect to find it
233+ foundMachinePool , err := CheckMachinePools (connection , "test-instascale" )
234+ test .Expect (err ).NotTo (HaveOccurred ())
235+ test .Expect (foundMachinePool ).To (BeFalse ())
236+ } else if nodePoolsExist {
237+ // look for node pool with aw name - expect to find it
238+ foundNodePool , err := CheckNodePools (connection , "test-instascale" )
239+ test .Expect (err ).NotTo (HaveOccurred ())
240+ test .Expect (foundNodePool ).To (BeFalse ())
256241 } else {
257- numMachineSetsFinal , err := MachineSetsCount ()
258- if err != nil {
259- test .T ().Errorf ("Unable to count machine sets - Error : %v" , err )
260- }
261- test .Expect (numMachineSetsFinal ).To (BeNumerically ("==" , numInitialMachineSets ))
262- test .T ().Logf ("number of machine sets decreased" )
242+ // TODO update to foundMachineSet
243+
263244 }
264245}
0 commit comments