|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +======================= |
| 4 | + 注意:必须手动运行 |
| 5 | +======================= |
| 6 | +""" |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import time |
| 10 | +import logging |
| 11 | +import pytest |
| 12 | +from qiniu import auth |
| 13 | +from qiniu.services import compute |
| 14 | + |
| 15 | + |
| 16 | +access_key = os.getenv('QINIU_ACCESS_KEY') |
| 17 | +secret_key = os.getenv('QINIU_SECRET_KEY') |
| 18 | +qn_auth = auth.QiniuMacAuth(access_key, secret_key) |
| 19 | +acc_client = compute.app.AccountClient(qn_auth) |
| 20 | +qcos_client = None |
| 21 | +user_name = '' |
| 22 | +app_uri = '' |
| 23 | +app_name = 'appjust4test' |
| 24 | +app_region = 'nq' |
| 25 | + |
| 26 | + |
| 27 | +def setup_module(module): |
| 28 | + acc_client = compute.app.AccountClient(qn_auth) |
| 29 | + user_info = acc_client.get_account_info()[0] |
| 30 | + acc_client.create_app({'name': app_name, 'title': 'whatever', 'region': app_region}) |
| 31 | + |
| 32 | + module.user_name = user_info['name'] |
| 33 | + module.app_uri = '{0}.{1}'.format(module.user_name, app_name) |
| 34 | + module.qcos_client = acc_client.create_qcos_client(module.app_uri) |
| 35 | + |
| 36 | + |
| 37 | +def teardown_module(module): |
| 38 | + module.app_uri |
| 39 | + acc_client.delete_app(module.app_uri) |
| 40 | + |
| 41 | + |
| 42 | +class TestApp: |
| 43 | + """应用测试用例""" |
| 44 | + |
| 45 | + def test_create_and_delete_app(self): |
| 46 | + _name_create = 'appjust4testcreate' |
| 47 | + _uri_create = '' |
| 48 | + _args = {'name': _name_create, 'title': 'whatever', 'region': app_region} |
| 49 | + |
| 50 | + with Call(acc_client, 'create_app', _args) as r: |
| 51 | + assert r[0] is not None |
| 52 | + _uri_create = r[0]['uri'] |
| 53 | + |
| 54 | + with Call(acc_client, 'delete_app', _uri_create) as r: |
| 55 | + assert r[0] == {} |
| 56 | + |
| 57 | + def test_get_app_keys(self): |
| 58 | + with Call(acc_client, 'get_app_keys', app_uri) as r: |
| 59 | + assert len(r[0]) > 0 |
| 60 | + |
| 61 | + def test_get_account_info(self): |
| 62 | + with Call(acc_client, 'get_account_info') as r: |
| 63 | + assert r[0] is not None |
| 64 | + |
| 65 | + |
| 66 | +class TestStack: |
| 67 | + """服务组测试用例""" |
| 68 | + |
| 69 | + _name = 'just4test' |
| 70 | + _name_del = 'just4del' |
| 71 | + _name_create = 'just4create' |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def setup_class(cls): |
| 75 | + qcos_client.create_stack({'name': cls._name}) |
| 76 | + qcos_client.create_stack({'name': cls._name_del}) |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def teardown_class(cls): |
| 80 | + qcos_client.delete_stack(cls._name) |
| 81 | + qcos_client.delete_stack(cls._name_create) |
| 82 | + qcos_client.delete_stack(cls._name_del) |
| 83 | + |
| 84 | + def test_create_stack(self): |
| 85 | + with Call(qcos_client, 'create_stack', {'name': self._name_create}) as r: |
| 86 | + assert r[0] == {} |
| 87 | + |
| 88 | + def test_delete_stack(self): |
| 89 | + with Call(qcos_client, 'delete_stack', self._name_del) as r: |
| 90 | + assert r[0] == {} |
| 91 | + |
| 92 | + def test_list_stacks(self): |
| 93 | + with Call(qcos_client, 'list_stacks') as r: |
| 94 | + assert len(r) > 0 |
| 95 | + assert self._name in [stack['name'] for stack in r[0]] |
| 96 | + |
| 97 | + def test_get_stack(self): |
| 98 | + with Call(qcos_client, 'get_stack', self._name) as r: |
| 99 | + assert r[0]['name'] == self._name |
| 100 | + |
| 101 | + def test_start_stack(self): |
| 102 | + with Call(qcos_client, 'start_stack', self._name) as r: |
| 103 | + assert r[0] == {} |
| 104 | + |
| 105 | + def test_stop_stack(self): |
| 106 | + with Call(qcos_client, 'stop_stack', self._name) as r: |
| 107 | + assert r[0] == {} |
| 108 | + |
| 109 | + |
| 110 | +class TestService: |
| 111 | + """服务测试用例""" |
| 112 | + |
| 113 | + _stack = 'just4test2' |
| 114 | + _name = 'spaceship' |
| 115 | + _name_del = 'spaceship4del' |
| 116 | + _name_create = 'spaceship4create' |
| 117 | + _image = 'library/nginx:stable' |
| 118 | + _unit = '1U1G' |
| 119 | + _spec = {'image': _image, 'unitType': _unit} |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def setup_class(cls): |
| 123 | + qcos_client.delete_stack(cls._stack) |
| 124 | + qcos_client.create_stack({'name': cls._stack}) |
| 125 | + qcos_client.create_service(cls._stack, {'name': cls._name, 'spec': cls._spec}) |
| 126 | + qcos_client.create_service(cls._stack, {'name': cls._name_del, 'spec': cls._spec}) |
| 127 | + |
| 128 | + _debug_info('waiting for services to setup ...') |
| 129 | + time.sleep(10) |
| 130 | + |
| 131 | + @classmethod |
| 132 | + def teardown_class(cls): |
| 133 | + # 删除stack会清理所有相关服务 |
| 134 | + qcos_client.delete_stack(cls._stack) |
| 135 | + |
| 136 | + def test_create_service(self): |
| 137 | + service = {'name': self._name_create, 'spec': self._spec} |
| 138 | + with Call(qcos_client, 'create_service', self._stack, service) as r: |
| 139 | + assert r[0] == {} |
| 140 | + |
| 141 | + def test_delete_service(self): |
| 142 | + with Call(qcos_client, 'delete_service', self._stack, self._name_del) as r: |
| 143 | + assert r[0] == {} |
| 144 | + |
| 145 | + def test_list_services(self): |
| 146 | + with Call(qcos_client, 'list_services', self._stack) as r: |
| 147 | + assert len(r) > 0 |
| 148 | + assert self._name in [service['name'] for service in r[0]] |
| 149 | + |
| 150 | + def test_get_service_inspect(self): |
| 151 | + with Call(qcos_client, 'get_service_inspect', self._stack, self._name) as r: |
| 152 | + assert r[0]['name'] == self._name |
| 153 | + assert r[0]['spec']['unitType'] == self._unit |
| 154 | + |
| 155 | + def test_update_service(self): |
| 156 | + data = {'spec': {'autoRestart': 'ON_FAILURE'}} |
| 157 | + with Call(qcos_client, 'update_service', self._stack, self._name, data) as r: |
| 158 | + assert r[0] == {} |
| 159 | + |
| 160 | + _debug_info('waiting for update services to ready ...') |
| 161 | + time.sleep(10) |
| 162 | + |
| 163 | + def test_scale_service(self): |
| 164 | + data = {'instanceNum': 2} |
| 165 | + with Call(qcos_client, 'scale_service', self._stack, self._name, data) as r: |
| 166 | + assert r[0] == {} |
| 167 | + |
| 168 | + _debug_info('waiting for scale services to ready ...') |
| 169 | + time.sleep(10) |
| 170 | + |
| 171 | + |
| 172 | +class TestContainer: |
| 173 | + """容器测试用例""" |
| 174 | + |
| 175 | + _stack = 'just4test3' |
| 176 | + _service = 'spaceship' |
| 177 | + _spec = {'image': 'library/nginx:stable', 'unitType': '1U1G'} |
| 178 | + # 为了方便测试,容器数量最少为2 |
| 179 | + _instanceNum = 2 |
| 180 | + |
| 181 | + @classmethod |
| 182 | + def setup_class(cls): |
| 183 | + qcos_client.delete_stack(cls._stack) |
| 184 | + qcos_client.create_stack({'name': cls._stack}) |
| 185 | + qcos_client.create_service(cls._stack, {'name': cls._service, 'spec': cls._spec, 'instanceNum': cls._instanceNum}) |
| 186 | + |
| 187 | + _debug_info('waiting for containers to setup ...') |
| 188 | + time.sleep(10) |
| 189 | + |
| 190 | + @classmethod |
| 191 | + def teardown_class(cls): |
| 192 | + qcos_client.delete_stack(cls._stack) |
| 193 | + |
| 194 | + def test_list_containers(self): |
| 195 | + with Call(qcos_client, 'list_containers', self._stack, self._service) as r: |
| 196 | + assert len(r[0]) > 0 |
| 197 | + assert len(r[0]) <= self._instanceNum |
| 198 | + |
| 199 | + def test_get_container_inspect(self): |
| 200 | + ips = qcos_client.list_containers(self._stack, self._service)[0] |
| 201 | + # 查看第1个容器 |
| 202 | + with Call(qcos_client, 'get_container_inspect', ips[0]) as r: |
| 203 | + assert r[0]['ip'] == ips[0] |
| 204 | + |
| 205 | + def test_stop_and_strat_container(self): |
| 206 | + ips = qcos_client.list_containers(self._stack, self._service)[0] |
| 207 | + # 停止第2个容器 |
| 208 | + with Call(qcos_client, 'stop_container', ips[1]) as r: |
| 209 | + assert r[0] == {} |
| 210 | + |
| 211 | + _debug_info('waiting for containers to stop ...') |
| 212 | + time.sleep(3) |
| 213 | + |
| 214 | + # 启动第2个容器 |
| 215 | + with Call(qcos_client, 'start_container', ips[1]) as r: |
| 216 | + assert r[0] == {} |
| 217 | + |
| 218 | + def test_restart_container(self): |
| 219 | + ips = qcos_client.list_containers(self._stack, self._service)[0] |
| 220 | + # 重启第1个容器 |
| 221 | + with Call(qcos_client, 'restart_container', ips[0]) as r: |
| 222 | + assert r[0] == {} |
| 223 | + |
| 224 | + |
| 225 | +class TestAp: |
| 226 | + """接入点测试用例""" |
| 227 | + |
| 228 | + _stack = 'just4test4' |
| 229 | + _service = 'spaceship' |
| 230 | + _spec = {'image': 'library/nginx:stable', 'unitType': '1U1G'} |
| 231 | + # 为了方便测试,容器数量最少为2 |
| 232 | + _instanceNum = 2 |
| 233 | + _apid_domain = {} |
| 234 | + _apid_ip = {} |
| 235 | + _apid_ip_port = 8080 |
| 236 | + _user_domain = 'just4test001.example.com' |
| 237 | + |
| 238 | + @classmethod |
| 239 | + def setup_class(cls): |
| 240 | + qcos_client.delete_stack(cls._stack) |
| 241 | + qcos_client.create_stack({'name': cls._stack}) |
| 242 | + qcos_client.create_service(cls._stack, {'name': cls._service, 'spec': cls._spec, 'instanceNum': cls._instanceNum}) |
| 243 | + cls._ap_domain = qcos_client.create_ap({'type': 'DOMAIN', 'provider': 'Telecom', 'unitType': 'BW_10M', 'title': 'public1'})[0] |
| 244 | + cls._ap_ip = qcos_client.create_ap({'type': 'PUBLIC_IP', 'provider': 'Telecom', 'unitType': 'BW_10M', 'title': 'public2'})[0] |
| 245 | + qcos_client.set_ap_port(cls._ap_ip['apid'], cls._apid_ip_port, {'proto': 'http'}) |
| 246 | + |
| 247 | + @classmethod |
| 248 | + def teardown_class(cls): |
| 249 | + qcos_client.delete_stack(cls._stack) |
| 250 | + qcos_client.delete_ap(cls._ap_domain['apid']) |
| 251 | + qcos_client.delete_ap(cls._ap_ip['apid']) |
| 252 | + |
| 253 | + def test_list_aps(self): |
| 254 | + with Call(qcos_client, 'list_aps') as r: |
| 255 | + assert len(r[0]) > 0 |
| 256 | + assert self._ap_domain['apid'] in [ap['apid'] for ap in r[0]] |
| 257 | + assert self._ap_domain['apid'] in [ap['apid'] for ap in r[0]] |
| 258 | + |
| 259 | + def test_create_and_delete_ap(self): |
| 260 | + apid = 0 |
| 261 | + ap = {'type': 'DOMAIN', 'provider': 'Telecom', 'unitType': 'BW_10M', 'title': 'public1'} |
| 262 | + |
| 263 | + with Call(qcos_client, 'create_ap', ap) as r: |
| 264 | + assert r[0] is not None and r[0]['apid'] > 0 |
| 265 | + apid = r[0]['apid'] |
| 266 | + |
| 267 | + with Call(qcos_client, 'delete_ap', apid) as r: |
| 268 | + assert r[0] == {} |
| 269 | + |
| 270 | + def test_search_ap(self): |
| 271 | + with Call(qcos_client, 'search_ap', 'ip', self._ap_ip['ip']) as r: |
| 272 | + assert str(r[0]['apid']) == self._ap_ip['apid'] |
| 273 | + |
| 274 | + def test_get_ap(self): |
| 275 | + with Call(qcos_client, 'get_ap', self._ap_ip['apid']) as r: |
| 276 | + assert str(r[0]['apid']) == self._ap_ip['apid'] |
| 277 | + |
| 278 | + def test_update_ap(self): |
| 279 | + with Call(qcos_client, 'update_ap', self._ap_ip['apid'], {}) as r: |
| 280 | + assert r[0] == {} |
| 281 | + |
| 282 | + def test_set_ap_port(self): |
| 283 | + with Call(qcos_client, 'set_ap_port', self._ap_ip['apid'], 80, {'proto': 'http'}) as r: |
| 284 | + assert r[0] == {} |
| 285 | + |
| 286 | + def test_publish_ap(self): |
| 287 | + domain = {'userDomain': self._user_domain} |
| 288 | + with Call(qcos_client, 'publish_ap', self._ap_domain['apid'], domain) as r: |
| 289 | + assert r[0] == {} |
| 290 | + |
| 291 | + def test_unpublish_ap(self): |
| 292 | + domain = {'userDomain': self._user_domain} |
| 293 | + with Call(qcos_client, 'unpublish_ap', self._ap_domain['apid'], domain) as r: |
| 294 | + assert r[0] == {} |
| 295 | + |
| 296 | + def test_get_ap_port_healthcheck(self): |
| 297 | + with Call(qcos_client, 'get_ap_port_healthcheck', self._ap_ip['apid'], self._apid_ip_port) as r: |
| 298 | + assert r[0] is not None |
| 299 | + |
| 300 | + def test_disable_ap_port(self): |
| 301 | + with Call(qcos_client, 'disable_ap_port', self._ap_ip['apid'], self._apid_ip_port) as r: |
| 302 | + assert r[0] == {} |
| 303 | + |
| 304 | + def test_enable_ap_port(self): |
| 305 | + with Call(qcos_client, 'enable_ap_port', self._ap_ip['apid'], self._apid_ip_port) as r: |
| 306 | + assert r[0] == {} |
| 307 | + |
| 308 | + def test_get_ap_providers(self): |
| 309 | + with Call(qcos_client, 'get_ap_providers') as r: |
| 310 | + assert len(r[0]) > 0 |
| 311 | + |
| 312 | + |
| 313 | +class Call(object): |
| 314 | + def __init__(self, obj, method, *args): |
| 315 | + self.context = (obj, method, args) |
| 316 | + self.result = None |
| 317 | + |
| 318 | + def __enter__(self): |
| 319 | + self.result = getattr(self.context[0], self.context[1])(*self.context[2]) |
| 320 | + assert self.result is not None |
| 321 | + return self.result |
| 322 | + |
| 323 | + def __exit__(self, type, value, traceback): |
| 324 | + _debug_info('\033[94m%s.%s\x1b[0m: %s', self.context[0].__class__, self.context[1], self.result) |
| 325 | + |
| 326 | + |
| 327 | +def _debug_info(*args): |
| 328 | + logger = logging.getLogger(__name__) |
| 329 | + logger.debug(*args) |
| 330 | + |
| 331 | + |
| 332 | +if __name__ == '__main__': |
| 333 | + logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 334 | + pytest.main() |
0 commit comments