コード例 #1
0
 def test_download_s3_object_bucket_exception(self, mock_logger):  # pylint: disable=no-self-use
     """LookupTables - S3 bucket doesn't exist"""
     lookup_tables = LookupTables({'wrong_bucket': ['foo.json']})
     lookup_tables.download_s3_objects()
     mock_logger.assert_called_with(
         'Encounterred error while downloading %s from %s, %s', 'foo.json',
         'wrong_bucket', 'The specified bucket does not exist')
コード例 #2
0
 def test_download_s3_object_file_exception(self):  # pylint: disable=no-self-use
     """LookupTables - S3 file doesn't exist"""
     lookup_tables = LookupTables({'bucket_name': ['wrong_file']})
     lookup_tables.download_s3_objects()
コード例 #3
0
class TestLookupTables(object):
    """Test LookupTables class"""
    def __init__(self):
        self.buckets_info = {'bucket_name': ['foo.json', 'bar.json']}
        self.region = 'us-east-1'

    def setup(self):
        """LookupTables - Setup S3 bucket mocking"""
        # pylint: disable=attribute-defined-outside-init
        self.config = load_config('tests/unit/conf')
        self.lookup_tables = LookupTables(self.buckets_info)
        self.s3_mock = mock_s3()
        self.s3_mock.start()
        for bucket, files in self.buckets_info.iteritems():
            for json_file in files:
                put_mock_s3_object(
                    bucket, json_file,
                    json.dumps({
                        '{}_key'.format(bucket):
                        '{}_value'.format(os.path.splitext(json_file)[0])
                    }), self.region)

    def teardown(self):
        """LookupTables - Stop S3 bucket mocking"""
        self.s3_mock.stop()
        LookupTables._LOOKUP_TABLES_LAST_REFRESH = datetime(year=1970,
                                                            month=1,
                                                            day=1)

    def test_download_s3_object(self):
        """LookupTables - Download s3 object"""
        result = self.lookup_tables.download_s3_objects()
        assert_equal(result.keys(), ['foo', 'bar'])
        expect_result = {
            'foo': {
                'bucket_name_key': 'foo_value'
            },
            'bar': {
                'bucket_name_key': 'bar_value'
            }
        }
        assert_equal(result, expect_result)

    @patch('logging.Logger.error')
    def test_download_s3_object_bucket_exception(self, mock_logger):  # pylint: disable=no-self-use
        """LookupTables - S3 bucket doesn't exist"""
        lookup_tables = LookupTables({'wrong_bucket': ['foo.json']})
        lookup_tables.download_s3_objects()
        mock_logger.assert_called_with(
            'Encounterred error while downloading %s from %s, %s', 'foo.json',
            'wrong_bucket', 'The specified bucket does not exist')

    def test_download_s3_object_file_exception(self):  # pylint: disable=no-self-use
        """LookupTables - S3 file doesn't exist"""
        lookup_tables = LookupTables({'bucket_name': ['wrong_file']})
        lookup_tables.download_s3_objects()

    @patch('logging.Logger.error')
    def test_load_lookup_tables_missing_config(self, mock_logger):
        """LookupTables - Load lookup tables with missing config"""
        # Remove lookup_tables config for this test case.
        self.config['global']['infrastructure'].pop('lookup_tables')
        lookup_tables = LookupTables.load_lookup_tables(self.config)
        assert_equal(lookup_tables, False)
        assert_equal(LookupTables._LOOKUP_TABLES_LAST_REFRESH,
                     datetime(year=1970, month=1, day=1))

        self.config['global']['infrastructure']['lookup_tables'] = {
            'cache_refresh_minutes': 10,
            'enabled': True
        }
        lookup_tables = LookupTables.load_lookup_tables(self.config)
        mock_logger.assert_called_with('Buckets not defined')

    @patch('logging.Logger.debug')
    def test_load_lookup_tables(self, mock_logger):
        """LookupTables - Load lookup table"""
        self.config['global']['infrastructure']['lookup_tables'][
            'enabled'] = True
        lookup_tables = LookupTables.load_lookup_tables(self.config)
        result = lookup_tables.download_s3_objects()

        assert_equal(result.get('foo'), {'bucket_name_key': 'foo_value'})
        assert_equal(result.get('bar'), {'bucket_name_key': 'bar_value'})
        assert_equal(result.get('not_exist'), None)

        LookupTables.load_lookup_tables(self.config)
        mock_logger.assert_called()