Path: blob/develop/tests/unit/customizations/codedeploy/test_uninstall.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.systems import Ubuntu, Windows, RHEL, System17from awscli.customizations.codedeploy.uninstall import Uninstall18from awscli.testutils import mock, unittest19from socket import timeout202122class TestUninstall(unittest.TestCase):23def setUp(self):24self.region = 'us-east-1'2526self.system_patcher = mock.patch('platform.system')27self.system = self.system_patcher.start()28self.system.return_value = 'Linux'2930self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution')31self.linux_distribution = self.linux_distribution_patcher.start()32self.linux_distribution.return_value = ('Ubuntu', '', '')3334self.urlopen_patcher = mock.patch(35'awscli.customizations.codedeploy.utils.urlopen'36)37self.urlopen = self.urlopen_patcher.start()38self.urlopen.side_effect = timeout('Not EC2 instance')3940self.geteuid_patcher = mock.patch('os.geteuid', create=True)41self.geteuid = self.geteuid_patcher.start()42self.geteuid.return_value = 04344self.remove_patcher = mock.patch('os.remove')45self.remove = self.remove_patcher.start()4647self.args = Namespace()48self.globals = Namespace()49self.globals.region = self.region50self.session = mock.MagicMock()51self.uninstall = Uninstall(self.session)5253def tearDown(self):54self.system_patcher.stop()55self.linux_distribution_patcher.stop()56self.urlopen_patcher.stop()57self.geteuid_patcher.stop()58self.remove_patcher.stop()5960def test_uninstall_throws_on_invalid_region(self):61self.globals.region = None62self.session.get_config_variable.return_value = None63with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):64self.uninstall._run_main(self.args, self.globals)6566def test_uninstall_throws_on_unsupported_system(self):67self.system.return_value = 'Unsupported'68with self.assertRaisesRegex(69RuntimeError, System.UNSUPPORTED_SYSTEM_MSG):70self.uninstall._run_main(self.args, self.globals)7172def test_uninstall_throws_on_ec2_instance(self):73self.urlopen.side_effect = None74with self.assertRaisesRegex(75RuntimeError, 'Amazon EC2 instances are not supported.'):76self.uninstall._run_main(self.args, self.globals)77self.assertIn('system', self.args)78self.assertTrue(isinstance(self.args.system, Ubuntu))7980def test_uninstall_throws_on_non_administrator(self):81self.geteuid.return_value = 182with self.assertRaisesRegex(83RuntimeError, 'You must run this command as sudo.'):84self.uninstall._run_main(self.args, self.globals)8586@mock.patch.object(Ubuntu, 'uninstall')87def test_uninstall_for_ubuntu(self, uninstall):88self.system.return_value = 'Linux'89self.linux_distribution.return_value = ('Ubuntu', '', '')90self.uninstall._run_main(self.args, self.globals)91uninstall.assert_called_with(self.args)92self.remove.assert_called_with(93'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'94)9596@mock.patch.object(RHEL, 'uninstall')97def test_uninstall_for_RHEL(self, uninstall):98self.system.return_value = 'Linux'99self.linux_distribution.return_value = ('Red Hat Enterprise Linux Server', '', '')100self.uninstall._run_main(self.args, self.globals)101uninstall.assert_called_with(self.args)102self.remove.assert_called_with(103'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'104)105106@mock.patch.object(Windows, 'uninstall')107@mock.patch.object(Windows, 'validate_administrator')108def test_uninstall_for_windows(self, validate_administrator, uninstall):109self.system.return_value = 'Windows'110self.uninstall._run_main(self.args, self.globals)111validate_administrator.assert_called_with()112uninstall.assert_called_with(self.args)113self.remove.assert_called_with(114r'C:\ProgramData\Amazon\CodeDeploy\conf.onpremises.yml'115)116117118if __name__ == "__main__":119unittest.main()120121122