Ejemplo n.º 1
0
 def setUp(self):
     self.client = None
     self.files = FileCreator()
     # List of local filenames.
     self.filenames = []
     self.root = self.files.rootdir
     self.bucket = 'bucket/'
     filename_1 = self.files.create_file('foo.txt',
                                         contents='foo.txt contents')
     self.filenames.append(filename_1)
     nested_dir = os.path.join(self.root, 'realfiles')
     os.mkdir(nested_dir)
     filename_2 = self.files.create_file(os.path.join(
         nested_dir, 'bar.txt'),
                                         contents='bar.txt contents')
     self.filenames.append(filename_2)
     # Names of symlinks.
     self.symlinks = []
     # Names of files if symlinks are followed.
     self.symlink_files = []
     # Create symlink to file foo.txt.
     symlink_1 = os.path.join(self.root, 'symlink_1')
     os.symlink(filename_1, symlink_1)
     self.symlinks.append(symlink_1)
     self.symlink_files.append(symlink_1)
     # Create a symlink to a file that does not exist.
     symlink_2 = os.path.join(self.root, 'symlink_2')
     os.symlink('non-existent-file', symlink_2)
     self.symlinks.append(symlink_2)
     # Create a symlink to directory realfiles
     symlink_3 = os.path.join(self.root, 'symlink_3')
     os.symlink(nested_dir, symlink_3)
     self.symlinks.append(symlink_3)
     self.symlink_files.append(os.path.join(symlink_3, 'bar.txt'))
Ejemplo n.º 2
0
    def setUp(self):
        super(TestCodeArtifactLogin, self).setUp()

        self.file_creator = FileCreator()
        self.test_pypi_rc_path = self.file_creator.full_path('pypirc')
        if not os.path.isdir(os.path.dirname(self.test_pypi_rc_path)):
            os.makedirs(os.path.dirname(self.test_pypi_rc_path))

        self.domain = 'domain'
        self.domain_owner = 'domain-owner'
        self.repository = 'repository'
        self.auth_token = 'auth-token'
        self.namespace = 'namespace'
        self.duration = 3600
        self.expiration = time.time() + self.duration
        self.expiration_as_datetime = parse_timestamp(self.expiration)

        self.pypi_rc_path_patch = mock.patch(
            'awscli.customizations.codeartifact.login.TwineLogin'
            '.get_pypi_rc_path'
        )
        self.pypi_rc_path_mock = self.pypi_rc_path_patch.start()
        self.pypi_rc_path_mock.return_value = self.test_pypi_rc_path

        self.subprocess_patch = mock.patch('subprocess.check_call')
        self.subprocess_mock = self.subprocess_patch.start()
Ejemplo n.º 3
0
class TestStreamingOutput(BaseAWSCommandParamsTest):
    def setUp(self):
        super(TestStreamingOutput, self).setUp()
        self.files = FileCreator()

    def tearDown(self):
        super(TestStreamingOutput, self).tearDown()
        self.files.remove_all()

    def test_get_media_streaming_output(self):
        cmdline = ('kinesis-video-media get-media --stream-name test-stream '
                   '--start-selector StartSelectorType=EARLIEST %s')
        self.parsed_response = {
            'ContentType': 'video/webm',
            'Payload': six.BytesIO(b'testbody')
        }
        outpath = self.files.full_path('outfile')
        params = {
            'StartSelector': {
                'StartSelectorType': 'EARLIEST'
            },
            'StreamName': 'test-stream'
        }
        self.assert_params_for_cmd(cmdline % outpath, params)
        with open(outpath, 'rb') as outfile:
            self.assertEqual(outfile.read(), b'testbody')
Ejemplo n.º 4
0
    def setUp(self):
        super(TestCreateFunction, self).setUp()

        # Make a temporary file
        self.files = FileCreator()
        self.contents_of_file = 'myzipcontents'
        self.temp_file = self.files.create_file('foo', self.contents_of_file)
Ejemplo n.º 5
0
 def setUp(self):
     super(TestAddModel, self).setUp()
     self.files = FileCreator()
     self.customer_data_root = self.files.rootdir
     self.data_loader = self.driver.session.get_component('data_loader')
     self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root
     self.service_definition = {
         "version": "2.0",
         "metadata": {
             "apiVersion": '2015-12-02',
             "endpointPrefix": 'myservice',
         },
         "operations": {},
         "shapes": {}
     }
     self.service_unicode_definition = {
         "version": "2.0",
         "metadata": {
             "apiVersion": '2015-12-02',
             "endpointPrefix": 'myservice',
             "keyWithUnicode": u'\u2713'
         },
         "operations": {},
         "shapes": {}
     }
Ejemplo n.º 6
0
    def setUp(self):
        self.session = mock.Mock(Session)

        self.output_stream_factory = mock.Mock(OutputStreamFactory)

        # MagicMock is needed because it can handle context managers.
        # Normal Mock will throw AttributeErrors
        output_stream_context = mock.MagicMock()
        self.output_stream = mock.Mock()
        output_stream_context.__enter__.return_value = self.output_stream

        self.output_stream_factory.get_output_stream.return_value = \
            output_stream_context

        self.db_reader = mock.Mock(DatabaseRecordReader)
        self.db_reader.iter_latest_records.return_value = []
        self.db_reader.iter_records.return_value = []

        self.show_cmd = ShowCommand(self.session, self.db_reader,
                                    self.output_stream_factory)

        self.formatter = mock.Mock(Formatter)
        self.add_formatter('mock', self.formatter)

        self.parsed_args = argparse.Namespace()
        self.parsed_args.format = 'mock'
        self.parsed_args.include = None
        self.parsed_args.exclude = None

        self.parsed_globals = argparse.Namespace()
        self.parsed_globals.color = 'auto'

        self.files = FileCreator()
Ejemplo n.º 7
0
    def setUp(self):
        self.file_creator = FileCreator()
        self.test_pypi_rc_path = self.file_creator.full_path('pypirc')
        if not os.path.isdir(os.path.dirname(self.test_pypi_rc_path)):
            os.makedirs(os.path.dirname(self.test_pypi_rc_path))

        self.domain = 'domain'
        self.domain_owner = 'domain-owner'
        self.repository = 'repository'
        self.auth_token = 'auth-token'
        self.namespace = 'namespace'
        self.nuget_index_url_fmt = '{endpoint}v3/index.json'
        self.nuget_source_name = self.domain + '/' + self.repository
        self.duration = 3600
        self.expiration = time.time() + self.duration
        self.expiration_as_datetime = parse_timestamp(self.expiration)

        self.pypi_rc_path_patch = mock.patch(
            'awscli.customizations.codeartifact.login.TwineLogin'
            '.get_pypi_rc_path')
        self.pypi_rc_path_mock = self.pypi_rc_path_patch.start()
        self.pypi_rc_path_mock.return_value = self.test_pypi_rc_path

        self.subprocess_patch = mock.patch('subprocess.check_call')
        self.subprocess_mock = self.subprocess_patch.start()
        self.subprocess_check_output_patch = mock.patch(
            'subprocess.check_output')
        self.subprocess_check_out_mock = \
            self.subprocess_check_output_patch.start()
        self.cli_runner = CLIRunner()
Ejemplo n.º 8
0
class TestTopicTagDB(unittest.TestCase):
    def setUp(self):
        self.topic_tag_db = TopicTagDB()
        self.file_creator = FileCreator()

    def tearDown(self):
        self.file_creator.remove_all()
