def test_configure_from_config_file(self): # this is not a known configuration so this is ignored. config_file_vars = { 'foo': 'bar', CDP_ACCESS_KEY_ID_KEY_NAME: 'key_id', CDP_PRIVATE_KEY_KEY_NAME: 'mysecretkey' } context = FakeContext( all_variables={'config_file': '/config/location'}, config_file_vars=config_file_vars) context.context_var_map = {'region': ('region', "CDP_REGION")} context.full_config = { 'profiles': { 'default': { 'region': 'CDP_REGION' } } } stream = six.StringIO() self.configure_list = ConfigureListCommand(stream) self.configure_list(context, args=[], parsed_globals=None) rendered = stream.getvalue() self.assertRegexpMatches(rendered, 'profile\\s+<not set>\\s+None\\s+None') self.assertRegexpMatches( rendered, 'cdp_access_key_id\\s+\\*+y_id\\s+config-file') self.assertRegexpMatches(rendered, 'cdp_private_key\\s+\\*+tkey\\s+config-file')
def test_configure_get_command(self): context = FakeContext({}) context.config['region'] = 'us-west-2' stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['region'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'us-west-2')
def test_dotted_get(self): context = FakeContext({}) context.full_config = {'preview': {'foo': 'true'}} stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['preview.foo'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'true')
def test_get_nested_attribute_from_default_does_not_exist(self): context = FakeContext({}) context.full_config = {'profiles': {}} stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['default.s3.signature_version'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), '')
def test_get_from_profile(self): context = FakeContext({}) context.full_config = \ {'profiles': {'testing': {'cdp_access_key_id': 'access_key'}}} stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['profile.testing.cdp_access_key_id'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'access_key')
def test_dotted_not_in_full_config_get(self): context = FakeContext({}) context.full_config = { 'profiles': { 'dev': { 'someconf': { 'foobar': 'true' } } } } stream = six.StringIO() context.variables['profile'] = 'dev' config_get = ConfigureGetCommand(stream) config_get(context, args=['someconf.foobar'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'true')
def test_get_nested_attribute(self): context = FakeContext({}) context.full_config = { 'profiles': { 'testing': { 's3': { 'signature_version': 's3v4' } } } } stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['profile.testing.s3.signature_version'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 's3v4')
def test_configure_list_command_nothing_set(self): # Test the case where the user only wants to change a single_value. context = FakeContext( all_variables={'config_file': '/config/location'}) context.full_config = { 'profiles': { 'default': { 'region': 'CDP_REGION' } } } stream = six.StringIO() self.configure_list = ConfigureListCommand(stream) self.configure_list(context, args=[], parsed_globals=None) rendered = stream.getvalue() self.assertRegexpMatches(rendered, 'profile\\s+<not set>') self.assertRegexpMatches(rendered, 'cdp_access_key_id\\s+<not set>') self.assertRegexpMatches(rendered, 'cdp_private_key\\s+<not set>')
def setUp(self): self.writer = mock.Mock() self.global_args = mock.Mock() self.global_args.profile = None self.precanned = PrecannedPrompter(access_key_id='new_access_key_id', private_key='new_private_key', cdp_endpoint_url=None) self.context = FakeContext({'config_file': 'myconfigfile'}) self.configure = configure.ConfigureCommand(prompter=self.precanned, config_writer=self.writer)
def setUp(self): self.writer = mock.Mock() self.global_args = mock.Mock() self.global_args.profile = None self.global_args.endpoint_url = None self.global_args.cdp_endpoint_url = None self.precanned = PrecannedPrompter(value='new_value') self.context = FakeContext({'config_file': 'myconfigfile'}) self.configure = configure.ConfigureCommand(prompter=self.precanned, config_writer=self.writer)
def test_configure_from_env(self): env_vars = {'profile': 'myprofilename'} context = FakeContext( all_variables={'config_file': '/config/location'}, environment_vars=env_vars) context.context_var_map = {'profile': (None, "PROFILE_ENV_VAR")} context.full_config = { 'profiles': { 'default': { 'region': 'CDP_REGION' } } } stream = six.StringIO() self.configure_list = ConfigureListCommand(stream) self.configure_list(context, args=[], parsed_globals=None) rendered = stream.getvalue() self.assertRegexpMatches( rendered, 'profile\\s+myprofilename\\s+env\\s+PROFILE_ENV_VAR')
def test_dotted_get_with_profile(self): context = FakeContext({}) context.full_config = { 'profiles': { 'thu-dev': { 'thu': { 'instance_profile': 'my_ip' } } } } context.config = {'thu': {'instance_profile': 'my_ip'}} stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['thu-dev.thu.instance_profile'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'my_ip')
def test_configure_get_command_no_exist(self): no_vars_defined = {} context = FakeContext(no_vars_defined) stream = six.StringIO() config_get = ConfigureGetCommand(stream) rc = config_get(context, args=['region'], parsed_globals=None) rendered = stream.getvalue() # If a config value does not exist, we don't print any output. self.assertEqual(rendered, '') # And we exit with an rc of 1. self.assertEqual(rc, 1)
def test_predefined_section_with_profile(self): # Test that we retrieve the predefined section config var even if it's # under a profile. context = FakeContext({}) context.full_config = { 'profiles': { 'thu-dev': { 'thu': { 'instance_profile': 'my_ip' } }, 'preview': { 'foo': 'true' } } } stream = six.StringIO() config_get = ConfigureGetCommand(stream) config_get(context, args=['preview.foo'], parsed_globals=None) rendered = stream.getvalue() self.assertEqual(rendered.strip(), 'true')
def test_configure_from_multiple_sources(self): # Here the profile is from an env var, the # region is from the config file, and the credentials # are from an iam-role. env_vars = {'profile': 'myprofilename'} credentials = mock.Mock() credentials.access_key_id = 'access_key' credentials.private_key = 'private_key' credentials.method = 'foobar' context = FakeContext( all_variables={'config_file': '/config/location'}, environment_vars=env_vars, config_file_vars={}, credentials=credentials) context.context_var_map = { 'region': ('region', 'CDP_REGION'), 'profile': ('profile', 'CDP_DEFAULT_PROFILE') } context.full_config = { 'profiles': { 'default': { 'region': 'CDP_REGION' } } } stream = six.StringIO() self.configure_list = ConfigureListCommand(stream) self.configure_list(context, args=[], parsed_globals=None) rendered = stream.getvalue() # The profile came from an env var. self.assertRegexpMatches( rendered, 'profile\\s+myprofilename\\s+env\\s+CDP_DEFAULT_PROFILE') # The credentials came from 'foobar'. Note how we're # also checking that the access_key/private_key are masked # with '*' chars except for the last 4 chars. self.assertRegexpMatches(rendered, r'cdp_access_key_id\s+\*+_key\s+foobar') self.assertRegexpMatches(rendered, r'cdp_private_key\s+\*+_key\s+foobar')
def setUp(self): self.loader = Mock() self.loader.load_service_data.return_value = yaml.safe_load( open(os.path.join(CLIENT_DIR, 'service.yaml'))) self.endpoint = Mock() self.endpoint.host = 'http://thunderhead.cloudera.altus.cloudera.com' self.endpoint.make_request.return_value = (Mock(status_code=200), {}) self.endpoint_creator = Mock() self.endpoint_creator.create_endpoint.return_value = self.endpoint self.retryhandler = Mock() self.context = FakeContext({}) self.client_creator = ClientCreator(self.loader, self.context, self.endpoint_creator, 'user-agent-header', ResponseParserFactory(), self.retryhandler) self.credentials = Mock() self.credentials.private_key = "A Private Key"
def test_context_says_profile_does_not_exist(self): # Whenever you try to get a config value from botocore, # it will raise an exception complaining about ProfileNotFound. # We should handle this case, and write out a new profile section # in the config file. context = FakeContext({'config_file': 'myconfigfile'}, profile_does_not_exist=True) self.configure = configure.ConfigureCommand(prompter=self.precanned, config_writer=self.writer) self.global_args.profile = 'profile-does-not-exist' self.configure(context, args=[], parsed_globals=self.global_args) self.assert_credentials_file_updated_with( { CDP_ACCESS_KEY_ID_KEY_NAME: 'new_value', CDP_PRIVATE_KEY_KEY_NAME: 'new_value', '__section__': 'profile-does-not-exist' }, config_file_comment=CREDENTIAL_FILE_COMMENT) self.writer.update_config.assert_called_with( {'__section__': 'profile profile-does-not-exist'}, 'myconfigfile')
def setUp(self): self.writer = mock.Mock() self.open_new_browser = mock.Mock() self.context = FakeContext({'config_file': 'myconfigfile'}) self.login = LoginCommand(config_writer=self.writer, open_new_browser=self.open_new_browser)
def setUp(self): self.context = FakeContext({'config_file': 'myconfigfile'}) self.fake_credentials_filename = os.path.expanduser( '~/fake_credentials_filename') self.context.profile = None self.config_writer = mock.Mock()
def setUp(self): self.writer = mock.Mock() self.context = FakeContext({'config_file': 'myconfigfile'}) self.args = [] self.logout = LogoutCommand(config_writer=self.writer)