def openstack(self, cmd, may_fail=False, use_json=False, may_print_to_stderr=False): to_exec = [] # Make all requests as a noauth admin user. to_exec += [ '--os-endpoint', self.placement.endpoint, '--os-token', self.placement.token, '--os-auth-type', 'admin_token', ] if self.VERSION is not None: to_exec += ['--os-placement-api-version', self.VERSION] to_exec += cmd.split() if use_json: to_exec += ['-f', 'json'] # Context manager here instead of setUp because we only want # output trapping around the run(). self.output = six.StringIO() self.error = six.StringIO() stdout_fix = fixtures.MonkeyPatch('sys.stdout', self.output) stderr_fix = fixtures.MonkeyPatch('sys.stderr', self.error) with stdout_fix, stderr_fix: try: os_shell = shell.OpenStackShell() return_code = os_shell.run(to_exec) # Catch SystemExit to trap some error responses, mostly from the # argparse lib which has a tendency to exit for you instead of # politely telling you it wants to. except SystemExit as exc: return_code = exc.code # We may have error/warning messages in stderr, so treat it # separately from the stdout. output = self.output.getvalue() error = self.error.getvalue() if return_code: msg = 'Command: "%s"\noutput: %s' % (' '.join(to_exec), error) if not may_fail: raise CommandException(msg, cmd=' '.join(to_exec)) if use_json and output: output = json.loads(output) if may_print_to_stderr: return output, error if error: msg = ('Test code error - The command did not fail but it ' 'has a warning message. Set the "may_print_to_stderr" ' 'argument to true to get and validate the message:\n' 'Command: "%s"\nstderr: %s') % (' '.join(to_exec), error) raise CommandException(msg, cmd=' '.join(to_exec)) return output
def deploy_and_wait(log, clients, stack, plan_name, verbose_level, timeout=None, run_validations=False, skip_deploy_identifier=False, deployment_options={}): """Start the deploy and wait for it to finish""" orchestration_client = clients.orchestration if stack is None: log.info("Performing Heat stack create") action = 'CREATE' marker = None else: log.info("Performing Heat stack update") # Make sure existing parameters for stack are reused # Find the last top-level event to use for the first marker events = event_utils.get_events(orchestration_client, stack_id=plan_name, event_args={ 'sort_dir': 'desc', 'limit': 1 }) marker = events[0].id if events else None action = 'UPDATE' set_deployment_status(clients=clients, plan=plan_name, status='DEPLOYING') try: deploy(container=plan_name, run_validations=run_validations, skip_deploy_identifier=skip_deploy_identifier, timeout=timeout, verbosity=verbose_level) except Exception: set_deployment_status(clients=clients, plan=plan_name, status='DEPLOY_FAILED') raise # we always want the heat stack output while it's going. verbose_events = True # TODO(rabi) Simplify call to get events as we don't need to wait # for stack to be ready anymore i.e just get the events. create_result = utils.wait_for_stack_ready(orchestration_client, plan_name, marker, action, verbose_events) if not create_result: shell.OpenStackShell().run(["stack", "failures", "list", plan_name]) set_deployment_status(clients=clients, plan=plan_name, status='DEPLOY_FAILED') if stack is None: raise exceptions.DeploymentError("Heat Stack create failed.") else: raise exceptions.DeploymentError("Heat Stack update failed.")
def test_shell_args_no_options(self): _shell = shell.OpenStackShell() _shell.run("extension list".split()) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertEqual( test_shell.DEFAULT_PROJECT_DOMAIN_ID, auth_req['auth']['identity']['password']['user']['domain']['id'], ) self.assertEqual( test_shell.DEFAULT_USERNAME, auth_req['auth']['identity']['password']['user']['name'], ) self.assertEqual( test_shell.DEFAULT_PASSWORD, auth_req['auth']['identity']['password']['user']['password'], )
def update(clients, **workflow_input): workflow_client = clients.workflow_engine tripleoclients = clients.tripleoclient plan_name = workflow_input['container'] with tripleoclients.messaging_websocket() as ws: execution = base.start_workflow( workflow_client, 'tripleo.package_update.v1.package_update_plan', workflow_input=workflow_input) for payload in base.wait_for_messages(workflow_client, ws, execution): assert payload['status'] == "SUCCESS", pprint.pformat(payload) orchestration_client = clients.orchestration events = event_utils.get_events(orchestration_client, stack_id=plan_name, event_args={ 'sort_dir': 'desc', 'limit': 1 }) marker = events[0].id if events else None time.sleep(10) create_result = utils.wait_for_stack_ready(orchestration_client, plan_name, marker, 'UPDATE', 1) if not create_result: shell.OpenStackShell().run(["stack", "failures", "list", plan_name]) raise exceptions.DeploymentError("Heat Stack update failed.")
def test_shell_args_precedence_1(self, config_mock, vendor_mock): """Precedence run 1 Run 1 has --os-password on CLI """ def config_mock_return(): return ('file.yaml', copy.deepcopy(test_shell.CLOUD_2)) config_mock.side_effect = config_mock_return def vendor_mock_return(): return ('file.yaml', copy.deepcopy(test_shell.PUBLIC_1)) vendor_mock.side_effect = vendor_mock_return print("CONFIG_MOCK_BASE=%s" % CONFIG_MOCK_BASE) _shell = shell.OpenStackShell() _shell.run("--os-password qaz configuration show".split(), ) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() # -env, -cli, -occ # No test, everything not specified tests this # -env, -cli, +occ self.assertEqual( "heart-o-gold", auth_req['auth']['scope']['project']['name'], ) # -env, +cli, -occ self.assertEqual( 'qaz', auth_req['auth']['identity']['password']['user']['password'], ) # -env, +cli, +occ # +env, -cli, -occ self.assertEqual( test_shell.DEFAULT_USER_DOMAIN_ID, auth_req['auth']['identity']['password']['user']['domain']['id'], ) # +env, -cli, +occ print("auth_req: %s" % auth_req['auth']) self.assertEqual( test_shell.DEFAULT_USERNAME, auth_req['auth']['identity']['password']['user']['name'], )
def _setup_shell_app(): # create app and run help command. This bootstrap the application. app = shell.OpenStackShell() try: # reduce noise - 1 org_print_message = argparse.ArgumentParser._print_message def devnull(obj, message, file): pass argparse.ArgumentParser._print_message = devnull # reduce noise - 2 stdout = sys.stdout stderr = sys.stderr app_stdout = app.stdout app_stderr = app.stderr dev_null = open(os.devnull, 'w') sys.stdout = dev_null sys.stderr = dev_null app.stdout = dev_null app.stderr = dev_null app.run(['--help']) except SystemExit: pass finally: # reassign original stdout and stderr sys.stdout = stdout sys.stderr = stderr app.stdout = app_stdout app.stderr = app_stderr argparse.ArgumentParser._print_message = org_print_message return app
def test_shell_args_no_options(self): _shell = shell.OpenStackShell() _shell.run("configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V2_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertEqual( test_shell.DEFAULT_PROJECT_NAME, auth_req['auth']['tenantName'], ) self.assertEqual( test_shell.DEFAULT_USERNAME, auth_req['auth']['passwordCredentials']['username'], ) self.assertEqual( test_shell.DEFAULT_PASSWORD, auth_req['auth']['passwordCredentials']['password'], )
def test_shell_args_verify(self): _shell = shell.OpenStackShell() _shell.run("--verify configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check verify self.assertTrue(self.requests_mock.request_history[0].verify)
def test_shell_args_cacert_insecure(self): _shell = shell.OpenStackShell() _shell.run("--os-cacert xyzpdq --insecure configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check verify self.assertFalse(self.requests_mock.request_history[0].verify)
def test_shell_args_insecure(self): _shell = shell.OpenStackShell() _shell.run("--insecure extension list".split()) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check verify self.assertFalse(self.requests_mock.request_history[0].verify)
def test_shell_args_cacert_insecure(self): # This test verifies the outcome of bug 1447784 # https://bugs.launchpad.net/python-openstackclient/+bug/1447784 _shell = shell.OpenStackShell() _shell.run("--os-cacert xyzpdq --insecure configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check verify self.assertFalse(self.requests_mock.request_history[0].verify)
def test_shell_args_cacert(self): _shell = shell.OpenStackShell() _shell.run("--os-cacert xyzpdq extension list".split()) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check verify self.assertEqual( 'xyzpdq', self.requests_mock.request_history[0].verify, )
def deploy_and_wait(log, clients, stack, plan_name, verbose_level, timeout=None, run_validations=False, skip_deploy_identifier=False, deployment_options={}): """Start the deploy and wait for it to finish""" workflow_input = { "container": plan_name, "run_validations": run_validations, "skip_deploy_identifier": skip_deploy_identifier, "deployment_options": deployment_options, } if timeout is not None: workflow_input['timeout'] = timeout deploy(log, clients, **workflow_input) orchestration_client = clients.orchestration if stack is None: log.info("Performing Heat stack create") action = 'CREATE' marker = None else: log.info("Performing Heat stack update") # Make sure existing parameters for stack are reused # Find the last top-level event to use for the first marker events = event_utils.get_events(orchestration_client, stack_id=plan_name, event_args={ 'sort_dir': 'desc', 'limit': 1 }) marker = events[0].id if events else None action = 'UPDATE' time.sleep(10) verbose_events = verbose_level >= 1 create_result = utils.wait_for_stack_ready(orchestration_client, plan_name, marker, action, verbose_events) if not create_result: shell.OpenStackShell().run(["stack", "failures", "list", plan_name]) set_deployment_status(clients, 'failed', plan=plan_name) if stack is None: raise exceptions.DeploymentError("Heat Stack create failed.") else: raise exceptions.DeploymentError("Heat Stack update failed.")
def test_shell_args_precedence_2(self, config_mock, vendor_mock): """Precedence run 2 Run 2 has --os-username, --os-password, --os-project-domain-id on CLI """ def config_mock_return(): log_file = self.get_temp_file_path('test_log_file') cloud2 = test_shell.get_cloud(log_file) return ('file.yaml', cloud2) config_mock.side_effect = config_mock_return def vendor_mock_return(): return ('file.yaml', copy.deepcopy(test_shell.PUBLIC_1)) vendor_mock.side_effect = vendor_mock_return print("CONFIG_MOCK_BASE=%s" % CONFIG_MOCK_BASE) _shell = shell.OpenStackShell() _shell.run( "--os-username zarquon --os-password qaz " "--os-project-domain-id 5678 extension list".split(), ) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() # +env, +cli, -occ self.assertEqual( '5678', auth_req['auth']['scope']['project']['domain']['id'], ) # +env, +cli, +occ print("auth_req: %s" % auth_req['auth']) self.assertEqual( 'zarquon', auth_req['auth']['identity']['password']['user']['name'], )
def test_project_id_env(self): _shell = shell.OpenStackShell() _shell.run("extension list".split()) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertIsNone(auth_req['auth'].get('tenantId', None)) self.assertIsNone(auth_req['auth'].get('tenantName', None))
def test_project_id_arg(self): _shell = shell.OpenStackShell() _shell.run("--os-project-id wsx configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertIsNone(auth_req['auth'].get('tenantId', None)) self.assertIsNone(auth_req['auth'].get('tenantName', None))
def test_shell_args_precedence_2(self, config_mock, vendor_mock): """Precedence run 2 Run 2 has --os-username, --os-password, --os-project-domain-id on CLI """ def config_mock_return(): return ('file.yaml', copy.deepcopy(test_shell.CLOUD_2)) config_mock.side_effect = config_mock_return def vendor_mock_return(): return ('file.yaml', copy.deepcopy(test_shell.PUBLIC_1)) vendor_mock.side_effect = vendor_mock_return _shell = shell.OpenStackShell() _shell.run( "--os-username zarquon --os-password qaz " "--os-project-domain-id 5678 configuration show".split(), ) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() # +env, +cli, -occ self.assertEqual( '5678', auth_req['auth']['scope']['project']['domain']['id'], ) # +env, +cli, +occ print("auth_req: %s" % auth_req['auth']) self.assertEqual( 'zarquon', auth_req['auth']['identity']['password']['user']['name'], )
def test_project_name_env(self): _shell = shell.OpenStackShell() _shell.run("configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V2_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertEqual( test_shell.DEFAULT_PROJECT_NAME, auth_req['auth']['tenantName'], )
def test_project_id_arg(self): _shell = shell.OpenStackShell() _shell.run("--os-project-id wsx extension list".split()) # Check general calls self.assertNotEqual(len(self.requests_mock.request_history), 0) # Check discovery request self.assertEqual( test_base.V2_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() self.assertEqual( "wsx", auth_req['auth']['tenantId'], )
def test_shell_callback(self, mock_prompt): mock_prompt.return_value = "qaz" _shell = shell.OpenStackShell() _shell.run("configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check password callback set correctly self.assertEqual(mock_prompt, _shell.cloud._openstack_config._pw_callback) # Check auth request auth_req = self.requests_mock.request_history[1].json() # Check returned password from prompt function self.assertEqual( "qaz", auth_req['auth']['identity']['password']['user']['password'], )
def test_shell_args_options(self): """Verify command line options override environment variables""" _shell = shell.OpenStackShell() _shell.run( "--os-username zarquon --os-password qaz " "configuration show".split(), ) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 2) # Check discovery request self.assertEqual( test_base.V3_AUTH_URL, self.requests_mock.request_history[0].url, ) # Check auth request auth_req = self.requests_mock.request_history[1].json() # -env, -cli # No test, everything not specified tests this # -env, +cli self.assertEqual( 'qaz', auth_req['auth']['identity']['password']['user']['password'], ) # +env, -cli self.assertEqual( test_shell.DEFAULT_PROJECT_DOMAIN_ID, auth_req['auth']['identity']['password']['user']['domain']['id'], ) # +env, +cli self.assertEqual( 'zarquon', auth_req['auth']['identity']['password']['user']['name'], )
def make_shell(): """Create a new command shell and mock out some bits.""" _shell = shell.OpenStackShell() _shell.command_manager = mock.Mock() return _shell
def test_shell_args_cacert(self): _shell = shell.OpenStackShell() _shell.run("--os-cacert xyzpdq configuration show".split()) # Check general calls self.assertEqual(len(self.requests_mock.request_history), 0)