|
1 | 1 | package client |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "strings" |
6 | | - "testing" |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
7 | 7 |
|
8 | | - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
9 | 9 |
|
10 | | - "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/testobj" |
11 | | - "github.com/stretchr/testify/require" |
12 | | - corev1 "k8s.io/api/core/v1" |
13 | | - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
14 | | - "k8s.io/apimachinery/pkg/runtime" |
15 | | - "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/testobj" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/runtime/schema" |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | func TestSetDefaultGroupVersionKind(t *testing.T) { |
19 | | - tests := []struct { |
20 | | - name string |
21 | | - schemebuilder runtime.SchemeBuilder |
22 | | - obj Object |
23 | | - want schema.GroupVersionKind |
24 | | - }{ |
25 | | - { |
26 | | - name: "DefaultGVK", |
27 | | - schemebuilder: corev1.SchemeBuilder, |
28 | | - obj: &corev1.ServiceAccount{}, |
29 | | - want: schema.GroupVersionKind{ |
30 | | - Group: "", |
31 | | - Version: "v1", |
32 | | - Kind: "ServiceAccount", |
33 | | - }, |
34 | | - }, |
35 | | - } |
36 | | - for _, tt := range tests { |
37 | | - t.Run(tt.name, func(t *testing.T) { |
38 | | - s := runtime.NewScheme() |
39 | | - err := tt.schemebuilder.AddToScheme(s) |
40 | | - require.NoError(t, err) |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + schemebuilder runtime.SchemeBuilder |
| 22 | + obj Object |
| 23 | + want schema.GroupVersionKind |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "DefaultGVK", |
| 27 | + schemebuilder: corev1.SchemeBuilder, |
| 28 | + obj: &corev1.ServiceAccount{}, |
| 29 | + want: schema.GroupVersionKind{ |
| 30 | + Group: "", |
| 31 | + Version: "v1", |
| 32 | + Kind: "ServiceAccount", |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + s := runtime.NewScheme() |
| 39 | + err := tt.schemebuilder.AddToScheme(s) |
| 40 | + require.NoError(t, err) |
41 | 41 |
|
42 | | - SetDefaultGroupVersionKind(tt.obj, s) |
43 | | - require.EqualValues(t, tt.want, tt.obj.GetObjectKind().GroupVersionKind()) |
44 | | - }) |
45 | | - } |
| 42 | + SetDefaultGroupVersionKind(tt.obj, s) |
| 43 | + require.EqualValues(t, tt.want, tt.obj.GetObjectKind().GroupVersionKind()) |
| 44 | + }) |
| 45 | + } |
46 | 46 | } |
47 | 47 |
|
48 | 48 | // fake client doesn't support Server Side Apply |
49 | 49 | func applyError(err error) bool { |
50 | | - if err == nil { |
51 | | - return false |
52 | | - } |
53 | | - return strings.Contains(err.Error(), "/issues/115598") |
| 50 | + if err == nil { |
| 51 | + return false |
| 52 | + } |
| 53 | + return strings.Contains(err.Error(), "/issues/115598") |
54 | 54 | } |
55 | 55 |
|
56 | 56 | func TestServerSideApply(t *testing.T) { |
57 | | - tests := []struct { |
58 | | - name string |
59 | | - obj Object |
60 | | - schemebuilder *runtime.SchemeBuilder |
61 | | - changeFunc interface{} |
62 | | - want Object |
63 | | - }{ |
64 | | - { |
65 | | - name: "ServerSideMetadataApply", |
66 | | - obj: &corev1.Pod{ |
67 | | - ObjectMeta: metav1.ObjectMeta{ |
68 | | - Name: "testpod", |
69 | | - Namespace: "testns", |
70 | | - Labels: map[string]string{ |
71 | | - "testlabel": "oldvalue", |
72 | | - }, |
73 | | - }, |
74 | | - }, |
75 | | - schemebuilder: &corev1.SchemeBuilder, |
76 | | - changeFunc: func(p *corev1.Pod) error { |
77 | | - p.Labels["testlabel"] = "newvalue" |
78 | | - return nil |
79 | | - }, |
80 | | - want: &corev1.Pod{ |
81 | | - TypeMeta: metav1.TypeMeta{ |
82 | | - Kind: "Pod", |
83 | | - APIVersion: "v1", |
84 | | - }, |
85 | | - ObjectMeta: metav1.ObjectMeta{ |
86 | | - Name: "testpod", |
87 | | - Namespace: "testns", |
88 | | - Labels: map[string]string{ |
89 | | - "testlabel": "newvalue", |
90 | | - }, |
91 | | - }, |
92 | | - }, |
93 | | - }, |
94 | | - { |
95 | | - name: "ServerSideStatusApply", |
96 | | - obj: &corev1.Pod{ |
97 | | - ObjectMeta: metav1.ObjectMeta{ |
98 | | - Name: "testpod", |
99 | | - Namespace: "testns", |
100 | | - }, |
101 | | - Status: corev1.PodStatus{Message: "new"}, |
102 | | - }, |
103 | | - schemebuilder: &corev1.SchemeBuilder, |
104 | | - changeFunc: func(p *corev1.Pod) error { |
105 | | - p.Status.Message = "new" |
106 | | - return nil |
107 | | - }, |
108 | | - want: &corev1.Pod{ |
109 | | - TypeMeta: metav1.TypeMeta{ |
110 | | - Kind: "Pod", |
111 | | - APIVersion: "v1", |
112 | | - }, |
113 | | - ObjectMeta: metav1.ObjectMeta{ |
114 | | - Name: "testpod", |
115 | | - Namespace: "testns", |
116 | | - }, |
117 | | - Status: corev1.PodStatus{Message: "new"}, |
118 | | - }, |
119 | | - }, |
120 | | - { |
121 | | - name: "ServerSideUnstructuredApply", |
122 | | - obj: &unstructured.Unstructured{ |
123 | | - Object: map[string]interface{}{ |
124 | | - "kind": "Pod", |
125 | | - "apiVersion": "v1", |
126 | | - "metadata": map[string]interface{}{ |
127 | | - "name": "testpod", |
128 | | - "namespace": "testns", |
129 | | - }, |
130 | | - "status": map[string]interface{}{ |
131 | | - "message": "old", |
132 | | - }, |
133 | | - }, |
134 | | - }, |
135 | | - schemebuilder: &corev1.SchemeBuilder, |
136 | | - changeFunc: func(u *unstructured.Unstructured) error { |
137 | | - return unstructured.SetNestedField(u.Object, "new", "status", "message") |
138 | | - }, |
139 | | - want: &corev1.Pod{ |
140 | | - TypeMeta: metav1.TypeMeta{ |
141 | | - Kind: "Pod", |
142 | | - APIVersion: "v1", |
143 | | - }, |
144 | | - ObjectMeta: metav1.ObjectMeta{ |
145 | | - Name: "testpod", |
146 | | - Namespace: "testns", |
147 | | - }, |
148 | | - Status: corev1.PodStatus{Message: "new"}, |
149 | | - }, |
150 | | - }, |
151 | | - } |
152 | | - for _, tt := range tests { |
153 | | - t.Run(tt.name, func(t *testing.T) { |
154 | | - s := runtime.NewScheme() |
155 | | - require.NoError(t, tt.schemebuilder.AddToScheme(s)) |
| 57 | + tests := []struct { |
| 58 | + name string |
| 59 | + obj Object |
| 60 | + schemebuilder *runtime.SchemeBuilder |
| 61 | + changeFunc interface{} |
| 62 | + want Object |
| 63 | + }{ |
| 64 | + { |
| 65 | + name: "ServerSideMetadataApply", |
| 66 | + obj: &corev1.Pod{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "testpod", |
| 69 | + Namespace: "testns", |
| 70 | + Labels: map[string]string{ |
| 71 | + "testlabel": "oldvalue", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + schemebuilder: &corev1.SchemeBuilder, |
| 76 | + changeFunc: func(p *corev1.Pod) error { |
| 77 | + p.Labels["testlabel"] = "newvalue" |
| 78 | + return nil |
| 79 | + }, |
| 80 | + want: &corev1.Pod{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Name: "testpod", |
| 83 | + Namespace: "testns", |
| 84 | + Labels: map[string]string{ |
| 85 | + "testlabel": "newvalue", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "ServerSideStatusApply", |
| 92 | + obj: &corev1.Pod{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "testpod", |
| 95 | + Namespace: "testns", |
| 96 | + }, |
| 97 | + Status: corev1.PodStatus{Message: "new"}, |
| 98 | + }, |
| 99 | + schemebuilder: &corev1.SchemeBuilder, |
| 100 | + changeFunc: func(p *corev1.Pod) error { |
| 101 | + p.Status.Message = "new" |
| 102 | + return nil |
| 103 | + }, |
| 104 | + want: &corev1.Pod{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Name: "testpod", |
| 107 | + Namespace: "testns", |
| 108 | + }, |
| 109 | + Status: corev1.PodStatus{Message: "new"}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "ServerSideUnstructuredApply", |
| 114 | + obj: &unstructured.Unstructured{ |
| 115 | + Object: map[string]interface{}{ |
| 116 | + "kind": "Pod", |
| 117 | + "apiVersion": "v1", |
| 118 | + "metadata": map[string]interface{}{ |
| 119 | + "name": "testpod", |
| 120 | + "namespace": "testns", |
| 121 | + }, |
| 122 | + "status": map[string]interface{}{ |
| 123 | + "message": "old", |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + schemebuilder: &corev1.SchemeBuilder, |
| 128 | + changeFunc: func(u *unstructured.Unstructured) error { |
| 129 | + return unstructured.SetNestedField(u.Object, "new", "status", "message") |
| 130 | + }, |
| 131 | + want: &corev1.Pod{ |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + Name: "testpod", |
| 134 | + Namespace: "testns", |
| 135 | + }, |
| 136 | + Status: corev1.PodStatus{Message: "new"}, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + for _, tt := range tests { |
| 141 | + t.Run(tt.name, func(t *testing.T) { |
| 142 | + s := runtime.NewScheme() |
| 143 | + require.NoError(t, tt.schemebuilder.AddToScheme(s)) |
156 | 144 |
|
157 | | - applier := NewFakeApplier(s, "testowner", tt.obj) |
158 | | - err := applier.Apply(context.TODO(), tt.obj, tt.changeFunc)() |
159 | | - if applyError(err) { |
160 | | - return |
161 | | - } |
162 | | - require.NoError(t, err) |
| 145 | + applier := NewFakeApplier(s, "testowner", tt.obj) |
| 146 | + err := applier.Apply(context.TODO(), tt.obj, tt.changeFunc)() |
| 147 | + if applyError(err) { |
| 148 | + return |
| 149 | + } |
| 150 | + require.NoError(t, err) |
163 | 151 |
|
164 | | - newObj := corev1.Pod{} |
165 | | - require.NoError(t, applier.client.Get(context.TODO(), testobj.NamespacedName(tt.obj), &newObj)) |
| 152 | + newObj := corev1.Pod{} |
| 153 | + require.NoError(t, applier.client.Get(context.TODO(), testobj.NamespacedName(tt.obj), &newObj)) |
166 | 154 |
|
167 | | - newObj.ResourceVersion = "" |
168 | | - require.EqualValues(t, tt.want, &newObj) |
169 | | - }) |
170 | | - } |
| 155 | + newObj.ResourceVersion = "" |
| 156 | + require.EqualValues(t, tt.want, &newObj) |
| 157 | + }) |
| 158 | + } |
171 | 159 | } |
0 commit comments