Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #1038

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/controllers/infomanifest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
158 changes: 158 additions & 0 deletions pkg/controllers/infomanifest/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
Loading