示例#1
0
    def test_add_profile_missing_file_creates_new(self, open_mock, parser_mock,
                                                  chmod_mock):
        fake_parser = mock.MagicMock(name="config_parser")
        parser_mock.return_value = fake_parser

        # First time its called, throw an IOError to indicate the file doesnt
        # exist. Second time its called it returns a Mock for fake writing of
        # data.
        open_mock.side_effect = [IOError(), mock.MagicMock()]

        profile = aws.Credentials("/test")
        profile.add_profile(
            name="TestProfile",
            region="us-east-1",
            access_key="key",
            secret_key="secret",
            session_token="token",
        )

        open_mock.assert_has_calls(
            [mock.call("/test", "r"),
             mock.call("/test", "w+")])

        # Verify we're setting the file permissions as 0600 for safety
        chmod_mock.assert_has_calls([mock.call("/test", 0o600)])
示例#2
0
    def test_add_profile(self, open_mock, parser_mock, chmod_mock):
        fake_parser = mock.MagicMock(name="config_parser")
        parser_mock.return_value = fake_parser

        # Trigger the code to try to create a new section
        fake_parser.has_section.return_value = None

        profile = aws.Credentials("/test")
        profile.add_profile(
            name="TestProfile",
            region="us-east-1",
            access_key="key",
            secret_key="secret",
            session_token="token",
        )

        fake_parser.assert_has_calls(
            [
                mock.call.has_section("TestProfile"),
                mock.call.add_section("TestProfile"),
                mock.call.set("TestProfile", "region", "us-east-1"),
                mock.call.set("TestProfile", "aws_session_token", "token"),
                mock.call.set("TestProfile", "aws_security_token", "token"),
                mock.call.set("TestProfile", "aws_secret_access_key",
                              "secret"),
                mock.call.set("TestProfile", "output", "json"),
                mock.call.set("TestProfile", "aws_access_key_id", "key"),
            ],
            any_order=True,
        )
示例#3
0
    def test_add_profile(self, open_mock, parser_mock, chmod_mock):
        fake_parser = mock.MagicMock(name='config_parser')
        parser_mock.return_value = fake_parser

        # Trigger the code to try to create a new section
        fake_parser.has_section.return_value = None

        profile = aws.Credentials('/test')
        profile.add_profile(name='TestProfile',
                            region='us-east-1',
                            access_key='key',
                            secret_key='secret',
                            session_token='token')

        fake_parser.assert_has_calls([
            mock.call.has_section('TestProfile'),
            mock.call.add_section('TestProfile'),
            mock.call.set('TestProfile', 'region', 'us-east-1'),
            mock.call.set('TestProfile', 'aws_session_token', 'token'),
            mock.call.set('TestProfile', 'aws_security_token', 'token'),
            mock.call.set('TestProfile', 'aws_secret_access_key', 'secret'),
            mock.call.set('TestProfile', 'output', 'json'),
            mock.call.set('TestProfile', 'aws_access_key_id', 'key')
        ],
                                     any_order=True)
示例#4
0
    def test_add_profile_missing_file_creates_new(self, open_mock,
                                                  parser_mock):
        fake_parser = mock.MagicMock(name='config_parser')
        parser_mock.return_value = fake_parser

        # First time its called, throw an IOError to indicate the file doesnt
        # exist. Second time its called it returns a Mock for fake writing of
        # data.
        open_mock.side_effect = [IOError(), mock.MagicMock()]

        profile = aws.Credentials('/test')
        profile.add_profile(name='TestProfile',
                            region='us-east-1',
                            access_key='key',
                            secret_key='secret',
                            session_token='token')

        open_mock.assert_has_calls(
            [mock.call('/test', 'r'),
             mock.call('/test', 'w+')])