-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_test.go
227 lines (185 loc) · 8.44 KB
/
app_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
//app ,应用增删改查测试文件
package main
import (
"github.com/kataras/iris/httptest"
"github.com/muxiyun/Mae/model"
"testing"
"time"
)
func TestAppCRUD(t *testing.T) {
e := httptest.New(t, newApp(), httptest.URL("http://127.0.0.1:8080"))
defer model.DB.RWdb.DropTableIfExists("users")
defer model.DB.RWdb.DropTableIfExists("casbin_rule")
defer model.DB.RWdb.DropTableIfExists("apps")
defer model.DB.RWdb.DropTableIfExists("versions")
defer model.DB.RWdb.DropTableIfExists("services")
CreateUserForTest(e, "andrew", "andrew123", "andrewpqc@mails.ccnu.edu.cn")
CreateAdminForTest(e, "andrewadmin", "andrewadmin123", "3480437308@qq.com")
andrew_token := GetTokenForTest(e, "andrew", "andrew123", 60*60)
andrewadmin_token := GetTokenForTest(e, "andrewadmin", "andrewadmin123", 60*60)
// Anonymous to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "学而",
"app_desc": "华师课程挖掘机",
}).Expect().Status(httptest.StatusForbidden)
//a normal user to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "学而",
"app_desc": "华师课程挖掘机",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
//an admin to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "华师匣子",
"app_desc": "华师校园助手",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
//Anonymous to get an app
e.GET("/api/v1.0/app/{appname}").WithPath("appname", "学而").Expect().Status(httptest.StatusForbidden)
//a normal user to get an app
e.GET("/api/v1.0/app/{appname}").WithPath("appname", "学而").WithBasicAuth(andrew_token, "").
Expect().Body().Contains("OK")
// an admin user to get an app
e.GET("/api/v1.0/app/{appname}").WithPath("appname", "学而").WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
// Anonymous to update a app
e.PUT("/api/v1.0/app/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_name": "xueer",
}).Expect().Status(httptest.StatusForbidden)
//a normal user to update an app
e.PUT("/api/v1.0/app/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_name": "Xueer",
"app_desc": "华师课程挖掘机鸡鸡鸡鸡",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
//an admin user to update an app
e.PUT("/api/v1.0/app/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_name": "xueer",
"app_desc": "山东蓝想挖掘机学学校",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
//anonymous to list apps
e.GET("/api/v1.0/app").Expect().Status(httptest.StatusForbidden)
//a normal user to list apps
e.GET("/api/v1.0/app").WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// an admin user to list apps
e.GET("/api/v1.0/app").WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
// anonymous to delete an app
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 1).Expect().Status(httptest.StatusForbidden)
//a normal user to delete an app
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 1).WithBasicAuth(andrew_token, "").
Expect().Status(httptest.StatusForbidden)
//an admin user to delete an app
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
// anonymous test app_name duplicate checker
e.GET("/api/v1.0/app/duplicate").WithQuery("appname", "华师匣子").
Expect().Status(httptest.StatusForbidden)
// a normal user to test app_name duplicate checker
e.GET("/api/v1.0/app/duplicate").WithQuery("appname", "华师匣子").
WithBasicAuth(andrew_token, "").Expect().Body().NotContains("not exist")
// an admin user to test app_name duplicate checker
e.GET("/api/v1.0/app/duplicate").WithQuery("appname", "木小犀机器人").
WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("not exist")
}
func TestRecursiveDeleteApp(t *testing.T){
time.Sleep(5*time.Second)
e := httptest.New(t, newApp(), httptest.URL("http://127.0.0.1:8080"))
defer model.DB.RWdb.DropTableIfExists("users")
defer model.DB.RWdb.DropTableIfExists("casbin_rule")
defer model.DB.RWdb.DropTableIfExists("apps")
defer model.DB.RWdb.DropTableIfExists("versions")
defer model.DB.RWdb.DropTableIfExists("services")
CreateUserForTest(e, "andrew", "andrew123", "andrewpqc@mails.ccnu.edu.cn")
CreateAdminForTest(e, "andrewadmin", "andrewadmin123", "3480437308@qq.com")
andrew_token := GetTokenForTest(e, "andrew", "andrew123", 60*60)
andrewadmin_token := GetTokenForTest(e, "andrewadmin", "andrewadmin123", 60*60)
//a normal user to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "学而1",
"app_desc": "华师课程挖掘机",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// delete an app which has no service
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
time.Sleep(3*time.Second)
//a normal user to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "学而2",
"app_desc": "华师课程挖掘机",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// a normal user to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 2,
"svc_name": "xueer_be2",
"svc_desc": "the backend part of xueer",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// an admin to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 2,
"svc_name": "xueer_fe2",
"svc_desc": "frontend part of xueer",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
// delete an app which has two services, but there are no versions of each service
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 2).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
time.Sleep(3*time.Second)
//a normal user to create an app
e.POST("/api/v1.0/app").WithJSON(map[string]interface{}{
"app_name": "学而3",
"app_desc": "华师课程挖掘机",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// a normal user to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 3,
"svc_name": "xueer_be3",
"svc_desc": "the backend part of xueer",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// an admin to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 3,
"svc_name": "xueer_fe3",
"svc_desc": "frontend part of xueer",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
// create a namespace mae-test-g
e.POST("/api/v1.0/ns/{ns}").WithPath("ns", "mae-test-g").
WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
//create a version which belongs to service xueer_be
e.POST("/api/v1.0/version").WithJSON(map[string]interface{}{
"svc_id": 3,
"version_name": "xueer-be-v1",
"version_desc": "xueer be version 1",
"version_conf": map[string]interface{}{
"deployment": map[string]interface{}{
"deploy_name": "xueer-be-v1-deployment",
"name_space": "mae-test-g",
"replicas": 1,
"labels": map[string]string{"run": "xueer-be"},
"containers": [](map[string]interface{}){
map[string]interface{}{
"ctr_name": "xueer-be-v1-ct",
"image_url": "pqcsdockerhub/kube-test",
"start_cmd": []string{"gunicorn", "app:app", "-b", "0.0.0.0:8080", "--log-level", "DEBUG"},
"ports": [](map[string]interface{}){
map[string]interface{}{
"image_port": 8080,
"target_port": 8090,
"protocol": "TCP",
},
},
},
},
},
"svc": map[string]interface{}{
"svc_name": "xueer-be-v1-service",
"selector": map[string]string{"run": "xueer-be"},
"labels": map[string]string{"run": "xueer-be"},
},
},
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
//apply version "xueer-be-v1"
e.GET("/api/v1.0/version/apply").WithQuery("version_name", "xueer-be-v1").
WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
time.Sleep(3*time.Second)
// to recursive delete the app and the service of the app and the versions of the service.
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 3).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "mae-test-g").WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
}