def _upload_and_restore_snapshot( self, snapshot_path, tenant_name=DEFAULT_TENANT_NAME, snapshot_id=None, desired_execution_status=Execution.TERMINATED, error_execution_status=Execution.FAILED, ignore_plugin_failure=False): """Upload the snapshot and launch the restore workflow """ snapshot_id = snapshot_id or self.SNAPSHOT_ID rest_client = utils.create_rest_client(tenant=tenant_name) self._upload_and_validate_snapshot(snapshot_path, snapshot_id, rest_client) self.logger.debug('Restoring snapshot...') execution = rest_client.snapshots.restore( snapshot_id, ignore_plugin_failure=ignore_plugin_failure) execution = self._wait_for_restore_execution_to_end( execution, rest_client) if execution.status == error_execution_status: self.logger.error('Execution error: {0}'.format(execution.error)) self.assertEqual(desired_execution_status, execution.status) # Wait for the restart of cloudify-restservice to end. # If the rest-security.conf file was changed the service is being # restarted. As you can see in snapshot_restore.py when calling the # function _add_restart_command. time.sleep(5)
def _upload_and_restore_snapshot( self, snapshot_path, tenant_name=DEFAULT_TENANT_NAME, snapshot_id=None, desired_execution_status=Execution.TERMINATED, error_execution_status=Execution.FAILED, ignore_plugin_failure=False): """Upload the snapshot and launch the restore workflow """ snapshot_id = snapshot_id or self.SNAPSHOT_ID rest_client = utils.create_rest_client(tenant=tenant_name) self._upload_and_validate_snapshot(snapshot_path, snapshot_id, rest_client) self.logger.debug('Restoring snapshot...') execution = rest_client.snapshots.restore( snapshot_id, ignore_plugin_failure=ignore_plugin_failure) rest_client.maintenance_mode.activate() self.wait_for_snapshot_restore_to_end(execution.id, client=rest_client) rest_client.maintenance_mode.deactivate() execution = self._wait_for_restore_execution_to_end( execution, rest_client, timeout_seconds=240) if execution.status == error_execution_status: self.logger.error('Execution error: {0}'.format(execution.error)) self.assertEqual(desired_execution_status, execution.status)
def _login_client(self, **kwargs): self.logger.info('performing login to test client with {0}'.format( str(kwargs))) try: self.test_client = utils.create_rest_client(**kwargs) yield finally: self.test_client = None
def _login_client(self, **kwargs): self.logger.info('Logging in to client with {0}'.format(str(kwargs))) client = self.client try: self.client = utils.create_rest_client(**kwargs) yield finally: self.client = client
def test_secret_export_not_authorized(self): self.client.secrets.create('key', 'value') self.client.users.create('user', 'password', 'default') self.client.tenants.add_user('user', 'default_tenant', 'viewer') viewer_client = utils.create_rest_client(username='******', password='******', tenant='default_tenant') self.assertRaisesRegexp(ForbiddenError, '403: User `user` is not permitted to perform' ' the action secret_export in the tenant ' '`default_tenant`', viewer_client.secrets.export)
def test_v_4_snapshot_restore_validation(self): snapshot = self._get_snapshot('snap_4.0.0.zip') self.client.snapshots.upload(snapshot, self.SNAPSHOT_ID) username = '******' password = '******' self.client.users.create(username, password, role='sys_admin') self.client.tenants.add_user(username, DEFAULT_TENANT_NAME) admin_client = utils.create_rest_client(username=username, password=password) self._try_restore_snapshot( snapshot_id=self.SNAPSHOT_ID, error_msg='Only the bootstrap admin is allowed ' 'to perform this action', client=admin_client)
def _upload_and_restore_snapshot(self, snapshot_path, tenant_name=DEFAULT_TENANT_NAME, snapshot_id=None): """Upload the snapshot and launch the restore workflow """ snapshot_id = snapshot_id or self.SNAPSHOT_ID rest_client = utils.create_rest_client(tenant=tenant_name) self._upload_and_validate_snapshot(snapshot_path, snapshot_id, rest_client) self.logger.debug('Restoring snapshot...') execution = rest_client.snapshots.restore(snapshot_id) execution = self._wait_for_execution_to_end(execution, rest_client) if execution.status == Execution.FAILED: self.logger.error('Execution error: {0}'.format(execution.error)) self.assertEqual(Execution.TERMINATED, execution.status)
def test_v_4_snapshot_restore_validation(self): snapshot = self._get_snapshot('snap_4.0.0.zip') self.client.snapshots.upload(snapshot, self.SNAPSHOT_ID) username = '******' password = '******' self.client.users.create(username, password, role='sys_admin') self.client.tenants.add_user(username, DEFAULT_TENANT_NAME, DEFAULT_TENANT_ROLE) admin_client = utils.create_rest_client(username=username, password=password) self._try_restore_snapshot( snapshot_id=self.SNAPSHOT_ID, error_msg='Only the bootstrap admin is allowed ' 'to perform this action', client=admin_client )
def test_schedule_execution_snapshot_running_multi_tenant(self): """ - default_tenant: system execution (snapshot) is running - tenant_0: scheduled execution Scheduled execution 'wakes up' while snapshot is running in a different tenant, we expect scheduled execution to become 'queued', and start only when the snapshot terminates. """ # The token in the container is invalid, create new valid one create_api_token() create_tenants_and_add_users(client=self.client, num_of_tenants=1) tenant_client = utils.create_rest_client(username='******', password='******', tenant='tenant_0') dsl_path = resource('dsl/sleep_workflows.yaml') dep = self.deploy(dsl_path, wait=False, client=tenant_client) dep_id = dep.id time.sleep(2) # default_tenant: create snapshot and keep it's status 'started' snapshot = self._create_snapshot_and_modify_execution_status( Execution.STARTED) # tenant_0: schedule an execution for 1 min in the future scheduled_time = generate_scheduled_for_date() execution = tenant_client.executions.start(deployment_id=dep_id, workflow_id='install', schedule=scheduled_time) self._assert_execution_status(execution.id, Execution.SCHEDULED, tenant_client) time.sleep(62) # Wait for exec to 'wake up' self._assert_execution_status(execution.id, Execution.QUEUED, tenant_client) self.client.executions.update(snapshot.id, Execution.TERMINATED) self.wait_for_execution_to_end(execution, client=tenant_client)
def test_v_4_snapshot_restore_validation(self): snapshot = self._get_snapshot('snap_4.0.0.zip') self.client.snapshots.upload(snapshot, self.SNAPSHOT_ID) self._try_restore_snapshot( snapshot_id=self.SNAPSHOT_ID, error_msg='Tenant name should only be passed when ' 'restoring versions prior to 4.0.0', tenant_name='tenant' ) username = '******' password = '******' self.client.users.create(username, password, role=ADMIN_ROLE) self.client.tenants.add_user(username, DEFAULT_TENANT_NAME) admin_client = utils.create_rest_client(username=username, password=password) self._try_restore_snapshot( snapshot_id=self.SNAPSHOT_ID, error_msg='Only the bootstrap admin is allowed ' 'to perform this action', tenant_name=None, client=admin_client )
def _upload_and_restore_snapshot( self, snapshot_path, tenant_name=DEFAULT_TENANT_NAME, snapshot_id=None, desired_execution_status=Execution.TERMINATED, error_execution_status=Execution.FAILED, ignore_plugin_failure=False): """Upload the snapshot and launch the restore workflow """ snapshot_id = snapshot_id or self.SNAPSHOT_ID rest_client = utils.create_rest_client(tenant=tenant_name) self._upload_and_validate_snapshot(snapshot_path, snapshot_id, rest_client) self.logger.debug('Restoring snapshot...') execution = rest_client.snapshots.restore( snapshot_id, ignore_plugin_failure=ignore_plugin_failure) execution = self._wait_for_restore_execution_to_end( execution, rest_client) if execution.status == error_execution_status: self.logger.error('Execution error: {0}'.format(execution.error)) self.assertEqual(desired_execution_status, execution.status)
def create_rest_client(**kwargs): return utils.create_rest_client(**kwargs)