Ejemplo n.º 9
0
class TestArgumentHelpers(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_only_validates_filename_when_set(self):
        resolve_given_outfile_path(None)

    def test_works_with_valid_filename(self):
        filename = self.files.create_file("valid", "")
        self.assertEquals(filename, resolve_given_outfile_path(filename))

    def test_works_with_relative_filename(self):
        filename = "../valid"
        self.assertEquals(filename, resolve_given_outfile_path(filename))

    def test_raises_when_cannot_write_to_file(self):
        filename = os.sep.join(["_path", "not", "_exist_", "file.xyz"])
        with self.assertRaises(ValueError):
            resolve_given_outfile_path(filename)

    def test_checks_if_valid_result(self):
        result = {"ResponseMetadata": {"HTTPStatusCode": 200}}
        self.assertTrue(is_parsed_result_successful(result))

    def test_checks_if_invalid_result(self):
        result = {"ResponseMetadata": {"HTTPStatusCode": 300}}
        self.assertFalse(is_parsed_result_successful(result))
Ejemplo n.º 10
0
class BaseSSOTest(BaseAWSCommandParamsTest):
    def setUp(self):
        super(BaseSSOTest, self).setUp()
        self.files = FileCreator()
        self.start_url = 'https://mysigin.com'
        self.sso_region = 'us-west-2'
        self.account = '012345678912'
        self.role_name = 'SSORole'
        self.config_file = self.files.full_path('config')
        self.environ['AWS_CONFIG_FILE'] = self.config_file
        self.set_config_file_content()
        self.access_token = 'foo.token.string'

    def tearDown(self):
        super(BaseSSOTest, self).tearDown()
        self.files.remove_all()

    def set_config_file_content(self, content=None):
        if content is None:
            content = ('[default]\n'
                       'sso_start_url=%s\n'
                       'sso_region=%s\n'
                       'sso_role_name=%s\n'
                       'sso_account_id=%s\n' %
                       (self.start_url, self.sso_region, self.role_name,
                        self.account))
        self.files.create_file(self.config_file, content)
        # We need to recreate the driver (which includes its session) in order
        # for the config changes to be pulled in by the session.
        self.driver = create_clidriver()
Ejemplo n.º 11
0
    def setUp(self):
        self.file_creator = FileCreator()
        self.domain = 'domain'
        self.domain_owner = 'domain-owner'
        self.package_format = 'pip'
        self.repository = 'repository'
        self.auth_token = 'auth-token'
        self.expiration = (datetime.now(tzlocal()) + relativedelta(years=1) +
                           relativedelta(months=9)).replace(microsecond=0)
        self.endpoint = 'https://{domain}-{domainOwner}.codeartifact.aws.' \
            'a2z.com/{format}/{repository}/'.format(
                domain=self.domain,
                domainOwner=self.domain_owner,
                format=self.package_format,
                repository=self.repository
            )
        self.default_pypi_rc = self.DEFAULT_PYPI_RC_FMT.format(
            repository_endpoint=self.endpoint, auth_token=self.auth_token)
        self.subprocess_utils = mock.Mock()
        self.test_pypi_rc_path = self.file_creator.full_path('pypirc')
        if not os.path.isdir(os.path.dirname(self.test_pypi_rc_path)):
            os.makedirs(os.path.dirname(self.test_pypi_rc_path))

        self.test_subject = TwineLogin(self.auth_token, self.expiration,
                                       self.endpoint, self.subprocess_utils,
                                       self.test_pypi_rc_path)
Ejemplo n.º 12
0
class TestSyncCommand(BaseAWSCommandParamsTest):

    prefix = 's3 sync '

    def setUp(self):
        super(TestSyncCommand, self).setUp()
        self.files = FileCreator()

    def tearDown(self):
        super(TestSyncCommand, self).tearDown()
        self.files.remove_all()

    def test_website_redirect_ignore_paramfile(self):
        full_path = self.files.create_file('foo.txt', 'mycontent')
        cmdline = '%s %s s3://bucket/key.txt --website-redirect %s' % \
            (self.prefix, self.files.rootdir, 'http://someserver')
        self.parsed_responses = [{
            "CommonPrefixes": [],
            "Contents": []
        }, {
            'ETag': '"c8afdb36c52cf4727836669019e69222"'
        }]
        self.run_cmd(cmdline, expected_rc=0)

        # The only operations we should have called are ListObjects/PutObject.
        self.assertEqual(len(self.operations_called), 2,
                         self.operations_called)
        self.assertEqual(self.operations_called[0][0].name, 'ListObjects')
        self.assertEqual(self.operations_called[1][0].name, 'PutObject')
        # Make sure that the specified web address is used as opposed to the
        # contents of the web address when uploading the object
        self.assertEqual(
            self.operations_called[1][1]['website_redirect_location'],
            'http://someserver')
Ejemplo n.º 13
0
class TestAPIVersions(BaseAWSCommandParamsTest):
    def setUp(self):
        super(TestAPIVersions, self).setUp()
        self.files = FileCreator()
        # We just pick ec2 because it is a service that actually has
        # multiple api versions.
        self.service_name = 'ec2'
        self.api_version = '2014-10-01'
        config_contents = ('[default]\n'
                           'api_versions =\n'
                           '    %s = %s\n' %
                           (self.service_name, self.api_version))
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'myconfig', config_contents)
        self.driver = create_clidriver()

    def tearDown(self):
        super(TestAPIVersions, self).tearDown()
        self.files.remove_all()

    def test_command_send_correct_api_version(self):
        cmdline = 'ec2 describe-instances'
        self.run_cmd(cmdline)
        # Make sure that the correct api version is used for the client
        # by checking the version that was sent in the request.
        self.assertEqual(self.last_params['Version'], self.api_version)

    def test_command_interface_reflects_api_version(self):
        # Take an arbitrary command such as describe-nat-gateways that is not
        # in the 2014-10-01 EC2 API version and make sure its CLI command
        # interface is not available as well.
        cmdline = 'ec2 describe-nat-gateways'
        _, stderr, _ = self.run_cmd(cmdline, expected_rc=2)
        self.assertIn("Invalid choice: 'describe-nat-gateways'", stderr)
Ejemplo n.º 14
0
class TestTopicTagDB(unittest.TestCase):
    def setUp(self):
        self.topic_tag_db = TopicTagDB()
        self.file_creator = FileCreator()

    def tearDown(self):
        self.file_creator.remove_all()
Ejemplo n.º 15
0
 def setUp(self):
     super(TestImortTerminology, self).setUp()
     self.files = FileCreator()
     self.temp_file = self.files.create_file(
         'foo', 'mycontents')
     with open(self.temp_file, 'rb') as f:
         self.temp_file_contents = f.read()
Ejemplo n.º 16
0
class TestHelpCommandBase(unittest.TestCase):
    def setUp(self):
        self.session = mock.Mock()
        self.file_creator = FileCreator()

    def tearDown(self):
        self.file_creator.remove_all()
Ejemplo n.º 17
0
class TestArgsResolution(BaseAWSCommandParamsTest):

    def setUp(self):
        super(TestArgsResolution, self).setUp()
        self.files = FileCreator()
        config_contents = (
            '[profile bar]\n'
            'region = us-west-2\n'
        )
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'myconfig', config_contents)
        self.driver = create_clidriver()

    def tearDown(self):
        super(TestArgsResolution, self).tearDown()
        self.files.remove_all()

    def test_profile_resolution_order(self):
        self.environ['AWS_PROFILE'] = 'foo'
        self.parsed_responses = [{"Reservations": []}]
        self.run_cmd('--profile bar ec2 describe-instances', expected_rc=0)
        self.assertEqual(self.driver.session.profile, 'bar')

    def test_can_get_version_with_non_existent_profile(self):
        self.environ['AWS_PROFILE'] = 'foo'
        # ProfileNotFound exception shouldn't be raised
        self.run_cmd('--version', expected_rc=0)
