Path: blob/develop/tests/unit/customizations/codedeploy/test_systems.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 subprocess1415from argparse import Namespace16from awscli.customizations.codedeploy.systems import Windows, Ubuntu, RHEL17from awscli.testutils import mock, unittest181920class TestWindows(unittest.TestCase):21def setUp(self):22self.popen_patcher = mock.patch('subprocess.Popen')23self.popen = self.popen_patcher.start()2425self.check_call_patcher = mock.patch('subprocess.check_call')26self.check_call = self.check_call_patcher.start()2728self.open_patcher = mock.patch(29'awscli.customizations.codedeploy.systems.open',30mock.mock_open(), create=True31)32self.open = self.open_patcher.start()3334self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy'35self.config_file = 'conf.onpremises.yml'36self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file)37self.installer = 'codedeploy-agent.msi'38self.bucket = 'bucket'39self.key = 'key'40self.region = 'us-east-1'4142self.body = 'install-script'43self.reader = mock.MagicMock()44self.reader.read.return_value = self.body45self.s3 = mock.MagicMock()46self.s3.get_object.return_value = {'Body': self.reader}4748self.session = mock.MagicMock()49self.session.create_client.return_value = self.s35051self.params = Namespace()52self.params.session = self.session53self.params.region = self.region54self.params.bucket = self.bucket55self.params.key = self.key5657self.windows = Windows(self.params)5859def tearDown(self):60self.popen_patcher.stop()61self.check_call_patcher.stop()62self.open_patcher.stop()6364def test_config_dir(self):65self.assertEqual(self.config_dir, self.windows.CONFIG_DIR)6667def test_config_file(self):68self.assertEqual(self.config_file, self.windows.CONFIG_FILE)6970def test_config_path(self):71self.assertEqual(self.config_path, self.windows.CONFIG_PATH)7273def test_installer(self):74self.assertEqual(self.installer, self.windows.INSTALLER)7576def test_install(self):77process = mock.MagicMock()78process.communicate.side_effect = [('', ''), ('Running', '')]79process.returncode = 080self.popen.return_value = process81self.windows.install(self.params)82self.popen.assert_has_calls([83mock.call(84[85'powershell.exe',86'-Command', 'Stop-Service',87'-Name', 'codedeployagent'88],89stdout=subprocess.PIPE,90stderr=subprocess.PIPE91),92mock.call().communicate(),93mock.call(94[95'powershell.exe',96'-Command', 'Get-Service',97'-Name', 'codedeployagent'98],99stdout=subprocess.PIPE,100stderr=subprocess.PIPE101),102mock.call().communicate()103])104self.check_call.assert_has_calls([105mock.call(106[107r'.\{0}'.format(self.installer),108'/quiet',109'/l', r'.\codedeploy-agent-install-log.txt'110],111shell=True112),113mock.call([114'powershell.exe',115'-Command', 'Restart-Service',116'-Name', 'codedeployagent'117])118])119self.open.assert_called_with(self.installer, 'wb')120self.open().write.assert_called_with(self.body)121122def test_uninstall(self):123process = mock.MagicMock()124process.communicate.side_effect = [('', ''), ('', '')]125process.returncode = 0126self.popen.return_value = process127self.windows.uninstall(self.params)128self.popen.assert_has_calls([129mock.call(130[131'powershell.exe',132'-Command', 'Stop-Service',133'-Name', 'codedeployagent'134],135stdout=subprocess.PIPE,136stderr=subprocess.PIPE137),138mock.call().communicate(),139mock.call(140[141'wmic',142'product', 'where', 'name="CodeDeploy Host Agent"',143'call', 'uninstall', '/nointeractive'144],145stdout=subprocess.PIPE,146stderr=subprocess.PIPE147),148mock.call().communicate()149])150151152class TestLinux(unittest.TestCase):153def setUp(self):154self.popen_patcher = mock.patch('subprocess.Popen')155self.popen = self.popen_patcher.start()156157self.check_call_patcher = mock.patch('subprocess.check_call')158self.check_call = self.check_call_patcher.start()159160self.open_patcher = mock.patch(161'awscli.customizations.codedeploy.systems.open',162mock.mock_open(), create=True163)164self.open = self.open_patcher.start()165166self.environ_patcher = mock.patch('os.environ')167self.environ = self.environ_patcher.start()168self.environ.copy.return_value = dict()169170self.config_dir = '/etc/codedeploy-agent/conf'171self.config_file = 'codedeploy.onpremises.yml'172self.config_path = '{0}/{1}'.format(self.config_dir, self.config_file)173self.installer = 'install'174self.bucket = 'bucket'175self.key = 'key'176self.region = 'us-east-1'177178self.access_key_id = 'ACCESSKEYID'179self.secret_access_key = 'SECRETACCESSKEY'180self.session_token = 'SESSION_TOKEN'181self.credentials = mock.MagicMock()182self.credentials.access_key = self.access_key_id183self.credentials.secret_key = self.secret_access_key184self.credentials.token = self.session_token185186self.environment = dict({187'AWS_REGION': self.region,188'AWS_ACCESS_KEY_ID': self.access_key_id,189'AWS_SECRET_ACCESS_KEY': self.secret_access_key,190'AWS_SESSION_TOKEN': self.session_token191})192193self.body = 'install-script'194self.reader = mock.MagicMock()195self.reader.read.return_value = self.body196self.s3 = mock.MagicMock()197self.s3.get_object.return_value = {'Body': self.reader}198199self.session = mock.MagicMock()200self.session.create_client.return_value = self.s3201self.session.get_credentials.return_value = self.credentials202203self.params = Namespace()204self.params.session = self.session205self.params.region = self.region206self.params.bucket = self.bucket207self.params.key = self.key208209def tearDown(self):210self.popen_patcher.stop()211self.check_call_patcher.stop()212self.open_patcher.stop()213self.environ_patcher.stop()214215216class TestUbuntu(TestLinux):217def setUp(self):218super(self.__class__, self).setUp()219self.ubuntu = Ubuntu(self.params)220221def test_config_dir(self):222self.assertEqual(self.config_dir, self.ubuntu.CONFIG_DIR)223224def test_config_file(self):225self.assertEqual(self.config_file, self.ubuntu.CONFIG_FILE)226227def test_config_path(self):228self.assertEqual(self.config_path, self.ubuntu.CONFIG_PATH)229230def test_installer(self):231self.assertEqual(self.installer, self.ubuntu.INSTALLER)232233@mock.patch('os.geteuid', create=True)234def test_validate_administrator_throws(self, geteuid):235geteuid.return_value = 1236with self.assertRaisesRegex(237RuntimeError, 'You must run this command as sudo.'):238self.ubuntu.validate_administrator()239240def test_install(self):241process = mock.MagicMock()242process.communicate.return_value = ('', '')243process.returncode = 0244self.popen.return_value = process245self.ubuntu.install(self.params)246self.popen.assert_has_calls([247mock.call(248['service', 'codedeploy-agent', 'stop'],249stdout=subprocess.PIPE,250stderr=subprocess.PIPE251),252mock.call().communicate()253])254self.check_call.assert_has_calls([255mock.call(['apt-get', '-y', 'update']),256mock.call(['apt-get', '-y', 'install', 'ruby2.0']),257mock.call(['chmod', '+x', './{0}'.format(self.installer)]),258mock.call(259['./{0}'.format(self.installer), 'auto'],260env=self.environment261)262])263self.open.assert_called_with(self.installer, 'wb')264self.open().write.assert_called_with(self.body)265266def test_uninstall(self):267process = mock.MagicMock()268process.communicate.return_value = ('', '')269process.returncode = 0270self.popen.return_value = process271self.ubuntu.uninstall(self.params)272self.popen.assert_has_calls([273mock.call(274['service', 'codedeploy-agent', 'stop'],275stdout=subprocess.PIPE,276stderr=subprocess.PIPE277),278mock.call().communicate()279])280self.check_call.assert_has_calls([281mock.call(['dpkg', '-r', 'codedeploy-agent'])282])283284class TestRHEL(TestLinux):285def setUp(self):286super(self.__class__, self).setUp()287self.rhel = RHEL(self.params)288289def test_config_dir(self):290self.assertEqual(self.config_dir, self.rhel.CONFIG_DIR)291292def test_config_file(self):293self.assertEqual(self.config_file, self.rhel.CONFIG_FILE)294295def test_config_path(self):296self.assertEqual(self.config_path, self.rhel.CONFIG_PATH)297298def test_installer(self):299self.assertEqual(self.installer, self.rhel.INSTALLER)300301@mock.patch('os.geteuid', create=True)302def test_validate_administrator_throws(self, geteuid):303geteuid.return_value = 1304with self.assertRaisesRegex(305RuntimeError, 'You must run this command as sudo.'):306self.rhel.validate_administrator()307308def test_install(self):309process = mock.MagicMock()310process.communicate.return_value = ('', '')311process.returncode = 0312self.popen.return_value = process313self.rhel.install(self.params)314self.popen.assert_has_calls([315mock.call(316['service', 'codedeploy-agent', 'stop'],317stdout=subprocess.PIPE,318stderr=subprocess.PIPE319),320mock.call().communicate()321])322self.check_call.assert_has_calls([323mock.call(['yum', '-y', 'install', 'ruby']),324mock.call(['chmod', '+x', './{0}'.format(self.installer)]),325mock.call(326['./{0}'.format(self.installer), 'auto'],327env=self.environment328)329])330self.open.assert_called_with(self.installer, 'wb')331self.open().write.assert_called_with(self.body)332333def test_uninstall(self):334process = mock.MagicMock()335process.communicate.return_value = ('', '')336process.returncode = 0337self.popen.return_value = process338self.rhel.uninstall(self.params)339self.popen.assert_has_calls([340mock.call(341['service', 'codedeploy-agent', 'stop'],342stdout=subprocess.PIPE,343stderr=subprocess.PIPE344),345mock.call().communicate()346])347self.check_call.assert_has_calls([348mock.call(['yum', '-y', 'erase', 'codedeploy-agent'])349])350351if __name__ == "__main__":352unittest.main()353354355