def test_copy(self, mock_env): """Test copy.""" mock_env.copy.return_value = mock_env obj = Context(command="test", deploy_environment=mock_env) obj_copy = obj.copy() assert obj_copy != obj assert obj_copy.command == obj.command assert obj_copy.env == mock_env mock_env.copy.assert_called_with()
def test_save_existing_iam_env_vars(self): """Test save_existing_iam_env_vars.""" context = Context(env_name='dev', env_region='us-east-1', env_root='./', env_vars=TEST_CREDENTIALS.copy()) context.save_existing_iam_env_vars() assert context.env_vars['OLD_AWS_ACCESS_KEY_ID'] == 'foo' assert context.env_vars['OLD_AWS_SECRET_ACCESS_KEY'] == 'bar' assert context.env_vars['OLD_AWS_SESSION_TOKEN'] == 'foobar'
def test_env_region(self): """Test env_region.""" mock_env = MagicMock() mock_env.aws_region = "us-east-1" ctx = Context(deploy_environment=mock_env) assert ctx.env_region == "us-east-1" ctx.env_region = "us-west-2" assert ctx.env_region == "us-west-2" assert mock_env.aws_region == "us-west-2"
def test_save_existing_iam_env_vars(self): """Test save_existing_iam_env_vars.""" context = Context(env_name='dev', env_region='us-east-1', env_root='./', env_vars={'AWS_ACCESS_KEY_ID': 'foo', 'AWS_SECRET_ACCESS_KEY': 'bar', # noqa 'AWS_SESSION_TOKEN': 'foobar'}) # noqa context.save_existing_iam_env_vars() self.assertEqual(context.env_vars['OLD_AWS_ACCESS_KEY_ID'], 'foo') self.assertEqual(context.env_vars['OLD_AWS_SECRET_ACCESS_KEY'], 'bar') self.assertEqual(context.env_vars['OLD_AWS_SESSION_TOKEN'], 'foobar')
def test_is_noninteractive(self): """Test is_noninteractive.""" context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'NON_EMPTY': '1'}) assert not context.is_noninteractive context.env_vars['CI'] = '1' assert context.is_noninteractive
def test_no_color_value_error(self, mock_stdout): """Test no_color with a ValueError.""" mock_stdout.isatty.return_value = True mock_env = MagicMock() mock_env.vars = {"RUNWAY_COLORIZE": "invalid"} ctx = Context(deploy_environment=mock_env) assert not ctx.no_color
def test_account_id(self, monkeypatch): """Test account_id.""" account_id = '123456789012' client = boto3.client('sts') stubber = Stubber(client) mock_session = MockBoto3Session(clients={'sts.us-east-1': client}, region_name='us-east-1') monkeypatch.setattr(Context, 'get_session', lambda self_: mock_session) context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={}) stubber.add_response(method='get_caller_identity', service_response={ 'UserId': 'test-user', 'Account': account_id, 'Arn': 'arn:aws:iam::{}:' 'user/test-user'.format(account_id) }) with stubber as stub: assert context.account_id == account_id stub.assert_no_pending_responses()
def test_use_concurrent(self, monkeypatch): """Test use_concurrent.""" mock_env = MagicMock() mock_env.ci = False mock_env_ci = MagicMock() mock_env_ci.ci = True ctx = Context(deploy_environment=mock_env) ctx_ci = Context(deploy_environment=mock_env_ci) monkeypatch.setattr(Context, "is_python3", False) assert not ctx.use_concurrent assert not ctx_ci.use_concurrent monkeypatch.setattr(Context, "is_python3", True) assert not ctx.use_concurrent assert ctx_ci.use_concurrent
def test_no_color(self, mock_stdout, colorize, isatty, expected): """Test no_color.""" mock_stdout.isatty.return_value = isatty env_vars = {} if colorize is not None: env_vars['RUNWAY_COLORIZE'] = colorize ctx = Context('test', 'us-east-1', './tests', env_vars=env_vars) assert ctx.no_color == expected
def test_echo_detected_environment_not_env(self, caplog): """Environment helper note when DEPLOY_ENVIRONMENT is not set.""" context = Context(env_name='test', env_region='us-east-1', env_root='./') expected = [ '', 'Environment "test" was determined from the ' 'current git branch or parent directory.', 'If this is not the environment name, update ' 'the branch/folder name or set an override value via the ' 'DEPLOY_ENVIRONMENT environment variable', '' ] with caplog.at_level(logging.INFO): context.echo_detected_environment() assert [rec.message for rec in caplog.records] == expected
def test_current_aws_creds(self): """Test current_aws_creds.""" context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars=TEST_CREDENTIALS.copy()) assert context.current_aws_creds == TEST_CREDENTIALS
def test_no_color(self, mock_stdout, colorize, isatty, expected): """Test no_color.""" mock_stdout.isatty.return_value = isatty mock_env = MagicMock() mock_env.vars = {} if colorize is not None: mock_env.vars["RUNWAY_COLORIZE"] = colorize ctx = Context(deploy_environment=mock_env) assert ctx.no_color == expected
def test_boto3_credentials(self, monkeypatch): """Test boto3_credentials.""" monkeypatch.setattr(Context, "current_aws_creds", TEST_CREDENTIALS) ctx = Context() assert ctx.boto3_credentials == { key.lower(): value for key, value in TEST_CREDENTIALS.items() }
def test_is_noninteractive(self): """Test is_noninteractive.""" mock_env = MagicMock() mock_env.ci = False ctx = Context(deploy_environment=mock_env) assert not ctx.is_noninteractive mock_env.ci = True assert ctx.is_noninteractive
def test_echo_detected_environment_from_env(self, caplog): """Environment helper note when DEPLOY_ENVIRONMENT is set.""" context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'DEPLOY_ENVIRONMENT': 'test'}) expected = [ '', 'Environment "test" was determined from the ' 'DEPLOY_ENVIRONMENT environment variable.', 'If this is not correct, update the value (or ' 'unset it to fall back to the name of the current git ' 'branch or parent directory).', '' ] with caplog.at_level(logging.INFO): context.echo_detected_environment() assert [rec.message for rec in caplog.records] == expected
def test_get_session_env_creds(self, mock_get_session, monkeypatch): """Test get_session with env creds.""" creds = { "aws_access_key_id": "test-key", "aws_secret_access_key": "test-secret", "aws_session_token": "test-token", } mock_env = MagicMock() mock_env.aws_region = "us-east-1" monkeypatch.setattr(Context, "boto3_credentials", creds) obj = Context(deploy_environment=mock_env) assert obj.get_session() mock_get_session.assert_called_once() call_kwargs = mock_get_session.call_args.kwargs assert call_kwargs.pop("access_key") == creds["aws_access_key_id"] assert call_kwargs.pop("region") == mock_env.aws_region assert call_kwargs.pop("secret_key") == creds["aws_secret_access_key"] assert call_kwargs.pop("session_token") == creds["aws_session_token"] assert not call_kwargs
def test_init_name_from_arg(self): """_env_name_from_env should be false when DEPLOY_ENVIRONMENT not set.""" context = Context(env_name='test', env_region='us-east-1', env_root='./') assert context.env_name == 'test' assert context.env_vars['DEPLOY_ENVIRONMENT'] == context.env_name, \ 'env_vars.DEPLOY_ENVIRONMENT should be set from env_name' assert not context._env_name_from_env, \ 'should be false when env_name was not derived from env_var'
def test_init_no_args(self, mock_env, monkeypatch): """Test init process with no args.""" mock_inject = MagicMock() monkeypatch.setattr(Context, "_Context__inject_profile_credentials", mock_inject) obj = Context() assert obj.command is None mock_env.assert_called_once_with() mock_inject.assert_called_once_with()
def test_boto3_credentials(self): """Test boto3_credentials.""" context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars=TEST_CREDENTIALS.copy()) assert context.boto3_credentials == { key.lower(): value for key, value in TEST_CREDENTIALS.items() }
def test_use_concurrent(self): """Test use_concurrent.""" from runway.context import sys context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'NON_EMPTY': '1'}) context_ci = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'CI': '1'}) with patch.object(sys, 'version_info') as version_info: version_info.major = 2 assert not context.use_concurrent assert not context_ci.use_concurrent with patch.object(sys, 'version_info') as version_info: version_info.major = 3 assert not context.use_concurrent assert context_ci.use_concurrent
def test_echo_detected_environment_not_env(self): """Environment helper note when DEPLOY_ENVIRONMENT is not set.""" if sys.version_info[0] < 3: return # this test method was not added until 3.4 context = Context(env_name='test', env_region='us-east-1', env_root='./') expected = [ 'INFO:runway:', 'INFO:runway:Environment "test" was determined from the ' 'current git branch or parent directory.', 'INFO:runway:If this is not the environment name, update ' 'the branch/folder name or set an override value via the ' 'DEPLOY_ENVIRONMENT environment variable', 'INFO:runway:' ] with self.assertLogs(LOGGER, logging.INFO) as logs: context.echo_detected_environment() self.assertEqual(logs.output, expected)
def test_max_concurrent_modules(self): """Test max_concurrent_modules.""" context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'RUNWAY_MAX_CONCURRENT_MODULES': '1'}) assert context.max_concurrent_modules == 1 del context.env_vars['RUNWAY_MAX_CONCURRENT_MODULES'] with patch('runway.context.multiprocessing.cpu_count') as cpu_count: cpu_count.return_value = 8 assert context.max_concurrent_modules == 8
def test_init_from_deploy_environment(self, monkeypatch): """Test init process with deploy environment.""" mock_env = MagicMock() mock_env.debug = "success" mock_inject = MagicMock() monkeypatch.setattr(Context, "_Context__inject_profile_credentials", mock_inject) obj = Context(command="test", deploy_environment=mock_env) assert obj.command == "test" assert obj.env == mock_env assert obj.debug == "success" mock_inject.assert_called_once_with()
def test_echo_detected_environment_from_env(self): """Environment helper note when DEPLOY_ENVIRONMENT is set.""" if sys.version_info[0] < 3: return # this test method was not added until 3.4 context = Context(env_name='test', env_region='us-east-1', env_root='./', env_vars={'DEPLOY_ENVIRONMENT': 'test'}) expected = [ 'INFO:runway:', 'INFO:runway:Environment "test" was determined from the ' 'DEPLOY_ENVIRONMENT environment variable.', 'INFO:runway:If this is not correct, update the value (or ' 'unset it to fall back to the name of the current git ' 'branch or parent directory).', 'INFO:runway:' ] with self.assertLogs(LOGGER, logging.INFO) as logs: context.echo_detected_environment() self.assertEqual(logs.output, expected)
def test_is_python3(self): """Test is_python3.""" from runway.context import sys # pylint: disable=import-outside-toplevel ctx = Context() with patch.object(sys, "version_info") as version_info: version_info.major = 2 assert not ctx.is_python3 with patch.object(sys, "version_info") as version_info: version_info.major = 3 assert ctx.is_python3
def test_init_name_from_arg(self): """_env_name_from_env should be false when DEPLOY_ENVIRONMENT not set""" context = Context(env_name='test', env_region='us-east-1', env_root='./') self.assertEqual(context.env_name, 'test') self.assertEqual( context.env_vars['DEPLOY_ENVIRONMENT'], context.env_name, 'env_vars.DEPLOY_ENVIRONMENT ' 'should be set from env_name') self.assertFalse( context._env_name_from_env, 'should be false when env_name was not derived ' 'from env_var')
def test_is_python3(self): """Test is_python3.""" from runway.context import sys context = Context(env_name='test', env_region='us-east-1', env_root='./') with patch.object(sys, 'version_info') as version_info: version_info.major = 2 assert not context.is_python3 with patch.object(sys, 'version_info') as version_info: version_info.major = 3 assert context.is_python3
def test_init(self, tmp_path): """Test init process.""" env_name = 'test' env_region = 'us-east-1' env_root = str(tmp_path) config_file = tmp_path / 'config' config_file.write_text(u'[profile test]\n' 'aws_access_key_id = bar\n' 'aws_secret_access_key = foo') with environ(dict(AWS_PROFILE='test', **TEST_CREDENTIALS)): env_vars = os.environ.copy() env_vars['DEPLOY_ENVIRONMENT'] = env_name ctx = Context(env_name, env_region, env_root) assert not ctx.command assert not ctx.debug assert ctx.env_name == env_name assert ctx.env_region == env_region assert ctx.env_root == env_root assert sorted(ctx.env_vars) == sorted(env_vars) with environ({ 'AWS_PROFILE': 'test', 'AWS_CONFIG_FILE': str(config_file) }): os.environ.pop('AWS_ACCESS_KEY_ID', None) os.environ.pop('AWS_SECRET_ACCESS_KEY', None) os.environ.pop('AWS_SESSION_TOKEN', None) env_vars = os.environ.copy() env_vars['DEPLOY_ENVIRONMENT'] = env_name ctx = Context(env_name, env_region, env_root) assert ctx.env_vars != os.environ assert ctx.env_vars['AWS_ACCESS_KEY_ID'] == 'bar' assert ctx.env_vars['AWS_SECRET_ACCESS_KEY'] == 'foo'
def get_context(name='test', region='us-east-1'): """Create a basic Runway context object.""" return Context(env_name=name, env_region=region, env_root=os.getcwd())
def test_env_vars(self): """Test env_vars.""" mock_env = MagicMock() mock_env.vars = {"test-key": "val"} ctx = Context(deploy_environment=mock_env) assert ctx.env_vars == {"test-key": "val"}