Ejemplo n.º 18
0
 def setUp(self):
     super(TestDeployCommand, self).setUp()
     # setup required values
     files = FileCreator()
     self.task_def_file = files.create_file(
         'taskDef.json', json.dumps(self.TASK_DEFINITION_JSON), mode='w')
     self.appspec_file = files.create_file(
         'appspec.yaml', self.YAML_APPSPEC, mode='w')
     self.appspec_file_json = files.create_file(
         'appspec.json', self.JSON_APPSPEC, mode='w')
     self.service_name = 'serviceTest'
     self.service_arn = 'arn:aws:ecs:::service/serviceTest'
     # setup default optional values
     self.cluster_name = 'default'
     self.cluster_arn = 'arn:aws:ecs:::cluster/default'
     self.application_name = get_app_name(
         self.service_name, self.cluster_name, None)
     self.deployment_group_name = get_deploy_group_name(
         self.service_name, self.cluster_name, None)
     # setup test response resources
     self.missing_properties_appspec = files.create_file(
         'appspec_bad.yaml', self.BAD_APPSPEC, mode='w')
     self.task_definition_arn = \
         'arn:aws:ecs::1234567890:task-definition\\test:2'
     self.deployment_id = 'd-1234567XX'
     self.mock_deployer = CodeDeployer(None, self.APPSPEC_DICT)
     self.mock_deployer.update_task_def_arn(self.task_definition_arn)
     self.expected_stdout = ("Successfully registered new ECS task "
                             "definition " + self.task_definition_arn + "\n"
                             "Successfully created deployment " +
                             self.deployment_id + "\n"
                             "Waiting for " + self.deployment_id +
                             " to succeed...\nSuccessfully deployed "
                             + self.task_definition_arn + " to service '"
                             + self.service_name + "'\n")
Ejemplo n.º 19
0
    def test_make_zip(self):
        test_file_creator = FileCreator()
        test_file_creator.append_file('index.js',
                                      'exports handler = (event, context, callback) => {callback(null, event);}')

        dirname = test_file_creator.rootdir

        expected_files = set(['index.js'])

        random_name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
        outfile = os.path.join(tempfile.gettempdir(), random_name)

        zipfile_name = None
        try:
            zipfile_name = make_zip(outfile, dirname)

            test_zip_file = zipfile.ZipFile(zipfile_name, "r")
            with closing(test_zip_file) as zf:
                files_in_zip = set()
                for info in zf.infolist():
                    files_in_zip.add(info.filename)

                self.assertEquals(files_in_zip, expected_files)

        finally:
            if zipfile_name:
                os.remove(zipfile_name)
            test_file_creator.remove_all()
Ejemplo n.º 20
0
class TestHelpCommandBase(unittest.TestCase):
    def setUp(self):
        self.session = mock.Mock()
        self.file_creator = FileCreator()

    def tearDown(self):
        self.file_creator.remove_all()
Ejemplo n.º 21
0
class TestArgumentHelpers(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_only_validates_filename_when_set(self):
        resolve_given_outfile_path(None)

    def test_works_with_valid_filename(self):
        filename = self.files.create_file('valid', '')
        self.assertEqual(filename, resolve_given_outfile_path(filename))

    def test_works_with_relative_filename(self):
        filename = '../valid'
        self.assertEqual(filename, resolve_given_outfile_path(filename))

    def test_raises_when_cannot_write_to_file(self):
        filename = os.sep.join(['_path', 'not', '_exist_', 'file.xyz'])
        with self.assertRaises(ValueError):
            resolve_given_outfile_path(filename)

    def test_checks_if_valid_result(self):
        result = {'ResponseMetadata': {'HTTPStatusCode': 200}}
        self.assertTrue(is_parsed_result_successful(result))

    def test_checks_if_invalid_result(self):
        result = {'ResponseMetadata': {'HTTPStatusCode': 300}}
        self.assertFalse(is_parsed_result_successful(result))
Ejemplo n.º 22
0
class TestStreamingOutput(BaseAWSCommandParamsTest):

    def setUp(self):
        super(TestStreamingOutput, self).setUp()
        self.files = FileCreator()

    def tearDown(self):
        super(TestStreamingOutput, self).tearDown()
        self.files.remove_all()

    def test_get_media_streaming_output(self):
        cmdline = (
            'kinesis-video-media get-media --stream-name test-stream '
            '--start-selector StartSelectorType=EARLIEST %s'
        )
        self.parsed_response = {
            'ContentType': 'video/webm',
            'Payload': six.BytesIO(b'testbody')
        }
        outpath = self.files.full_path('outfile')
        params = {
            'StartSelector': {'StartSelectorType': 'EARLIEST'},
            'StreamName': 'test-stream'
        }
        self.assert_params_for_cmd(cmdline % outpath, params)
        with open(outpath, 'rb') as outfile:
            self.assertEqual(outfile.read(), b'testbody')
Ejemplo n.º 23
0
class TestAPIVersions(BaseAWSCommandParamsTest):
    def setUp(self):
        super(TestAPIVersions, self).setUp()
        self.files = FileCreator()
        # We just pick ec2 because it is a service that actually has
        # multiple api versions.
        self.service_name = 'ec2'
        self.api_version = '2014-10-01'
        config_contents = (
            '[default]\n'
            'api_versions =\n'
            '    %s = %s\n' % (self.service_name, self.api_version)
        )
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'myconfig', config_contents)
        self.driver = create_clidriver()

    def tearDown(self):
        super(TestAPIVersions, self).tearDown()
        self.files.remove_all()

    def test_command_send_correct_api_version(self):
        cmdline = 'ec2 describe-instances'
        self.run_cmd(cmdline)
        # Make sure that the correct api version is used for the client
        # by checking the version that was sent in the request.
        self.assertEqual(self.last_params['Version'], self.api_version)

    def test_command_interface_reflects_api_version(self):
        # Take an arbitrary command such as describe-nat-gateways that is not
        # in the 2014-10-01 EC2 API version and make sure its CLI command
        # interface is not available as well.
        cmdline = 'ec2 describe-nat-gateways'
        _, stderr, _ = self.run_cmd(cmdline, expected_rc=2)
        self.assertIn("Invalid choice: 'describe-nat-gateways'", stderr)
Ejemplo n.º 24
0
class TestGetGameSessionLogCommand(unittest.TestCase):
    def setUp(self):
        self.create_client_patch = mock.patch(
            'botocore.session.Session.create_client')
        self.mock_create_client = self.create_client_patch.start()
        self.session = get_session()

        self.client = mock.Mock()
        self.mock_create_client.return_value = self.client

        self.cmd = GetGameSessionLogCommand(self.session)

        self.contents = b'mycontents'
        self.file_creator = FileCreator()
        self.urlopen_patch = mock.patch(
            'awscli.customizations.gamelift.getlog.urlopen')
        self.urlopen_mock = self.urlopen_patch.start()
        self.urlopen_mock.return_value = six.BytesIO(self.contents)

    def tearDown(self):
        self.create_client_patch.stop()
        self.file_creator.remove_all()
        self.urlopen_patch.stop()

    def test_get_game_session_log(self):
        session_id = 'mysessionid'
        save_as = os.path.join(self.file_creator.rootdir, 'mylog')

        args = ['--game-session-id', session_id, '--save-as', save_as]
        global_args = Namespace()
        global_args.region = 'us-west-2'
        global_args.endpoint_url = None
        global_args.verify_ssl = None

        presigned_url = 'mypresignedurl'
        self.client.get_game_session_log_url.return_value = {
            'PreSignedUrl': presigned_url
        }

        # Call the command
        self.cmd(args, global_args)

        # Ensure the client was created properly
        self.mock_create_client.assert_called_once_with(
            'gamelift',
            region_name='us-west-2',
            endpoint_url=None,
            verify=None)

        # Ensure the client was called correctly
        self.client.get_game_session_log_url.assert_called_once_with(
            GameSessionId=session_id)

        # Ensure the presigned url was used
        self.urlopen_mock.assert_called_once_with(presigned_url)

        # Ensure the contents were saved to the file
        with open(save_as, 'rb') as f:
            self.assertEqual(f.read(), self.contents)
