Repository: kubean-io/kubean
Pull request #1038 (2023-11-29 06:57:29)

Add unit test
<!--  

Thanks for sending a pull request!  Here are some tips for you:

* make sure your commit is signed off

-->

#### What type of PR is this?
/kind enhancement
<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind enhancement
/kind feature

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind design
/kind regression
-->

#### What this PR does / why we need it:
Add unit test

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:



Patch:
diff --git a/pkg/controllers/infomanifest/controller.go b/pkg/controllers/infomanifest/controller.go
index 8055dc8ae..2c70385f8 100644
--- a/pkg/controllers/infomanifest/controller.go
+++ b/pkg/controllers/infomanifest/controller.go
@@ -94,14 +94,11 @@ func (m *VersionedManifest) update(manifest1, manifest2 *manifestv1alpha1.Manife
 	newSprayRelease, ok2 := manifest2.ObjectMeta.Labels[constants.KeySprayRelease]
 	if !ok1 && !ok2 {
 		return
-	}
-	if ok1 && !ok2 {
+	} else if ok1 && !ok2 {
 		m.delete(manifest1)
-	}
-	if !ok1 && ok2 {
+	} else if !ok1 && ok2 {
 		m.add(manifest2)
-	}
-	if oldSprayRelease != newSprayRelease || !reflect.DeepEqual(manifest1, manifest2) {
+	} else if oldSprayRelease != newSprayRelease || !reflect.DeepEqual(manifest1, manifest2) {
 		m.delete(manifest1)
 		m.add(manifest2)
 	}


Test patch:
diff --git a/pkg/controllers/infomanifest/controller_test.go b/pkg/controllers/infomanifest/controller_test.go
index 1984327eb..876534dbf 100644
--- a/pkg/controllers/infomanifest/controller_test.go
+++ b/pkg/controllers/infomanifest/controller_test.go
@@ -547,6 +547,14 @@ func TestUpdateLocalService(t *testing.T) {
 	}
 }
 
+func TestGetVersionedManifest(t *testing.T) {
+	t.Run("get versioned manifest", func(t *testing.T) {
+		if !reflect.DeepEqual(GetVersionedManifest(), versionedManifest) {
+			t.Fatal()
+		}
+	})
+}
+
 func TestOp(t *testing.T) {
 	var versionedManifest VersionedManifest
 	type args struct {
@@ -654,6 +662,99 @@ func TestOp(t *testing.T) {
 				}
 			},
 		},
+		{
+			name: "update with no release label",
+			args: args{
+				op: "update",
+				m1: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+					},
+				},
+				m2: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+						Labels: map[string]string{
+							"label": "value",
+						},
+					},
+				},
+			},
+			prerequisites: func(versionedManifest *VersionedManifest) {},
+			want: func() map[string][]*manifestv1alpha1.Manifest {
+				return map[string][]*manifestv1alpha1.Manifest{}
+			},
+		},
+		{
+			name: "update with label removing",
+			args: args{
+				op: "update",
+				m1: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+						Labels: map[string]string{
+							constants.KeySprayRelease: "2.23",
+						},
+					},
+				},
+				m2: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+					},
+				},
+			},
+			prerequisites: func(versionedManifest *VersionedManifest) {
+				versionedManifest.Manifests = map[string][]*manifestv1alpha1.Manifest{
+					"2.23": {
+						{
+							ObjectMeta: metav1.ObjectMeta{
+								Name: "manifest-1",
+								Labels: map[string]string{
+									constants.KeySprayRelease: "2.23",
+								},
+							},
+						},
+					},
+				}
+			},
+			want: func() map[string][]*manifestv1alpha1.Manifest {
+				return map[string][]*manifestv1alpha1.Manifest{}
+			},
+		},
+		{
+			name: "update with label adding",
+			args: args{
+				op: "update",
+				m1: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+					},
+				},
+				m2: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+						Labels: map[string]string{
+							constants.KeySprayRelease: "2.23",
+						},
+					},
+				},
+			},
+			prerequisites: func(versionedManifest *VersionedManifest) {},
+			want: func() map[string][]*manifestv1alpha1.Manifest {
+				return map[string][]*manifestv1alpha1.Manifest{
+					"2.23": {
+						{
+							ObjectMeta: metav1.ObjectMeta{
+								Name: "manifest-1",
+								Labels: map[string]string{
+									constants.KeySprayRelease: "2.23",
+								},
+							},
+						},
+					},
+				}
+			},
+		},
 		{
 			name: "delete",
 			args: args{
@@ -685,6 +786,63 @@ func TestOp(t *testing.T) {
 				return map[string][]*manifestv1alpha1.Manifest{}
 			},
 		},
+		{
+			name: "delete with no release label",
+			args: args{
+				op: "delete",
+				m1: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-2",
+					},
+				},
+			},
+			prerequisites: func(versionedManifest *VersionedManifest) {
+				versionedManifest.Manifests = map[string][]*manifestv1alpha1.Manifest{
+					"2.23": {
+						{
+							ObjectMeta: metav1.ObjectMeta{
+								Name: "manifest-1",
+								Labels: map[string]string{
+									constants.KeySprayRelease: "2.23",
+								},
+							},
+						},
+					},
+				}
+			},
+			want: func() map[string][]*manifestv1alpha1.Manifest {
+				return map[string][]*manifestv1alpha1.Manifest{
+					"2.23": {
+						{
+							ObjectMeta: metav1.ObjectMeta{
+								Name: "manifest-1",
+								Labels: map[string]string{
+									constants.KeySprayRelease: "2.23",
+								},
+							},
+						},
+					},
+				}
+			},
+		},
+		{
+			name: "delete but no manifest cached",
+			args: args{
+				op: "delete",
+				m1: &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-1",
+						Labels: map[string]string{
+							constants.KeySprayRelease: "2.23",
+						},
+					},
+				},
+			},
+			prerequisites: func(versionedManifest *VersionedManifest) {},
+			want: func() map[string][]*manifestv1alpha1.Manifest {
+				return map[string][]*manifestv1alpha1.Manifest{}
+			},
+		},
 	}
 
 	for _, test := range tests {