1 package project
2
3 import (
4 "testing"
5
6 "github.com/golang/mock/gomock"
7 "github.com/google/go-cmp/cmp"
8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
10 "github.com/redhat-developer/odo/pkg/kclient"
11 )
12
13 func TestCreate(t *testing.T) {
14
15 tests := []struct {
16 name string
17 projectName string
18 wait bool
19 isProjectSupported bool
20 isProjectSupportedErr error
21 expectedErr bool
22 }{
23 {
24 name: "empty project name",
25 projectName: "",
26 expectedErr: true,
27 },
28 {
29 name: "new project without project resource",
30 projectName: "new-project",
31 wait: false,
32 isProjectSupported: false,
33 isProjectSupportedErr: nil,
34 expectedErr: false,
35 },
36 {
37 name: "new project with project resource",
38 projectName: "new-project",
39 wait: false,
40 isProjectSupported: true,
41 isProjectSupportedErr: nil,
42 expectedErr: false,
43 },
44 {
45 name: "new project without project resource and wait",
46 projectName: "new-project",
47 wait: true,
48 isProjectSupported: false,
49 isProjectSupportedErr: nil,
50 expectedErr: false,
51 },
52 {
53 name: "new project with project resource and wait",
54 projectName: "new-project",
55 wait: true,
56 isProjectSupported: true,
57 isProjectSupportedErr: nil,
58 expectedErr: false,
59 },
60 }
61
62 for _, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 ctrl := gomock.NewController(t)
65 kc := kclient.NewMockClientInterface(ctrl)
66 appClient := NewClient(kc)
67
68 if tt.expectedErr == false {
69 kc.EXPECT().IsProjectSupported().Return(tt.isProjectSupported, tt.isProjectSupportedErr)
70 if tt.isProjectSupported {
71 kc.EXPECT().CreateNewProject(tt.projectName, tt.wait).Times(1)
72 } else {
73 kc.EXPECT().CreateNamespace(tt.projectName).Times(1)
74 }
75 if tt.wait {
76 kc.EXPECT().WaitForServiceAccountInNamespace(tt.projectName, "default").Times(1)
77 }
78 }
79
80 err := appClient.Create(tt.projectName, tt.wait)
81
82 if err != nil != tt.expectedErr {
83 t.Errorf("expected error %v, got %v", tt.expectedErr, err)
84 }
85 })
86 }
87 }
88
89 func TestDelete(t *testing.T) {
90 tests := []struct {
91 name string
92 projectName string
93 wait bool
94 isProjectSupported bool
95 isProjectSupportedErr error
96 expectedErr bool
97 }{
98 {
99 name: "empty project name",
100 projectName: "",
101 expectedErr: true,
102 },
103 {
104 name: "delete project without project resource",
105 projectName: "new-project",
106 wait: false,
107 isProjectSupported: false,
108 isProjectSupportedErr: nil,
109 expectedErr: false,
110 },
111 {
112 name: "delete project with project resource",
113 projectName: "new-project",
114 wait: false,
115 isProjectSupported: true,
116 isProjectSupportedErr: nil,
117 expectedErr: false,
118 },
119 {
120 name: "delete project without project resource and wait",
121 projectName: "new-project",
122 wait: true,
123 isProjectSupported: false,
124 isProjectSupportedErr: nil,
125 expectedErr: false,
126 },
127 {
128 name: "delete project with project resource and wait",
129 projectName: "new-project",
130 wait: true,
131 isProjectSupported: true,
132 isProjectSupportedErr: nil,
133 expectedErr: false,
134 },
135 }
136
137 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
139 ctrl := gomock.NewController(t)
140 kc := kclient.NewMockClientInterface(ctrl)
141 appClient := NewClient(kc)
142
143 if tt.expectedErr == false {
144 kc.EXPECT().IsProjectSupported().Return(tt.isProjectSupported, tt.isProjectSupportedErr)
145 if tt.isProjectSupported {
146 kc.EXPECT().DeleteProject(tt.projectName, tt.wait).Times(1)
147 } else {
148 kc.EXPECT().DeleteNamespace(tt.projectName, tt.wait).Times(1)
149 }
150 }
151
152 err := appClient.Delete(tt.projectName, tt.wait)
153
154 if err != nil != tt.expectedErr {
155 t.Errorf("expected error %v, got %v", tt.expectedErr, err)
156 }
157 })
158 }
159 }
160
161 func TestList(t *testing.T) {
162
163 expectedList := ProjectList{
164 TypeMeta: metav1.TypeMeta{
165 Kind: "List",
166 APIVersion: "odo.dev/v1alpha1",
167 },
168 Items: []Project{
169 {
170 TypeMeta: metav1.TypeMeta{
171 Kind: "Project",
172 APIVersion: "odo.dev/v1alpha1",
173 },
174 ObjectMeta: metav1.ObjectMeta{
175 Name: "project1",
176 },
177 },
178 {
179 TypeMeta: metav1.TypeMeta{
180 Kind: "Project",
181 APIVersion: "odo.dev/v1alpha1",
182 },
183 ObjectMeta: metav1.ObjectMeta{
184 Name: "project2",
185 },
186 },
187 },
188 }
189
190 tests := []struct {
191 name string
192 isProjectSupported bool
193 isProjectSupportedErr error
194 listNames []string
195 expectedErr bool
196 expectedList ProjectList
197 }{
198 {
199 name: "list projects without project resource",
200 isProjectSupported: false,
201 listNames: []string{"project1", "project2"},
202 isProjectSupportedErr: nil,
203 expectedErr: false,
204 expectedList: expectedList,
205 },
206 {
207 name: "list projects with project resource",
208 isProjectSupported: true,
209 listNames: []string{"project1", "project2"},
210 isProjectSupportedErr: nil,
211 expectedErr: false,
212 expectedList: expectedList,
213 },
214 }
215
216 for _, tt := range tests {
217 t.Run(tt.name, func(t *testing.T) {
218 ctrl := gomock.NewController(t)
219 kc := kclient.NewMockClientInterface(ctrl)
220 appClient := NewClient(kc)
221
222 kc.EXPECT().GetCurrentNamespace().Times(1)
223
224 if tt.expectedErr == false {
225 kc.EXPECT().IsProjectSupported().Return(tt.isProjectSupported, tt.isProjectSupportedErr)
226 if tt.isProjectSupported {
227 kc.EXPECT().ListProjectNames().Return(tt.listNames, nil).Times(1)
228 } else {
229 kc.EXPECT().GetNamespaces().Return(tt.listNames, nil).Times(1)
230 }
231 }
232
233 list, err := appClient.List()
234
235 if err != nil != tt.expectedErr {
236 t.Errorf("expected error %v, got %v", tt.expectedErr, err)
237 return
238 }
239
240 if diff := cmp.Diff(tt.expectedList, list); diff != "" {
241 t.Errorf("Client.List() expectedList mismatch (-want +got):\n%s", diff)
242 }
243 })
244 }
245 }
246
247 func TestExists(t *testing.T) {
248
249 tests := []struct {
250 name string
251 projectName string
252 isProjectSupported bool
253 isProjectSupportedErr error
254 expectedErr bool
255 }{
256 {
257 name: "project without project resource",
258 projectName: "new-project",
259 isProjectSupported: false,
260 isProjectSupportedErr: nil,
261 expectedErr: false,
262 },
263 {
264 name: "project with project resource",
265 projectName: "new-project",
266 isProjectSupported: true,
267 isProjectSupportedErr: nil,
268 expectedErr: false,
269 },
270 }
271
272 for _, tt := range tests {
273 t.Run(tt.name, func(t *testing.T) {
274 ctrl := gomock.NewController(t)
275 kc := kclient.NewMockClientInterface(ctrl)
276 appClient := NewClient(kc)
277
278 if tt.expectedErr == false {
279 kc.EXPECT().IsProjectSupported().Return(tt.isProjectSupported, tt.isProjectSupportedErr)
280 if tt.isProjectSupported {
281 kc.EXPECT().GetProject(tt.projectName).Times(1)
282 } else {
283 kc.EXPECT().GetNamespace(tt.projectName).Times(1)
284 }
285 }
286
287 _, err := appClient.Exists(tt.projectName)
288
289 if err != nil != tt.expectedErr {
290 t.Errorf("expected error %v, got %v", tt.expectedErr, err)
291 }
292 })
293 }
294 }
295
View as plain text