Skip to content

Commit 71a9475

Browse files
committed
add test for updateRun API
1 parent ea53057 commit 71a9475

File tree

1 file changed

+208
-2
lines changed

1 file changed

+208
-2
lines changed

test/apis/placement/v1beta1/api_validation_integration_test.go

Lines changed: 208 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"reflect"
12+
"time"
1213

1314
. "github.com/onsi/ginkgo/v2"
1415
. "github.com/onsi/gomega"
@@ -20,10 +21,16 @@ import (
2021
)
2122

2223
const (
23-
crpdbNameTemplate = "test-crpdb-%d"
24+
crpdbNameTemplate = "test-crpdb-%d"
25+
validupdateRunNameTemplate = "test-update-run-%d"
26+
invalidupdateRunNameTemplate = "test-update-run-with-invalid-length-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-%d"
27+
updateRunStrategyNameTemplate = "test-update-run-strategy-%d"
28+
updateRunStageNameTemplate = "stage%d%d"
29+
invalidupdateRunStageNameTemplate = "stage012345678901234567890123456789012345678901234567890123456789%d%d"
30+
approveRequestNameTemplate = "test-approve-request-%d"
2431
)
2532

26-
var _ = Describe("Test placement v1alpha1 API validation", func() {
33+
var _ = Describe("Test placement v1beta1 API validation", func() {
2734
Context("Test ClusterPlacementDisruptionBudget API validation - valid cases", func() {
2835
It("should allow creation of ClusterPlacementDisruptionBudget with valid maxUnavailable - int", func() {
2936
crpdb := placementv1beta1.ClusterResourcePlacementDisruptionBudget{
@@ -292,4 +299,203 @@ var _ = Describe("Test placement v1alpha1 API validation", func() {
292299
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("invalid: spec.minAvailable"))
293300
})
294301
})
302+
303+
Context("Test ClusterStagedUpdateRun API validation - valid cases", func() {
304+
It("Should allow creation of ClusterStagedUpdateRun with valid name length", func() {
305+
updateRun := placementv1beta1.ClusterStagedUpdateRun{
306+
ObjectMeta: metav1.ObjectMeta{
307+
Name: fmt.Sprintf(validupdateRunNameTemplate, GinkgoParallelProcess()),
308+
},
309+
}
310+
Expect(hubClient.Create(ctx, &updateRun)).Should(Succeed())
311+
Expect(hubClient.Delete(ctx, &updateRun)).Should(Succeed())
312+
})
313+
})
314+
315+
Context("Test ClusterStagedUpdateRun API validation - invalid cases", func() {
316+
It("Should deny creation of ClusterStagedUpdateRun with name length > 127", func() {
317+
updateRun := placementv1beta1.ClusterStagedUpdateRun{
318+
ObjectMeta: metav1.ObjectMeta{
319+
Name: fmt.Sprintf(invalidupdateRunNameTemplate, GinkgoParallelProcess()),
320+
},
321+
}
322+
err := hubClient.Create(ctx, &updateRun)
323+
var statusErr *k8sErrors.StatusError
324+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create updateRun call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
325+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("metadata.name max length is 127"))
326+
})
327+
328+
It("Should deny update of ClusterStagedUpdateRun spec", func() {
329+
updateRun := placementv1beta1.ClusterStagedUpdateRun{
330+
ObjectMeta: metav1.ObjectMeta{
331+
Name: fmt.Sprintf(validupdateRunNameTemplate, GinkgoParallelProcess()),
332+
},
333+
Spec: placementv1beta1.StagedUpdateRunSpec{
334+
PlacementName: "test-placement",
335+
},
336+
}
337+
Expect(hubClient.Create(ctx, &updateRun)).Should(Succeed())
338+
339+
updateRun.Spec.PlacementName = "test-placement-2"
340+
err := hubClient.Update(ctx, &updateRun)
341+
var statusErr *k8sErrors.StatusError
342+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Update updateRun call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
343+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("The spec field is immutable"))
344+
Expect(hubClient.Delete(ctx, &updateRun)).Should(Succeed())
345+
})
346+
})
347+
348+
Context("Test ClusterStagedUpdateStrategy API validation - valid cases", func() {
349+
It("Should allow creation of ClusterStagedUpdateStrategy with valid stage config", func() {
350+
strategy := placementv1beta1.ClusterStagedUpdateStrategy{
351+
ObjectMeta: metav1.ObjectMeta{
352+
Name: fmt.Sprintf(updateRunStrategyNameTemplate, GinkgoParallelProcess()),
353+
},
354+
Spec: placementv1beta1.StagedUpdateStrategySpec{
355+
Stages: []placementv1beta1.StageConfig{
356+
{
357+
Name: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), 1),
358+
AfterStageTasks: []placementv1beta1.AfterStageTask{
359+
{
360+
Type: placementv1beta1.AfterStageTaskTypeApproval,
361+
},
362+
{
363+
Type: placementv1beta1.AfterStageTaskTypeTimedWait,
364+
WaitTime: metav1.Duration{Duration: time.Second * 10},
365+
},
366+
},
367+
},
368+
},
369+
},
370+
}
371+
Expect(hubClient.Create(ctx, &strategy)).Should(Succeed())
372+
Expect(hubClient.Delete(ctx, &strategy)).Should(Succeed())
373+
})
374+
})
375+
376+
Context("Test ClusterStagedUpdateStrategy API validation - invalid cases", func() {
377+
It("Should deny creation of ClusterStagedUpdateStrategy with more than allowed staged", func() {
378+
strategy := placementv1beta1.ClusterStagedUpdateStrategy{
379+
ObjectMeta: metav1.ObjectMeta{
380+
Name: fmt.Sprintf(updateRunStrategyNameTemplate, GinkgoParallelProcess()),
381+
},
382+
}
383+
for i := 0; i < 32; i++ {
384+
strategy.Spec.Stages = append(strategy.Spec.Stages, placementv1beta1.StageConfig{
385+
Name: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), i),
386+
})
387+
}
388+
err := hubClient.Create(ctx, &strategy)
389+
var statusErr *k8sErrors.StatusError
390+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create updateRunStrategy call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
391+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("spec.stages: Too many: 32: must have at most 31 items"))
392+
})
393+
394+
It("Should deny creation of ClusterStagedUpdateStrategy with invalid stage config - too long stage name", func() {
395+
strategy := placementv1beta1.ClusterStagedUpdateStrategy{
396+
ObjectMeta: metav1.ObjectMeta{
397+
Name: fmt.Sprintf(updateRunStrategyNameTemplate, GinkgoParallelProcess()),
398+
},
399+
Spec: placementv1beta1.StagedUpdateStrategySpec{
400+
Stages: []placementv1beta1.StageConfig{
401+
{
402+
Name: fmt.Sprintf(invalidupdateRunStageNameTemplate, GinkgoParallelProcess(), 1),
403+
},
404+
},
405+
},
406+
}
407+
err := hubClient.Create(ctx, &strategy)
408+
var statusErr *k8sErrors.StatusError
409+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create updateRunStrategy call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
410+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("Too long: may not be longer than 63"))
411+
})
412+
413+
It("Should deny creation of ClusterStagedUpdateStrategy with invalid stage config - stage name with invalid characters", func() {
414+
strategy := placementv1beta1.ClusterStagedUpdateStrategy{
415+
ObjectMeta: metav1.ObjectMeta{
416+
Name: fmt.Sprintf(updateRunStrategyNameTemplate, GinkgoParallelProcess()),
417+
},
418+
Spec: placementv1beta1.StagedUpdateStrategySpec{
419+
Stages: []placementv1beta1.StageConfig{
420+
{
421+
Name: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), 1) + "-A",
422+
},
423+
},
424+
},
425+
}
426+
err := hubClient.Create(ctx, &strategy)
427+
var statusErr *k8sErrors.StatusError
428+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create updateRunStrategy call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
429+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("in body should match.*a-z0-9"))
430+
})
431+
432+
It("Should deny creation of ClusterStagedUpdateStrategy with invalid stage config - more than 2 AfterStageTasks", func() {
433+
strategy := placementv1beta1.ClusterStagedUpdateStrategy{
434+
ObjectMeta: metav1.ObjectMeta{
435+
Name: fmt.Sprintf(updateRunStrategyNameTemplate, GinkgoParallelProcess()),
436+
},
437+
Spec: placementv1beta1.StagedUpdateStrategySpec{
438+
Stages: []placementv1beta1.StageConfig{
439+
{
440+
Name: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), 1),
441+
AfterStageTasks: []placementv1beta1.AfterStageTask{
442+
{
443+
Type: placementv1beta1.AfterStageTaskTypeApproval,
444+
},
445+
{
446+
Type: placementv1beta1.AfterStageTaskTypeApproval,
447+
},
448+
{
449+
Type: placementv1beta1.AfterStageTaskTypeTimedWait,
450+
WaitTime: metav1.Duration{Duration: time.Second * 10},
451+
},
452+
},
453+
},
454+
},
455+
},
456+
}
457+
err := hubClient.Create(ctx, &strategy)
458+
var statusErr *k8sErrors.StatusError
459+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create updateRunStrategy call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
460+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("Too many: 3: must have at most 2 items"))
461+
})
462+
})
463+
464+
Context("Test ClusterApprovalRequest API validation - valid cases", func() {
465+
It("Should allow creation of ClusterApprovalRequest with valid configurations", func() {
466+
appReq := placementv1beta1.ClusterApprovalRequest{
467+
ObjectMeta: metav1.ObjectMeta{
468+
Name: fmt.Sprintf(approveRequestNameTemplate, GinkgoParallelProcess()),
469+
},
470+
Spec: placementv1beta1.ApprovalRequestSpec{
471+
TargetUpdateRun: fmt.Sprintf(validupdateRunNameTemplate, GinkgoParallelProcess()),
472+
TargetStage: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), 1),
473+
},
474+
}
475+
Expect(hubClient.Create(ctx, &appReq)).Should(Succeed())
476+
Expect(hubClient.Delete(ctx, &appReq)).Should(Succeed())
477+
})
478+
})
479+
480+
Context("Test ClusterApprovalRequest API validation - invalid cases", func() {
481+
It("Should deny update of ClusterApprovalRequest spec", func() {
482+
appReq := placementv1beta1.ClusterApprovalRequest{
483+
ObjectMeta: metav1.ObjectMeta{
484+
Name: fmt.Sprintf(approveRequestNameTemplate, GinkgoParallelProcess()),
485+
},
486+
Spec: placementv1beta1.ApprovalRequestSpec{
487+
TargetUpdateRun: fmt.Sprintf(validupdateRunNameTemplate, GinkgoParallelProcess()),
488+
TargetStage: fmt.Sprintf(updateRunStageNameTemplate, GinkgoParallelProcess(), 1),
489+
},
490+
}
491+
Expect(hubClient.Create(ctx, &appReq)).Should(Succeed())
492+
493+
appReq.Spec.TargetUpdateRun += "1"
494+
err := hubClient.Update(ctx, &appReq)
495+
var statusErr *k8sErrors.StatusError
496+
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Update clusterApprovalRequest call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
497+
Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("The spec field is immutable"))
498+
Expect(hubClient.Delete(ctx, &appReq)).Should(Succeed())
499+
})
500+
})
295501
})

0 commit comments

Comments
 (0)