-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
288 lines (208 loc) · 5.45 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"sort"
"testing"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
testclient "k8s.io/client-go/kubernetes/fake"
)
var mockK8s K8s
var mockAWS MockAWSed
func setupTest() {
log.Println("setup suite")
mockK8s.clientset = testclient.NewSimpleClientset()
clientset := mockK8s.clientset
mockAwsRepo, err := getAwsJson()
mockAWS = MockAWSed{AWSRepo: mockAwsRepo}
if err != nil {
log.Fatal(err)
}
// creats users in mock k8s clientset that are in mock_AWS.json
for _, user := range mockAwsRepo {
namespace := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: user.Username,
},
}
clientset.CoreV1().Namespaces().Create(context.Background(), namespace, v1.CreateOptions{})
// creates volumes according to the volume type
pv := &corev1.PersistentVolume{
ObjectMeta: v1.ObjectMeta{
Name: user.Username,
}}
clientset.CoreV1().PersistentVolumes().Create(context.Background(), pv, v1.CreateOptions{})
}
// creates a namespaces and associated PVs that are not in AWSed roster
namespace := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "dvader",
},
}
clientset.CoreV1().Namespaces().Create(context.Background(), namespace, v1.CreateOptions{})
pv := &corev1.PersistentVolume{
ObjectMeta: v1.ObjectMeta{
Name: "dvader",
}}
clientset.CoreV1().PersistentVolumes().Create(context.Background(), pv, v1.CreateOptions{})
}
func TestMain(m *testing.M) {
setupTest()
code := m.Run()
// shutdown()
os.Exit(code)
}
/*Testing out enrollment logic*/
func TestGetUserEnrollmentNotInRepo(t *testing.T) {
got, err := mockAWS.getUserEnrollment("dvadre")
if err != nil {
log.Fatal(err)
}
expected := false
if got != expected {
t.Errorf("User not in repo to delete")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
}
func TestGetUserEnrollmentInRepoShouldFalse(t *testing.T) {
got, err := mockAWS.getUserEnrollment("tix034")
if err != nil {
log.Fatal(err)
}
expected := false
if got != expected {
t.Errorf("User in repo to delete")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
}
func TestGetUserEnrollmentInRepoShouldTrue(t *testing.T) {
got, err := mockAWS.getUserEnrollment("pbotros")
if err != nil {
log.Fatal(err)
}
expected := true
if got != expected {
t.Errorf("User in repo to keep")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
}
/* Test namespace logic */
func TestListNamespaces(t *testing.T) {
log.Println("TestListNamespaces running")
expected := []string{"btice", "dvader", "pbotros", "n2nazar", "tix034"}
got, err := listNamespaces(mockK8s)
if err != nil {
log.Fatal(err)
}
sort.Strings(expected)
sort.Strings(got)
if !reflect.DeepEqual(expected, got) {
t.Errorf("lists don't match")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
log.Println("TestListNamespaces Ok")
}
func TestDeleteNamespace(t *testing.T) {
log.Println("TestDeleteNamespace running")
expected := []string{"btice", "pbotros", "n2nazar", "tix034"}
deleteNamespace(mockK8s, "dvader")
got, err := listNamespaces(mockK8s)
if err != nil {
log.Fatal(err)
}
sort.Strings(expected)
sort.Strings(got)
if !reflect.DeepEqual(expected, got) {
t.Errorf("lists don't match")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
log.Println("TestDeleteNamespace Ok")
}
func TestDeletePV(t *testing.T) {
log.Println("TestDeletePV running")
expected := []string{"btice", "dvader", "pbotros", "tix034"}
deletePV(mockK8s, "n2nazar")
var got []string
namspaceList, err := mockK8s.clientset.CoreV1().
PersistentVolumes().
List(context.Background(), v1.ListOptions{})
if err != nil {
t.Errorf("lists don't match")
}
for _, n := range namspaceList.Items {
got = append(got, n.Name)
}
sort.Strings(expected)
sort.Strings(got)
if !reflect.DeepEqual(expected, got) {
t.Errorf("lists don't match")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
log.Println("TestDeletePV Ok")
}
func TestCleanup(t *testing.T) {
log.Println("TestCleanup running")
expected := []string{"btice", "dvader", "tix034"}
err := cleanup(mockK8s, mockAWS, false)
if err != nil {
log.Fatal(err)
}
got, err := listNamespaces(mockK8s)
if err != nil {
log.Fatal(err)
}
sort.Strings(expected)
sort.Strings(got)
if !reflect.DeepEqual(expected, got) {
t.Errorf("lists don't match")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
log.Println("TestCleanup Ok")
}
/* Test JSON reader */
func TestGetAwsJson(t *testing.T) {
log.Println()
log.Println("Test Get Aws JSON mock function")
awsRepo, err := getAwsJson()
if err != nil {
return
}
var expectedEnrollments []string
expectedEnrollments = append(expectedEnrollments, "MUS206_WI23_D00")
expected := AWSedResponse{Username: "btice", FirstName: "brian", LastName: "tice", Uid: 130507, Enrollments: expectedEnrollments}
got := awsRepo[0]
if !reflect.DeepEqual(expected, got) {
t.Errorf("lists don't match")
fmt.Println("Expected", expected)
fmt.Println("Got", got)
}
log.Print("Ok")
log.Println()
}
func getAwsJson() ([]AWSedResponse, error) {
var awsRepo []AWSedResponse
userFile, err := os.Open("tests/mock_AWS.json")
if err != nil {
return nil, err
}
defer userFile.Close()
responseBytes, err := ioutil.ReadAll(userFile)
if err != nil {
return nil, err
}
json.Unmarshal(responseBytes, &awsRepo)
return awsRepo, nil
}