Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/codedeploy/test_uninstall.py
2637 views
1
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
import sys
15
16
from argparse import Namespace
17
from awscli.customizations.codedeploy.systems import Ubuntu, Windows, RHEL, System
18
from awscli.customizations.codedeploy.uninstall import Uninstall
19
from awscli.testutils import mock, unittest
20
from socket import timeout
21
22
23
class TestUninstall(unittest.TestCase):
24
def setUp(self):
25
self.region = 'us-east-1'
26
27
self.system_patcher = mock.patch('platform.system')
28
self.system = self.system_patcher.start()
29
self.system.return_value = 'Linux'
30
31
self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution')
32
self.linux_distribution = self.linux_distribution_patcher.start()
33
self.linux_distribution.return_value = ('Ubuntu', '', '')
34
35
self.urlopen_patcher = mock.patch(
36
'awscli.customizations.codedeploy.utils.urlopen'
37
)
38
self.urlopen = self.urlopen_patcher.start()
39
self.urlopen.side_effect = timeout('Not EC2 instance')
40
41
self.geteuid_patcher = mock.patch('os.geteuid', create=True)
42
self.geteuid = self.geteuid_patcher.start()
43
self.geteuid.return_value = 0
44
45
self.remove_patcher = mock.patch('os.remove')
46
self.remove = self.remove_patcher.start()
47
48
self.args = Namespace()
49
self.globals = Namespace()
50
self.globals.region = self.region
51
self.session = mock.MagicMock()
52
self.uninstall = Uninstall(self.session)
53
54
def tearDown(self):
55
self.system_patcher.stop()
56
self.linux_distribution_patcher.stop()
57
self.urlopen_patcher.stop()
58
self.geteuid_patcher.stop()
59
self.remove_patcher.stop()
60
61
def test_uninstall_throws_on_invalid_region(self):
62
self.globals.region = None
63
self.session.get_config_variable.return_value = None
64
with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):
65
self.uninstall._run_main(self.args, self.globals)
66
67
def test_uninstall_throws_on_unsupported_system(self):
68
self.system.return_value = 'Unsupported'
69
with self.assertRaisesRegex(
70
RuntimeError, System.UNSUPPORTED_SYSTEM_MSG):
71
self.uninstall._run_main(self.args, self.globals)
72
73
def test_uninstall_throws_on_ec2_instance(self):
74
self.urlopen.side_effect = None
75
with self.assertRaisesRegex(
76
RuntimeError, 'Amazon EC2 instances are not supported.'):
77
self.uninstall._run_main(self.args, self.globals)
78
self.assertIn('system', self.args)
79
self.assertTrue(isinstance(self.args.system, Ubuntu))
80
81
def test_uninstall_throws_on_non_administrator(self):
82
self.geteuid.return_value = 1
83
with self.assertRaisesRegex(
84
RuntimeError, 'You must run this command as sudo.'):
85
self.uninstall._run_main(self.args, self.globals)
86
87
@mock.patch.object(Ubuntu, 'uninstall')
88
def test_uninstall_for_ubuntu(self, uninstall):
89
self.system.return_value = 'Linux'
90
self.linux_distribution.return_value = ('Ubuntu', '', '')
91
self.uninstall._run_main(self.args, self.globals)
92
uninstall.assert_called_with(self.args)
93
self.remove.assert_called_with(
94
'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'
95
)
96
97
@mock.patch.object(RHEL, 'uninstall')
98
def test_uninstall_for_RHEL(self, uninstall):
99
self.system.return_value = 'Linux'
100
self.linux_distribution.return_value = ('Red Hat Enterprise Linux Server', '', '')
101
self.uninstall._run_main(self.args, self.globals)
102
uninstall.assert_called_with(self.args)
103
self.remove.assert_called_with(
104
'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'
105
)
106
107
@mock.patch.object(Windows, 'uninstall')
108
@mock.patch.object(Windows, 'validate_administrator')
109
def test_uninstall_for_windows(self, validate_administrator, uninstall):
110
self.system.return_value = 'Windows'
111
self.uninstall._run_main(self.args, self.globals)
112
validate_administrator.assert_called_with()
113
uninstall.assert_called_with(self.args)
114
self.remove.assert_called_with(
115
r'C:\ProgramData\Amazon\CodeDeploy\conf.onpremises.yml'
116
)
117
118
119
if __name__ == "__main__":
120
unittest.main()
121
122