コード例 #1
0
    def test_modules_and_plugins_can_be_fetched(self, terraform_backend_input):
        base_directory = terraform_backend_input['base_directory']
        sub_directory = terraform_backend_input['sub_directory']
        bucket_name = terraform_backend_input['bucket_name']
        lock_table_name = terraform_backend_input['lock_table_name']
        environment_name = terraform_backend_input['environment_name']
        component_name = terraform_backend_input['component_name']
        tfstate_filename = terraform_backend_input['tfstate_filename']
        team_name = terraform_backend_input['team_name']
        boto_session = MagicMock(spec=Session)
        account_scheme = MagicMock(spec=AccountScheme)
        account_scheme.classic_metadata_handling = True

        state_file_key = (
            f'{environment_name}/{component_name}/{tfstate_filename}')

        with ExitStack() as stack:
            stack.enter_context(
                patch('cdflow_commands.state.NamedTemporaryFile'))
            stack.enter_context(patch('cdflow_commands.state.atexit'))
            check_call = stack.enter_context(
                patch('cdflow_commands.state.check_call'))
            LockTableFactory = stack.enter_context(
                patch('cdflow_commands.state.LockTableFactory'))
            LockTableFactory.return_value.get_table_name.return_value = \
                lock_table_name
            S3BucketFactory = stack.enter_context(
                patch('cdflow_commands.state.S3BucketFactory'))
            S3BucketFactory.return_value.get_bucket_name.return_value = \
                bucket_name

            state = terraform_state(
                base_directory,
                sub_directory,
                boto_session,
                environment_name,
                component_name,
                tfstate_filename,
                account_scheme,
                team_name,
            )
            state.init(get_terraform_modules=True)

        check_call.assert_called_once_with(
            [
                'terraform',
                'init',
                '-get=true',
                '-get-plugins=true',
                f'-backend-config=bucket={bucket_name}',
                f'-backend-config=region={boto_session.region_name}',
                f'-backend-config=key={state_file_key}',
                f'-backend-config=dynamodb_table={lock_table_name}',
                ANY,
                ANY,
                ANY,
                join(base_directory, sub_directory),
            ],
            cwd=base_directory,
        )
コード例 #2
0
    def test_backend_config_written_into_infra_code(self,
                                                    terraform_backend_input):
        base_directory = terraform_backend_input['base_directory']
        sub_directory = terraform_backend_input['sub_directory']
        bucket_name = terraform_backend_input['bucket_name']
        lock_table_name = terraform_backend_input['lock_table_name']
        environment_name = terraform_backend_input['environment_name']
        component_name = terraform_backend_input['component_name']
        tfstate_filename = terraform_backend_input['tfstate_filename']
        team_name = terraform_backend_input['team_name']
        boto_session = MagicMock(spec=Session)
        account_scheme = MagicMock(spec=AccountScheme)
        account_scheme.classic_metadata_handling = True

        with ExitStack() as stack:
            stack.enter_context(patch('cdflow_commands.state.check_call'))
            stack.enter_context(patch('cdflow_commands.state.atexit'))
            NamedTemporaryFile = stack.enter_context(
                patch('cdflow_commands.state.NamedTemporaryFile'))

            mock_file = MagicMock(spec=BufferedRandom)
            NamedTemporaryFile.return_value.__enter__.return_value = mock_file
            LockTableFactory = stack.enter_context(
                patch('cdflow_commands.state.LockTableFactory'))
            LockTableFactory.return_value.get_table_name.return_value = \
                lock_table_name
            S3BucketFactory = stack.enter_context(
                patch('cdflow_commands.state.S3BucketFactory'))
            S3BucketFactory.return_value.get_bucket_name.return_value = \
                bucket_name

            state = terraform_state(
                base_directory,
                sub_directory,
                boto_session,
                environment_name,
                component_name,
                tfstate_filename,
                account_scheme,
                team_name,
            )
            state.init()

        NamedTemporaryFile.assert_called_once_with(prefix='cdflow_backend_',
                                                   suffix='.tf',
                                                   dir=join(
                                                       base_directory,
                                                       sub_directory),
                                                   delete=False,
                                                   mode='w+')

        mock_file.write.assert_called_once_with(
            dedent('''
            terraform {
                backend "s3" {
                }
            }
        ''').strip())