Ejemplo n.º 25
0
class BaseCLIInputArgumentTest(BaseAWSCommandParamsTest):
    def setUp(self):
        super(BaseCLIInputArgumentTest, self).setUp()
        self.files = FileCreator()

    def tearDown(self):
        super(BaseCLIInputArgumentTest, self).tearDown()
        self.files.remove_all()
Ejemplo n.º 26
0
class BaseHistoryCommandParamsTest(BaseAWSCommandParamsTest):
    def setUp(self):
        history_recorder = self._make_clean_history_recorder()
        super(BaseHistoryCommandParamsTest, self).setUp()
        self.history_recorder = history_recorder
        self.files = FileCreator()
        config_contents = ('[default]\n' 'cli_history = enabled')
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'config', config_contents)
        self.environ['AWS_CLI_HISTORY_FILE'] = self.files.create_file(
            'history.db', '')
        self.driver = create_clidriver()
        # The run_cmd patches stdout with a StringIO object (similar to what
        # nose does). Therefore it will run into issues when
        # get_binary_stdout is called because it returns sys.stdout.buffer
        # for Py3 and StringIO does not have a buffer
        self.binary_stdout_patch = mock.patch('awscli.utils.get_binary_stdout')
        mock_get_binary_stdout = self.binary_stdout_patch.start()
        self.binary_stdout = BytesIO()
        mock_get_binary_stdout.return_value = self.binary_stdout

    def _make_clean_history_recorder(self):
        # This is to ensure that for each new test run the CLI is using
        # a brand new HistoryRecorder as this is global so previous test
        # runs could have injected handlers onto it as all of the tests
        # are ran in the same process.
        history_recorder = HistoryRecorder()

        # The HISTORY_RECORDER is instantiated on module import before we
        # doing any patching which means we cannot simply patch
        # botocore.get_global_history_recorder as the objects are already
        # instantiated as so we have to individually patch each one of these...
        self._apply_history_recorder_patch('awscli.clidriver',
                                           history_recorder)
        self._apply_history_recorder_patch('awscli.customizations.history',
                                           history_recorder)
        return history_recorder

    def _apply_history_recorder_patch(self, module, history_recorder):
        patch_history_recorder = mock.patch(module + '.HISTORY_RECORDER',
                                            history_recorder)
        patch_history_recorder.start()
        self.addCleanup(patch_history_recorder.stop)

    def _cleanup_db_connections(self):
        # Reaching into private data to close out the database connection.
        # Windows won't let us delete the tempdir until these connections are
        # closed in the tearDown step and we have no other way of forcing
        # them to close.
        handlers = self.history_recorder._handlers
        for handler in handlers:
            handler._writer.close()

    def tearDown(self):
        super(BaseHistoryCommandParamsTest, self).tearDown()
        self._cleanup_db_connections()
        self.files.remove_all()
        self.binary_stdout_patch.stop()
Ejemplo n.º 27
0
class TestShow(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()
        self.environ = os.environ.copy()
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'config', ('[default]\n'
                       'cli_history = enabled'))
        self.environ['AWS_DEFAULT_PROFILE'] = 'default'
        self.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
        self.environ['AWS_CLI_HISTORY_FILE'] = os.path.join(
            self.files.rootdir, 'history.db')

    def tearDown(self):
        self.files.remove_all()

    def remove_color(self, output):
        return re.compile(r'\x1b[^m]*m').sub('', output)

    def assert_contains_in_order(self, lines, contents):
        current_pos = 0
        prev_line = None
        for line in lines:
            self.assertIn(line, contents)
            new_pos = contents.find(line)
            if new_pos < current_pos:
                self.fail('Line: "%s" should have came after line: "%s"' %
                          (line, prev_line))
            prev_line = line
            current_pos = new_pos

    def test_show(self):
        # Make a call that does not require credentials just in case the
        # user was using the config file to provide credentials.
        cmd = 'sts assume-role-with-saml '
        cmd += '--role-arn  arn:aws:iam::...:invalid '
        cmd += '--principal-arn  arn:aws:iam::...:invalid  '
        cmd += '--saml-assertion fake-assertion'
        aws(cmd, env_vars=self.environ)
        # Now run the show command and make sure the general output is all
        # there.
        result = aws('history show', env_vars=self.environ)
        uncolored_content = self.remove_color(result.stdout)

        self.assert_contains_in_order([
            'AWS CLI command entered', 'with AWS CLI version: aws-cli/',
            "with arguments: ['sts', 'assume-role-with-saml',",
            '[0] API call made', 'to service: sts',
            'using operation: AssumeRoleWithSAML', 'with parameters: {',
            '    "PrincipalArn": "arn:aws:iam::...:invalid",',
            '    "RoleArn": "arn:aws:iam::...:invalid",',
            '    "SAMLAssertion": "fake-assertion"', '[0] HTTP request sent',
            'to URL: https://sts.amazonaws.com/', 'with method: POST',
            'with body: Action=AssumeRoleWithSAML&Version=2011-06-15',
            '[0] HTTP response received', 'with status code: 400',
            'with body: <?xml version="1.0" ?>', '[0] HTTP response parsed',
            'parsed to: {', '    "Error": {', 'AWS CLI command exited',
            'with return code: 255'
        ], uncolored_content)
Ejemplo n.º 28
0
class TestQueryFileArgument(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_proxies_to_super_ctor(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "bar.baz", "event", 0o600)
        self.assertEqual("foo", arg.name)
        self.assertEqual("bar.baz", arg.query)

    def test_adds_default_help_text(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "bar.baz", "event", 0o600)
        self.assertEqual(("Saves the command output contents of bar.baz " "to the given filename"), arg.documentation)

    def test_does_not_add_help_text_if_set(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "bar.baz", "event", 0o600, help_text="abc")
        self.assertEqual("abc", arg.documentation)

    def test_saves_query_to_file(self):
        outfile = self.files.create_file("not-empty-test", "")
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "baz", "event", 0o600)
        arg.add_to_params({}, outfile)
        arg.save_query({"ResponseMetadata": {"HTTPStatusCode": 200}, "baz": "abc123"})
        with open(outfile) as fp:
            self.assertEquals("abc123", fp.read())
        self.assertEquals(1, session.register.call_count)
        session.register.assert_called_with("event", arg.save_query)

    def test_does_not_save_when_not_set(self):
        session = mock.Mock()
        QueryOutFileArgument(session, "foo", "baz", "event", 0o600)
        self.assertEquals(0, session.register.call_count)

    def test_saves_query_to_file_as_empty_string_when_none_result(self):
        outfile = self.files.create_file("none-test", "")
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "baz", "event", 0o600)
        arg.add_to_params({}, outfile)
        arg.save_query({"ResponseMetadata": {"HTTPStatusCode": 200}})
        with open(outfile) as fp:
            self.assertEquals("", fp.read())

    @skip_if_windows("Test not valid on windows.")
    def test_permissions_on_created_file(self):
        outfile = self.files.create_file("not-empty-test", "")
        session = mock.Mock()
        arg = QueryOutFileArgument(session, "foo", "baz", "event", 0o600)
        arg.add_to_params({}, outfile)
        arg.save_query({"ResponseMetadata": {"HTTPStatusCode": 200}, "baz": "abc123"})
        with open(outfile) as fp:
            fp.read()
        self.assertEqual(os.stat(outfile).st_mode & 0xFFF, 0o600)
