-
Notifications
You must be signed in to change notification settings - Fork 3
/
service_test.go
220 lines (177 loc) · 8.07 KB
/
service_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
//service 服务增删改查测试文件
package main
import (
"time"
"testing"
"github.com/muxiyun/Mae/model"
"github.com/kataras/iris/httptest"
)
func TestServiceCRUD(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("services")
defer model.DB.RWdb.DropTableIfExists("versions")
// create two users and get their token
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(andrew) 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(andrewadmin) 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 create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 1, // Be careful, it's type is int,but not string
"svc_name": "xueer_be",
"svc_desc": "the backend part of xueer",
}).Expect().Status(httptest.StatusForbidden)
// a normal user to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 1,
"svc_name": "xueer_be",
"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": 1,
"svc_name": "xueer_fe",
"svc_desc": "frontend part of xueer",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
// anonymous to get a service
e.GET("/api/v1.0/service/{svc_name}").WithPath("svc_name", "xueer_be").
Expect().Status(httptest.StatusForbidden)
// a normal user to get a service
e.GET("/api/v1.0/service/{svc_name}").WithPath("svc_name", "xueer_be").
WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// a admin user to get a service
e.GET("/api/v1.0/service/{svc_name}").WithPath("svc_name", "xueer_be").
WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
// anonymous to update a service
e.PUT("/api/v1.0/service/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_id": 2,
"svc_name": "XUEER_BE",
"svc_desc": "xueer backend",
}).Expect().Status(httptest.StatusForbidden)
// a normal user to update a service
e.PUT("/api/v1.0/service/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_id": 2,
"svc_name": "XUEER_BE",
"svc_desc": "xueer backend",
}).WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
// a admin user to update a service
e.PUT("/api/v1.0/service/{id}").WithPath("id", 1).WithJSON(map[string]interface{}{
"app_id": 1,
"svc_name": "Xueer_Be",
}).WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
//anonymous to list services
e.GET("/api/v1.0/service").Expect().Status(httptest.StatusForbidden) // list all
e.GET("/api/v1.0/service").WithQuery("app_id", 1).
Expect().Status(httptest.StatusForbidden) // list service belongs to an app
//a normal user to list service
e.GET("/api/v1.0/service").WithBasicAuth(andrew_token, "").Expect().Status(httptest.StatusForbidden) // list all
e.GET("/api/v1.0/service").WithQuery("app_id", 1).WithBasicAuth(andrew_token, "").
Expect().Body().Contains("OK") // list service belongs to an app
// admin user to list service
e.GET("/api/v1.0/service").WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK") // list all
e.GET("/api/v1.0/service").WithQuery("app_id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK") // list service belongs to an app
// anonymous to delete a service
e.DELETE("/api/v1.0/service/{id}").WithPath("id", 1).Expect().Status(httptest.StatusForbidden)
// a normal user to delete a service
e.DELETE("/api/v1.0/service/{id}").WithPath("id", 1).WithBasicAuth(andrew_token, "").
Expect().Status(httptest.StatusForbidden)
// an admin user to delete a service
e.DELETE("/api/v1.0/service/{id}").WithPath("id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
// delete a app who has service
e.DELETE("/api/v1.0/app/{id}").WithPath("id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
}
func TestRecursiveDeleteService(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")
// a normal user to create a service
e.POST("/api/v1.0/service").WithJSON(map[string]interface{}{
"app_id": 1,
"svc_name": "xueer_be",
"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": 1,
"svc_name": "xueer_fe",
"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-h").
WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
time.Sleep(3*time.Second)
//create a version which belongs to service xueer_be
e.POST("/api/v1.0/version").WithJSON(map[string]interface{}{
"svc_id": 1,
"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-h",
"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)
// an admin user to delete a service
e.DELETE("/api/v1.0/service/{id}").WithPath("id", 2).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
// an admin user to delete a service
e.DELETE("/api/v1.0/service/{id}").WithPath("id", 1).WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "mae-test-h").WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
}