コード例 #3
0
    def test_modules_and_plugins_can_be_fetched(self, terraform_backend_input):
        base_directory = terraform_backend_input['base_directory']
        sub_directory = terraform_backend_input['sub_directory']
        bucket_name = terraform_backend_input['bucket_name']
        lock_table_name = terraform_backend_input['lock_table_name']
        environment_name = terraform_backend_input['environment_name']
        component_name = terraform_backend_input['component_name']
        tfstate_filename = terraform_backend_input['tfstate_filename']
        team_name = terraform_backend_input['team_name']
        boto_session = MagicMock(spec=Session)
        account_scheme = MagicMock(spec=AccountScheme)
        account_scheme.classic_metadata_handling = False
        account_scheme.backend_s3_bucket = bucket_name
        account_scheme.backend_s3_dynamodb_table = lock_table_name

        with ExitStack() as stack:
            stack.enter_context(
                patch('cdflow_commands.state.NamedTemporaryFile'))
            stack.enter_context(patch('cdflow_commands.state.atexit'))
            check_call = stack.enter_context(
                patch('cdflow_commands.state.check_call'))
            check_output = stack.enter_context(
                patch('cdflow_commands.state.check_output'))
            check_output.return_value = '* default'.encode('utf-8')

            state = terraform_state(
                base_directory,
                sub_directory,
                boto_session,
                environment_name,
                component_name,
                tfstate_filename,
                account_scheme,
                team_name,
            )
            state.init(get_terraform_modules=True)

        check_call.assert_any_call(
            [
                'terraform',
                'init',
                '-get=true',
                '-get-plugins=true',
                f'-backend-config=bucket={bucket_name}',
                f'-backend-config=region={boto_session.region_name}',
                f'-backend-config=key={tfstate_filename}',
                ('-backend-config=workspace_key_prefix'
                 f'={team_name}/{component_name}'),
                f'-backend-config=dynamodb_table={lock_table_name}',
                ANY,
                ANY,
                ANY,
                join(base_directory, sub_directory),
            ],
            cwd=base_directory,
        )
コード例 #4
0
    def test_config_file_is_removed_at_exit(self, test_fixtures):
        terraform_backend_input = test_fixtures['terraform_backend_input']
        base_directory = terraform_backend_input['base_directory']
        sub_directory = terraform_backend_input['sub_directory']
        bucket_name = test_fixtures['terraform_backend_input']['bucket_name']
        boto_session = MagicMock(spec=Session)
        account_scheme = MagicMock(spec=AccountScheme)
        account_scheme.classic_metadata_handling = True

        lock_table_name = (
            test_fixtures['terraform_backend_input']['lock_table_name'])
        environment_name = (
            test_fixtures['terraform_backend_input']['environment_name'])
        component_name = (
            test_fixtures['terraform_backend_input']['component_name'])
        tfstate_filename = (
            test_fixtures['terraform_backend_input']['tfstate_filename'])

        backend_config_file_name = (
            f'cdflow_backend_{test_fixtures["temp_file_name"]}.tf')

        with ExitStack() as stack:
            stack.enter_context(patch('cdflow_commands.state.check_call'))
            NamedTemporaryFile = stack.enter_context(
                patch('cdflow_commands.state.NamedTemporaryFile'))
            atexit = stack.enter_context(patch('cdflow_commands.state.atexit'))

            NamedTemporaryFile.return_value.__enter__.return_value.name = \
                backend_config_file_name
            LockTableFactory = stack.enter_context(
                patch('cdflow_commands.state.LockTableFactory'))
            LockTableFactory.return_value.get_table_name.return_value = \
                lock_table_name
            S3BucketFactory = stack.enter_context(
                patch('cdflow_commands.state.S3BucketFactory'))
            S3BucketFactory.return_value.get_bucket_name.return_value = \
                bucket_name

            state = terraform_state(
                base_directory,
                sub_directory,
                boto_session,
                environment_name,
                component_name,
                tfstate_filename,
                account_scheme,
                'team-name',
            )
            state.init()

        atexit.register.assert_called_once_with(remove_file,
                                                backend_config_file_name)
コード例 #5
0
    def test_exiting_workspace_is_selected(self, terraform_backend_input):
        base_directory = terraform_backend_input['base_directory']
        sub_directory = terraform_backend_input['sub_directory']
        bucket_name = terraform_backend_input['bucket_name']
        lock_table_name = terraform_backend_input['lock_table_name']
        environment_name = terraform_backend_input['environment_name']
        component_name = terraform_backend_input['component_name']
        tfstate_filename = terraform_backend_input['tfstate_filename']
        team_name = terraform_backend_input['team_name']
        boto_session = MagicMock(spec=Session)
        account_scheme = MagicMock(spec=AccountScheme)
        account_scheme.classic_metadata_handling = False
        account_scheme.backend_s3_bucket = bucket_name
        account_scheme.backend_s3_dynamodb_table = lock_table_name

        with ExitStack() as stack:
            stack.enter_context(
                patch('cdflow_commands.state.NamedTemporaryFile'))
            stack.enter_context(patch('cdflow_commands.state.atexit'))
            check_call = stack.enter_context(
                patch('cdflow_commands.state.check_call'))
            check_output = stack.enter_context(
                patch('cdflow_commands.state.check_output'))

            check_output.return_value = (
                f'* default\n  {environment_name}\n\n'.encode('utf-8'))

            state = terraform_state(
                base_directory,
                sub_directory,
                boto_session,
                environment_name,
                component_name,
                tfstate_filename,
                account_scheme,
                team_name,
            )
            state.init()

        check_call.assert_any_call(
            [
                'terraform',
                'workspace',
                'select',
                environment_name,
                join(base_directory, sub_directory),
            ],
            cwd=base_directory,
        )