Ejemplo n.º 29
0
 def setUp(self):
     super(TestPlugins, self).setUp()
     self.files = FileCreator()
     self.plugins_site_packages = os.path.join(self.files.rootdir,
                                               'site-packages')
     self.plugin_module_name = 'add_awscli_cmd_plugin'
     self.plugin_filename = os.path.join(os.path.dirname(__file__),
                                         self.plugin_module_name) + '.py'
     self.setup_plugin_site_packages()
Ejemplo n.º 30
0
class TestQueryFileArgument(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_proxies_to_super_ctor(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event')
        self.assertEqual('foo', arg.name)
        self.assertEqual('bar.baz', arg.query)

    def test_adds_default_help_text(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event')
        self.assertEqual(('Saves the command output contents of bar.baz '
                          'to the given filename'), arg.documentation)

    def test_does_not_add_help_text_if_set(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session,
                                   'foo',
                                   'bar.baz',
                                   'event',
                                   help_text='abc')
        self.assertEqual('abc', arg.documentation)

    def test_saves_query_to_file(self):
        outfile = self.files.create_file('not-empty-test', '')
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'baz', 'event')
        arg.add_to_params({}, outfile)
        arg.save_query({
            'ResponseMetadata': {
                'HTTPStatusCode': 200
            },
            'baz': 'abc123'
        })
        with open(outfile) as fp:
            self.assertEquals('abc123', fp.read())
        self.assertEquals(1, session.register.call_count)
        session.register.assert_called_with('event', arg.save_query)

    def test_does_not_save_when_not_set(self):
        session = mock.Mock()
        QueryOutFileArgument(session, 'foo', 'baz', 'event')
        self.assertEquals(0, session.register.call_count)

    def test_saves_query_to_file_as_empty_string_when_none_result(self):
        outfile = self.files.create_file('none-test', '')
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'baz', 'event')
        arg.add_to_params({}, outfile)
        arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}})
        with open(outfile) as fp:
            self.assertEquals('', fp.read())
Ejemplo n.º 31
0
 def setUp(self):
     super(BaseLambdaTests, self).setUp()
     self.files = FileCreator()
     self.temp_file = self.files.create_file('foo', 'mycontents')
     self.zip_file = os.path.join(self.files.rootdir, 'foo.zip')
     with closing(zipfile.ZipFile(self.zip_file, 'w')) as f:
         f.write(self.temp_file)
     with open(self.zip_file, 'rb') as f:
         self.zip_file_contents = f.read()
Ejemplo n.º 32
0
 def setUp(self):
     super(TestGetGameSessionLog, self).setUp()
     self.files = FileCreator()
     self.filename = os.path.join(self.files.rootdir, 'myfile')
     self.urlopen_patch = mock.patch(
         'awscli.customizations.gamelift.getlog.urlopen')
     self.contents = b'My Contents'
     self.urlopen_mock = self.urlopen_patch.start()
     self.urlopen_mock.return_value = six.BytesIO(self.contents)
Ejemplo n.º 33
0
class TestAliasCommandInjector(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()
        self.alias_file = self.files.create_file("alias", "[toplevel]\n")
        self.alias_loader = AliasLoader(self.alias_file)
        self.session = mock.Mock(spec=Session)
        self.alias_cmd_injector = AliasCommandInjector(self.session, self.alias_loader)
        self.command_table = {}
        self.parser = MainArgParser(
            command_table=self.command_table, version_string="version", description="description", argument_table={}
        )

    def tearDown(self):
        self.files.remove_all()

    def test_service_alias_command(self):
        with open(self.alias_file, "a+") as f:
            f.write("my-alias = my-alias-value\n")

        self.alias_cmd_injector.inject_aliases(self.command_table, self.parser)
        self.assertIn("my-alias", self.command_table)
        self.assertIsInstance(self.command_table["my-alias"], ServiceAliasCommand)

    def test_external_alias_command(self):
        with open(self.alias_file, "a+") as f:
            f.write("my-alias = !my-alias-value\n")

        self.alias_cmd_injector.inject_aliases(self.command_table, self.parser)
        self.assertIn("my-alias", self.command_table)
        self.assertIsInstance(self.command_table["my-alias"], ExternalAliasCommand)

    def test_clobbers_builtins(self):
        builtin_cmd = mock.Mock(spec=CLICommand)
        self.command_table["builtin"] = builtin_cmd

        with open(self.alias_file, "a+") as f:
            f.write("builtin = my-alias-value\n")

        self.alias_cmd_injector.inject_aliases(self.command_table, self.parser)
        self.assertIn("builtin", self.command_table)
        self.assertIsInstance(self.command_table["builtin"], ServiceAliasCommand)

    def test_shadow_proxy_command(self):
        builtin_cmd = mock.Mock(spec=CLICommand)
        builtin_cmd.name = "builtin"
        self.command_table["builtin"] = builtin_cmd

        with open(self.alias_file, "a+") as f:
            f.write("builtin = builtin\n")

        self.alias_cmd_injector.inject_aliases(self.command_table, self.parser)

        self.command_table["builtin"]([], FakeParsedArgs(command="builtin"))
        # The builtin command should be passed to the alias
        # command when added to the table.
        builtin_cmd.assert_called_with([], FakeParsedArgs(command="builtin"))
Ejemplo n.º 34
0
 def setUp(self):
     super(TestCLITimestampParser, self).setUp()
     self.files = FileCreator()
     self.wire_response = json.dumps({
         'builds': [{
             'startTime': 0,
         }]
     }).encode('utf-8')
     self.command = ['codebuild', 'batch-get-builds', '--ids', 'foo']
     self.patch_send(content=self.wire_response)
Ejemplo n.º 35
0
 def setUp(self):
     self.files = FileCreator()
     self.environ = os.environ.copy()
     self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
         'config', ('[default]\n'
                    'cli_history = enabled'))
     self.environ['AWS_DEFAULT_PROFILE'] = 'default'
     self.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
     self.environ['AWS_CLI_HISTORY_FILE'] = os.path.join(
         self.files.rootdir, 'history.db')
Ejemplo n.º 36
0
 def setUp(self):
     super(TestArgsResolution, self).setUp()
     self.files = FileCreator()
     config_contents = (
         '[profile bar]\n'
         'region = us-west-2\n'
     )
     self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
         'myconfig', config_contents)
     self.driver = create_clidriver()
