def test_main_without_value_error_raised(self): mock_client = mock.MagicMock() mock_parser = mock.MagicMock() mock_config_env = mock.MagicMock() mock_config = mock.MagicMock() mock_config.action = 'list_permissions' mock_config.roles = ['r1', 'r2'] mock_config.role_prefixes = None mock_config.out_format = 'json' mock_config.service = 'explainer' with mock.patch('google.cloud.forseti.services.cli.create_parser') as mock_create_parser: mock_create_parser.return_value = mock_parser mock_parser.parse_args.return_value = mock_config cli.main([], mock_config_env, mock_client) mock_parser.error.assert_not_called()
def test_cli_grpc_server_unknown(self, test_cases): """Grpc server unavailable.""" for (commandline, client_func, func_args, func_kwargs, config_string, config_expect) in test_cases: args = shlex.split(commandline) env_config = cli.DefaultConfig( json.load(io.StringIO(config_string))) with mock.patch('sys.stdout', new_callable=io.StringIO) as mock_out: cli.main(args=args, config_env=env_config, client=ERROR_CLIENT, parser_cls=MockArgumentParser) cli_output = mock_out.getvalue().strip() self.assertTrue( 'Error occurred on the server side' in cli_output)
def test_cli(self, test_cases): """Test if the CLI hits specific client methods.""" tmp_config = os.path.join(self.test_dir, '.forseti') with mock.patch.dict(os.environ, {'FORSETI_CLIENT_CONFIG': tmp_config}): for (commandline, client_func, func_args, func_kwargs, config_string, config_expect) in test_cases: try: args = shlex.split(commandline) env_config = cli.DefaultConfig( json.load(io.StringIO(config_string))) # Capture stdout, so it doesn't pollute the test output with mock.patch('sys.stdout', new_callable=io.StringIO): config = cli.main(args=args, config_env=env_config, client=CLIENT, parser_cls=MockArgumentParser) if client_func is not None: client_func.assert_called_with(*func_args, **func_kwargs) # Check attribute values for attribute, value in config_expect.items(): self.assertEqual( getattr(config, attribute), value, 'Attribute value unexpected: {}, {}'.format( attribute, value)) except ArgumentParserError as e: self.fail('Argument parser failed on {}, {}'.format( commandline, e.message))
def RunForsetiCli(): """Run Forseti CLI.""" import google.cloud.forseti.services.cli as forseti_cli forseti_cli.main()