def onboard_unit(context, tenant): (code, result, error) = execute(["systemctl", 'enable', 'vault-unit@{}'.format(tenant)]) assert code == 0, str(result) + ' ' + str(error) (code, result, error) = execute(["systemctl", 'start', 'vault-unit@{}'.format(tenant)]) assert code == 0, str(result) + ' ' + str(error) unit_running(context, 'vault-unit@{}'.format(tenant))
def step_impl(context, package, operation): if operation == 'installed': (code, result, error) = execute(["apt-get", "install", "-f", "-qq", "-o=Dpkg::Use-Pty=0", "-o=Dpkg::Options::=--force-confold", context.unit.binary]) assert code == 0, "unable to install with code {} and {} {}".format(code, result, error) assert os.path.isfile('/etc/vault/conf.d/init.conf') is True elif operation == 'uninstalled': (code, result, error) = execute(["apt-get", "-y", "remove", package]) assert code == 0, "unable to uninstall with code {} and {} {}".format(code, result, error) assert os.path.isfile('/etc/vault/conf.d/init.conf') is False else: assert False
def collect_logs(self): (code, result, error) = execute(['journalctl', '-o', 'cat', '--no-pager']) if code == 0: with open('reports/blackbox-tests/logs/journal.log', 'w') as fd: fd.write(result) for unit in set(self.__get_systemd_units() + self.units): (code, result, error) = execute( ['journalctl', '-o', 'cat', '-u', unit, '--no-pager']) if code != 0 or not result: continue with open('reports/blackbox-tests/logs/{}.log'.format(unit), 'w') as fd: fd.write(result)
def collect_logs(self): cwd = os.path.realpath('{}/../..'.format(os.path.dirname(__file__))) os.makedirs('{}/reports/blackbox-tests/logs'.format(cwd), exist_ok=True) (code, result, error) = execute(['journalctl', '-o', 'cat', '--no-pager']) if code == 0: with open('{}/reports/blackbox-tests/logs/journal.log'.format(cwd), 'w') as fd: fd.write(result) for unit in set(self.__get_systemd_units() + self.units): (code, result, error) = execute(['journalctl', '-o', 'cat', '-u', unit, '--no-pager']) if code != 0 or not result: continue with open('{}/reports/blackbox-tests/logs/{}.log'.format(cwd, unit), 'w') as fd: fd.write(result)
def install(self, file): cwd = os.path.realpath('{}/../..'.format(os.path.dirname(__file__))) (code, result, error) = execute(['dpkg', '-c', file]) if code != 0: raise RuntimeError('code: {}, stdout: [{}], stderr: [{}]'.format( code, result, error)) else: os.makedirs('{}/reports/blackbox-tests/meta'.format(cwd), exist_ok=True) with open( '{}/reports/blackbox-tests/meta/debian.vault.txt'.format( cwd), 'w') as fd: fd.write(result) result = [item for item in result.split(os.linesep)] result = [ item.rsplit('/', 1)[-1].strip() for item in result if "/lib/systemd/system/vault" in item ] result = [ item for item in result if not item.endswith('unit.slice') ] self.units = result
def impl(): (code, result, error) = execute( ['journalctl', '-o', 'cat', '-u', unit, '--no-pager']) result = ansi_escape.sub('', result) assert code == 0, str(result) + ' ' + str(error) actual_lines_merged = [ item.strip() for item in result.split('\n') if len(item.strip()) ] actual_lines = [] idx = len(actual_lines_merged) - 1 while True: if idx < 0 or (">>> Start <<<" in actual_lines_merged[idx]): break actual_lines.append(actual_lines_merged[idx]) idx -= 1 actual_lines = reversed(actual_lines) for expected in expected_lines: found = False for actual in actual_lines: if expected in actual: found = True break assert found == True, 'message "{}" was not found in logs'.format( context.text.strip())
def __get_systemd_units(self): (code, result, error) = execute(['systemctl', 'list-units', '--no-legend']) result = [ item.split(' ')[0].strip() for item in result.split(os.linesep) ] result = [ item for item in result if "ledger" in item and not item.endswith('unit.slice') ] return result
def step_impl(context): (code, result, error) = execute(["systemctl", "list-units", "--no-legend"]) assert code == 0 items = [] for row in context.table: items.append(row['name'] + '.' + row['type']) result = [item.split(' ')[0].strip() for item in result.split(os.linesep)] result = [item for item in result if item in items] assert len(result) == 0, 'units found\n{}'.format(result)
def offboard_unit(context, tenant): (code, result, error) = execute([ 'journalctl', '-o', 'cat', '-u', 'ledger-unit@{}.service'.format(tenant), '--no-pager' ]) if code == 0 and result: with open( 'reports/blackbox-tests/logs/ledger-unit.{}.log'.format( tenant), 'w') as f: f.write(result) execute(['systemctl', 'stop', 'ledger-unit@{}.service'.format(tenant)]) (code, result, error) = execute([ 'journalctl', '-o', 'cat', '-u', 'ledger-unit@{}.service'.format(tenant), '--no-pager' ]) if code == 0 and result: with open( 'reports/blackbox-tests/logs/ledger-unit.{}.log'.format( tenant), 'w') as fd: fd.write(result) execute(['systemctl', 'disable', 'ledger-unit@{}.service'.format(tenant)]) unit_not_running(context, 'ledger-unit@{}'.format(tenant))
def offboard_unit(context, tenant): logfile = os.path.realpath( '{}/../../reports/blackbox-tests/logs/vault-unit.{}.log'.format( os.path.dirname(__file__), tenant)) (code, result, error) = execute([ 'journalctl', '-o', 'cat', '-u', 'vault-unit@{}.service'.format(tenant), '--no-pager' ]) if code == 0 and result: with open(logfile, 'w') as f: f.write(result) execute(['systemctl', 'stop', 'vault-unit@{}.service'.format(tenant)]) (code, result, error) = execute([ 'journalctl', '-o', 'cat', '-u', 'vault-unit@{}.service'.format(tenant), '--no-pager' ]) if code == 0 and result: with open(logfile, 'w') as fd: fd.write(result) execute(['systemctl', 'disable', 'vault-unit@{}.service'.format(tenant)]) unit_not_running(context, 'vault-unit@{}'.format(tenant))
def unit_is_configured(context, unit): params = dict() for row in context.table: params[row['property']] = row['value'] context.unit.configure(params) (code, result, error) = execute([ 'systemctl', 'list-units', '--no-legend' ]) result = [item.split(' ')[0].strip() for item in result.split(os.linesep)] result = [item for item in result if ("{}-".format(unit) in item and ".service" in item)] for unit in result: operation_unit(context, 'restart', unit)
def teardown(self): self.collect_logs() for unit in self.__get_systemd_units(): execute(['systemctl', 'stop', unit]) self.collect_logs()
def operation_unit(context, operation, unit): (code, result, error) = execute(["systemctl", operation, unit]) assert code == 0, str(result) + ' ' + str(error) if operation == 'restart': unit_running(context, unit)
def unit_not_running(context, unit): (code, result, error) = execute(["systemctl", "show", "-p", "SubState", unit]) assert code == 0, str(result) + ' ' + str(error) assert 'SubState=dead' in result, str(result) + ' ' + str(error)
args.append('@{}/order.txt'.format(cwd)) for path in [ 'reports/blackbox-tests/metrics', 'reports/blackbox-tests/logs', 'reports/blackbox-tests/meta', 'reports/blackbox-tests/data', 'reports/blackbox-tests/behave', 'reports/blackbox-tests/cucumber', 'reports/blackbox-tests/junit' ]: os.system('mkdir -p {}'.format(path)) os.system('rm -rf {}/*'.format(path)) from behave import __main__ as behave_executable exit_code = behave_executable.main(args=' '.join(args)) with open('reports/blackbox-tests/behave/results.json', 'r') as fd_behave: cucumber_data = None with open('reports/blackbox-tests/cucumber/results.json', 'w') as fd_cucumber: behave_data = json.loads(fd_behave.read()) cucumber_data = json.dumps(behave2cucumber.convert(behave_data)) fd_cucumber.write(cucumber_data) execute([ 'json_to_junit', 'reports/blackbox-tests/cucumber/results.json', 'reports/blackbox-tests/junit/results.xml' ]) sys.exit(exit_code)
def download(self): failure = None os.makedirs('/tmp/packages', exist_ok=True) self.image_version = os.environ.get('IMAGE_VERSION', '') self.debian_version = os.environ.get('UNIT_VERSION', '') if self.debian_version.startswith('v'): self.debian_version = self.debian_version[1:] assert self.image_version, 'IMAGE_VERSION not provided' assert self.debian_version, 'UNIT_VERSION not provided' image = 'openbank/ledger:{}'.format(self.image_version) package = '/opt/artifacts/ledger_{}_{}.deb'.format( self.debian_version, self.arch) target = '/tmp/packages/ledger.deb' temp = tempfile.NamedTemporaryFile(delete=True) try: with open(temp.name, 'w') as fd: fd.write( str(os.linesep).join([ 'FROM alpine', 'COPY --from={} {} {}'.format(image, package, target) ])) image, stream = self.docker.images.build( fileobj=temp, rm=True, pull=False, tag='bbtest_artifacts-scratch') for chunk in stream: if not 'stream' in chunk: continue for line in chunk['stream'].splitlines(): l = line.strip(os.linesep) if not len(l): continue print(l) scratch = self.docker.containers.run('bbtest_artifacts-scratch', ['/bin/true'], detach=True) tar_name = tempfile.NamedTemporaryFile(delete=True) with open(tar_name.name, 'wb') as fd: bits, stat = scratch.get_archive(target) for chunk in bits: fd.write(chunk) archive = tarfile.TarFile(tar_name.name) archive.extract(os.path.basename(target), os.path.dirname(target)) (code, result, error) = execute(['dpkg', '-c', target]) if code != 0: raise RuntimeError( 'code: {}, stdout: [{}], stderr: [{}]'.format( code, result, error)) else: with open('reports/blackbox-tests/meta/debian.ledger.txt', 'w') as fd: fd.write(result) result = [item for item in result.split(os.linesep)] result = [ item.rsplit('/', 1)[-1].strip() for item in result if "/lib/systemd/system/ledger" in item ] result = [ item for item in result if not item.endswith('unit.slice') ] self.units = result scratch.remove() except Exception as ex: failure = ex finally: temp.close() try: self.docker.images.remove('bbtest_artifacts-scratch', force=True) except: pass if failure: raise failure
'/data', '{}/../reports/blackbox-tests/logs'.format(cwd), '{}/../reports/blackbox-tests/meta'.format(cwd), '{}/../reports/blackbox-tests/behave'.format(cwd), '{}/../reports/blackbox-tests/cucumber'.format(cwd), '{}/../reports/blackbox-tests/junit'.format(cwd) ]: os.system('mkdir -p {}'.format(path)) os.system('rm -rf {}/*'.format(path)) from behave import __main__ as behave_executable exit_code = behave_executable.main(args=' '.join(args)) with open('{}/../reports/blackbox-tests/behave/results.json'.format(cwd), 'r') as fd_behave: cucumber_data = None with open( '{}/../reports/blackbox-tests/cucumber/results.json'.format( cwd), 'w') as fd_cucumber: behave_data = json.loads(fd_behave.read()) cucumber_data = json.dumps(behave2cucumber.convert(behave_data)) fd_cucumber.write(cucumber_data) execute([ 'json_to_junit', '{}/../reports/blackbox-tests/cucumber/results.json'.format(cwd), '{}/../reports/blackbox-tests/junit/results.xml'.format(cwd) ]) sys.exit(exit_code)
def wait_for_unit_state_change(): (code, result, error) = execute(["systemctl", "show", "-p", "SubState", unit]) assert code == 0, code assert 'SubState=running' in result, result