def test_pkg_upgrade_agent_token(distro_agent, distro_server): """Check if Stork agent and server can be upgraded from latest release to localy built packages.""" server = containers.StorkServerContainer(alias=distro_server) agent = containers.StorkAgentContainer(alias=distro_agent) # install the latest version of stork from cloudsmith server.setup_bg('cloudsmith') while server.mgmt_ip is None: time.sleep(0.1) agent.setup_bg('cloudsmith', server.mgmt_ip) server.setup_wait() agent.setup_wait() # install local packages banner('UPGRADING STORK') server.prepare_stork_server() agent.prepare_stork_agent() # install kea on the agent machine agent.install_kea() # login r = server.api_post('/sessions', json=dict(useremail='admin', userpassword='******'), expected_status=200) # TODO: POST should return 201 assert r.json()['login'] == 'admin' # get machine that automatically registered in the server and authorize it m = _get_machines_and_authorize_them(server)[0] assert m['address'] == agent.mgmt_ip # retrieve state of machines m = _get_machine_state(server, m['id']) assert m['apps'] is not None assert len(m['apps']) == 1 assert m['apps'][0]['version'] == KEA_LATEST.split('-')[0]
def test_pkg_upgrade_server_token(distro_agent, distro_server): """Check if Stork agent and server can be upgraded from latest release to localy built packages.""" server = containers.StorkServerContainer(alias=distro_server) agent = containers.StorkAgentContainer(alias=distro_agent) # install the latest version of stork from cloudsmith server.setup_bg('cloudsmith') while server.mgmt_ip is None: time.sleep(0.1) agent.setup_bg('cloudsmith', server.mgmt_ip) server.setup_wait() agent.setup_wait() # login r = server.api_post('/sessions', json=dict(useremail='admin', userpassword='******'), expected_status=200) # TODO: POST should return 201 assert r.json()['login'] == 'admin' # install local packages banner('UPGRADING STORK SERVER') server.prepare_stork_server() # get server token from server for i in range(100): try: r = server.api_get('/machines-server-token') break except: if i == 99: raise time.sleep(1) data = r.json() server_token = data['token'] # install kea on the agent machine agent.install_kea() # install local packages using server token based way banner('UPGRADING STORK AGENT') server_url = 'http://%s:8080' % server.mgmt_ip agent.run('curl -o stork-install-agent.sh %s/stork-install-agent.sh' % server_url) agent.run('chmod a+x stork-install-agent.sh') env = { 'STORK_AGENT_ADDRESS': '%s:8080' % agent.mgmt_ip, 'STORK_AGENT_SERVER_URL': server_url, 'STORK_AGENT_SERVER_TOKEN': server_token } agent.run('./stork-install-agent.sh', env=env) # check current machines, there should be one authorized machines = _get_machines(server, agent, authorized=True, expected_items=1) # retrieve state of machines m = _get_machine_state(server, machines[0]['id']) assert m['apps'] is not None assert len(m['apps']) == 1 assert m['apps'][0]['version'] == KEA_LATEST.split('-')[0]
def _prepare_containers(containers_to_create: Sequence[Tuple[str, str]]) \ -> Sequence[Tuple[Literal['agent', 'server'], Any]]: """Build and run all necessary containers. Accepts sequence of tuples with container type (agent or server) and container operating system name. Return created, started containers.""" import containers # change test case arguments from container system names # to actual started container instances server_containers: List[containers.StorkServerContainer] = [] agent_containers: List[containers.StorkAgentContainer] = [] # Must have a specific order # Items are tuples with container type and container object all_containers: List[Tuple[Literal['agent', 'server'], containers.Container]] = [] for name, val in containers_to_create: if name.startswith('agent'): a = containers.StorkAgentContainer(alias=val) agent_containers.append(a) all_containers.append(('agent', a)) elif name.startswith('server'): s = containers.StorkServerContainer(alias=val) server_containers.append(s) all_containers.append(('server', s)) assert len(server_containers) <= 1 # start all agent containers in background so they can run in parallel and be ready quickly if server_containers: server_containers[0].setup_bg() while server_containers[0].mgmt_ip is None: time.sleep(0.1) for c in agent_containers: c.setup_bg(None, server_containers[0].mgmt_ip) # wait for all containers for _, c in all_containers: c.setup_wait() print('CONTAINER %s READY @ %s' % (c.name, c.mgmt_ip)) time.sleep(3) return all_containers
def test_pkg_upgrade(distro_agent, distro_server): """Check if Stork agent and server can be upgraded from latest release to localy built packages.""" server = containers.StorkServerContainer(alias=distro_server) agent = containers.StorkAgentContainer(alias=distro_agent) # install the latest version of stork from cloudsmith server.setup_bg('cloudsmith') agent.setup_bg('cloudsmith') server.setup_wait() agent.setup_wait() # install local packages banner('UPGRADING STORK') agent.prepare_stork_agent() server.prepare_stork_server() # install kea on the agent machine agent.install_kea() # login r = server.api_post('/sessions', json=dict(useremail='admin', userpassword='******'), expected_status=200) # TODO: POST should return 201 assert r.json()['login'] == 'admin' # add machine and check if it can be retrieved machine = dict(address=agent.mgmt_ip, agentPort=8080, agentCSR='TODO') r = server.api_post('/machines', json=machine, expected_status=200) # TODO: POST should return 201 data = r.json() m_id = data['id'] for i in range(100): r = server.api_get('/machines/%d/state' % m_id) data = r.json() if data['apps'] and len(data['apps'][0]['details']['daemons']) > 1: break time.sleep(2) assert data['apps'] is not None assert len(data['apps']) == 1 assert data['apps'][0]['version'] == KEA_LATEST.split('-')[0]
def pytest_pyfunc_call(pyfuncitem): """The purpose of this hook is: 1. replacing test case system name arguments with container instances 2. run the test case 3. after testing collect logs from these containers and store them in `test-results` folder """ import containers # change test case arguments from container system names # to actual started container instances server_containers = [] agent_containers = [] for name, val in pyfuncitem.funcargs.items(): if name.startswith('agent'): a = containers.StorkAgentContainer(alias=val) agent_containers.append(a) pyfuncitem.funcargs[name] = a elif name.startswith('server'): s = containers.StorkServerContainer(alias=val) server_containers.append(s) pyfuncitem.funcargs[name] = s assert len(server_containers) <= 1 all_containers = server_containers + agent_containers # start all agent containers in background so they can run in parallel and be ready quickly if server_containers: server_containers[0].setup_bg() while server_containers[0].mgmt_ip is None: time.sleep(0.1) for c in agent_containers: c.setup_bg(None, server_containers[0].mgmt_ip) # wait for all containers for c in all_containers: c.setup_wait() print('CONTAINER %s READY @ %s' % (c.name, c.mgmt_ip)) time.sleep(3) # DO RUN TEST CASE outcome = yield print('TEST %s FINISHED: %s, COLLECTING LOGS' % (pyfuncitem.name, outcome.get_result())) # prepare test directory for logs, etc tests_dir = Path('test-results') tests_dir.mkdir(exist_ok=True) test_name = pyfuncitem.name test_name = test_name.replace('[', '__') test_name = test_name.replace('/', '_') test_name = test_name.replace(']', '') test_dir = tests_dir / test_name if test_dir.exists(): shutil.rmtree(test_dir) test_dir.mkdir() # download stork server and agent logs to test dir for idx, s in enumerate(server_containers): _, out, _ = s.run('journalctl -u isc-stork-server') fname = test_dir / ('stork-server-%d.log' % idx) with open(fname, 'w') as f: f.write(out) s.stop() for idx, a in enumerate(agent_containers): _, out, _ = a.run('journalctl -u isc-stork-agent') fname = test_dir / ('stork-agent-%d.log' % idx) with open(fname, 'w') as f: f.write(out) a.stop()