Skip to content
Open
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
112 changes: 112 additions & 0 deletions pkg/util/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,115 @@ func TestIsWorkSuspendDispatching(t *testing.T) {
})
}
}

func TestGetWorkSuspendDispatching(t *testing.T) {
trueVal := true
falseVal := false
tests := []struct {
name string
spec *workv1alpha1.WorkSpec
want []string
}{
{
name: "SuspendDispatching is true",
spec: &workv1alpha1.WorkSpec{SuspendDispatching: &trueVal},
want: []string{"true"},
},
{
name: "SuspendDispatching is false",
spec: &workv1alpha1.WorkSpec{SuspendDispatching: &falseVal},
want: []string{"false"},
},
{
name: "SuspendDispatching is nil, defaults to false",
spec: &workv1alpha1.WorkSpec{},
want: []string{"false"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetWorkSuspendDispatching(tt.spec)
assert.Equal(t, tt.want, got)
})
}
}

func TestSetLabelsAndAnnotationsForWorkload(t *testing.T) {
tests := []struct {
name string
workload *unstructured.Unstructured
work *workv1alpha1.Work
wantLabelKey string
wantLabelValue string
wantAnnotationKey string
}{
Comment on lines +294 to +297
{
name: "work has permanent ID label — propagated to workload",
workload: &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
},
work: &workv1alpha1.Work{
Spec: workv1alpha1.WorkSpec{},
},
},
{
name: "work has permanent ID label set",
workload: &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
},
work: &workv1alpha1.Work{
Spec: workv1alpha1.WorkSpec{},
},
},
Comment on lines +314 to +329
}
Comment on lines +290 to +330

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The table-driven test TestSetLabelsAndAnnotationsForWorkload contains several issues:

  1. The struct fields wantLabelKey, wantLabelValue, and wantAnnotationKey are defined but never used in the test cases or assertions.
  2. The two test cases are completely identical and neither of them actually sets the permanent ID label on the work object, despite their names indicating they do.
  3. Since permanent ID propagation is already thoroughly tested in TestSetLabelsAndAnnotationsForWorkload_WithPermanentID, we can simplify this test to a single basic test case and remove the unused fields.
	tests := []struct {
		name     string
		workload *unstructured.Unstructured
		work     *workv1alpha1.Work
	}{
		{
			name: "basic workload and work",
			workload: &unstructured.Unstructured{
				Object: map[string]any{
					"apiVersion": "apps/v1",
					"kind":       "Deployment",
					"metadata": map[string]any{
						"name":      "demo",
						"namespace": "default",
					},
				},
			},
			work: &workv1alpha1.Work{
				Spec: workv1alpha1.WorkSpec{},
			},
		},
	}


for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetLabelsAndAnnotationsForWorkload(tt.workload, tt.work)
// RecordManagedAnnotations should always set the managed-annotations annotation.
annotations := tt.workload.GetAnnotations()
assert.NotNil(t, annotations)
assert.Contains(t, annotations, workv1alpha2.ManagedAnnotation)
// RecordManagedLabels should always set the managed-labels annotation.
assert.Contains(t, annotations, workv1alpha2.ManagedLabels)
})
}
}

func TestSetLabelsAndAnnotationsForWorkload_WithPermanentID(t *testing.T) {
const permanentID = "test-permanent-id-123"
workload := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
}
work := &workv1alpha1.Work{}
work.Labels = map[string]string{
workv1alpha2.WorkPermanentIDLabel: permanentID,
}

SetLabelsAndAnnotationsForWorkload(workload, work)

labels := workload.GetLabels()
assert.Equal(t, permanentID, labels[workv1alpha2.WorkPermanentIDLabel])
}