1 package kclient
2
3 import (
4 "errors"
5 "testing"
6
7 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
8 "github.com/devfile/library/v2/pkg/devfile/generator"
9 devfileParser "github.com/devfile/library/v2/pkg/devfile/parser"
10 "github.com/devfile/library/v2/pkg/devfile/parser/data"
11 "github.com/devfile/library/v2/pkg/testingutil"
12
13 odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil"
14
15 appsv1 "k8s.io/api/apps/v1"
16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17 "k8s.io/apimachinery/pkg/runtime"
18 "k8s.io/apimachinery/pkg/types"
19 ktesting "k8s.io/client-go/testing"
20 )
21
22
23 func createFakeDeployment(fkclient *Client, fkclientset *FakeClientset, podName string, labels map[string]string) (*appsv1.Deployment, error) {
24 fakeUID := types.UID("12345")
25 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
26 if err != nil {
27 return nil, err
28 }
29
30 err = devfileData.AddComponents([]devfilev1.Component{
31 testingutil.GetFakeContainerComponent("container1"),
32 })
33 if err != nil {
34 return nil, err
35 }
36
37 devObj := devfileParser.DevfileObj{Data: devfileData}
38
39 podTemplateSpec, err := generator.GetPodTemplateSpec(devObj, generator.PodTemplateParams{})
40 if err != nil {
41 return nil, err
42 }
43
44 objectMeta := generator.GetObjectMeta(podName, "default", labels, nil)
45
46 deploymentParams := generator.DeploymentParams{
47 ObjectMeta: objectMeta,
48 PodTemplateSpec: podTemplateSpec,
49 }
50 deploy, _ := generator.GetDeployment(devObj, deploymentParams)
51 fkclientset.Kubernetes.PrependReactor("patch", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
52 if podName == "" {
53 return true, nil, errors.New("deployment name is empty")
54 }
55 deployment := appsv1.Deployment{
56 TypeMeta: metav1.TypeMeta{
57 Kind: DeploymentKind,
58 APIVersion: DeploymentAPIVersion,
59 },
60 ObjectMeta: metav1.ObjectMeta{
61 Name: podName,
62 UID: fakeUID,
63 },
64 }
65 return true, &deployment, nil
66 })
67
68 createdDeployment, err := fkclient.ApplyDeployment(*deploy)
69 if err != nil {
70 return nil, err
71 }
72 return createdDeployment, nil
73 }
74
75 func TestCreateDeployment(t *testing.T) {
76
77 labels := map[string]string{
78 "app": "app",
79 "component": "frontend",
80 }
81
82 tests := []struct {
83 name string
84 deploymentName string
85 wantErr bool
86 }{
87 {
88 name: "Case: Valid deployment name",
89 deploymentName: "pod",
90 wantErr: false,
91 },
92 {
93 name: "Case: Invalid deployment name",
94 deploymentName: "",
95 wantErr: true,
96 },
97 }
98 for _, tt := range tests {
99 t.Run(tt.name, func(t *testing.T) {
100
101 fkclient, fkclientset := FakeNew()
102 fkclient.Namespace = "default"
103
104 createdDeployment, err := createFakeDeployment(fkclient, fkclientset, tt.deploymentName, labels)
105
106 if !tt.wantErr == (err != nil) {
107 t.Errorf("fkclient.CreateDeployment(pod) unexpected error %v, wantErr %v", err, tt.wantErr)
108 }
109
110 if err == nil {
111
112 if len(fkclientset.Kubernetes.Actions()) != 2 {
113 t.Errorf("expected 2 action in StartDeployment got %d: %v", len(fkclientset.Kubernetes.Actions()), fkclientset.Kubernetes.Actions())
114 } else {
115 if createdDeployment.Name != tt.deploymentName {
116 t.Errorf("deployment name does not match the expected name, expected: %s, got %s", tt.deploymentName, createdDeployment.Name)
117 }
118 }
119
120 }
121
122 })
123 }
124 }
125
126 func TestGetDeploymentByName(t *testing.T) {
127
128 tests := []struct {
129 name string
130 deploymentName string
131 wantDeploymentName string
132 wantErr bool
133 }{
134 {
135 name: "Case 1: Valid deployment name",
136 deploymentName: "mydeploy1",
137 wantDeploymentName: "mydeploy1",
138 wantErr: false,
139 },
140 {
141 name: "Case 2: Invalid deployment name",
142 deploymentName: "mydeploy2",
143 wantDeploymentName: "",
144 wantErr: false,
145 },
146 {
147 name: "Case 3: Error condition",
148 deploymentName: "mydeploy1",
149 wantErr: true,
150 },
151 }
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154
155 fkclient, fkclientset := FakeNew()
156 fkclient.Namespace = "default"
157
158 fkclientset.Kubernetes.PrependReactor("get", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
159 if tt.deploymentName == "mydeploy2" {
160 emptyDeployment := odoTestingUtil.CreateFakeDeployment("", false)
161 return true, emptyDeployment, nil
162 } else if tt.deploymentName == "mydeploy1" {
163 deployment := odoTestingUtil.CreateFakeDeployment(tt.deploymentName, false)
164 return true, deployment, nil
165 } else {
166 return true, nil, errors.New("deployment get error")
167 }
168
169 })
170
171 deployment, err := fkclient.GetDeploymentByName(tt.deploymentName)
172 if !tt.wantErr && err != nil {
173 t.Errorf("TestGetDeploymentByName unexpected error: %v", err)
174 } else if !tt.wantErr && deployment.GetName() != tt.wantDeploymentName {
175 t.Errorf("TestGetDeploymentByName error: expected %v, got %v", tt.wantDeploymentName, deployment.GetName())
176 }
177
178 })
179 }
180 }
181
182 func TestUpdateDeployment(t *testing.T) {
183
184 labels := map[string]string{
185 "app": "app",
186 "component": "frontend",
187 }
188
189 devObj := devfileParser.DevfileObj{
190 Data: func() data.DevfileData {
191 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
192 _ = devfileData.AddComponents([]devfilev1.Component{
193 testingutil.GetFakeContainerComponent("container1"),
194 })
195 return devfileData
196 }(),
197 }
198
199 podTemplateSpec, err := generator.GetPodTemplateSpec(devObj, generator.PodTemplateParams{})
200 if err != nil {
201 t.Errorf("generator.GetPodTemplateSpec unexpected error %v", err)
202 }
203
204 tests := []struct {
205 name string
206 deploymentName string
207 wantErr bool
208 }{
209 {
210 name: "Case: Valid deployment name",
211 deploymentName: "pod",
212 wantErr: false,
213 },
214 {
215 name: "Case: Invalid deployment name",
216 deploymentName: "",
217 wantErr: true,
218 },
219 }
220 for _, tt := range tests {
221 t.Run(tt.name, func(t *testing.T) {
222
223 fkclient, fkclientset := FakeNew()
224 fkclient.Namespace = "default"
225
226 objectMeta := generator.GetObjectMeta(tt.deploymentName, "default", labels, nil)
227
228 deploymentParams := generator.DeploymentParams{
229 ObjectMeta: objectMeta,
230 PodTemplateSpec: podTemplateSpec,
231 }
232 deploy, _ := generator.GetDeployment(devObj, deploymentParams)
233 fkclientset.Kubernetes.PrependReactor("patch", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
234 if tt.deploymentName == "" {
235 return true, nil, errors.New("deployment name is empty")
236 }
237 deployment := appsv1.Deployment{
238 TypeMeta: metav1.TypeMeta{
239 Kind: DeploymentKind,
240 APIVersion: DeploymentAPIVersion,
241 },
242 ObjectMeta: metav1.ObjectMeta{
243 Name: tt.deploymentName,
244 },
245 }
246 return true, &deployment, nil
247 })
248
249 updatedDeployment, err := fkclient.ApplyDeployment(*deploy)
250
251
252 if !tt.wantErr == (err != nil) {
253 t.Errorf("fkclient.UpdateDeployment(pod) unexpected error %v, wantErr %v", err, tt.wantErr)
254 }
255
256 if err == nil {
257
258 if len(fkclientset.Kubernetes.Actions()) != 2 {
259 t.Errorf("expected 2 action in UpdateDeployment got %d: %v", len(fkclientset.Kubernetes.Actions()), fkclientset.Kubernetes.Actions())
260 } else {
261 if updatedDeployment.Name != tt.deploymentName {
262 t.Errorf("deployment name does not match the expected name, expected: %s, got %s", tt.deploymentName, updatedDeployment.Name)
263 }
264 }
265
266 }
267
268 })
269 }
270 }
271
View as plain text