Path: blob/develop/tests/unit/customizations/codedeploy/test_install.py
2637 views
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import sys1415from argparse import Namespace16from awscli.customizations.codedeploy.install import Install17from awscli.customizations.codedeploy.systems import Ubuntu, Windows, RHEL, System18from awscli.testutils import mock, unittest19from socket import timeout202122class TestInstall(unittest.TestCase):23def setUp(self):24self.region = 'us-east-1'25self.config_file = 'config-file'26self.installer = 'install'27self.bucket = 'aws-codedeploy-{0}'.format(self.region)28self.key = 'latest/{0}'.format(self.installer)29self.agent_installer = 's3://{0}/{1}'.format(self.bucket, self.key)3031self.system_patcher = mock.patch('platform.system')32self.system = self.system_patcher.start()33self.system.return_value = 'Linux'3435self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution')36self.linux_distribution = self.linux_distribution_patcher.start()37self.linux_distribution.return_value = ('Ubuntu', '', '')3839self.urlopen_patcher = mock.patch(40'awscli.customizations.codedeploy.utils.urlopen'41)42self.urlopen = self.urlopen_patcher.start()43self.urlopen.side_effect = timeout('Not EC2 instance')4445self.geteuid_patcher = mock.patch('os.geteuid', create=True)46self.geteuid = self.geteuid_patcher.start()47self.geteuid.return_value = 04849self.isfile_patcher = mock.patch('os.path.isfile')50self.isfile = self.isfile_patcher.start()51self.isfile.return_value = False5253self.makedirs_patcher = mock.patch('os.makedirs')54self.makedirs = self.makedirs_patcher.start()5556self.copyfile_patcher = mock.patch('shutil.copyfile')57self.copyfile = self.copyfile_patcher.start()5859self.open_patcher = mock.patch(60'awscli.customizations.codedeploy.systems.open',61mock.mock_open(), create=True62)63self.open = self.open_patcher.start()6465self.args = Namespace()66self.args.override_config = False67self.args.config_file = self.config_file68self.args.agent_installer = None6970self.globals = Namespace()71self.globals.region = self.region7273self.body = 'install-script'74self.reader = mock.MagicMock()75self.reader.read.return_value = self.body76self.s3 = mock.MagicMock()77self.s3.get_object.return_value = {'Body': self.reader}7879self.session = mock.MagicMock()80self.session.create_client.return_value = self.s381self.install = Install(self.session)8283def tearDown(self):84self.system_patcher.stop()85self.linux_distribution_patcher.stop()86self.urlopen_patcher.stop()87self.geteuid_patcher.stop()88self.isfile_patcher.stop()89self.makedirs_patcher.stop()90self.copyfile_patcher.stop()91self.open_patcher.stop()9293def test_install_throws_on_invalid_region(self):94self.globals.region = None95self.session.get_config_variable.return_value = None96with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):97self.install._run_main(self.args, self.globals)9899def test_install_throws_on_unsupported_system(self):100self.system.return_value = 'Unsupported'101with self.assertRaisesRegex(102RuntimeError, System.UNSUPPORTED_SYSTEM_MSG):103self.install._run_main(self.args, self.globals)104105def test_install_throws_on_ec2_instance(self):106self.urlopen.side_effect = None107with self.assertRaisesRegex(108RuntimeError, 'Amazon EC2 instances are not supported.'):109self.install._run_main(self.args, self.globals)110self.assertIn('system', self.args)111self.assertTrue(isinstance(self.args.system, Ubuntu))112113def test_install_throws_on_non_administrator(self):114self.geteuid.return_value = 1115with self.assertRaisesRegex(116RuntimeError, 'You must run this command as sudo.'):117self.install._run_main(self.args, self.globals)118119def test_install_throws_on_no_override_config(self):120self.isfile.return_value = True121self.args.override_config = False122with self.assertRaisesRegex(123RuntimeError,124'The on-premises instance configuration file already exists. '125'Specify --override-config to update the existing on-premises '126'instance configuration file.'):127self.install._run_main(self.args, self.globals)128129def test_install_throws_on_invalid_agent_installer(self):130self.args.agent_installer = 'invalid-s3-location'131with self.assertRaisesRegex(132ValueError,133'--agent-installer must specify the Amazon S3 URL format as '134's3://<bucket>/<key>.'):135self.install._run_main(self.args, self.globals)136137@mock.patch.object(Ubuntu, 'install')138def test_install_with_agent_installer(self, install):139self.args.agent_installer = self.agent_installer140self.install._run_main(self.args, self.globals)141self.assertIn('bucket', self.args)142self.assertEqual(self.bucket, self.args.bucket)143self.assertIn('key', self.args)144self.assertEqual(self.key, self.args.key)145self.assertIn('installer', self.args)146self.assertEqual(self.installer, self.args.installer)147install.assert_called_with(self.args)148149@mock.patch.object(Ubuntu, 'install')150def test_install_for_ubuntu(self, install):151self.system.return_value = 'Linux'152self.linux_distribution.return_value = ('Ubuntu', '', '')153self.install._run_main(self.args, self.globals)154self.assertIn('bucket', self.args)155self.assertEqual(self.bucket, self.args.bucket)156self.assertIn('key', self.args)157self.assertEqual('latest/install', self.args.key)158self.assertIn('installer', self.args)159self.assertEqual('install', self.args.installer)160self.makedirs.assert_called_with('/etc/codedeploy-agent/conf')161self.copyfile.assset_called_with(162'codedeploy.onpremises.yml',163'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'164)165install.assert_called_with(self.args)166167@mock.patch.object(Windows, 'install')168@mock.patch.object(Windows, 'validate_administrator')169def test_install_for_windows(self, validate_administrator, install):170self.system.return_value = 'Windows'171self.install._run_main(self.args, self.globals)172self.assertIn('bucket', self.args)173self.assertEqual(self.bucket, self.args.bucket)174self.assertIn('key', self.args)175self.assertEqual('latest/codedeploy-agent.msi', self.args.key)176self.assertIn('installer', self.args)177self.assertEqual('codedeploy-agent.msi', self.args.installer)178self.makedirs.assert_called_with(r'C:\ProgramData\Amazon\CodeDeploy')179self.copyfile.assset_called_with(180'conf.onpremises.yml',181r'C:\ProgramData\Amazon\CodeDeploy\conf.onpremises.yml'182)183validate_administrator.assert_called_with()184install.assert_called_with(self.args)185186187if __name__ == "__main__":188unittest.main()189190191