Skip to content

Commit

Permalink
chore: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Oct 20, 2023
1 parent 3a7c6c0 commit 8b1e33c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion controllers/apps/configuration/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GcRevision(annotations map[string]string) []ConfigurationRevision {

func GetLastRevision(annotations map[string]string, revision int64) (ConfigurationRevision, bool) {
revisions := RetrieveRevision(annotations)
for i := len(revisions) - 1; i >= 0; i++ {
for i := len(revisions) - 1; i >= 0; i-- {
if revisions[i].Revision == revision {
return revisions[i], true
}
Expand Down
83 changes: 66 additions & 17 deletions controllers/apps/configuration/revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,35 @@ import (

func TestGcConfigRevision(t *testing.T) {
cm := builder.NewConfigMapBuilder("default", "test").
AddAnnotations(core.GenerateRevisionPhaseKey("1"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("2"), "init").
AddAnnotations(core.GenerateRevisionPhaseKey("3"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("4"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("1"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("2"), "Init").
AddAnnotations(core.GenerateRevisionPhaseKey("3"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("4"), "Finished").
GetObject()
revisions := GcRevision(cm.GetAnnotations())
assert.Equal(t, 0, len(revisions))

cm = builder.NewConfigMapBuilder("default", "test").
AddAnnotations(core.GenerateRevisionPhaseKey("1"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("2"), "init").
AddAnnotations(core.GenerateRevisionPhaseKey("3"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("4"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("5"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("6"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("7"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("8"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("9"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("10"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("11"), "finished").
AddAnnotations(core.GenerateRevisionPhaseKey("1"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("2"), "Init").
AddAnnotations(core.GenerateRevisionPhaseKey("3"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("4"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("5"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("6"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("7"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("8"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("9"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("10"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("11"), "Finished").
AddAnnotations(core.GenerateRevisionPhaseKey("12"), `{"Phase":"Finished","Revision":"12","Policy":"","ExecResult":"","SucceedCount":0,"ExpectedCount":0,"Retry":false,"Failed":false,"Message":"the configuration file has not been modified, skip reconfigure"}`).
GetObject()

assert.Equal(t, 12, len(RetrieveRevision(cm.GetAnnotations())))

revisions = GcRevision(cm.GetAnnotations())
assert.Equal(t, 2, len(revisions))
assert.Equal(t, "init", string(revisions[1].Phase))
assert.Equal(t, "finished", string(revisions[0].Phase))
assert.Equal(t, string(appsv1alpha1.CInitPhase), string(revisions[1].Phase))
assert.Equal(t, string(appsv1alpha1.CFinishedPhase), string(revisions[0].Phase))

GcConfigRevision(cm)
assert.Equal(t, 10, len(RetrieveRevision(cm.GetAnnotations())))
Expand Down Expand Up @@ -156,3 +156,52 @@ func TestGetCurrentRevision(t *testing.T) {
})
}
}

func TestGetLastRevision(t *testing.T) {
type args struct {
annotations map[string]string
revision int64
}
tests := []struct {
name string
args args
want ConfigurationRevision
want1 bool
}{{
name: "test",
args: args{
annotations: map[string]string{
core.GenerateRevisionPhaseKey("1"): "Finished",
core.GenerateRevisionPhaseKey("2"): "Running",
},
revision: 2,
},
want: ConfigurationRevision{
Revision: 2,
StrRevision: "2",
Phase: appsv1alpha1.CRunningPhase,
Result: intctrlutil.Result{
Phase: appsv1alpha1.CRunningPhase,
Revision: "2",
},
},
want1: true,
}, {
name: "test",
args: args{
annotations: map[string]string{
core.GenerateRevisionPhaseKey("1"): "Finished",
core.GenerateRevisionPhaseKey("2"): "Running",
},
revision: 3,
},
want1: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetLastRevision(tt.args.annotations, tt.args.revision)
assert.Equalf(t, tt.want, got, "GetLastRevision(%v, %v)", tt.args.annotations, tt.args.revision)
assert.Equalf(t, tt.want1, got1, "GetLastRevision(%v, %v)", tt.args.annotations, tt.args.revision)
})
}
}
4 changes: 0 additions & 4 deletions controllers/apps/operations/reconfigure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ var _ = Describe("Reconfigure OpsRequest", func() {
initClusterForOps(opsRes)

opsManager := GetOpsManager()
//reAction := reconfigureAction{}
By("init ops phase")
_, err := opsManager.Do(reqCtx, k8sClient, opsRes)
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -236,9 +235,6 @@ var _ = Describe("Reconfigure OpsRequest", func() {
_, err = opsManager.Do(reqCtx, k8sClient, opsRes)
Expect(err).ShouldNot(HaveOccurred())

//Expect(reAction.Action(reqCtx, k8sClient, opsRes)).Should(Succeed())
//By("configuration Reconcile callback")
// Expect(reAction.Handle(eventContext, ops.Name, appsv1alpha1.OpsRunningPhase, nil)).Should(Succeed())
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(opsRes.OpsRequest), opsRes.OpsRequest)).Should(Succeed())
_, _ = opsManager.Reconcile(reqCtx, k8sClient, opsRes)
Expect(opsRes.OpsRequest.Status.Phase).Should(Equal(appsv1alpha1.OpsRunningPhase))
Expand Down

0 comments on commit 8b1e33c

Please sign in to comment.