Ejemplo n.º 37
0
class TestIgnoreFilesLocally(unittest.TestCase):
    """
    This class tests the ability to ignore particular files.  This includes
    skipping symlink when desired.
    """
    def setUp(self):
        self.session = FakeSession()
        self.service = self.session.get_service('s3')
        self.endpoint = self.service.get_endpoint('us-east-1')
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_bad_symlink(self):
        path = os.path.join(self.files.rootdir, 'badsymlink')
        os.symlink('non-existent-file', path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertFalse(filegenerator.should_ignore_file(path))

    def test_skip_symlink(self):
        filename = 'foo.txt'
        self.files.create_file(os.path.join(self.files.rootdir,
                               filename),
                               contents='foo.txt contents')
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(filename, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', False)
        self.assertTrue(filegenerator.should_ignore_file(sym_path))

    def test_no_skip_symlink(self):
        filename = 'foo.txt'
        path = self.files.create_file(os.path.join(self.files.rootdir,
                                                   filename),
                                      contents='foo.txt contents')
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(path, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertFalse(filegenerator.should_ignore_file(sym_path))
        self.assertFalse(filegenerator.should_ignore_file(path))

    def test_no_skip_symlink_dir(self):
        filename = 'dir'
        path = os.path.join(self.files.rootdir, 'dir/')
        os.mkdir(path)
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(path, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertFalse(filegenerator.should_ignore_file(sym_path))
        self.assertFalse(filegenerator.should_ignore_file(path))
Ejemplo n.º 38
0
 def setUp(self):
     super(BaseSSOTest, self).setUp()
     self.files = FileCreator()
     self.start_url = 'https://mysigin.com'
     self.sso_region = 'us-west-2'
     self.account = '012345678912'
     self.role_name = 'SSORole'
     self.config_file = self.files.full_path('config')
     self.environ['AWS_CONFIG_FILE'] = self.config_file
     self.set_config_file_content()
     self.access_token = 'foo.token.string'
Ejemplo n.º 39
0
class TestPlugins(BaseCLIDriverTest):
    def setUp(self):
        super(TestPlugins, self).setUp()
        self.files = FileCreator()
        self.plugins_site_packages = os.path.join(self.files.rootdir,
                                                  'site-packages')
        self.plugin_module_name = 'add_awscli_cmd_plugin'
        self.plugin_filename = os.path.join(os.path.dirname(__file__),
                                            self.plugin_module_name) + '.py'
        self.setup_plugin_site_packages()

    def setup_plugin_site_packages(self):
        os.makedirs(self.plugins_site_packages)
        shutil.copy(self.plugin_filename, self.plugins_site_packages)

    def tearDown(self):
        super(TestPlugins, self).tearDown()
        self.files.remove_all()

    def assert_plugin_loaded(self, clidriver):
        self.assertIn('plugin-test-cmd', clidriver.subcommand_table)

    def assert_plugin_not_loaded(self, clidriver):
        self.assertNotIn('plugin-test-cmd', clidriver.subcommand_table)

    def create_config(self, config_contents):
        config_file = self.files.create_file('config', config_contents)
        self.environ['AWS_CONFIG_FILE'] = config_file

    def test_plugins_loaded_from_specified_path(self):
        self.create_config(
            '[plugins]\n'
            'cli_legacy_plugin_path = %s\n'
            'myplugin = %s\n' %
            (self.plugins_site_packages, self.plugin_module_name))
        clidriver = create_clidriver()
        self.assert_plugin_loaded(clidriver)

    def test_plugins_are_not_loaded_when_path_specified(self):
        self.create_config('[plugins]\n'
                           'myplugin = %s\n' % self.plugin_module_name)
        clidriver = create_clidriver()
        self.assert_plugin_not_loaded(clidriver)

    def test_looks_in_all_specified_paths(self):
        nonexistent_dir = os.path.join(self.files.rootdir, 'no-exist')
        plugin_path = os.pathsep.join(
            [nonexistent_dir, self.plugins_site_packages])
        self.create_config('[plugins]\n'
                           'cli_legacy_plugin_path = %s\n'
                           'myplugin = %s\n' %
                           (plugin_path, self.plugin_module_name))
        clidriver = create_clidriver()
        self.assert_plugin_loaded(clidriver)
Ejemplo n.º 40
0
class TestIgnoreFilesLocally(unittest.TestCase):
    """
    This class tests the ability to ignore particular files.  This includes
    skipping symlink when desired.
    """
    def setUp(self):
        self.session = FakeSession()
        self.service = self.session.get_service('s3')
        self.endpoint = self.service.get_endpoint('us-east-1')
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_warning(self):
        path = os.path.join(self.files.rootdir, 'badsymlink')
        os.symlink('non-existent-file', path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertTrue(filegenerator.should_ignore_file(path))

    def test_skip_symlink(self):
        filename = 'foo.txt'
        self.files.create_file(os.path.join(self.files.rootdir,
                               filename),
                               contents='foo.txt contents')
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(filename, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', False)
        self.assertTrue(filegenerator.should_ignore_file(sym_path))

    def test_no_skip_symlink(self):
        filename = 'foo.txt'
        path = self.files.create_file(os.path.join(self.files.rootdir,
                                                   filename),
                                      contents='foo.txt contents')
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(path, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertFalse(filegenerator.should_ignore_file(sym_path))
        self.assertFalse(filegenerator.should_ignore_file(path))

    def test_no_skip_symlink_dir(self):
        filename = 'dir'
        path = os.path.join(self.files.rootdir, 'dir/')
        os.mkdir(path)
        sym_path = os.path.join(self.files.rootdir, 'symlink')
        os.symlink(path, sym_path)
        filegenerator = FileGenerator(self.service, self.endpoint,
                                      '', True)
        self.assertFalse(filegenerator.should_ignore_file(sym_path))
        self.assertFalse(filegenerator.should_ignore_file(path))
Ejemplo n.º 41
0
class BaseRekognitionTest(BaseAWSCommandParamsTest):
    def setUp(self):
        super(BaseRekognitionTest, self).setUp()
        self.files = FileCreator()
        self.temp_file = self.files.create_file(
            'foo', 'mycontents')
        with open(self.temp_file, 'rb') as f:
            self.temp_file_bytes = f.read()

    def tearDown(self):
        super(BaseRekognitionTest, self).tearDown()
        self.files.remove_all()
Ejemplo n.º 42
0
class TestGetGameSessionLogCommand(unittest.TestCase):
    def setUp(self):
        self.create_client_patch = mock.patch("botocore.session.Session.create_client")
        self.mock_create_client = self.create_client_patch.start()
        self.session = get_session()

        self.client = mock.Mock()
        self.mock_create_client.return_value = self.client

        self.cmd = GetGameSessionLogCommand(self.session)

        self.contents = b"mycontents"
        self.file_creator = FileCreator()
        self.urlopen_patch = mock.patch("awscli.customizations.gamelift.getlog.urlopen")
        self.urlopen_mock = self.urlopen_patch.start()
        self.urlopen_mock.return_value = six.BytesIO(self.contents)

    def tearDown(self):
        self.create_client_patch.stop()
        self.file_creator.remove_all()
        self.urlopen_patch.stop()

    def test_get_game_session_log(self):
        session_id = "mysessionid"
        save_as = os.path.join(self.file_creator.rootdir, "mylog")

        args = ["--game-session-id", session_id, "--save-as", save_as]
        global_args = Namespace()
        global_args.region = "us-west-2"
        global_args.endpoint_url = None
        global_args.verify_ssl = None

        presigned_url = "mypresignedurl"
        self.client.get_game_session_log_url.return_value = {"PreSignedUrl": presigned_url}

        # Call the command
        self.cmd(args, global_args)

        # Ensure the client was created properly
        self.mock_create_client.assert_called_once_with(
            "gamelift", region_name="us-west-2", endpoint_url=None, verify=None
        )

        # Ensure the client was called correctly
        self.client.get_game_session_log_url.assert_called_once_with(GameSessionId=session_id)

        # Ensure the presigned url was used
        self.urlopen_mock.assert_called_once_with(presigned_url)

        # Ensure the contents were saved to the file
        with open(save_as, "rb") as f:
            self.assertEqual(f.read(), self.contents)
Ejemplo n.º 43
0
class TestCreateFunction(BaseAWSCommandParamsTest):

    prefix = 'lambda create-function'

    def setUp(self):
        super(TestCreateFunction, self).setUp()

        # Make a temporary file
        self.files = FileCreator()
        self.contents_of_file = 'myzipcontents'
        self.temp_file = self.files.create_file(
            'foo', self.contents_of_file)

    def tearDown(self):
        super(TestCreateFunction, self).tearDown()
        self.files.remove_all()

    def test_create_function(self):
        cmdline = self.prefix
        cmdline += ' --function-name myfunction --runtime myruntime'
        cmdline += ' --role myrole --handler myhandler --zip-file myzip'
        result = {
            'FunctionName': 'myfunction',
            'Runtime': 'myruntime',
            'Role': 'myrole',
            'Handler': 'myhandler',
            'Code': {'ZipFile': 'myzip'}
        }
        self.assert_params_for_cmd(cmdline, result)

    def test_create_function_with_file(self):
        cmdline = self.prefix
        cmdline += ' --function-name myfunction --runtime myruntime'
        cmdline += ' --role myrole --handler myhandler'
        cmdline += ' --zip-file file://%s' % self.temp_file
        result = {
            'FunctionName': 'myfunction',
            'Runtime': 'myruntime',
            'Role': 'myrole',
            'Handler': 'myhandler',
            'Code': {'ZipFile': self.contents_of_file}
        }
        self.assert_params_for_cmd(cmdline, result)

    def test_create_function_code_argument_cause_error(self):
        cmdline = self.prefix
        cmdline += ' --function-name myfunction --runtime myruntime'
        cmdline += ' --role myrole --handler myhandler --zip-file myzip'
        cmdline += ' --code mycode'
        stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255)
        self.assertIn('Unknown options: --code', stderr)
Ejemplo n.º 44
0
class BaseLambdaTests(BaseAWSCommandParamsTest):
    def setUp(self):
        super(BaseLambdaTests, self).setUp()
        self.files = FileCreator()
        self.temp_file = self.files.create_file("foo", "mycontents")
        self.zip_file = os.path.join(self.files.rootdir, "foo.zip")
        with closing(zipfile.ZipFile(self.zip_file, "w")) as f:
            f.write(self.temp_file)
        with open(self.zip_file, "rb") as f:
            self.zip_file_contents = f.read()

    def tearDown(self):
        super(BaseLambdaTests, self).tearDown()
        self.files.remove_all()
Ejemplo n.º 45
0
class TestQueryFileArgument(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def test_proxies_to_super_ctor(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event')
        self.assertEqual('foo', arg.name)
        self.assertEqual('bar.baz', arg.query)

    def test_adds_default_help_text(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event')
        self.assertEqual(('Saves the command output contents of bar.baz '
                          'to the given filename'), arg.documentation)

    def test_does_not_add_help_text_if_set(self):
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event',
                                   help_text='abc')
        self.assertEqual('abc', arg.documentation)

    def test_saves_query_to_file(self):
        outfile = self.files.create_file('not-empty-test', '')
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'baz', 'event')
        arg.add_to_params({}, outfile)
        arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200},
                        'baz': 'abc123'})
        with open(outfile) as fp:
            self.assertEquals('abc123', fp.read())
        self.assertEquals(1, session.register.call_count)
        session.register.assert_called_with('event', arg.save_query)

    def test_does_not_save_when_not_set(self):
        session = mock.Mock()
        QueryOutFileArgument(session, 'foo', 'baz', 'event')
        self.assertEquals(0, session.register.call_count)

    def test_saves_query_to_file_as_empty_string_when_none_result(self):
        outfile = self.files.create_file('none-test', '')
        session = mock.Mock()
        arg = QueryOutFileArgument(session, 'foo', 'baz', 'event')
        arg.add_to_params({}, outfile)
        arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}})
        with open(outfile) as fp:
            self.assertEquals('', fp.read())
Ejemplo n.º 46
0
class TestCLITimestampParser(BaseCLIWireResponseTest):
    def setUp(self):
        super(TestCLITimestampParser, self).setUp()
        self.files = FileCreator()
        self.wire_response = json.dumps({
            'builds': [{
                'startTime': 0,
            }]
        }).encode('utf-8')
        self.command = ['codebuild', 'batch-get-builds', '--ids', 'foo']
        self.patch_send(content=self.wire_response)

    def tearDown(self):
        super(TestCLITimestampParser, self).tearDown()
        self.files.remove_all()

    def test_iso(self):
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'iso',
            '[default]\ncli_timestamp_format = iso8601\n')
        self.driver = create_clidriver()
        expected_time = datetime.datetime.fromtimestamp(0).replace(
            tzinfo=tzlocal()).isoformat()

        stdout, _, _ = self.run_cmd(self.command)
        json_response = json.loads(stdout)
        start_time = json_response["builds"][0]["startTime"]
        self.assertEqual(expected_time, start_time)

    def test_none(self):
        self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
            'none',
            '[default]\ncli_timestamp_format = none\n')
        self.driver = create_clidriver()
        expected_time = 0

        stdout, _, _ = self.run_cmd(self.command)
        json_response = json.loads(stdout)
        start_time = json_response["builds"][0]["startTime"]
        self.assertEqual(expected_time, start_time)

    def test_default(self):
        self.driver = create_clidriver()
        expected_time = 0

        stdout, _, _ = self.run_cmd(self.command)
        json_response = json.loads(stdout)
        start_time = json_response["builds"][0]["startTime"]
        self.assertEqual(expected_time, start_time)
