Skip to content

Commit 29d6475

Browse files
committed
pkg/payload/task_test: create tests for Task.String()
1 parent e697a59 commit 29d6475

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

pkg/payload/task_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package payload
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/openshift/cluster-version-operator/lib"
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
10+
"k8s.io/apimachinery/pkg/util/diff"
11+
)
12+
13+
func TestTaskString(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
task *Task
17+
want string
18+
}{
19+
{
20+
name: "Manifest with name",
21+
task: &Task{Manifest: &lib.Manifest{
22+
OriginalFilename: "a",
23+
Obj: &unstructured.Unstructured{
24+
Object: map[string]interface{}{
25+
"kind": "Test",
26+
"apiVersion": "v1",
27+
"metadata": map[string]interface{}{
28+
"name": "file-json",
29+
},
30+
},
31+
},
32+
}},
33+
want: ` "file-json" (0 of 0)`,
34+
},
35+
{
36+
name: "Manifest with GVK",
37+
task: &Task{Manifest: &lib.Manifest{
38+
GVK: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Ingress"},
39+
Obj: &unstructured.Unstructured{
40+
Object: map[string]interface{}{
41+
"kind": "Test",
42+
"apiVersion": "v1",
43+
"metadata": map[string]interface{}{
44+
"name": "file-json",
45+
},
46+
},
47+
},
48+
}},
49+
want: `ingress "file-json" (0 of 0)`,
50+
},
51+
{
52+
name: "Manifest with name and namespace",
53+
task: &Task{Manifest: &lib.Manifest{
54+
OriginalFilename: "a",
55+
Obj: &unstructured.Unstructured{
56+
Object: map[string]interface{}{
57+
"kind": "Test",
58+
"apiVersion": "v1",
59+
"metadata": map[string]interface{}{
60+
"name": "file-json",
61+
"namespace": "foo",
62+
},
63+
},
64+
},
65+
}},
66+
want: ` "foo/file-json" (0 of 0)`,
67+
},
68+
{
69+
name: "Manifest with GVK and namespace",
70+
task: &Task{Manifest: &lib.Manifest{
71+
GVK: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Ingress"},
72+
Obj: &unstructured.Unstructured{
73+
Object: map[string]interface{}{
74+
"kind": "Test",
75+
"apiVersion": "v1",
76+
"metadata": map[string]interface{}{
77+
"name": "file-json",
78+
"namespace": "foo",
79+
},
80+
},
81+
},
82+
}},
83+
want: `ingress "foo/file-json" (0 of 0)`,
84+
},
85+
{
86+
name: "index and total",
87+
task: &Task{
88+
Manifest: &lib.Manifest{
89+
GVK: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Ingress"},
90+
Obj: &unstructured.Unstructured{
91+
Object: map[string]interface{}{
92+
"kind": "Test",
93+
"apiVersion": "v1",
94+
"metadata": map[string]interface{}{
95+
"name": "file-json",
96+
},
97+
},
98+
},
99+
},
100+
Index: 3,
101+
Total: 7,
102+
},
103+
want: `ingress "file-json" (3 of 7)`,
104+
},
105+
{
106+
name: "Namespaced, index and total",
107+
task: &Task{
108+
Manifest: &lib.Manifest{
109+
GVK: schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "Ingress"},
110+
Obj: &unstructured.Unstructured{
111+
Object: map[string]interface{}{
112+
"kind": "Test",
113+
"apiVersion": "v1",
114+
"metadata": map[string]interface{}{
115+
"name": "file-json",
116+
"namespace": "foo",
117+
},
118+
},
119+
},
120+
},
121+
Index: 5,
122+
Total: 10,
123+
},
124+
want: `ingress "foo/file-json" (5 of 10)`,
125+
},
126+
{
127+
name: "List",
128+
task: &Task{Manifest: &lib.Manifest{
129+
OriginalFilename: "list_manifest.yaml",
130+
Obj: &unstructured.Unstructured{
131+
Object: map[string]interface{}{
132+
"kind": "List",
133+
"apiVersion": "v1",
134+
"items": []interface{}{
135+
"foo",
136+
"bar",
137+
},
138+
},
139+
},
140+
}},
141+
want: ` "list_manifest.yaml" (0 of 0)`,
142+
},
143+
{
144+
name: "List and GVK",
145+
task: &Task{Manifest: &lib.Manifest{
146+
OriginalFilename: "list_manifest.yaml",
147+
GVK: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"},
148+
Obj: &unstructured.Unstructured{
149+
Object: map[string]interface{}{
150+
"kind": "List",
151+
"apiVersion": "v1",
152+
"items": []interface{}{
153+
"foo",
154+
"bar",
155+
},
156+
},
157+
},
158+
}},
159+
want: `list "list_manifest.yaml" (0 of 0)`,
160+
},
161+
}
162+
for _, tt := range tests {
163+
t.Run(tt.name, func(t *testing.T) {
164+
if got := tt.task.String(); !reflect.DeepEqual(got, tt.want) {
165+
t.Fatalf("%s", diff.ObjectReflectDiff(tt.want, got))
166+
}
167+
})
168+
}
169+
}

0 commit comments

Comments
 (0)