Ejemplo n.º 1
0
import unittest

from botocore import loaders, model

from tests._utils import _import
shape_parser = _import('bac', 'shape_parser')

SERVICE = 's3'
OPERATION = 'ListBuckets'


class TestShapeParser(unittest.TestCase):
    def setUp(self):
        self.shape = self.get_shape()
        self.parser = shape_parser.ShapeParser()

    def get_shape(self):
        loader = loaders.Loader()
        service_data = loader.load_service_model(SERVICE, 'service-2')
        shape_resolver = model.ShapeResolver(service_data.get('shapes', {}))
        operation_data = service_data['operations'][OPERATION]
        return shape_resolver.resolve_shape_ref(operation_data['output'])

    def test_parse_shape(self):
        expected = {
            'Buckets': [{
                'CreationDate': 'CreationDate:timestamp',
                'Name': 'BucketName:string'
            }],
            'Owner': {
                'DisplayName': 'DisplayName:string',
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
"""
This test suite for nested completer is insiped by the one
implemented within the python-prompt-toolkit (ver. >= 3.0)
library github repository.
"""
import unittest

from prompt_toolkit.document import Document
from prompt_toolkit.completion import CompleteEvent
from six import assertCountEqual, text_type

from tests._utils import _import, transform

nested = _import('bac', 'nested_completer')


class NestedCompleterTest(unittest.TestCase):
    def setUp(self):
        self.completer = nested.NestedCompleter.from_nested_dict(
            {
                "show": {
                    "version": None,
                    "clock": None,
                    "interfaces": None,
                    "ip": {"interface": {"brief"}},
                },
                "exit": None,
            }
        )
Ejemplo n.º 3
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import logging
import mock
import os
import unittest

from botocore.exceptions import ClientError
from six import text_type
from testfixtures import log_capture, TempDirectory

from tests._utils import _import, transform
caching = _import('bac', 'caching')

FAKE_CACHED = {'--testing': 'FakeResource'}
USER1 = text_type('123456789012')
USER2 = text_type('098765432109')
REG1 = text_type('us-east-1')
REG2 = text_type('eu-west-1')
VALUE1 = transform(['foo', 'bar', 'baz'])
VALUE2 = transform(['oof', 'rab', 'zab'])


class FakeResource(object):
    pass


class ResourceInitTest(unittest.TestCase):
    @mock.patch('bac.caching.CACHED_RESOURCES', FAKE_CACHED)
    @mock.patch('bac.caching.resources')
Ejemplo n.º 4
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import mock
import os
import unittest

from botocore.exceptions import ClientError
from testfixtures import TempDirectory

from tests._utils import _import
resources = _import('bac', 'resources')

BASE_PATH = os.path.dirname(os.path.realpath(__file__))
CACHE_PATH = os.path.join(BASE_PATH, 'cache')
RESOURCE_CACHE = ('123456789012,foo,bar,baz\n' '098765432109,oof,rab,zab\n')
RESOURCE_CACHE_REG = ('123456789012:us-east-1,foo,bar,baz\n'
                      '098765432109:eu-west-1,oof,rab,zab')
WRITEABLE_RESOURCE = [('123456789012', ['foo', 'bar', 'baz']),
                      ('098765432109', ['oof', 'rab', 'zab'])]


class FakeResource(resources.CachedResource):
    resource_type = 'test-resource'
    service = 'test'
    operation = 'list_resources'
    query = 'Resources[].Name'


class CachedResourceTest(unittest.TestCase):
    def setUp(self):
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import logging
import mock
import unittest

from botocore.exceptions import BotoCoreError, ClientError
from six.moves import configparser
from testfixtures import LogCapture, log_capture

from tests._utils import _import, captured_output, check_logs

profile_manager = _import('bac', 'profile_manager')
errors = _import('bac', 'errors')

ACC1 = '123456789012'
ACC2 = '098765432109'
PROFILE1 = 'profile_uno'
PROFILE2 = 'profile_dos'
REG1 = 'us-east-1'
REG2 = 'eu-west-1'
PM_ABS_IMPORT = 'bac.profile_manager.ProfileManager'


class ProfileManagerInitTest(unittest.TestCase):
    @mock.patch('bac.profile_manager.AWS_CREDENTIALS', '')
    @log_capture(level=logging.ERROR)
    def test_handle_missing_credentials_file(self, captured_log):
        with self.assertRaises(errors.NoProfilesError):
            profile_manager.ProfileManager()
Ejemplo n.º 6
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import mock
import unittest

from botocore.exceptions import UnknownServiceError
from botocore.model import OperationNotFoundError
from prompt_toolkit.document import Document
from prompt_toolkit.completion import CompleteEvent
from six import assertCountEqual, text_type

from tests._utils import _import, transform
query_completer = _import('bac', 'query_completer')
errors = _import('bac', 'errors')

SHAPE = {
    'Buckets': [{
        'CreationDate': 'CreationDate:timestamp',
        'Name': 'BucketName:string'
    }],
    'Owner': {
        'DisplayName': 'DisplayName:string',
        'ID': 'ID:string'
    }
}


class QueryCompleterInitializationTest(unittest.TestCase):
    @mock.patch('botocore.session.Session')
    @mock.patch('bac.shape_parser.ShapeParser', mock.Mock())
Ejemplo n.º 7
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import unittest
import mock

from six import text_type

from tests._utils import _import
toolbar = _import('bac', 'toolbar')


class FakeBAC(object):
    def __init__(self, pm):
        self._profile_manager = pm
        self._fuzzy = True
        self._cache_completion = True

    def toggle_fuzzy(self):
        self._fuzzy = not self._fuzzy

    def toggle_cache(self):
        self._cache_completion = not self._cache_completion


class ToolbarTest(unittest.TestCase):
    def setUp(self):
        self.fake_pm = mock.Mock()
        self.fake_pm.active_profiles = set()
        self.fake_pm.active_regions = set()
        self._set_active([], [])
Ejemplo n.º 8
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import logging
import mock
import unittest

import yaml

from six import text_type
from testfixtures import LogCapture, log_capture

from tests._utils import _import, captured_output, check_logs
batch = _import('bac', 'batch')
errors = _import('bac', 'errors')
utils = _import('bac', 'utils')

COMMAND1 = [
    'aws', 's3api', 'list-buckets', '--profile', 'uno', '--region', 'us-east-1'
]
COMMAND2 = [
    'aws', 's3api', 'list-buckets', '--profile', 'uno', '--region', 'eu-west-1'
]
COMMAND3 = [
    'aws', 's3api', 'list-buckets', '--profile', 'dos', '--region', 'us-east-1'
]
COMMAND4 = [
    'aws', 's3api', 'list-buckets', '--profile', 'dos', '--region', 'eu-west-1'
]
VALID_DEF_PATH = 'tests/valid_batch_command.yml'
INVALID_DEF_PATH = 'tests/invalid_batch_command.yml'
Ejemplo n.º 9
0
import mock
import unittest

from six import text_type

from tests._utils import (_import, captured_output)
from tests.fake_session import FakeSession
checker = _import('bac', 'checker')
errors = _import('bac', 'errors')


class CheckerTest(unittest.TestCase):
    def setUp(self):
        def fakeSession(profile=None):
            return FakeSession(profile)

        with mock.patch('bac.checker.Session') as fake_session:
            fake_session.side_effect = fakeSession
            self.checker = checker.CLIChecker('foo')


class CheckerSyntaxTest(CheckerTest):
    def test_standard_successful_check(self):
        cmd = ['s3api', 'list-objects', '--bucket', 'foo']
        self.checker.check(cmd)

    def test_handle_missing_parameter_value(self):
        cmd = ['s3api', 'list-objects', '--bucket']
        with captured_output() as (out, err):
            with self.assertRaises(errors.CLICheckerSyntaxError):
                self.checker.check(cmd)
Ejemplo n.º 10
0
import datetime
import logging
import os
import shlex
import sys
import unittest

import boto3
import botocore

from testfixtures import log_capture, TempDirectory
from botocore.stub import ANY, Stubber

from tests._utils import _import, captured_output, check_logs, transform

utils = _import('bac', 'utils')
errors = _import('bac', 'errors')


class UtilsTest(unittest.TestCase):
    def setUp(self):
        pass

    @log_capture('bac.utils', level=logging.ERROR)
    def test_arg_parser_help(self, captured_log):
        parser = utils.ArgumentParser()
        argv = ['--help']
        with self.assertRaises(errors.ArgumentParserDoneException):
            with captured_output() as (out, err):
                parser.parse_args(argv)
Ejemplo n.º 11
0
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2020, GoodData(R) Corporation. All rights reserved.
import logging
import mock
import os
import unittest

from prompt_toolkit.document import Document
from prompt_toolkit.completion import CompleteEvent
from six import assertCountEqual, text_type
from testfixtures import LogCapture, log_capture

from tests._utils import _import, check_items_in_result, check_logs, transform

bac_completer = _import('bac', 'bac_completer')
errors = _import('bac', 'errors')

PROFILE_SESSIONS = {'prof1': mock.Mock(), 'prof2': mock.Mock()}
ACC_NAMES = {'prof1': '123456789012', 'prof2': '098765432109'}
REGS = {'us-east-1', 'eu-west-1'}
BUCKETS = ['foo', 'bar', 'baz', 'foobar']


class BACCommandsTest(unittest.TestCase):
    @mock.patch('bac.bac_completer.CacheProvider')
    def setUp(self, cache_provider):

        fake_cp = mock.Mock()
        fake_cp.get_cached_resource.return_value = transform(BUCKETS)
        cache_provider.return_value = fake_cp