1 package kclient
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/devfile/library/v2/pkg/devfile/parser/data"
8 "github.com/google/go-cmp/cmp"
9
10 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
11 "github.com/devfile/library/v2/pkg/devfile/generator"
12 devfileParser "github.com/devfile/library/v2/pkg/devfile/parser"
13 parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
14 "github.com/devfile/library/v2/pkg/testingutil"
15 corev1 "k8s.io/api/core/v1"
16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17 "k8s.io/apimachinery/pkg/runtime"
18
19 odolabels "github.com/redhat-developer/odo/pkg/labels"
20 odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil"
21
22 ktesting "k8s.io/client-go/testing"
23 )
24
25 func TestCreateService(t *testing.T) {
26
27 devObj := devfileParser.DevfileObj{
28 Data: func() data.DevfileData {
29 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
30 if err != nil {
31 t.Error(err)
32 }
33 err = devfileData.AddComponents([]devfilev1.Component{testingutil.GetFakeContainerComponent("container1")})
34 if err != nil {
35 t.Error(err)
36 }
37 return devfileData
38 }(),
39 }
40
41 tests := []struct {
42 name string
43 componentName string
44 wantErr bool
45 }{
46 {
47 name: "Case: Valid component name",
48 componentName: "testComponent",
49 wantErr: false,
50 },
51 {
52 name: "Case: Invalid component name",
53 componentName: "",
54 wantErr: true,
55 },
56 }
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
59
60 fkclient, fkclientset := FakeNew()
61 fkclient.Namespace = "default"
62
63 objectMeta := generator.GetObjectMeta(tt.componentName, "default", nil, nil)
64
65 labels := map[string]string{
66 "component": tt.componentName,
67 }
68
69 fkclientset.Kubernetes.PrependReactor("create", "services", func(action ktesting.Action) (bool, runtime.Object, error) {
70 if tt.componentName == "" {
71 return true, nil, fmt.Errorf("component name is empty")
72 }
73 service := corev1.Service{
74 ObjectMeta: metav1.ObjectMeta{
75 Name: tt.componentName,
76 },
77 }
78 return true, &service, nil
79 })
80
81 serviceParams := generator.ServiceParams{
82 ObjectMeta: objectMeta,
83 SelectorLabels: labels,
84 }
85
86 service, err := generator.GetService(devObj, serviceParams, parsercommon.DevfileOptions{})
87 if err != nil {
88 t.Errorf("generator.GetService unexpected error %v", err)
89 }
90
91 createdService, err := fkclient.CreateService(*service)
92
93
94 if !tt.wantErr == (err != nil) {
95 t.Errorf("fkclient.createService unexpected error %v, wantErr %v", err, tt.wantErr)
96 }
97
98 if err == nil {
99 if len(fkclientset.Kubernetes.Actions()) != 1 {
100 t.Errorf("expected 1 action, got: %v", fkclientset.Kubernetes.Actions())
101 } else {
102 if createdService.Name != tt.componentName {
103 t.Errorf("component name does not match the expected name, expected: %s, got %s", tt.componentName, createdService.Name)
104 }
105 }
106 }
107
108 })
109 }
110 }
111
112 func TestUpdateService(t *testing.T) {
113
114 devObj := devfileParser.DevfileObj{
115 Data: func() data.DevfileData {
116 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
117 if err != nil {
118 t.Error(err)
119 }
120 err = devfileData.AddComponents([]devfilev1.Component{testingutil.GetFakeContainerComponent("container1")})
121 if err != nil {
122 t.Error(err)
123 }
124 return devfileData
125 }(),
126 }
127
128 tests := []struct {
129 name string
130 componentName string
131 wantErr bool
132 }{
133 {
134 name: "Case: Valid component name",
135 componentName: "testComponent",
136 wantErr: false,
137 },
138 {
139 name: "Case: Invalid component name",
140 componentName: "",
141 wantErr: true,
142 },
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146
147 fkclient, fkclientset := FakeNew()
148 fkclient.Namespace = "default"
149
150 objectMeta := generator.GetObjectMeta(tt.componentName, "default", nil, nil)
151
152 labels := map[string]string{
153 "component": tt.componentName,
154 }
155
156 fkclientset.Kubernetes.PrependReactor("update", "services", func(action ktesting.Action) (bool, runtime.Object, error) {
157 if tt.componentName == "" {
158 return true, nil, fmt.Errorf("component name is empty")
159 }
160 service := corev1.Service{
161 ObjectMeta: metav1.ObjectMeta{
162 Name: tt.componentName,
163 },
164 }
165 return true, &service, nil
166 })
167
168 serviceParams := generator.ServiceParams{
169 ObjectMeta: objectMeta,
170 SelectorLabels: labels,
171 }
172
173 service, err := generator.GetService(devObj, serviceParams, parsercommon.DevfileOptions{})
174 if err != nil {
175 t.Errorf("generator.GetService unexpected error %v", err)
176 }
177
178 updatedService, err := fkclient.UpdateService(*service)
179
180
181 if !tt.wantErr == (err != nil) {
182 t.Errorf("fkclient.UpdateService unexpected error %v, wantErr %v", err, tt.wantErr)
183 }
184
185 if err == nil {
186
187 if len(fkclientset.Kubernetes.Actions()) != 1 {
188 t.Errorf("expected 1 action, got: %v", fkclientset.Kubernetes.Actions())
189 } else {
190 if updatedService.Name != tt.componentName {
191 t.Errorf("service name does not match the expected name, expected: %s, got %s", tt.componentName, updatedService.Name)
192 }
193 }
194
195 }
196
197 })
198 }
199 }
200
201 func TestListServices(t *testing.T) {
202 type args struct {
203 selector string
204 }
205 tests := []struct {
206 name string
207 args args
208 returnedServices corev1.ServiceList
209 want []corev1.Service
210 wantErr bool
211 }{
212 {
213 name: "case 1: returned 3 services",
214 args: args{
215 selector: odolabels.GetSelector("nodejs", "app", odolabels.ComponentDevMode, false),
216 },
217 returnedServices: corev1.ServiceList{
218 Items: odoTestingUtil.FakeKubeServices("nodejs"),
219 },
220 want: odoTestingUtil.FakeKubeServices("nodejs"),
221 },
222 {
223 name: "case 2: no service returned",
224 args: args{
225 selector: odolabels.GetSelector("nodejs", "app", odolabels.ComponentDevMode, false),
226 },
227 returnedServices: corev1.ServiceList{
228 Items: nil,
229 },
230 want: nil,
231 },
232 }
233 for _, tt := range tests {
234 t.Run(tt.name, func(t *testing.T) {
235
236 fkclient, fkclientset := FakeNew()
237 fkclient.Namespace = "default"
238
239 fkclientset.Kubernetes.PrependReactor("list", "services", func(action ktesting.Action) (bool, runtime.Object, error) {
240 return true, &tt.returnedServices, nil
241 })
242
243 got, err := fkclient.ListServices(tt.args.selector)
244 if (err != nil) != tt.wantErr {
245 t.Errorf("ListServices() error = %v, wantErr %v", err, tt.wantErr)
246 return
247 }
248 if diff := cmp.Diff(tt.want, got); diff != "" {
249 t.Errorf("Client.ListServices() mismatch (-want +got):\n%s", diff)
250 }
251 })
252 }
253 }
254
255 func TestClient_GetOneServiceFromSelector(t *testing.T) {
256 wantService := odoTestingUtil.FakeKubeService("nodejs", "nodejs-app")
257
258 type args struct {
259 selector string
260 }
261 tests := []struct {
262 name string
263 args args
264 returnedServices corev1.ServiceList
265 want *corev1.Service
266 wantErr bool
267 }{
268
269 {
270 name: "case 1: returned the correct service",
271 args: args{
272 selector: odolabels.GetSelector("nodejs", "app", odolabels.ComponentDevMode, false),
273 },
274 returnedServices: corev1.ServiceList{
275 Items: []corev1.Service{
276 odoTestingUtil.FakeKubeService("nodejs", "nodejs-app"),
277 },
278 },
279 want: &wantService,
280 },
281 {
282 name: "case 2: no service returned",
283 args: args{
284 selector: odolabels.GetSelector("nodejs", "app", odolabels.ComponentDevMode, false),
285 },
286 returnedServices: corev1.ServiceList{
287 Items: nil,
288 },
289 wantErr: true,
290 },
291 {
292 name: "case 3: more than one service returned",
293 args: args{
294 selector: odolabels.GetSelector("nodejs", "app", odolabels.ComponentDevMode, false),
295 },
296 returnedServices: corev1.ServiceList{
297 Items: odoTestingUtil.FakeKubeServices("nodejs"),
298 },
299 wantErr: true,
300 },
301 }
302 for _, tt := range tests {
303 t.Run(tt.name, func(t *testing.T) {
304
305 fkclient, fkclientset := FakeNew()
306 fkclient.Namespace = "default"
307
308 fkclientset.Kubernetes.PrependReactor("list", "services", func(action ktesting.Action) (bool, runtime.Object, error) {
309 return true, &tt.returnedServices, nil
310 })
311
312 got, err := fkclient.GetOneServiceFromSelector(tt.args.selector)
313 if (err != nil) != tt.wantErr {
314 t.Errorf("GetOneServiceFromSelector() error = %v, wantErr %v", err, tt.wantErr)
315 return
316 }
317 if diff := cmp.Diff(tt.want, got); diff != "" {
318 t.Errorf("Client.GetOneServiceFromSelector() mismatch (-want +got):\n%s", diff)
319 }
320 })
321 }
322 }
323
View as plain text