Ejemplo n.º 47
0
 def setUp(self):
     super(BaseRekognitionTest, self).setUp()
     self.files = FileCreator()
     self.temp_file = self.files.create_file(
         'foo', 'mycontents')
     with open(self.temp_file, 'rb') as f:
         self.temp_file_bytes = f.read()
Ejemplo n.º 48
0
 def setUp(self):
     super(TestAddModel, self).setUp()
     self.files = FileCreator()
     self.customer_data_root = self.files.rootdir
     self.data_loader = self.driver.session.get_component('data_loader')
     self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root
     self.service_definition = {
         "version": "2.0",
         "metadata": {
             "apiVersion": '2015-12-02',
             "endpointPrefix": 'myservice',
         },
         "operations": {},
         "shapes": {}
     }
     self.service_unicode_definition = {
         "version": "2.0",
         "metadata": {
             "apiVersion": '2015-12-02',
             "endpointPrefix": 'myservice',
             "keyWithUnicode": u'\u2713'
         },
         "operations": {},
         "shapes": {}
     }
Ejemplo n.º 49
0
class BaseS3TransferCommandTest(BaseAWSCommandParamsTest):
    def setUp(self):
        super(BaseS3TransferCommandTest, self).setUp()
        self.files = FileCreator()

    def tearDown(self):
        super(BaseS3TransferCommandTest, self).tearDown()
        self.files.remove_all()

    def assert_operations_called(self, expected_operations_with_params):
        actual_operations_with_params = [
            (operation_called[0].name, operation_called[1])
            for operation_called in self.operations_called
        ]
        self.assertEqual(
            actual_operations_with_params, expected_operations_with_params)
Ejemplo n.º 50
0
 def setUp(self):
     self.client = None
     self.files = FileCreator()
     # List of local filenames.
     self.filenames = []
     self.root = self.files.rootdir
     self.bucket = 'bucket/'
     filename_1 = self.files.create_file('foo.txt',
                                         contents='foo.txt contents')
     self.filenames.append(filename_1)
     nested_dir = os.path.join(self.root, 'realfiles')
     os.mkdir(nested_dir)
     filename_2 = self.files.create_file(os.path.join(nested_dir,
                                                      'bar.txt'),
                                         contents='bar.txt contents')
     self.filenames.append(filename_2)
     # Names of symlinks.
     self.symlinks = []
     # Names of files if symlinks are followed.
     self.symlink_files = []
     # Create symlink to file foo.txt.
     symlink_1 = os.path.join(self.root, 'symlink_1')
     os.symlink(filename_1, symlink_1)
     self.symlinks.append(symlink_1)
     self.symlink_files.append(symlink_1)
     # Create a symlink to a file that does not exist.
     symlink_2 = os.path.join(self.root, 'symlink_2')
     os.symlink('non-existent-file', symlink_2)
     self.symlinks.append(symlink_2)
     # Create a symlink to directory realfiles
     symlink_3 = os.path.join(self.root, 'symlink_3')
     os.symlink(nested_dir, symlink_3)
     self.symlinks.append(symlink_3)
     self.symlink_files.append(os.path.join(symlink_3, 'bar.txt'))
