def setUp(self): """Update postgres timezone and create a deployment.""" run_postgresql_command( self.env.container_id, "ALTER DATABASE cloudify_db SET TIME ZONE '{}'".format( self.TIMEZONE)) # restart all users of the db so that they get a new session which # uses the just-set timezone service_command = \ self.get_service_management_command() docker.execute( self.env.container_id, "{0} restart cloudify-amqp-postgres " "cloudify-restservice".format(service_command)) docker.execute( self.env.container_id, "{0} restart cloudify-amqp-postgres " "cloudify-restservice".format(service_command)) # Make sure that database timezone is correctly set query_result = run_postgresql_command(self.env.container_id, 'SHOW TIME ZONE') self.assertEqual(query_result.split('\n')[2].strip(), self.TIMEZONE) time.sleep(1) # give time for services to restart self.start_timestamp = datetime.utcnow().isoformat() super(EventsAlternativeTimezoneTest, self).setUp() # log storing is async, add a few seconds to allow for that self.stop_timestamp = \ (datetime.utcnow() + timedelta(seconds=3)).isoformat()
def set_ldap(config_data): logger.info('Setting LDAP configuration') _prepare_set_ldap_script() execute("{manager_python} {script_path} --config '{cfg_data}'" .format(manager_python=MANAGER_PYTHON, script_path='/tmp/set_ldap.py', cfg_data=json.dumps(config_data)))
def restart_service(self, service_name): """restart service by name in the manager container""" service_command = self.get_service_management_command() docker.execute(self.env.container_id, '{0} stop {1}'.format(service_command, service_name)) docker.execute(self.env.container_id, '{0} start {1}'.format(service_command, service_name))
def prepare_reset_storage_script(container_id): reset_script = get_resource('scripts/reset_storage.py') prepare = get_resource('scripts/prepare_reset_storage.py') copy_file_to_manager(container_id, reset_script, SCRIPT_PATH) copy_file_to_manager(container_id, prepare, PREPARE_SCRIPT_PATH) execute(container_id, [MANAGER_PYTHON, PREPARE_SCRIPT_PATH, '--config', CONFIG_PATH])
def run_collector_scripts_and_assert(self, messages): for script in COLLECTOR_SCRIPTS: docker.execute(self.env.container_id, '{0} {1}.py'.format( MANAGER_PYTHON, join(SCRIPTS_DESTINATION_PATH, script)) ) assert_messages_in_log(self.env.container_id, self.workdir, messages, join(LOG_PATH, LOG_FILE))
def copy_file_to_manager(self, source, target, owner=None): """Copy a file to the cloudify manager filesystem""" ret_val = docker.copy_file_to_manager(self.env.container_id, source=source, target=target) if owner: docker.execute(self.env.container_id, ['chown', owner, target]) do_retries_boolean(docker.file_exists, container_id=self.env.container_id, file_path=target) return ret_val
def setUpClass(cls): """Configure database timezone.""" super(EventsAlternativeTimezoneTest, cls).setUpClass() # Container is launched once per unittest.TestCase class. # Timezone configuration just needs to updated at the class level. # Between tests cases tables are re-created, # but timezone configuration is preserved. postgres_conf = get_postgres_conf() run_query("ALTER USER {} SET TIME ZONE '{}'".format( postgres_conf.username, cls.TIMEZONE)) # restart all users of the db so that they get a new session which # uses the just-set timezone docker.execute( cls.env.container_id, "systemctl restart cloudify-amqp-postgres cloudify-restservice")
def clear_directory(self, dir_path): """ Remove all contents from a directory """ # Add a wildcard to the end, to remove everything *inside* the folder command = 'rm -rf {0}/*'.format(dir_path) # Need to invoke a shell directly, because `docker exec` ignores # wildcards by default return docker.execute(self.env.container_id, ['sh', '-c', command])
def _select(self, query): """Run a SELECT query on the manager. Note that this will not parse the results, so the calling functions must deal with strings as returned by psql (eg. false is the string 'f', NULL is the empty string, etc.) """ out = docker.execute(self.env.container_id, [ 'sudo', '-upostgres', 'psql', '-t', '-F,', 'cloudify_db', '-c', '\\copy ({0}) to stdout with csv'.format(query) ]) return [line.split(',') for line in out.split('\n') if line.strip()]
def prepare_manager_storage(request, manager_container): """Make sure that for each test, the manager storage is the same. This involves uploading the license before the tests, and cleaning the db & storage directories between tests. """ container_id = manager_container.container_id dirs_to_clean = [ '/opt/mgmtworker/work/deployments', '/opt/manager/resources/blueprints', '/opt/manager/resources/uploaded-blueprints' ] docker.upload_mock_license(container_id) try: yield finally: request.session.testsfinished = \ getattr(request.session, 'testsfinished', 0) + 1 if request.session.testsfinished != request.session.testscollected: reset_storage(container_id) for directory in dirs_to_clean: docker.execute( container_id, ['sh', '-c', 'rm -rf {0}/*'.format(directory)])
def verify_deployment_env_created(container_id, deployment_id, client=None): # A workaround for waiting for the deployment environment creation to # complete client = client or create_rest_client(docker.get_manager_ip(container_id)) execs = client.executions.list(deployment_id=deployment_id) if not execs \ or execs[0].status != Execution.TERMINATED \ or execs[0].workflow_id != 'create_deployment_environment': log_path = '/var/log/cloudify/mgmtworker/mgmtworker.log' logs = docker.execute(container_id, ['tail', '-n', '100', log_path]) raise RuntimeError( "Expected a single execution for workflow " "'create_deployment_environment' with status 'terminated'; " "Found these executions instead: {0}.\nLast 100 lines for " "management worker log:\n{1}" .format(json.dumps(execs.items, indent=2), logs))
def create_api_token(): """ Create a new valid API token """ command = 'sudo {0}'.format(ADMIN_TOKEN_SCRIPT) docker.execute(command)
def run_postgresql_command(cmd): return docker.execute('sudo -u postgres psql cloudify_db ' '-c "{0}"'.format(cmd))
def execute_on_manager(self, command): """ Execute a shell command on the cloudify manager container. """ return docker.execute(self.env.container_id, command)
def delete_manager_file(self, file_path, quiet=True): """Remove file from a cloudify manager""" return docker.execute(self.env.container_id, 'rm -rf {0}'.format(file_path))
def reset_storage(container_id): logger.info('Resetting PostgreSQL DB') # reset the storage by calling a script on the manager, to access # localhost-only APIs (rabbitmq management api) execute(container_id, [MANAGER_PYTHON, SCRIPT_PATH, '--config', CONFIG_PATH])
def run_postgresql_command(container_id, cmd): return docker.execute( container_id, 'sudo -u postgres psql cloudify_db -c "{0}"'.format(cmd))
def restart_service(self, service_name): """restart service by name in the manager container""" docker.execute( self.env.container_id, 'systemctl stop {0}'.format(service_name)) docker.execute( self.env.container_id, 'systemctl start {0}'.format(service_name))