From 8b1e33cb499d89fcf733dd43ec46474c991239d5 Mon Sep 17 00:00:00 2001 From: sophon Date: Fri, 20 Oct 2023 16:45:25 +0800 Subject: [PATCH] chore: add ut --- controllers/apps/configuration/revision.go | 2 +- .../apps/configuration/revision_test.go | 83 +++++++++++++++---- .../apps/operations/reconfigure_test.go | 4 - 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/controllers/apps/configuration/revision.go b/controllers/apps/configuration/revision.go index 9c788432a0cd..ea222f50f1b4 100644 --- a/controllers/apps/configuration/revision.go +++ b/controllers/apps/configuration/revision.go @@ -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 } diff --git a/controllers/apps/configuration/revision_test.go b/controllers/apps/configuration/revision_test.go index 9d1666a66dc4..9f38cdc5c5bf 100644 --- a/controllers/apps/configuration/revision_test.go +++ b/controllers/apps/configuration/revision_test.go @@ -34,26 +34,26 @@ 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() @@ -61,8 +61,8 @@ func TestGcConfigRevision(t *testing.T) { 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()))) @@ -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) + }) + } +} diff --git a/controllers/apps/operations/reconfigure_test.go b/controllers/apps/operations/reconfigure_test.go index fb580d37c033..c4f8f5020bb8 100644 --- a/controllers/apps/operations/reconfigure_test.go +++ b/controllers/apps/operations/reconfigure_test.go @@ -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()) @@ -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))