Ejemplo n.º 51
0
    def setUp(self):
        super(TestCreateFunction, self).setUp()

        # Make a temporary file
        self.files = FileCreator()
        self.contents_of_file = 'myzipcontents'
        self.temp_file = self.files.create_file(
            'foo', self.contents_of_file)
Ejemplo n.º 52
0
class TestAddModel(BaseAWSCommandParamsTest):
    prefix = 'configure add-model'

    def setUp(self):
        super(TestAddModel, self).setUp()
        self.files = FileCreator()
        self.customer_data_root = self.files.rootdir
        self.data_loader = self.driver.session.get_component('data_loader')
        self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root
        self.service_definition = {
            "version": "2.0",
            "metadata": {
                "apiVersion": '2015-12-02',
                "endpointPrefix": 'myservice',
            },
            "operations": {},
            "shapes": {}
        }

    def tearDown(self):
        super(TestAddModel, self).tearDown()
        self.files.remove_all()

    def test_add_model(self):
        cmdline = self.prefix + ' --service-model %s' % json.dumps(
            self.service_definition, separators=(',', ':'))
        self.run_cmd(cmdline)

        # Ensure that the model exists in the correct location.
        self.assertTrue(
            os.path.exists(os.path.join(
                self.customer_data_root, 'myservice', '2015-12-02',
                'service-2.json')))

    def test_add_model_with_service_name(self):
        cmdline = self.prefix + ' --service-model %s' % json.dumps(
            self.service_definition, separators=(',', ':'))
        cmdline += ' --service-name override-name'
        self.run_cmd(cmdline)

        # Ensure that the model exists in the correct location.
        self.assertTrue(
            os.path.exists(os.path.join(
                self.customer_data_root, 'override-name', '2015-12-02',
                'service-2.json')))
Ejemplo n.º 53
0
class TestGetGameSessionLog(BaseAWSCommandParamsTest):

    prefix = 'gamelift get-game-session-log'

    def setUp(self):
        super(TestGetGameSessionLog, self).setUp()
        self.files = FileCreator()
        self.filename = os.path.join(self.files.rootdir, 'myfile')
        self.urlopen_patch = mock.patch(
            'awscli.customizations.gamelift.getlog.urlopen')
        self.contents = b'My Contents'
        self.urlopen_mock = self.urlopen_patch.start()
        self.urlopen_mock.return_value = six.BytesIO(self.contents)

    def tearDown(self):
        super(TestGetGameSessionLog, self).tearDown()
        self.files.remove_all()
        self.urlopen_patch.stop()

    def test_get_game_session_log(self):
        cmdline = self.prefix
        cmdline += ' --game-session-id mysession'
        cmdline += ' --save-as %s' % self.filename

        self.parsed_responses = [{'PreSignedUrl': 'myurl'}]
        stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0)
        self.assertEqual(len(self.operations_called), 1)
        self.assertEqual(
            self.operations_called[0][0].name, 'GetGameSessionLogUrl')
        self.assertEqual(
            self.operations_called[0][1],
            {'GameSessionId': 'mysession'}
        )

        # Ensure the contents were saved to the file
        self.assertTrue(os.path.exists(self.filename))
        with open(self.filename, 'rb') as f:
            self.assertEqual(f.read(), self.contents)

        # Ensure the output is as expected
        self.assertIn(
            'Successfully downloaded log archive for game session '
            'mysession to %s' % self.filename,
            stdout
        )
Ejemplo n.º 54
0
 def setUp(self):
     super(BaseLambdaTests, self).setUp()
     self.files = FileCreator()
     self.temp_file = self.files.create_file("foo", "mycontents")
     self.zip_file = os.path.join(self.files.rootdir, "foo.zip")
     with closing(zipfile.ZipFile(self.zip_file, "w")) as f:
         f.write(self.temp_file)
     with open(self.zip_file, "rb") as f:
         self.zip_file_contents = f.read()
Ejemplo n.º 55
0
class TestParamFile(unittest.TestCase):
    def setUp(self):
        self.files = FileCreator()

    def tearDown(self):
        self.files.remove_all()

    def get_paramfile(self, path):
        return get_paramfile(path, LOCAL_PREFIX_MAP.copy())

    def test_text_file(self):
        contents = 'This is a test'
        filename = self.files.create_file('foo', contents)
        prefixed_filename = 'file://' + filename
        data = self.get_paramfile(prefixed_filename)
        self.assertEqual(data, contents)
        self.assertIsInstance(data, six.string_types)

    def test_binary_file(self):
        contents = 'This is a test'
        filename = self.files.create_file('foo', contents)
        prefixed_filename = 'fileb://' + filename
        data = self.get_paramfile(prefixed_filename)
        self.assertEqual(data, b'This is a test')
        self.assertIsInstance(data, six.binary_type)

    @skip_if_windows('Binary content error only occurs '
                     'on non-Windows platforms.')
    def test_cannot_load_text_file(self):
        contents = b'\xbfX\xac\xbe'
        filename = self.files.create_file('foo', contents, mode='wb')
        prefixed_filename = 'file://' + filename
        with self.assertRaises(ResourceLoadingError):
            self.get_paramfile(prefixed_filename)

    def test_file_does_not_exist_raises_error(self):
        with self.assertRaises(ResourceLoadingError):
            self.get_paramfile('file://file/does/not/existsasdf.txt')

    def test_no_match_uris_returns_none(self):
        self.assertIsNone(self.get_paramfile('foobar://somewhere.bar'))

    def test_non_string_type_returns_none(self):
        self.assertIsNone(self.get_paramfile(100))
Ejemplo n.º 56
0
 def setUp(self):
     super(BaseLambdaTests, self).setUp()
     self.files = FileCreator()
     self.temp_file = self.files.create_file(
         'foo', 'mycontents')
     self.zip_file = os.path.join(self.files.rootdir, 'foo.zip')
     with closing(zipfile.ZipFile(self.zip_file, 'w')) as f:
         f.write(self.temp_file)
     with open(self.zip_file, 'rb') as f:
         self.zip_file_contents = f.read()
Ejemplo n.º 57
0
 def setUp(self):
     self.files = FileCreator()
     self.alias_file = self.files.create_file("alias", "[toplevel]\n")
     self.alias_loader = AliasLoader(self.alias_file)
     self.session = mock.Mock(spec=Session)
     self.alias_cmd_injector = AliasCommandInjector(self.session, self.alias_loader)
     self.command_table = {}
     self.parser = MainArgParser(
         command_table=self.command_table, version_string="version", description="description", argument_table={}
     )
Ejemplo n.º 58
0
 def setUp(self):
     super(TestCLITimestampParser, self).setUp()
     self.files = FileCreator()
     self.wire_response = json.dumps({
         'builds': [{
             'startTime': 0,
         }]
     }).encode('utf-8')
     self.command = ['codebuild', 'batch-get-builds', '--ids', 'foo']
     self.patch_send(content=self.wire_response)