PageRenderTime 421ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/customizations/codedeploy/test_systems.py

https://gitlab.com/github-cloud-corp/aws-cli
Python | 354 lines | 325 code | 17 blank | 12 comment | 0 complexity | e3f8e71f918a01b2bedb7635e5d8fd4a MD5 | raw file
  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. import subprocess
  14. from argparse import Namespace
  15. from mock import MagicMock, patch, call, mock_open
  16. from awscli.customizations.codedeploy.systems import Windows, Ubuntu, RHEL
  17. from awscli.testutils import unittest
  18. class TestWindows(unittest.TestCase):
  19. def setUp(self):
  20. self.popen_patcher = patch('subprocess.Popen')
  21. self.popen = self.popen_patcher.start()
  22. self.check_call_patcher = patch('subprocess.check_call')
  23. self.check_call = self.check_call_patcher.start()
  24. self.open_patcher = patch(
  25. 'awscli.customizations.codedeploy.systems.open',
  26. mock_open(), create=True
  27. )
  28. self.open = self.open_patcher.start()
  29. self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy'
  30. self.config_file = 'conf.onpremises.yml'
  31. self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file)
  32. self.installer = 'codedeploy-agent.msi'
  33. self.bucket = 'bucket'
  34. self.key = 'key'
  35. self.region = 'us-east-1'
  36. self.body = 'install-script'
  37. self.reader = MagicMock()
  38. self.reader.read.return_value = self.body
  39. self.s3 = MagicMock()
  40. self.s3.get_object.return_value = {'Body': self.reader}
  41. self.session = MagicMock()
  42. self.session.create_client.return_value = self.s3
  43. self.params = Namespace()
  44. self.params.session = self.session
  45. self.params.region = self.region
  46. self.params.bucket = self.bucket
  47. self.params.key = self.key
  48. self.windows = Windows(self.params)
  49. def tearDown(self):
  50. self.popen_patcher.stop()
  51. self.check_call_patcher.stop()
  52. self.open_patcher.stop()
  53. def test_config_dir(self):
  54. self.assertEqual(self.config_dir, self.windows.CONFIG_DIR)
  55. def test_config_file(self):
  56. self.assertEquals(self.config_file, self.windows.CONFIG_FILE)
  57. def test_config_path(self):
  58. self.assertEquals(self.config_path, self.windows.CONFIG_PATH)
  59. def test_installer(self):
  60. self.assertEquals(self.installer, self.windows.INSTALLER)
  61. def test_install(self):
  62. process = MagicMock()
  63. process.communicate.side_effect = [('', ''), ('Running', '')]
  64. process.returncode = 0
  65. self.popen.return_value = process
  66. self.windows.install(self.params)
  67. self.popen.assert_has_calls([
  68. call(
  69. [
  70. 'powershell.exe',
  71. '-Command', 'Stop-Service',
  72. '-Name', 'codedeployagent'
  73. ],
  74. stdout=subprocess.PIPE,
  75. stderr=subprocess.PIPE
  76. ),
  77. call().communicate(),
  78. call(
  79. [
  80. 'powershell.exe',
  81. '-Command', 'Get-Service',
  82. '-Name', 'codedeployagent'
  83. ],
  84. stdout=subprocess.PIPE,
  85. stderr=subprocess.PIPE
  86. ),
  87. call().communicate()
  88. ])
  89. self.check_call.assert_has_calls([
  90. call(
  91. [
  92. r'.\{0}'.format(self.installer),
  93. '/quiet',
  94. '/l', r'.\codedeploy-agent-install-log.txt'
  95. ],
  96. shell=True
  97. ),
  98. call([
  99. 'powershell.exe',
  100. '-Command', 'Restart-Service',
  101. '-Name', 'codedeployagent'
  102. ])
  103. ])
  104. self.open.assert_called_with(self.installer, 'wb')
  105. self.open().write.assert_called_with(self.body)
  106. def test_uninstall(self):
  107. process = MagicMock()
  108. process.communicate.side_effect = [('', ''), ('', '')]
  109. process.returncode = 0
  110. self.popen.return_value = process
  111. self.windows.uninstall(self.params)
  112. self.popen.assert_has_calls([
  113. call(
  114. [
  115. 'powershell.exe',
  116. '-Command', 'Stop-Service',
  117. '-Name', 'codedeployagent'
  118. ],
  119. stdout=subprocess.PIPE,
  120. stderr=subprocess.PIPE
  121. ),
  122. call().communicate(),
  123. call(
  124. [
  125. 'wmic',
  126. 'product', 'where', 'name="CodeDeploy Host Agent"',
  127. 'call', 'uninstall', '/nointeractive'
  128. ],
  129. stdout=subprocess.PIPE,
  130. stderr=subprocess.PIPE
  131. ),
  132. call().communicate()
  133. ])
  134. class TestLinux(unittest.TestCase):
  135. def setUp(self):
  136. self.popen_patcher = patch('subprocess.Popen')
  137. self.popen = self.popen_patcher.start()
  138. self.check_call_patcher = patch('subprocess.check_call')
  139. self.check_call = self.check_call_patcher.start()
  140. self.open_patcher = patch(
  141. 'awscli.customizations.codedeploy.systems.open',
  142. mock_open(), create=True
  143. )
  144. self.open = self.open_patcher.start()
  145. self.environ_patcher = patch('os.environ')
  146. self.environ = self.environ_patcher.start()
  147. self.environ.copy.return_value = dict()
  148. self.config_dir = '/etc/codedeploy-agent/conf'
  149. self.config_file = 'codedeploy.onpremises.yml'
  150. self.config_path = '{0}/{1}'.format(self.config_dir, self.config_file)
  151. self.installer = 'install'
  152. self.bucket = 'bucket'
  153. self.key = 'key'
  154. self.region = 'us-east-1'
  155. self.access_key_id = 'ACCESSKEYID'
  156. self.secret_access_key = 'SECRETACCESSKEY'
  157. self.session_token = 'SESSION_TOKEN'
  158. self.credentials = MagicMock()
  159. self.credentials.access_key = self.access_key_id
  160. self.credentials.secret_key = self.secret_access_key
  161. self.credentials.token = self.session_token
  162. self.environment = dict({
  163. 'AWS_REGION': self.region,
  164. 'AWS_ACCESS_KEY_ID': self.access_key_id,
  165. 'AWS_SECRET_ACCESS_KEY': self.secret_access_key,
  166. 'AWS_SESSION_TOKEN': self.session_token
  167. })
  168. self.body = 'install-script'
  169. self.reader = MagicMock()
  170. self.reader.read.return_value = self.body
  171. self.s3 = MagicMock()
  172. self.s3.get_object.return_value = {'Body': self.reader}
  173. self.session = MagicMock()
  174. self.session.create_client.return_value = self.s3
  175. self.session.get_credentials.return_value = self.credentials
  176. self.params = Namespace()
  177. self.params.session = self.session
  178. self.params.region = self.region
  179. self.params.bucket = self.bucket
  180. self.params.key = self.key
  181. def tearDown(self):
  182. self.popen_patcher.stop()
  183. self.check_call_patcher.stop()
  184. self.open_patcher.stop()
  185. self.environ_patcher.stop()
  186. class TestUbuntu(TestLinux):
  187. def setUp(self):
  188. super(self.__class__, self).setUp()
  189. self.ubuntu = Ubuntu(self.params)
  190. def test_config_dir(self):
  191. self.assertEquals(self.config_dir, self.ubuntu.CONFIG_DIR)
  192. def test_config_file(self):
  193. self.assertEquals(self.config_file, self.ubuntu.CONFIG_FILE)
  194. def test_config_path(self):
  195. self.assertEquals(self.config_path, self.ubuntu.CONFIG_PATH)
  196. def test_installer(self):
  197. self.assertEquals(self.installer, self.ubuntu.INSTALLER)
  198. @patch('os.geteuid', create=True)
  199. def test_validate_administrator_throws(self, geteuid):
  200. geteuid.return_value = 1
  201. with self.assertRaisesRegexp(
  202. RuntimeError, 'You must run this command as sudo.'):
  203. self.ubuntu.validate_administrator()
  204. def test_install(self):
  205. process = MagicMock()
  206. process.communicate.return_value = ('', '')
  207. process.returncode = 0
  208. self.popen.return_value = process
  209. self.ubuntu.install(self.params)
  210. self.popen.assert_has_calls([
  211. call(
  212. ['service', 'codedeploy-agent', 'stop'],
  213. stdout=subprocess.PIPE,
  214. stderr=subprocess.PIPE
  215. ),
  216. call().communicate()
  217. ])
  218. self.check_call.assert_has_calls([
  219. call(['apt-get', '-y', 'update']),
  220. call(['apt-get', '-y', 'install', 'ruby2.0']),
  221. call(['chmod', '+x', './{0}'.format(self.installer)]),
  222. call(
  223. ['./{0}'.format(self.installer), 'auto'],
  224. env=self.environment
  225. )
  226. ])
  227. self.open.assert_called_with(self.installer, 'wb')
  228. self.open().write.assert_called_with(self.body)
  229. def test_uninstall(self):
  230. process = MagicMock()
  231. process.communicate.return_value = ('', '')
  232. process.returncode = 0
  233. self.popen.return_value = process
  234. self.ubuntu.uninstall(self.params)
  235. self.popen.assert_has_calls([
  236. call(
  237. ['service', 'codedeploy-agent', 'stop'],
  238. stdout=subprocess.PIPE,
  239. stderr=subprocess.PIPE
  240. ),
  241. call().communicate()
  242. ])
  243. self.check_call.assert_has_calls([
  244. call(['dpkg', '-r', 'codedeploy-agent'])
  245. ])
  246. class TestRHEL(TestLinux):
  247. def setUp(self):
  248. super(self.__class__, self).setUp()
  249. self.rhel = RHEL(self.params)
  250. def test_config_dir(self):
  251. self.assertEquals(self.config_dir, self.rhel.CONFIG_DIR)
  252. def test_config_file(self):
  253. self.assertEquals(self.config_file, self.rhel.CONFIG_FILE)
  254. def test_config_path(self):
  255. self.assertEquals(self.config_path, self.rhel.CONFIG_PATH)
  256. def test_installer(self):
  257. self.assertEquals(self.installer, self.rhel.INSTALLER)
  258. @patch('os.geteuid', create=True)
  259. def test_validate_administrator_throws(self, geteuid):
  260. geteuid.return_value = 1
  261. with self.assertRaisesRegexp(
  262. RuntimeError, 'You must run this command as sudo.'):
  263. self.rhel.validate_administrator()
  264. def test_install(self):
  265. process = MagicMock()
  266. process.communicate.return_value = ('', '')
  267. process.returncode = 0
  268. self.popen.return_value = process
  269. self.rhel.install(self.params)
  270. self.popen.assert_has_calls([
  271. call(
  272. ['service', 'codedeploy-agent', 'stop'],
  273. stdout=subprocess.PIPE,
  274. stderr=subprocess.PIPE
  275. ),
  276. call().communicate()
  277. ])
  278. self.check_call.assert_has_calls([
  279. call(['yum', '-y', 'install', 'ruby']),
  280. call(['chmod', '+x', './{0}'.format(self.installer)]),
  281. call(
  282. ['./{0}'.format(self.installer), 'auto'],
  283. env=self.environment
  284. )
  285. ])
  286. self.open.assert_called_with(self.installer, 'wb')
  287. self.open().write.assert_called_with(self.body)
  288. def test_uninstall(self):
  289. process = MagicMock()
  290. process.communicate.return_value = ('', '')
  291. process.returncode = 0
  292. self.popen.return_value = process
  293. self.rhel.uninstall(self.params)
  294. self.popen.assert_has_calls([
  295. call(
  296. ['service', 'codedeploy-agent', 'stop'],
  297. stdout=subprocess.PIPE,
  298. stderr=subprocess.PIPE
  299. ),
  300. call().communicate()
  301. ])
  302. self.check_call.assert_has_calls([
  303. call(['yum', '-y', 'erase', 'codedeploy-agent'])
  304. ])
  305. if __name__ == "__main__":
  306. unittest.main()