Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/codedeploy/test_install.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.install import Install
18
from awscli.customizations.codedeploy.systems import Ubuntu, Windows, RHEL, System
19
from awscli.testutils import mock, unittest
20
from socket import timeout
21
22
23
class TestInstall(unittest.TestCase):
24
def setUp(self):
25
self.region = 'us-east-1'
26
self.config_file = 'config-file'
27
self.installer = 'install'
28
self.bucket = 'aws-codedeploy-{0}'.format(self.region)
29
self.key = 'latest/{0}'.format(self.installer)
30
self.agent_installer = 's3://{0}/{1}'.format(self.bucket, self.key)
31
32
self.system_patcher = mock.patch('platform.system')
33
self.system = self.system_patcher.start()
34
self.system.return_value = 'Linux'
35
36
self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution')
37
self.linux_distribution = self.linux_distribution_patcher.start()
38
self.linux_distribution.return_value = ('Ubuntu', '', '')
39
40
self.urlopen_patcher = mock.patch(
41
'awscli.customizations.codedeploy.utils.urlopen'
42
)
43
self.urlopen = self.urlopen_patcher.start()
44
self.urlopen.side_effect = timeout('Not EC2 instance')
45
46
self.geteuid_patcher = mock.patch('os.geteuid', create=True)
47
self.geteuid = self.geteuid_patcher.start()
48
self.geteuid.return_value = 0
49
50
self.isfile_patcher = mock.patch('os.path.isfile')
51
self.isfile = self.isfile_patcher.start()
52
self.isfile.return_value = False
53
54
self.makedirs_patcher = mock.patch('os.makedirs')
55
self.makedirs = self.makedirs_patcher.start()
56
57
self.copyfile_patcher = mock.patch('shutil.copyfile')
58
self.copyfile = self.copyfile_patcher.start()
59
60
self.open_patcher = mock.patch(
61
'awscli.customizations.codedeploy.systems.open',
62
mock.mock_open(), create=True
63
)
64
self.open = self.open_patcher.start()
65
66
self.args = Namespace()
67
self.args.override_config = False
68
self.args.config_file = self.config_file
69
self.args.agent_installer = None
70
71
self.globals = Namespace()
72
self.globals.region = self.region
73
74
self.body = 'install-script'
75
self.reader = mock.MagicMock()
76
self.reader.read.return_value = self.body
77
self.s3 = mock.MagicMock()
78
self.s3.get_object.return_value = {'Body': self.reader}
79
80
self.session = mock.MagicMock()
81
self.session.create_client.return_value = self.s3
82
self.install = Install(self.session)
83
84
def tearDown(self):
85
self.system_patcher.stop()
86
self.linux_distribution_patcher.stop()
87
self.urlopen_patcher.stop()
88
self.geteuid_patcher.stop()
89
self.isfile_patcher.stop()
90
self.makedirs_patcher.stop()
91
self.copyfile_patcher.stop()
92
self.open_patcher.stop()
93
94
def test_install_throws_on_invalid_region(self):
95
self.globals.region = None
96
self.session.get_config_variable.return_value = None
97
with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):
98
self.install._run_main(self.args, self.globals)
99
100
def test_install_throws_on_unsupported_system(self):
101
self.system.return_value = 'Unsupported'
102
with self.assertRaisesRegex(
103
RuntimeError, System.UNSUPPORTED_SYSTEM_MSG):
104
self.install._run_main(self.args, self.globals)
105
106
def test_install_throws_on_ec2_instance(self):
107
self.urlopen.side_effect = None
108
with self.assertRaisesRegex(
109
RuntimeError, 'Amazon EC2 instances are not supported.'):
110
self.install._run_main(self.args, self.globals)
111
self.assertIn('system', self.args)
112
self.assertTrue(isinstance(self.args.system, Ubuntu))
113
114
def test_install_throws_on_non_administrator(self):
115
self.geteuid.return_value = 1
116
with self.assertRaisesRegex(
117
RuntimeError, 'You must run this command as sudo.'):
118
self.install._run_main(self.args, self.globals)
119
120
def test_install_throws_on_no_override_config(self):
121
self.isfile.return_value = True
122
self.args.override_config = False
123
with self.assertRaisesRegex(
124
RuntimeError,
125
'The on-premises instance configuration file already exists. '
126
'Specify --override-config to update the existing on-premises '
127
'instance configuration file.'):
128
self.install._run_main(self.args, self.globals)
129
130
def test_install_throws_on_invalid_agent_installer(self):
131
self.args.agent_installer = 'invalid-s3-location'
132
with self.assertRaisesRegex(
133
ValueError,
134
'--agent-installer must specify the Amazon S3 URL format as '
135
's3://<bucket>/<key>.'):
136
self.install._run_main(self.args, self.globals)
137
138
@mock.patch.object(Ubuntu, 'install')
139
def test_install_with_agent_installer(self, install):
140
self.args.agent_installer = self.agent_installer
141
self.install._run_main(self.args, self.globals)
142
self.assertIn('bucket', self.args)
143
self.assertEqual(self.bucket, self.args.bucket)
144
self.assertIn('key', self.args)
145
self.assertEqual(self.key, self.args.key)
146
self.assertIn('installer', self.args)
147
self.assertEqual(self.installer, self.args.installer)
148
install.assert_called_with(self.args)
149
150
@mock.patch.object(Ubuntu, 'install')
151
def test_install_for_ubuntu(self, install):
152
self.system.return_value = 'Linux'
153
self.linux_distribution.return_value = ('Ubuntu', '', '')
154
self.install._run_main(self.args, self.globals)
155
self.assertIn('bucket', self.args)
156
self.assertEqual(self.bucket, self.args.bucket)
157
self.assertIn('key', self.args)
158
self.assertEqual('latest/install', self.args.key)
159
self.assertIn('installer', self.args)
160
self.assertEqual('install', self.args.installer)
161
self.makedirs.assert_called_with('/etc/codedeploy-agent/conf')
162
self.copyfile.assset_called_with(
163
'codedeploy.onpremises.yml',
164
'/etc/codedeploy-agent/conf/codedeploy.onpremises.yml'
165
)
166
install.assert_called_with(self.args)
167
168
@mock.patch.object(Windows, 'install')
169
@mock.patch.object(Windows, 'validate_administrator')
170
def test_install_for_windows(self, validate_administrator, install):
171
self.system.return_value = 'Windows'
172
self.install._run_main(self.args, self.globals)
173
self.assertIn('bucket', self.args)
174
self.assertEqual(self.bucket, self.args.bucket)
175
self.assertIn('key', self.args)
176
self.assertEqual('latest/codedeploy-agent.msi', self.args.key)
177
self.assertIn('installer', self.args)
178
self.assertEqual('codedeploy-agent.msi', self.args.installer)
179
self.makedirs.assert_called_with(r'C:\ProgramData\Amazon\CodeDeploy')
180
self.copyfile.assset_called_with(
181
'conf.onpremises.yml',
182
r'C:\ProgramData\Amazon\CodeDeploy\conf.onpremises.yml'
183
)
184
validate_administrator.assert_called_with()
185
install.assert_called_with(self.args)
186
187
188
if __name__ == "__main__":
189
unittest.main()
190
191