def setUp(self): self.iam_user_arn = 'arn:aws:iam::012345678912:user/AWS/CodeDeploy/foo' self.region = 'us-east-1' self.arg_name = 's3-location' self.bucket = 'bucket' self.key = 'key' self.system_patcher = mock.patch('platform.system') self.system = self.system_patcher.start() self.system.return_value = 'Linux' self.linux_distribution_patcher = mock.patch( 'awscli.compat.linux_distribution') self.linux_distribution = self.linux_distribution_patcher.start() self.linux_distribution.return_value = ('Ubuntu', '', '') self.urlopen_patcher = mock.patch( 'awscli.customizations.codedeploy.utils.urlopen') self.urlopen = self.urlopen_patcher.start() self.urlopen.side_effect = timeout('Not EC2 instance') self.globals = mock.MagicMock() self.session = mock.MagicMock() self.params = Namespace() self.params.session = self.session
def test_push_output_message(self, stdout_mock): self.args.bucket = self.bucket self.args.key = self.key self.push._compress = mock.MagicMock(return_value=self.bundle_mock) self.push._upload_to_s3 = mock.MagicMock(return_value=self.upload_response) self.push._register_revision = mock.MagicMock() self.push._push(self.args) output = stdout_mock.getvalue().strip() expected_revision_output = ( '--s3-location bucket={0},key={1},' 'bundleType=zip,eTag={2},version={3}'.format( self.bucket, self.key, self.eTag.replace('"',""), self.version_id) ) expected_output = ( 'To deploy with this revision, run:\n' 'aws deploy create-deployment ' '--application-name {0} {1} ' '--deployment-group-name <deployment-group-name> ' '--deployment-config-name <deployment-config-name> ' '--description <description>'.format( self.application_name, expected_revision_output ) ) self.assertEqual(expected_output, output)
def setUp(self): self.region = 'us-east-1' self.config_file = 'config-file' self.installer = 'install' self.bucket = 'aws-codedeploy-{0}'.format(self.region) self.key = 'latest/{0}'.format(self.installer) self.agent_installer = 's3://{0}/{1}'.format(self.bucket, self.key) self.system_patcher = mock.patch('platform.system') self.system = self.system_patcher.start() self.system.return_value = 'Linux' self.linux_distribution_patcher = mock.patch( 'awscli.compat.linux_distribution') self.linux_distribution = self.linux_distribution_patcher.start() self.linux_distribution.return_value = ('Ubuntu', '', '') self.urlopen_patcher = mock.patch( 'awscli.customizations.codedeploy.utils.urlopen') self.urlopen = self.urlopen_patcher.start() self.urlopen.side_effect = timeout('Not EC2 instance') self.geteuid_patcher = mock.patch('os.geteuid', create=True) self.geteuid = self.geteuid_patcher.start() self.geteuid.return_value = 0 self.isfile_patcher = mock.patch('os.path.isfile') self.isfile = self.isfile_patcher.start() self.isfile.return_value = False self.makedirs_patcher = mock.patch('os.makedirs') self.makedirs = self.makedirs_patcher.start() self.copyfile_patcher = mock.patch('shutil.copyfile') self.copyfile = self.copyfile_patcher.start() self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.systems.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.args = Namespace() self.args.override_config = False self.args.config_file = self.config_file self.args.agent_installer = None self.globals = Namespace() self.globals.region = self.region self.body = 'install-script' self.reader = mock.MagicMock() self.reader.read.return_value = self.body self.s3 = mock.MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = mock.MagicMock() self.session.create_client.return_value = self.s3 self.install = Install(self.session)
def test_push_throws_on_upload_to_s3_error(self): self.args.bucket = self.bucket self.args.key = self.key self.push._compress = mock.MagicMock(return_value=self.bundle_mock) self.push._upload_to_s3 = mock.MagicMock() self.push._upload_to_s3.side_effect = RuntimeError() with self.assertRaises(RuntimeError): self.push._push(self.args)
def setUp(self): self.environ = {} self.environ_patch = mock.patch('os.environ', self.environ) self.environ_patch.start() self.mock = mock.MagicMock() self.mock.get_config = mock.MagicMock(return_value={'region': None}) self.file_creator = FileCreator() self.loc_files = make_loc_files(self.file_creator) self.bucket = 's3testbucket'
def test_push_strips_quotes_from_etag(self): self.args.bucket = self.bucket self.args.key = self.key self.push._compress = mock.MagicMock(return_value=self.bundle_mock) self.push._upload_to_s3 = mock.MagicMock(return_value=self.upload_response) self.push._register_revision = mock.MagicMock() self.push._push(self.args) self.push._register_revision.assert_called_with(self.args) self.assertEqual(str(self.args.eTag), self.upload_response['ETag'].replace('"',""))
def setUp(self): self.popen_patcher = mock.patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = mock.patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.systems.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.environ_patcher = mock.patch('os.environ') self.environ = self.environ_patcher.start() self.environ.copy.return_value = dict() self.config_dir = '/etc/codedeploy-agent/conf' self.config_file = 'codedeploy.onpremises.yml' self.config_path = '{0}/{1}'.format(self.config_dir, self.config_file) self.installer = 'install' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.access_key_id = 'ACCESSKEYID' self.secret_access_key = 'SECRETACCESSKEY' self.session_token = 'SESSION_TOKEN' self.credentials = mock.MagicMock() self.credentials.access_key = self.access_key_id self.credentials.secret_key = self.secret_access_key self.credentials.token = self.session_token self.environment = dict({ 'AWS_REGION': self.region, 'AWS_ACCESS_KEY_ID': self.access_key_id, 'AWS_SECRET_ACCESS_KEY': self.secret_access_key, 'AWS_SESSION_TOKEN': self.session_token }) self.body = 'install-script' self.reader = mock.MagicMock() self.reader.read.return_value = self.body self.s3 = mock.MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = mock.MagicMock() self.session.create_client.return_value = self.s3 self.session.get_credentials.return_value = self.credentials self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key
def setUp(self): self.path = '/AWS/CodeDeploy/' self.instance_name = 'instance-name' self.tags = [{'Key': 'k1', 'Value': 'v1'}] self.iam_user_arn = 'arn:aws:iam::012345678912:user/instance-name' self.access_key_id = 'ACCESSKEYID' self.secret_access_key = 'SECRETACCESSKEY' self.region = 'us-east-1' self.policy_name = 'codedeploy-agent' self.policy_document = ( '{\n' ' "Version": "2012-10-17",\n' ' "Statement": [ {\n' ' "Action": [ "s3:Get*", "s3:List*" ],\n' ' "Effect": "Allow",\n' ' "Resource": "*"\n' ' } ]\n' '}') self.config_file = 'codedeploy.onpremises.yml' self.endpoint_url = 'https://codedeploy.aws.amazon.com' self.args = Namespace() self.args.instance_name = self.instance_name self.args.tags = None self.args.iam_user_arn = None self.globals = Namespace() self.globals.region = self.region self.globals.endpoint_url = self.endpoint_url self.globals.verify_ssl = False self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.register.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.codedeploy = mock.MagicMock() self.iam = mock.MagicMock() self.iam.create_user.return_value = { 'User': { 'Arn': self.iam_user_arn } } self.iam.create_access_key.return_value = { 'AccessKey': { 'AccessKeyId': self.access_key_id, 'SecretAccessKey': self.secret_access_key } } self.session = mock.MagicMock() self.session.create_client.side_effect = [self.codedeploy, self.iam] self.register = Register(self.session)
def test_command_invoked(self, mock_yaml_parse): """ Tests that deploy method is invoked when command is run """ fake_parameter_overrides = [] fake_tags_dict = {"tagkey1": "tagvalue1"} fake_tags = [{"Key": "tagkey1", "Value": "tagvalue1"}] fake_parameters = "some return value" template_str = "some template" with tempfile.NamedTemporaryFile() as handle: file_path = handle.name open_mock = mock.mock_open() # Patch the file open method to return template string with mock.patch("awscli.customizations.cloudformation.deploy.open", open_mock(read_data=template_str)) as open_mock: fake_template = get_example_template() mock_yaml_parse.return_value = fake_template self.deploy_command.deploy = mock.MagicMock() self.deploy_command.deploy.return_value = 0 self.deploy_command.parse_key_value_arg = mock.Mock() self.deploy_command.parse_key_value_arg.side_effect = [ fake_parameter_overrides, fake_tags_dict ] self.deploy_command.merge_parameters = mock.MagicMock( return_value=fake_parameters) self.parsed_args.template_file = file_path result = self.deploy_command._run_main( self.parsed_args, parsed_globals=self.parsed_globals) self.assertEqual(0, result) open_mock.assert_called_once_with(file_path, "r") self.deploy_command.deploy.assert_called_once_with( mock.ANY, 'some_stack_name', mock.ANY, fake_parameters, None, not self.parsed_args.no_execute_changeset, None, [], None, fake_tags, True, True) self.deploy_command.parse_key_value_arg.assert_has_calls([ mock.call(self.parsed_args.parameter_overrides, "parameter-overrides"), mock.call(self.parsed_args.tags, "tags") ]) self.deploy_command.merge_parameters.assert_called_once_with( fake_template, fake_parameter_overrides) self.assertEqual(1, mock_yaml_parse.call_count)
def test_run_main_creates_clients(self): self.push._validate_args = mock.MagicMock() self.push._push = mock.MagicMock() self.push._run_main(self.args, self.globals) self.session.create_client.assert_has_calls([ mock.call( 'codedeploy', region_name=self.region, endpoint_url=self.endpoint_url, verify=self.globals.verify_ssl ), mock.call('s3', region_name=self.region) ])
def setUp(self): self.region = 'us-east-1' self.system_patcher = mock.patch('platform.system') self.system = self.system_patcher.start() self.system.return_value = 'Linux' self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution') self.linux_distribution = self.linux_distribution_patcher.start() self.linux_distribution.return_value = ('Ubuntu', '', '') self.urlopen_patcher = mock.patch( 'awscli.customizations.codedeploy.utils.urlopen' ) self.urlopen = self.urlopen_patcher.start() self.urlopen.side_effect = timeout('Not EC2 instance') self.geteuid_patcher = mock.patch('os.geteuid', create=True) self.geteuid = self.geteuid_patcher.start() self.geteuid.return_value = 0 self.remove_patcher = mock.patch('os.remove') self.remove = self.remove_patcher.start() self.args = Namespace() self.globals = Namespace() self.globals.region = self.region self.session = mock.MagicMock() self.uninstall = Uninstall(self.session)
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()
def test_install(self): process = mock.MagicMock() process.communicate.side_effect = [('', ''), ('Running', '')] process.returncode = 0 self.popen.return_value = process self.windows.install(self.params) self.popen.assert_has_calls([ mock.call([ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate(), mock.call([ 'powershell.exe', '-Command', 'Get-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ]) self.check_call.assert_has_calls([ mock.call([ r'.\{0}'.format(self.installer), '/quiet', '/l', r'.\codedeploy-agent-install-log.txt' ], shell=True), mock.call([ 'powershell.exe', '-Command', 'Restart-Service', '-Name', 'codedeployagent' ]) ]) self.open.assert_called_with(self.installer, 'wb') self.open().write.assert_called_with(self.body)
def setUp(self): self.instance_name = 'instance-name' self.tags = [{'Key': 'k1', 'Value': 'v1'}] self.iam_user_arn = 'arn:aws:iam::012345678912:user/{0}'.format( self.instance_name) self.access_key_id = 'ACCESSKEYID' self.region = 'us-east-1' self.policy_name = 'codedeploy-agent' self.endpoint_url = 'https://codedeploy.aws.amazon.com' self.args = Namespace() self.args.instance_name = self.instance_name self.args.no_delete_iam_user = False self.globals = Namespace() self.globals.region = self.region self.globals.endpoint_url = self.endpoint_url self.globals.verify_ssl = False self.codedeploy = mock.MagicMock() self.codedeploy.get_on_premises_instance.return_value = { 'instanceInfo': { 'iamUserArn': self.iam_user_arn, 'tags': None } } self.iam = mock.MagicMock() self.list_user_policies = mock.MagicMock() self.list_user_policies.paginate.return_value = [{ 'PolicyNames': [self.policy_name] }] self.list_access_keys = mock.MagicMock() self.list_access_keys.paginate.return_value = [{ 'AccessKeyMetadata': [{ 'AccessKeyId': self.access_key_id }] }] self.iam.get_paginator.side_effect = [ self.list_user_policies, self.list_access_keys ] self.session = mock.MagicMock() self.session.create_client.side_effect = [self.codedeploy, self.iam] self.deregister = Deregister(self.session)
def setUp(self): self.credentials = Credentials('access', 'secret') self.args = Namespace() self.args.ignore_host_check = False self.globals = Namespace() self.globals.region = 'us-east-1' self.globals.verify_ssl = False self.session = mock.MagicMock() self.session.get_config_variable.return_value = 'us-east-1' self.session.get_credentials.return_value = self.credentials
def test_uninstall(self): process = mock.MagicMock() process.communicate.return_value = ('', '') process.returncode = 0 self.popen.return_value = process self.rhel.uninstall(self.params) self.popen.assert_has_calls([ mock.call(['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ]) self.check_call.assert_has_calls( [mock.call(['yum', '-y', 'erase', 'codedeploy-agent'])])
def setUp(self): self.popen_patcher = mock.patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = mock.patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.systems.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy' self.config_file = 'conf.onpremises.yml' self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file) self.installer = 'codedeploy-agent.msi' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.body = 'install-script' self.reader = mock.MagicMock() self.reader.read.return_value = self.body self.s3 = mock.MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = mock.MagicMock() self.session.create_client.return_value = self.s3 self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key self.windows = Windows(self.params)
def test_install(self): process = mock.MagicMock() process.communicate.return_value = ('', '') process.returncode = 0 self.popen.return_value = process self.rhel.install(self.params) self.popen.assert_has_calls([ mock.call(['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ]) self.check_call.assert_has_calls([ mock.call(['yum', '-y', 'install', 'ruby']), mock.call(['chmod', '+x', './{0}'.format(self.installer)]), mock.call(['./{0}'.format(self.installer), 'auto'], env=self.environment) ]) self.open.assert_called_with(self.installer, 'wb') self.open().write.assert_called_with(self.body)
def test_uninstall(self): process = mock.MagicMock() process.communicate.side_effect = [('', ''), ('', '')] process.returncode = 0 self.popen.return_value = process self.windows.uninstall(self.params) self.popen.assert_has_calls([ mock.call([ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate(), mock.call([ 'wmic', 'product', 'where', 'name="CodeDeploy Host Agent"', 'call', 'uninstall', '/nointeractive' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ])
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_pager_stream.return_value = \ output_stream_context self.db_reader = mock.Mock(DatabaseRecordReader) self.db_reader.iter_all_records.return_value = iter([]) self.list_cmd = ListCommand(self.session, self.db_reader, self.output_stream_factory) self.parsed_args = argparse.Namespace() self.parsed_globals = argparse.Namespace() self.parsed_globals.color = 'auto'
def __init__(self): self.execute = mock.MagicMock() self.closed = False
def test_run_main_calls_push(self): self.push._validate_args = mock.MagicMock() self.push._push = mock.MagicMock() self.push._run_main(self.args, self.globals) self.push._push.assert_called_with(self.args)
def setUp(self): self.application_name = 'MyApp' self.description = 'MyApp revision' self.source = '/tmp' self.appspec = 'appspec.yml' self.appspec_path = '{0}/{1}'.format(self.source, self.appspec) self.bucket = 'foo' self.key = 'bar/baz.zip' self.s3_location = 's3://' + self.bucket + '/' + self.key self.eTag = '"1a2b3cd45e"' self.version_id = '12341234-1234-1234-1234-123412341234' self.upload_id = 'upload_id' self.region = 'us-east-1' self.endpoint_url = 'https://codedeploy.aws.amazon.com' self.args = Namespace() self.args.application_name = self.application_name self.args.s3_location = self.s3_location self.args.ignore_hidden_files = False self.args.no_ignore_hidden_files = False self.args.description = self.description self.args.source = self.source self.globals = Namespace() self.globals.region = self.region self.globals.endpoint_url = self.endpoint_url self.globals.verify_ssl = False self.upload_response = { 'ETag': self.eTag, 'VersionId': self.version_id } self.revision = { 'revisionType': 'S3', 's3Location': { 'bucket': self.bucket, 'key': self.key, 'bundleType': 'zip', 'eTag': self.eTag, 'version': self.version_id } } self.bundle_mock = mock.MagicMock() self.bundle_mock.tell.return_value = (5 << 20) self.bundle_mock.read.return_value = b'a' * (5 << 20) self.bundle_mock.__enter__.return_value = self.bundle_mock self.bundle_mock.__exit__.return_value = None self.zipfile_mock = mock.MagicMock() self.zipfile_mock.write.return_value = None self.zipfile_mock.close.return_value = None self.zipfile_mock.__enter__.return_value = self.zipfile_mock self.zipfile_mock.__exit__.return_value = None self.session = mock.MagicMock() self.push = Push(self.session) self.push.s3 = mock.MagicMock() self.push.s3.put_object.return_value = self.upload_response self.push.s3.create_multipart_upload.return_value = { 'UploadId': self.upload_id } self.push.s3.upload_part.return_value = { 'ETag': self.eTag } self.push.s3.complete_multipart_upload\ .return_value = self.upload_response self.push.codedeploy = mock.MagicMock()
def test_run_main_throws_on_invalid_args(self): self.push._validate_args = mock.MagicMock() self.push._validate_args.side_effect = RuntimeError() with self.assertRaises(RuntimeError): self.push._run_main(self.args, self.globals)