|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestService(t *testing.T) { |
| 28 | + tests := []HttpTestCase{ |
| 29 | + { |
| 30 | + Desc: "create service without plugin", |
| 31 | + Object: ManagerApiExpect(t), |
| 32 | + Method: http.MethodPut, |
| 33 | + Path: "/apisix/admin/services/s1", |
| 34 | + Headers: map[string]string{"Authorization": token}, |
| 35 | + Body: `{ |
| 36 | + "name": "testservice", |
| 37 | + "upstream": { |
| 38 | + "type": "roundrobin", |
| 39 | + "nodes": [{ |
| 40 | + "host": "172.16.238.20", |
| 41 | + "port": 1980, |
| 42 | + "weight": 1 |
| 43 | + }, |
| 44 | + { |
| 45 | + "host": "172.16.238.20", |
| 46 | + "port": 1981, |
| 47 | + "weight": 2 |
| 48 | + }, |
| 49 | + { |
| 50 | + "host": "172.16.238.20", |
| 51 | + "port": 1982, |
| 52 | + "weight": 3 |
| 53 | + }] |
| 54 | + } |
| 55 | + }`, |
| 56 | + ExpectStatus: http.StatusOK, |
| 57 | + }, |
| 58 | + { |
| 59 | + Desc: "get the service s1", |
| 60 | + Object: ManagerApiExpect(t), |
| 61 | + Method: http.MethodGet, |
| 62 | + Path: "/apisix/admin/services/s1", |
| 63 | + Headers: map[string]string{"Authorization": token}, |
| 64 | + ExpectCode: http.StatusOK, |
| 65 | + ExpectBody: "\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1},{\"host\":\"172.16.238.20\",\"port\":1981,\"weight\":2},{\"host\":\"172.16.238.20\",\"port\":1982,\"weight\":3}],\"type\":\"roundrobin\"}", |
| 66 | + }, |
| 67 | + { |
| 68 | + Desc: "create route using the service just created", |
| 69 | + Object: ManagerApiExpect(t), |
| 70 | + Method: http.MethodPut, |
| 71 | + Path: "/apisix/admin/routes/r1", |
| 72 | + Body: `{ |
| 73 | + "uri": "/server_port", |
| 74 | + "service_id": "s1" |
| 75 | + }`, |
| 76 | + Headers: map[string]string{"Authorization": token}, |
| 77 | + ExpectStatus: http.StatusOK, |
| 78 | + Sleep: sleepTime, |
| 79 | + }, |
| 80 | + } |
| 81 | + for _, tc := range tests { |
| 82 | + testCaseCheck(tc, t) |
| 83 | + } |
| 84 | + |
| 85 | + // batch test /server_port api |
| 86 | + time.Sleep(sleepTime) |
| 87 | + res := BatchTestServerPort(t, 18) |
| 88 | + assert.True(t, res["1980"] == 3) |
| 89 | + assert.True(t, res["1981"] == 6) |
| 90 | + assert.True(t, res["1982"] == 9) |
| 91 | + |
| 92 | + tests = []HttpTestCase{ |
| 93 | + { |
| 94 | + Desc: "create service with plugin", |
| 95 | + Object: ManagerApiExpect(t), |
| 96 | + Method: http.MethodPut, |
| 97 | + Path: "/apisix/admin/services/s1", |
| 98 | + Headers: map[string]string{"Authorization": token}, |
| 99 | + Body: `{ |
| 100 | + "name": "testservice", |
| 101 | + "plugins": { |
| 102 | + "limit-count": { |
| 103 | + "count": 100, |
| 104 | + "time_window": 60, |
| 105 | + "rejected_code": 503, |
| 106 | + "key": "remote_addr" |
| 107 | + } |
| 108 | + }, |
| 109 | + "upstream": { |
| 110 | + "type": "roundrobin", |
| 111 | + "nodes": [{ |
| 112 | + "host": "172.16.238.20", |
| 113 | + "port": 1980, |
| 114 | + "weight": 1 |
| 115 | + }] |
| 116 | + } |
| 117 | + }`, |
| 118 | + ExpectStatus: http.StatusOK, |
| 119 | + }, |
| 120 | + { |
| 121 | + Desc: "get the service s1", |
| 122 | + Object: ManagerApiExpect(t), |
| 123 | + Method: http.MethodGet, |
| 124 | + Path: "/apisix/admin/services/s1", |
| 125 | + Headers: map[string]string{"Authorization": token}, |
| 126 | + ExpectCode: http.StatusOK, |
| 127 | + ExpectBody: "\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}}", |
| 128 | + }, |
| 129 | + { |
| 130 | + Desc: "create route using the service just created", |
| 131 | + Object: ManagerApiExpect(t), |
| 132 | + Method: http.MethodPut, |
| 133 | + Path: "/apisix/admin/routes/r1", |
| 134 | + Body: `{ |
| 135 | + "uri": "/server_port", |
| 136 | + "service_id": "s1" |
| 137 | + }`, |
| 138 | + Headers: map[string]string{"Authorization": token}, |
| 139 | + ExpectStatus: http.StatusOK, |
| 140 | + Sleep: sleepTime, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tc := range tests { |
| 144 | + testCaseCheck(tc, t) |
| 145 | + } |
| 146 | + |
| 147 | + // hit routes and check the response header |
| 148 | + time.Sleep(sleepTime) |
| 149 | + basepath := "http://127.0.0.1:9080" |
| 150 | + request, _ := http.NewRequest("GET", basepath+"/server_port", nil) |
| 151 | + request.Header.Add("Authorization", token) |
| 152 | + resp, err := http.DefaultClient.Do(request) |
| 153 | + assert.Nil(t, err) |
| 154 | + defer resp.Body.Close() |
| 155 | + assert.Equal(t, 200, resp.StatusCode) |
| 156 | + assert.Equal(t, "100", resp.Header["X-Ratelimit-Limit"][0]) |
| 157 | + assert.Equal(t, "99", resp.Header["X-Ratelimit-Remaining"][0]) |
| 158 | + |
| 159 | + tests = []HttpTestCase{ |
| 160 | + { |
| 161 | + Desc: "create service with all options", |
| 162 | + Object: ManagerApiExpect(t), |
| 163 | + Method: http.MethodPut, |
| 164 | + Path: "/apisix/admin/services/s1", |
| 165 | + Headers: map[string]string{"Authorization": token}, |
| 166 | + Body: `{ |
| 167 | + "name": "testservice", |
| 168 | + "desc": "testservice_desc", |
| 169 | + "labels": { |
| 170 | + "build":"16", |
| 171 | + "env":"production", |
| 172 | + "version":"v2" |
| 173 | + }, |
| 174 | + "enable_websocket":true, |
| 175 | + "plugins": { |
| 176 | + "limit-count": { |
| 177 | + "count": 100, |
| 178 | + "time_window": 60, |
| 179 | + "rejected_code": 503, |
| 180 | + "key": "remote_addr" |
| 181 | + } |
| 182 | + }, |
| 183 | + "upstream": { |
| 184 | + "type": "roundrobin", |
| 185 | + "create_time":1602883670, |
| 186 | + "update_time":1602893670, |
| 187 | + "nodes": [{ |
| 188 | + "host": "172.16.238.20", |
| 189 | + "port": 1980, |
| 190 | + "weight": 1 |
| 191 | + }] |
| 192 | + } |
| 193 | + }`, |
| 194 | + ExpectStatus: http.StatusOK, |
| 195 | + }, |
| 196 | + { |
| 197 | + Desc: "get the service s1", |
| 198 | + Object: ManagerApiExpect(t), |
| 199 | + Method: http.MethodGet, |
| 200 | + Path: "/apisix/admin/services/s1", |
| 201 | + Headers: map[string]string{"Authorization": token}, |
| 202 | + ExpectCode: http.StatusOK, |
| 203 | + ExpectBody: "\"name\":\"testservice\",\"desc\":\"testservice_desc\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"enable_websocket\":true}", |
| 204 | + }, |
| 205 | + { |
| 206 | + Desc: "create route using the service just created", |
| 207 | + Object: ManagerApiExpect(t), |
| 208 | + Method: http.MethodPut, |
| 209 | + Path: "/apisix/admin/routes/r1", |
| 210 | + Body: `{ |
| 211 | + "uri": "/hello", |
| 212 | + "service_id": "s1" |
| 213 | + }`, |
| 214 | + Headers: map[string]string{"Authorization": token}, |
| 215 | + ExpectStatus: http.StatusOK, |
| 216 | + Sleep: sleepTime, |
| 217 | + }, |
| 218 | + { |
| 219 | + Desc: "verify route", |
| 220 | + Object: APISIXExpect(t), |
| 221 | + Method: http.MethodGet, |
| 222 | + Path: "/hello", |
| 223 | + ExpectStatus: http.StatusOK, |
| 224 | + ExpectBody: "hello world", |
| 225 | + }, |
| 226 | + } |
| 227 | + for _, tc := range tests { |
| 228 | + testCaseCheck(tc, t) |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +func TestService_Teardown(t *testing.T) { |
| 233 | + tests := []HttpTestCase{ |
| 234 | + { |
| 235 | + Desc: "delete the service", |
| 236 | + Object: ManagerApiExpect(t), |
| 237 | + Method: http.MethodDelete, |
| 238 | + Path: "/apisix/admin/services/s1", |
| 239 | + Headers: map[string]string{"Authorization": token}, |
| 240 | + ExpectStatus: http.StatusOK, |
| 241 | + Sleep: sleepTime, |
| 242 | + }, |
| 243 | + { |
| 244 | + Desc: "delete route", |
| 245 | + Object: ManagerApiExpect(t), |
| 246 | + Method: http.MethodDelete, |
| 247 | + Path: "/apisix/admin/routes/r1", |
| 248 | + Headers: map[string]string{"Authorization": token}, |
| 249 | + ExpectStatus: http.StatusOK, |
| 250 | + }, |
| 251 | + } |
| 252 | + for _, tc := range tests { |
| 253 | + testCaseCheck(tc, t) |
| 254 | + } |
| 255 | +} |
0 commit comments