test_azure_helper.py 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. # This file is part of cloud-init. See LICENSE file for license information.
  2. import copy
  3. import os
  4. import re
  5. import unittest
  6. from textwrap import dedent
  7. from xml.etree import ElementTree
  8. from xml.sax.saxutils import escape, unescape
  9. from cloudinit.sources.helpers import azure as azure_helper
  10. from cloudinit.tests.helpers import CiTestCase, ExitStack, mock, populate_dir
  11. from cloudinit.util import load_file
  12. from cloudinit.sources.helpers.azure import WALinuxAgentShim as wa_shim
  13. GOAL_STATE_TEMPLATE = """\
  14. <?xml version="1.0" encoding="utf-8"?>
  15. <GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16. xsi:noNamespaceSchemaLocation="goalstate10.xsd">
  17. <Version>2012-11-30</Version>
  18. <Incarnation>{incarnation}</Incarnation>
  19. <Machine>
  20. <ExpectedState>Started</ExpectedState>
  21. <StopRolesDeadlineHint>300000</StopRolesDeadlineHint>
  22. <LBProbePorts>
  23. <Port>16001</Port>
  24. </LBProbePorts>
  25. <ExpectHealthReport>FALSE</ExpectHealthReport>
  26. </Machine>
  27. <Container>
  28. <ContainerId>{container_id}</ContainerId>
  29. <RoleInstanceList>
  30. <RoleInstance>
  31. <InstanceId>{instance_id}</InstanceId>
  32. <State>Started</State>
  33. <Configuration>
  34. <HostingEnvironmentConfig>
  35. http://100.86.192.70:80/...hostingEnvironmentConfig...
  36. </HostingEnvironmentConfig>
  37. <SharedConfig>http://100.86.192.70:80/..SharedConfig..</SharedConfig>
  38. <ExtensionsConfig>
  39. http://100.86.192.70:80/...extensionsConfig...
  40. </ExtensionsConfig>
  41. <FullConfig>http://100.86.192.70:80/...fullConfig...</FullConfig>
  42. <Certificates>{certificates_url}</Certificates>
  43. <ConfigName>68ce47.0.68ce47.0.utl-trusty--292258.1.xml</ConfigName>
  44. </Configuration>
  45. </RoleInstance>
  46. </RoleInstanceList>
  47. </Container>
  48. </GoalState>
  49. """
  50. HEALTH_REPORT_XML_TEMPLATE = '''\
  51. <?xml version="1.0" encoding="utf-8"?>
  52. <Health xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  54. <GoalStateIncarnation>{incarnation}</GoalStateIncarnation>
  55. <Container>
  56. <ContainerId>{container_id}</ContainerId>
  57. <RoleInstanceList>
  58. <Role>
  59. <InstanceId>{instance_id}</InstanceId>
  60. <Health>
  61. <State>{health_status}</State>
  62. {health_detail_subsection}
  63. </Health>
  64. </Role>
  65. </RoleInstanceList>
  66. </Container>
  67. </Health>
  68. '''
  69. HEALTH_DETAIL_SUBSECTION_XML_TEMPLATE = dedent('''\
  70. <Details>
  71. <SubStatus>{health_substatus}</SubStatus>
  72. <Description>{health_description}</Description>
  73. </Details>
  74. ''')
  75. HEALTH_REPORT_DESCRIPTION_TRIM_LEN = 512
  76. class SentinelException(Exception):
  77. pass
  78. class TestFindEndpoint(CiTestCase):
  79. def setUp(self):
  80. super(TestFindEndpoint, self).setUp()
  81. patches = ExitStack()
  82. self.addCleanup(patches.close)
  83. self.load_file = patches.enter_context(
  84. mock.patch.object(azure_helper.util, 'load_file'))
  85. self.dhcp_options = patches.enter_context(
  86. mock.patch.object(wa_shim, '_load_dhclient_json'))
  87. self.networkd_leases = patches.enter_context(
  88. mock.patch.object(wa_shim, '_networkd_get_value_from_leases'))
  89. self.networkd_leases.return_value = None
  90. def test_missing_file(self):
  91. """wa_shim find_endpoint uses default endpoint if leasefile not found
  92. """
  93. self.assertEqual(wa_shim.find_endpoint(), "168.63.129.16")
  94. def test_missing_special_azure_line(self):
  95. """wa_shim find_endpoint uses default endpoint if leasefile is found
  96. but does not contain DHCP Option 245 (whose value is the endpoint)
  97. """
  98. self.load_file.return_value = ''
  99. self.dhcp_options.return_value = {'eth0': {'key': 'value'}}
  100. self.assertEqual(wa_shim.find_endpoint(), "168.63.129.16")
  101. @staticmethod
  102. def _build_lease_content(encoded_address):
  103. endpoint = azure_helper._get_dhcp_endpoint_option_name()
  104. return '\n'.join([
  105. 'lease {',
  106. ' interface "eth0";',
  107. ' option {0} {1};'.format(endpoint, encoded_address),
  108. '}'])
  109. def test_from_dhcp_client(self):
  110. self.dhcp_options.return_value = {"eth0": {"unknown_245": "5:4:3:2"}}
  111. self.assertEqual('5.4.3.2', wa_shim.find_endpoint(None))
  112. @mock.patch('cloudinit.sources.helpers.azure.util.is_FreeBSD')
  113. def test_latest_lease_used(self, m_is_freebsd):
  114. m_is_freebsd.return_value = False # To avoid hitting load_file
  115. encoded_addresses = ['5:4:3:2', '4:3:2:1']
  116. file_content = '\n'.join([self._build_lease_content(encoded_address)
  117. for encoded_address in encoded_addresses])
  118. self.load_file.return_value = file_content
  119. self.assertEqual(encoded_addresses[-1].replace(':', '.'),
  120. wa_shim.find_endpoint("foobar"))
  121. class TestExtractIpAddressFromLeaseValue(CiTestCase):
  122. def test_hex_string(self):
  123. ip_address, encoded_address = '98.76.54.32', '62:4c:36:20'
  124. self.assertEqual(
  125. ip_address, wa_shim.get_ip_from_lease_value(encoded_address))
  126. def test_hex_string_with_single_character_part(self):
  127. ip_address, encoded_address = '4.3.2.1', '4:3:2:1'
  128. self.assertEqual(
  129. ip_address, wa_shim.get_ip_from_lease_value(encoded_address))
  130. def test_packed_string(self):
  131. ip_address, encoded_address = '98.76.54.32', 'bL6 '
  132. self.assertEqual(
  133. ip_address, wa_shim.get_ip_from_lease_value(encoded_address))
  134. def test_packed_string_with_escaped_quote(self):
  135. ip_address, encoded_address = '100.72.34.108', 'dH\\"l'
  136. self.assertEqual(
  137. ip_address, wa_shim.get_ip_from_lease_value(encoded_address))
  138. def test_packed_string_containing_a_colon(self):
  139. ip_address, encoded_address = '100.72.58.108', 'dH:l'
  140. self.assertEqual(
  141. ip_address, wa_shim.get_ip_from_lease_value(encoded_address))
  142. class TestGoalStateParsing(CiTestCase):
  143. default_parameters = {
  144. 'incarnation': 1,
  145. 'container_id': 'MyContainerId',
  146. 'instance_id': 'MyInstanceId',
  147. 'certificates_url': 'MyCertificatesUrl',
  148. }
  149. def _get_formatted_goal_state_xml_string(self, **kwargs):
  150. parameters = self.default_parameters.copy()
  151. parameters.update(kwargs)
  152. xml = GOAL_STATE_TEMPLATE.format(**parameters)
  153. if parameters['certificates_url'] is None:
  154. new_xml_lines = []
  155. for line in xml.splitlines():
  156. if 'Certificates' in line:
  157. continue
  158. new_xml_lines.append(line)
  159. xml = '\n'.join(new_xml_lines)
  160. return xml
  161. def _get_goal_state(self, m_azure_endpoint_client=None, **kwargs):
  162. if m_azure_endpoint_client is None:
  163. m_azure_endpoint_client = mock.MagicMock()
  164. xml = self._get_formatted_goal_state_xml_string(**kwargs)
  165. return azure_helper.GoalState(xml, m_azure_endpoint_client)
  166. def test_incarnation_parsed_correctly(self):
  167. incarnation = '123'
  168. goal_state = self._get_goal_state(incarnation=incarnation)
  169. self.assertEqual(incarnation, goal_state.incarnation)
  170. def test_container_id_parsed_correctly(self):
  171. container_id = 'TestContainerId'
  172. goal_state = self._get_goal_state(container_id=container_id)
  173. self.assertEqual(container_id, goal_state.container_id)
  174. def test_instance_id_parsed_correctly(self):
  175. instance_id = 'TestInstanceId'
  176. goal_state = self._get_goal_state(instance_id=instance_id)
  177. self.assertEqual(instance_id, goal_state.instance_id)
  178. def test_instance_id_byte_swap(self):
  179. """Return true when previous_iid is byteswapped current_iid"""
  180. previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
  181. current_iid = "544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8"
  182. self.assertTrue(
  183. azure_helper.is_byte_swapped(previous_iid, current_iid))
  184. def test_instance_id_no_byte_swap_same_instance_id(self):
  185. previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
  186. current_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
  187. self.assertFalse(
  188. azure_helper.is_byte_swapped(previous_iid, current_iid))
  189. def test_instance_id_no_byte_swap_diff_instance_id(self):
  190. previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
  191. current_iid = "G0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
  192. self.assertFalse(
  193. azure_helper.is_byte_swapped(previous_iid, current_iid))
  194. def test_certificates_xml_parsed_and_fetched_correctly(self):
  195. m_azure_endpoint_client = mock.MagicMock()
  196. certificates_url = 'TestCertificatesUrl'
  197. goal_state = self._get_goal_state(
  198. m_azure_endpoint_client=m_azure_endpoint_client,
  199. certificates_url=certificates_url)
  200. certificates_xml = goal_state.certificates_xml
  201. self.assertEqual(1, m_azure_endpoint_client.get.call_count)
  202. self.assertEqual(
  203. certificates_url,
  204. m_azure_endpoint_client.get.call_args[0][0])
  205. self.assertTrue(
  206. m_azure_endpoint_client.get.call_args[1].get(
  207. 'secure', False))
  208. self.assertEqual(
  209. m_azure_endpoint_client.get.return_value.contents,
  210. certificates_xml)
  211. def test_missing_certificates_skips_http_get(self):
  212. m_azure_endpoint_client = mock.MagicMock()
  213. goal_state = self._get_goal_state(
  214. m_azure_endpoint_client=m_azure_endpoint_client,
  215. certificates_url=None)
  216. certificates_xml = goal_state.certificates_xml
  217. self.assertEqual(0, m_azure_endpoint_client.get.call_count)
  218. self.assertIsNone(certificates_xml)
  219. def test_invalid_goal_state_xml_raises_parse_error(self):
  220. xml = 'random non-xml data'
  221. with self.assertRaises(ElementTree.ParseError):
  222. azure_helper.GoalState(xml, mock.MagicMock())
  223. def test_missing_container_id_in_goal_state_xml_raises_exc(self):
  224. xml = self._get_formatted_goal_state_xml_string()
  225. xml = re.sub('<ContainerId>.*</ContainerId>', '', xml)
  226. with self.assertRaises(azure_helper.InvalidGoalStateXMLException):
  227. azure_helper.GoalState(xml, mock.MagicMock())
  228. def test_missing_instance_id_in_goal_state_xml_raises_exc(self):
  229. xml = self._get_formatted_goal_state_xml_string()
  230. xml = re.sub('<InstanceId>.*</InstanceId>', '', xml)
  231. with self.assertRaises(azure_helper.InvalidGoalStateXMLException):
  232. azure_helper.GoalState(xml, mock.MagicMock())
  233. def test_missing_incarnation_in_goal_state_xml_raises_exc(self):
  234. xml = self._get_formatted_goal_state_xml_string()
  235. xml = re.sub('<Incarnation>.*</Incarnation>', '', xml)
  236. with self.assertRaises(azure_helper.InvalidGoalStateXMLException):
  237. azure_helper.GoalState(xml, mock.MagicMock())
  238. class TestAzureEndpointHttpClient(CiTestCase):
  239. regular_headers = {
  240. 'x-ms-agent-name': 'WALinuxAgent',
  241. 'x-ms-version': '2012-11-30',
  242. }
  243. def setUp(self):
  244. super(TestAzureEndpointHttpClient, self).setUp()
  245. patches = ExitStack()
  246. self.addCleanup(patches.close)
  247. self.m_http_with_retries = patches.enter_context(
  248. mock.patch.object(azure_helper, 'http_with_retries'))
  249. def test_non_secure_get(self):
  250. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  251. url = 'MyTestUrl'
  252. response = client.get(url, secure=False)
  253. self.assertEqual(1, self.m_http_with_retries.call_count)
  254. self.assertEqual(self.m_http_with_retries.return_value, response)
  255. self.assertEqual(
  256. mock.call(url, headers=self.regular_headers),
  257. self.m_http_with_retries.call_args)
  258. def test_non_secure_get_raises_exception(self):
  259. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  260. url = 'MyTestUrl'
  261. self.m_http_with_retries.side_effect = SentinelException
  262. self.assertRaises(SentinelException, client.get, url, secure=False)
  263. self.assertEqual(1, self.m_http_with_retries.call_count)
  264. def test_secure_get(self):
  265. url = 'MyTestUrl'
  266. m_certificate = mock.MagicMock()
  267. expected_headers = self.regular_headers.copy()
  268. expected_headers.update({
  269. "x-ms-cipher-name": "DES_EDE3_CBC",
  270. "x-ms-guest-agent-public-x509-cert": m_certificate,
  271. })
  272. client = azure_helper.AzureEndpointHttpClient(m_certificate)
  273. response = client.get(url, secure=True)
  274. self.assertEqual(1, self.m_http_with_retries.call_count)
  275. self.assertEqual(self.m_http_with_retries.return_value, response)
  276. self.assertEqual(
  277. mock.call(url, headers=expected_headers),
  278. self.m_http_with_retries.call_args)
  279. def test_secure_get_raises_exception(self):
  280. url = 'MyTestUrl'
  281. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  282. self.m_http_with_retries.side_effect = SentinelException
  283. self.assertRaises(SentinelException, client.get, url, secure=True)
  284. self.assertEqual(1, self.m_http_with_retries.call_count)
  285. def test_post(self):
  286. m_data = mock.MagicMock()
  287. url = 'MyTestUrl'
  288. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  289. response = client.post(url, data=m_data)
  290. self.assertEqual(1, self.m_http_with_retries.call_count)
  291. self.assertEqual(self.m_http_with_retries.return_value, response)
  292. self.assertEqual(
  293. mock.call(url, data=m_data, headers=self.regular_headers),
  294. self.m_http_with_retries.call_args)
  295. def test_post_raises_exception(self):
  296. m_data = mock.MagicMock()
  297. url = 'MyTestUrl'
  298. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  299. self.m_http_with_retries.side_effect = SentinelException
  300. self.assertRaises(SentinelException, client.post, url, data=m_data)
  301. self.assertEqual(1, self.m_http_with_retries.call_count)
  302. def test_post_with_extra_headers(self):
  303. url = 'MyTestUrl'
  304. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  305. extra_headers = {'test': 'header'}
  306. client.post(url, extra_headers=extra_headers)
  307. expected_headers = self.regular_headers.copy()
  308. expected_headers.update(extra_headers)
  309. self.assertEqual(1, self.m_http_with_retries.call_count)
  310. self.assertEqual(
  311. mock.call(url, data=mock.ANY, headers=expected_headers),
  312. self.m_http_with_retries.call_args)
  313. def test_post_with_sleep_with_extra_headers_raises_exception(self):
  314. m_data = mock.MagicMock()
  315. url = 'MyTestUrl'
  316. extra_headers = {'test': 'header'}
  317. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  318. self.m_http_with_retries.side_effect = SentinelException
  319. self.assertRaises(
  320. SentinelException, client.post,
  321. url, data=m_data, extra_headers=extra_headers)
  322. self.assertEqual(1, self.m_http_with_retries.call_count)
  323. class TestAzureHelperHttpWithRetries(CiTestCase):
  324. with_logs = True
  325. max_readurl_attempts = 240
  326. default_readurl_timeout = 5
  327. periodic_logging_attempts = 12
  328. def setUp(self):
  329. super(TestAzureHelperHttpWithRetries, self).setUp()
  330. patches = ExitStack()
  331. self.addCleanup(patches.close)
  332. self.m_readurl = patches.enter_context(
  333. mock.patch.object(
  334. azure_helper.url_helper, 'readurl', mock.MagicMock()))
  335. patches.enter_context(
  336. mock.patch.object(azure_helper.time, 'sleep', mock.MagicMock()))
  337. def test_http_with_retries(self):
  338. self.m_readurl.return_value = 'TestResp'
  339. self.assertEqual(
  340. azure_helper.http_with_retries('testurl'),
  341. self.m_readurl.return_value)
  342. self.assertEqual(self.m_readurl.call_count, 1)
  343. def test_http_with_retries_propagates_readurl_exc_and_logs_exc(
  344. self):
  345. self.m_readurl.side_effect = SentinelException
  346. self.assertRaises(
  347. SentinelException, azure_helper.http_with_retries, 'testurl')
  348. self.assertEqual(self.m_readurl.call_count, self.max_readurl_attempts)
  349. self.assertIsNotNone(
  350. re.search(
  351. r'Failed HTTP request with Azure endpoint \S* during '
  352. r'attempt \d+ with exception: \S*',
  353. self.logs.getvalue()))
  354. self.assertIsNone(
  355. re.search(
  356. r'Successful HTTP request with Azure endpoint \S* after '
  357. r'\d+ attempts',
  358. self.logs.getvalue()))
  359. def test_http_with_retries_delayed_success_due_to_temporary_readurl_exc(
  360. self):
  361. self.m_readurl.side_effect = \
  362. [SentinelException] * self.periodic_logging_attempts + \
  363. ['TestResp']
  364. self.m_readurl.return_value = 'TestResp'
  365. response = azure_helper.http_with_retries('testurl')
  366. self.assertEqual(
  367. response,
  368. self.m_readurl.return_value)
  369. self.assertEqual(
  370. self.m_readurl.call_count,
  371. self.periodic_logging_attempts + 1)
  372. def test_http_with_retries_long_delay_logs_periodic_failure_msg(self):
  373. self.m_readurl.side_effect = \
  374. [SentinelException] * self.periodic_logging_attempts + \
  375. ['TestResp']
  376. self.m_readurl.return_value = 'TestResp'
  377. azure_helper.http_with_retries('testurl')
  378. self.assertEqual(
  379. self.m_readurl.call_count,
  380. self.periodic_logging_attempts + 1)
  381. self.assertIsNotNone(
  382. re.search(
  383. r'Failed HTTP request with Azure endpoint \S* during '
  384. r'attempt \d+ with exception: \S*',
  385. self.logs.getvalue()))
  386. self.assertIsNotNone(
  387. re.search(
  388. r'Successful HTTP request with Azure endpoint \S* after '
  389. r'\d+ attempts',
  390. self.logs.getvalue()))
  391. def test_http_with_retries_short_delay_does_not_log_periodic_failure_msg(
  392. self):
  393. self.m_readurl.side_effect = \
  394. [SentinelException] * \
  395. (self.periodic_logging_attempts - 1) + \
  396. ['TestResp']
  397. self.m_readurl.return_value = 'TestResp'
  398. azure_helper.http_with_retries('testurl')
  399. self.assertEqual(
  400. self.m_readurl.call_count,
  401. self.periodic_logging_attempts)
  402. self.assertIsNone(
  403. re.search(
  404. r'Failed HTTP request with Azure endpoint \S* during '
  405. r'attempt \d+ with exception: \S*',
  406. self.logs.getvalue()))
  407. self.assertIsNotNone(
  408. re.search(
  409. r'Successful HTTP request with Azure endpoint \S* after '
  410. r'\d+ attempts',
  411. self.logs.getvalue()))
  412. def test_http_with_retries_calls_url_helper_readurl_with_args_kwargs(self):
  413. testurl = mock.MagicMock()
  414. kwargs = {
  415. 'headers': mock.MagicMock(),
  416. 'data': mock.MagicMock(),
  417. # timeout kwarg should not be modified or deleted if present
  418. 'timeout': mock.MagicMock()
  419. }
  420. azure_helper.http_with_retries(testurl, **kwargs)
  421. self.m_readurl.assert_called_once_with(testurl, **kwargs)
  422. def test_http_with_retries_adds_timeout_kwarg_if_not_present(self):
  423. testurl = mock.MagicMock()
  424. kwargs = {
  425. 'headers': mock.MagicMock(),
  426. 'data': mock.MagicMock()
  427. }
  428. expected_kwargs = copy.deepcopy(kwargs)
  429. expected_kwargs['timeout'] = self.default_readurl_timeout
  430. azure_helper.http_with_retries(testurl, **kwargs)
  431. self.m_readurl.assert_called_once_with(testurl, **expected_kwargs)
  432. def test_http_with_retries_deletes_retries_kwargs_passed_in(
  433. self):
  434. """http_with_retries already implements retry logic,
  435. so url_helper.readurl should not have retries.
  436. http_with_retries should delete kwargs that
  437. cause url_helper.readurl to retry.
  438. """
  439. testurl = mock.MagicMock()
  440. kwargs = {
  441. 'headers': mock.MagicMock(),
  442. 'data': mock.MagicMock(),
  443. 'timeout': mock.MagicMock(),
  444. 'retries': mock.MagicMock(),
  445. 'infinite': mock.MagicMock()
  446. }
  447. expected_kwargs = copy.deepcopy(kwargs)
  448. expected_kwargs.pop('retries', None)
  449. expected_kwargs.pop('infinite', None)
  450. azure_helper.http_with_retries(testurl, **kwargs)
  451. self.m_readurl.assert_called_once_with(testurl, **expected_kwargs)
  452. self.assertIn(
  453. 'retries kwarg passed in for communication with Azure endpoint.',
  454. self.logs.getvalue())
  455. self.assertIn(
  456. 'infinite kwarg passed in for communication with Azure endpoint.',
  457. self.logs.getvalue())
  458. class TestOpenSSLManager(CiTestCase):
  459. def setUp(self):
  460. super(TestOpenSSLManager, self).setUp()
  461. patches = ExitStack()
  462. self.addCleanup(patches.close)
  463. self.subp = patches.enter_context(
  464. mock.patch.object(azure_helper.subp, 'subp'))
  465. try:
  466. self.open = patches.enter_context(
  467. mock.patch('__builtin__.open'))
  468. except ImportError:
  469. self.open = patches.enter_context(
  470. mock.patch('builtins.open'))
  471. @mock.patch.object(azure_helper, 'cd', mock.MagicMock())
  472. @mock.patch.object(azure_helper.temp_utils, 'mkdtemp')
  473. def test_openssl_manager_creates_a_tmpdir(self, mkdtemp):
  474. manager = azure_helper.OpenSSLManager()
  475. self.assertEqual(mkdtemp.return_value, manager.tmpdir)
  476. def test_generate_certificate_uses_tmpdir(self):
  477. subp_directory = {}
  478. def capture_directory(*args, **kwargs):
  479. subp_directory['path'] = os.getcwd()
  480. self.subp.side_effect = capture_directory
  481. manager = azure_helper.OpenSSLManager()
  482. self.assertEqual(manager.tmpdir, subp_directory['path'])
  483. manager.clean_up()
  484. @mock.patch.object(azure_helper, 'cd', mock.MagicMock())
  485. @mock.patch.object(azure_helper.temp_utils, 'mkdtemp', mock.MagicMock())
  486. @mock.patch.object(azure_helper.util, 'del_dir')
  487. def test_clean_up(self, del_dir):
  488. manager = azure_helper.OpenSSLManager()
  489. manager.clean_up()
  490. self.assertEqual([mock.call(manager.tmpdir)], del_dir.call_args_list)
  491. class TestOpenSSLManagerActions(CiTestCase):
  492. def setUp(self):
  493. super(TestOpenSSLManagerActions, self).setUp()
  494. self.allowed_subp = True
  495. def _data_file(self, name):
  496. path = 'tests/data/azure'
  497. return os.path.join(path, name)
  498. @unittest.skip("todo move to cloud_test")
  499. def test_pubkey_extract(self):
  500. cert = load_file(self._data_file('pubkey_extract_cert'))
  501. good_key = load_file(self._data_file('pubkey_extract_ssh_key'))
  502. sslmgr = azure_helper.OpenSSLManager()
  503. key = sslmgr._get_ssh_key_from_cert(cert)
  504. self.assertEqual(good_key, key)
  505. good_fingerprint = '073E19D14D1C799224C6A0FD8DDAB6A8BF27D473'
  506. fingerprint = sslmgr._get_fingerprint_from_cert(cert)
  507. self.assertEqual(good_fingerprint, fingerprint)
  508. @unittest.skip("todo move to cloud_test")
  509. @mock.patch.object(azure_helper.OpenSSLManager, '_decrypt_certs_from_xml')
  510. def test_parse_certificates(self, mock_decrypt_certs):
  511. """Azure control plane puts private keys as well as certificates
  512. into the Certificates XML object. Make sure only the public keys
  513. from certs are extracted and that fingerprints are converted to
  514. the form specified in the ovf-env.xml file.
  515. """
  516. cert_contents = load_file(self._data_file('parse_certificates_pem'))
  517. fingerprints = load_file(self._data_file(
  518. 'parse_certificates_fingerprints')
  519. ).splitlines()
  520. mock_decrypt_certs.return_value = cert_contents
  521. sslmgr = azure_helper.OpenSSLManager()
  522. keys_by_fp = sslmgr.parse_certificates('')
  523. for fp in keys_by_fp.keys():
  524. self.assertIn(fp, fingerprints)
  525. for fp in fingerprints:
  526. self.assertIn(fp, keys_by_fp)
  527. class TestGoalStateHealthReporter(CiTestCase):
  528. maxDiff = None
  529. default_parameters = {
  530. 'incarnation': 1634,
  531. 'container_id': 'MyContainerId',
  532. 'instance_id': 'MyInstanceId'
  533. }
  534. test_azure_endpoint = 'TestEndpoint'
  535. test_health_report_url = 'http://{0}/machine?comp=health'.format(
  536. test_azure_endpoint)
  537. test_default_headers = {'Content-Type': 'text/xml; charset=utf-8'}
  538. provisioning_success_status = 'Ready'
  539. provisioning_not_ready_status = 'NotReady'
  540. provisioning_failure_substatus = 'ProvisioningFailed'
  541. provisioning_failure_err_description = (
  542. 'Test error message containing provisioning failure details')
  543. def setUp(self):
  544. super(TestGoalStateHealthReporter, self).setUp()
  545. patches = ExitStack()
  546. self.addCleanup(patches.close)
  547. patches.enter_context(
  548. mock.patch.object(azure_helper.time, 'sleep', mock.MagicMock()))
  549. self.read_file_or_url = patches.enter_context(
  550. mock.patch.object(azure_helper.url_helper, 'read_file_or_url'))
  551. self.post = patches.enter_context(
  552. mock.patch.object(azure_helper.AzureEndpointHttpClient,
  553. 'post'))
  554. self.GoalState = patches.enter_context(
  555. mock.patch.object(azure_helper, 'GoalState'))
  556. self.GoalState.return_value.container_id = \
  557. self.default_parameters['container_id']
  558. self.GoalState.return_value.instance_id = \
  559. self.default_parameters['instance_id']
  560. self.GoalState.return_value.incarnation = \
  561. self.default_parameters['incarnation']
  562. def _text_from_xpath_in_xroot(self, xroot, xpath):
  563. element = xroot.find(xpath)
  564. if element is not None:
  565. return element.text
  566. return None
  567. def _get_formatted_health_report_xml_string(self, **kwargs):
  568. return HEALTH_REPORT_XML_TEMPLATE.format(**kwargs)
  569. def _get_formatted_health_detail_subsection_xml_string(self, **kwargs):
  570. return HEALTH_DETAIL_SUBSECTION_XML_TEMPLATE.format(**kwargs)
  571. def _get_report_ready_health_document(self):
  572. return self._get_formatted_health_report_xml_string(
  573. incarnation=escape(str(self.default_parameters['incarnation'])),
  574. container_id=escape(self.default_parameters['container_id']),
  575. instance_id=escape(self.default_parameters['instance_id']),
  576. health_status=escape(self.provisioning_success_status),
  577. health_detail_subsection='')
  578. def _get_report_failure_health_document(self):
  579. health_detail_subsection = \
  580. self._get_formatted_health_detail_subsection_xml_string(
  581. health_substatus=escape(self.provisioning_failure_substatus),
  582. health_description=escape(
  583. self.provisioning_failure_err_description))
  584. return self._get_formatted_health_report_xml_string(
  585. incarnation=escape(str(self.default_parameters['incarnation'])),
  586. container_id=escape(self.default_parameters['container_id']),
  587. instance_id=escape(self.default_parameters['instance_id']),
  588. health_status=escape(self.provisioning_not_ready_status),
  589. health_detail_subsection=health_detail_subsection)
  590. def test_send_ready_signal_sends_post_request(self):
  591. with mock.patch.object(
  592. azure_helper.GoalStateHealthReporter,
  593. 'build_report') as m_build_report:
  594. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  595. reporter = azure_helper.GoalStateHealthReporter(
  596. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  597. client, self.test_azure_endpoint)
  598. reporter.send_ready_signal()
  599. self.assertEqual(1, self.post.call_count)
  600. self.assertEqual(
  601. mock.call(
  602. self.test_health_report_url,
  603. data=m_build_report.return_value,
  604. extra_headers=self.test_default_headers),
  605. self.post.call_args)
  606. def test_send_failure_signal_sends_post_request(self):
  607. with mock.patch.object(
  608. azure_helper.GoalStateHealthReporter,
  609. 'build_report') as m_build_report:
  610. client = azure_helper.AzureEndpointHttpClient(mock.MagicMock())
  611. reporter = azure_helper.GoalStateHealthReporter(
  612. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  613. client, self.test_azure_endpoint)
  614. reporter.send_failure_signal(
  615. description=self.provisioning_failure_err_description)
  616. self.assertEqual(1, self.post.call_count)
  617. self.assertEqual(
  618. mock.call(
  619. self.test_health_report_url,
  620. data=m_build_report.return_value,
  621. extra_headers=self.test_default_headers),
  622. self.post.call_args)
  623. def test_build_report_for_ready_signal_health_document(self):
  624. health_document = self._get_report_ready_health_document()
  625. reporter = azure_helper.GoalStateHealthReporter(
  626. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  627. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  628. self.test_azure_endpoint)
  629. generated_health_document = reporter.build_report(
  630. incarnation=self.default_parameters['incarnation'],
  631. container_id=self.default_parameters['container_id'],
  632. instance_id=self.default_parameters['instance_id'],
  633. status=self.provisioning_success_status)
  634. self.assertEqual(health_document, generated_health_document)
  635. generated_xroot = ElementTree.fromstring(generated_health_document)
  636. self.assertEqual(
  637. self._text_from_xpath_in_xroot(
  638. generated_xroot, './GoalStateIncarnation'),
  639. str(self.default_parameters['incarnation']))
  640. self.assertEqual(
  641. self._text_from_xpath_in_xroot(
  642. generated_xroot, './Container/ContainerId'),
  643. str(self.default_parameters['container_id']))
  644. self.assertEqual(
  645. self._text_from_xpath_in_xroot(
  646. generated_xroot,
  647. './Container/RoleInstanceList/Role/InstanceId'),
  648. str(self.default_parameters['instance_id']))
  649. self.assertEqual(
  650. self._text_from_xpath_in_xroot(
  651. generated_xroot,
  652. './Container/RoleInstanceList/Role/Health/State'),
  653. escape(self.provisioning_success_status))
  654. self.assertIsNone(
  655. self._text_from_xpath_in_xroot(
  656. generated_xroot,
  657. './Container/RoleInstanceList/Role/Health/Details'))
  658. self.assertIsNone(
  659. self._text_from_xpath_in_xroot(
  660. generated_xroot,
  661. './Container/RoleInstanceList/Role/Health/Details/SubStatus'))
  662. self.assertIsNone(
  663. self._text_from_xpath_in_xroot(
  664. generated_xroot,
  665. './Container/RoleInstanceList/Role/Health/Details/Description')
  666. )
  667. def test_build_report_for_failure_signal_health_document(self):
  668. health_document = self._get_report_failure_health_document()
  669. reporter = azure_helper.GoalStateHealthReporter(
  670. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  671. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  672. self.test_azure_endpoint)
  673. generated_health_document = reporter.build_report(
  674. incarnation=self.default_parameters['incarnation'],
  675. container_id=self.default_parameters['container_id'],
  676. instance_id=self.default_parameters['instance_id'],
  677. status=self.provisioning_not_ready_status,
  678. substatus=self.provisioning_failure_substatus,
  679. description=self.provisioning_failure_err_description)
  680. self.assertEqual(health_document, generated_health_document)
  681. generated_xroot = ElementTree.fromstring(generated_health_document)
  682. self.assertEqual(
  683. self._text_from_xpath_in_xroot(
  684. generated_xroot, './GoalStateIncarnation'),
  685. str(self.default_parameters['incarnation']))
  686. self.assertEqual(
  687. self._text_from_xpath_in_xroot(
  688. generated_xroot, './Container/ContainerId'),
  689. self.default_parameters['container_id'])
  690. self.assertEqual(
  691. self._text_from_xpath_in_xroot(
  692. generated_xroot,
  693. './Container/RoleInstanceList/Role/InstanceId'),
  694. self.default_parameters['instance_id'])
  695. self.assertEqual(
  696. self._text_from_xpath_in_xroot(
  697. generated_xroot,
  698. './Container/RoleInstanceList/Role/Health/State'),
  699. escape(self.provisioning_not_ready_status))
  700. self.assertEqual(
  701. self._text_from_xpath_in_xroot(
  702. generated_xroot,
  703. './Container/RoleInstanceList/Role/Health/Details/'
  704. 'SubStatus'),
  705. escape(self.provisioning_failure_substatus))
  706. self.assertEqual(
  707. self._text_from_xpath_in_xroot(
  708. generated_xroot,
  709. './Container/RoleInstanceList/Role/Health/Details/'
  710. 'Description'),
  711. escape(self.provisioning_failure_err_description))
  712. def test_send_ready_signal_calls_build_report(self):
  713. with mock.patch.object(
  714. azure_helper.GoalStateHealthReporter, 'build_report'
  715. ) as m_build_report:
  716. reporter = azure_helper.GoalStateHealthReporter(
  717. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  718. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  719. self.test_azure_endpoint)
  720. reporter.send_ready_signal()
  721. self.assertEqual(1, m_build_report.call_count)
  722. self.assertEqual(
  723. mock.call(
  724. incarnation=self.default_parameters['incarnation'],
  725. container_id=self.default_parameters['container_id'],
  726. instance_id=self.default_parameters['instance_id'],
  727. status=self.provisioning_success_status),
  728. m_build_report.call_args)
  729. def test_send_failure_signal_calls_build_report(self):
  730. with mock.patch.object(
  731. azure_helper.GoalStateHealthReporter, 'build_report'
  732. ) as m_build_report:
  733. reporter = azure_helper.GoalStateHealthReporter(
  734. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  735. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  736. self.test_azure_endpoint)
  737. reporter.send_failure_signal(
  738. description=self.provisioning_failure_err_description)
  739. self.assertEqual(1, m_build_report.call_count)
  740. self.assertEqual(
  741. mock.call(
  742. incarnation=self.default_parameters['incarnation'],
  743. container_id=self.default_parameters['container_id'],
  744. instance_id=self.default_parameters['instance_id'],
  745. status=self.provisioning_not_ready_status,
  746. substatus=self.provisioning_failure_substatus,
  747. description=self.provisioning_failure_err_description),
  748. m_build_report.call_args)
  749. def test_build_report_escapes_chars(self):
  750. incarnation = 'jd8\'9*&^<\'A><A[p&o+\"SD()*&&&LKAJSD23'
  751. container_id = '&&<\"><><ds8\'9+7&d9a86!@($09asdl;<>'
  752. instance_id = 'Opo>>>jas\'&d;[p&fp\"a<<!!@&&'
  753. health_status = '&<897\"6&>&aa\'sd!@&!)((*<&>'
  754. health_substatus = '&as\"d<<a&s>d<\'^@!5&6<7'
  755. health_description = '&&&>!#$\"&&<as\'1!@$d&>><>&\"sd<67<]>>'
  756. health_detail_subsection = \
  757. self._get_formatted_health_detail_subsection_xml_string(
  758. health_substatus=escape(health_substatus),
  759. health_description=escape(health_description))
  760. health_document = self._get_formatted_health_report_xml_string(
  761. incarnation=escape(incarnation),
  762. container_id=escape(container_id),
  763. instance_id=escape(instance_id),
  764. health_status=escape(health_status),
  765. health_detail_subsection=health_detail_subsection)
  766. reporter = azure_helper.GoalStateHealthReporter(
  767. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  768. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  769. self.test_azure_endpoint)
  770. generated_health_document = reporter.build_report(
  771. incarnation=incarnation,
  772. container_id=container_id,
  773. instance_id=instance_id,
  774. status=health_status,
  775. substatus=health_substatus,
  776. description=health_description)
  777. self.assertEqual(health_document, generated_health_document)
  778. def test_build_report_conforms_to_length_limits(self):
  779. reporter = azure_helper.GoalStateHealthReporter(
  780. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  781. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  782. self.test_azure_endpoint)
  783. long_err_msg = 'a9&ea8>>>e as1< d\"q2*&(^%\'a=5<' * 100
  784. generated_health_document = reporter.build_report(
  785. incarnation=self.default_parameters['incarnation'],
  786. container_id=self.default_parameters['container_id'],
  787. instance_id=self.default_parameters['instance_id'],
  788. status=self.provisioning_not_ready_status,
  789. substatus=self.provisioning_failure_substatus,
  790. description=long_err_msg)
  791. generated_xroot = ElementTree.fromstring(generated_health_document)
  792. generated_health_report_description = self._text_from_xpath_in_xroot(
  793. generated_xroot,
  794. './Container/RoleInstanceList/Role/Health/Details/Description')
  795. self.assertEqual(
  796. len(unescape(generated_health_report_description)),
  797. HEALTH_REPORT_DESCRIPTION_TRIM_LEN)
  798. def test_trim_description_then_escape_conforms_to_len_limits_worst_case(
  799. self):
  800. """When unescaped characters are XML-escaped, the length increases.
  801. Char Escape String
  802. < &lt;
  803. > &gt;
  804. " &quot;
  805. ' &apos;
  806. & &amp;
  807. We (step 1) trim the health report XML's description field,
  808. and then (step 2) XML-escape the health report XML's description field.
  809. The health report XML's description field limit within cloud-init
  810. is HEALTH_REPORT_DESCRIPTION_TRIM_LEN.
  811. The Azure platform's limit on the health report XML's description field
  812. is 4096 chars.
  813. For worst-case chars, there is a 5x blowup in length
  814. when the chars are XML-escaped.
  815. ' and " when XML-escaped have a 5x blowup.
  816. Ensure that (1) trimming and then (2) XML-escaping does not blow past
  817. the Azure platform's limit for health report XML's description field
  818. (4096 chars).
  819. """
  820. reporter = azure_helper.GoalStateHealthReporter(
  821. azure_helper.GoalState(mock.MagicMock(), mock.MagicMock()),
  822. azure_helper.AzureEndpointHttpClient(mock.MagicMock()),
  823. self.test_azure_endpoint)
  824. long_err_msg = '\'\"' * 10000
  825. generated_health_document = reporter.build_report(
  826. incarnation=self.default_parameters['incarnation'],
  827. container_id=self.default_parameters['container_id'],
  828. instance_id=self.default_parameters['instance_id'],
  829. status=self.provisioning_not_ready_status,
  830. substatus=self.provisioning_failure_substatus,
  831. description=long_err_msg)
  832. generated_xroot = ElementTree.fromstring(generated_health_document)
  833. generated_health_report_description = self._text_from_xpath_in_xroot(
  834. generated_xroot,
  835. './Container/RoleInstanceList/Role/Health/Details/Description')
  836. # The escaped description string should be less than
  837. # the Azure platform limit for the escaped description string.
  838. self.assertLessEqual(len(generated_health_report_description), 4096)
  839. class TestWALinuxAgentShim(CiTestCase):
  840. def setUp(self):
  841. super(TestWALinuxAgentShim, self).setUp()
  842. patches = ExitStack()
  843. self.addCleanup(patches.close)
  844. self.AzureEndpointHttpClient = patches.enter_context(
  845. mock.patch.object(azure_helper, 'AzureEndpointHttpClient'))
  846. self.find_endpoint = patches.enter_context(
  847. mock.patch.object(wa_shim, 'find_endpoint'))
  848. self.GoalState = patches.enter_context(
  849. mock.patch.object(azure_helper, 'GoalState'))
  850. self.OpenSSLManager = patches.enter_context(
  851. mock.patch.object(azure_helper, 'OpenSSLManager', autospec=True))
  852. patches.enter_context(
  853. mock.patch.object(azure_helper.time, 'sleep', mock.MagicMock()))
  854. self.test_incarnation = 'TestIncarnation'
  855. self.test_container_id = 'TestContainerId'
  856. self.test_instance_id = 'TestInstanceId'
  857. self.GoalState.return_value.incarnation = self.test_incarnation
  858. self.GoalState.return_value.container_id = self.test_container_id
  859. self.GoalState.return_value.instance_id = self.test_instance_id
  860. def test_http_client_does_not_use_certificate_for_report_ready(self):
  861. shim = wa_shim()
  862. shim.register_with_azure_and_fetch_data()
  863. self.assertEqual(
  864. [mock.call(None)],
  865. self.AzureEndpointHttpClient.call_args_list)
  866. def test_http_client_does_not_use_certificate_for_report_failure(self):
  867. shim = wa_shim()
  868. shim.register_with_azure_and_report_failure(description='TestDesc')
  869. self.assertEqual(
  870. [mock.call(None)],
  871. self.AzureEndpointHttpClient.call_args_list)
  872. def test_correct_url_used_for_goalstate_during_report_ready(self):
  873. self.find_endpoint.return_value = 'test_endpoint'
  874. shim = wa_shim()
  875. shim.register_with_azure_and_fetch_data()
  876. m_get = self.AzureEndpointHttpClient.return_value.get
  877. self.assertEqual(
  878. [mock.call('http://test_endpoint/machine/?comp=goalstate')],
  879. m_get.call_args_list)
  880. self.assertEqual(
  881. [mock.call(
  882. m_get.return_value.contents,
  883. self.AzureEndpointHttpClient.return_value,
  884. False
  885. )],
  886. self.GoalState.call_args_list)
  887. def test_correct_url_used_for_goalstate_during_report_failure(self):
  888. self.find_endpoint.return_value = 'test_endpoint'
  889. shim = wa_shim()
  890. shim.register_with_azure_and_report_failure(description='TestDesc')
  891. m_get = self.AzureEndpointHttpClient.return_value.get
  892. self.assertEqual(
  893. [mock.call('http://test_endpoint/machine/?comp=goalstate')],
  894. m_get.call_args_list)
  895. self.assertEqual(
  896. [mock.call(
  897. m_get.return_value.contents,
  898. self.AzureEndpointHttpClient.return_value,
  899. False
  900. )],
  901. self.GoalState.call_args_list)
  902. def test_certificates_used_to_determine_public_keys(self):
  903. # if register_with_azure_and_fetch_data() isn't passed some info about
  904. # the user's public keys, there's no point in even trying to parse the
  905. # certificates
  906. shim = wa_shim()
  907. mypk = [{'fingerprint': 'fp1', 'path': 'path1'},
  908. {'fingerprint': 'fp3', 'path': 'path3', 'value': ''}]
  909. certs = {'fp1': 'expected-key',
  910. 'fp2': 'should-not-be-found',
  911. 'fp3': 'expected-no-value-key',
  912. }
  913. sslmgr = self.OpenSSLManager.return_value
  914. sslmgr.parse_certificates.return_value = certs
  915. data = shim.register_with_azure_and_fetch_data(pubkey_info=mypk)
  916. self.assertEqual(
  917. [mock.call(self.GoalState.return_value.certificates_xml)],
  918. sslmgr.parse_certificates.call_args_list)
  919. self.assertIn('expected-key', data['public-keys'])
  920. self.assertIn('expected-no-value-key', data['public-keys'])
  921. self.assertNotIn('should-not-be-found', data['public-keys'])
  922. def test_absent_certificates_produces_empty_public_keys(self):
  923. mypk = [{'fingerprint': 'fp1', 'path': 'path1'}]
  924. self.GoalState.return_value.certificates_xml = None
  925. shim = wa_shim()
  926. data = shim.register_with_azure_and_fetch_data(pubkey_info=mypk)
  927. self.assertEqual([], data['public-keys'])
  928. def test_correct_url_used_for_report_ready(self):
  929. self.find_endpoint.return_value = 'test_endpoint'
  930. shim = wa_shim()
  931. shim.register_with_azure_and_fetch_data()
  932. expected_url = 'http://test_endpoint/machine?comp=health'
  933. self.assertEqual(
  934. [mock.call(expected_url, data=mock.ANY, extra_headers=mock.ANY)],
  935. self.AzureEndpointHttpClient.return_value.post
  936. .call_args_list)
  937. def test_correct_url_used_for_report_failure(self):
  938. self.find_endpoint.return_value = 'test_endpoint'
  939. shim = wa_shim()
  940. shim.register_with_azure_and_report_failure(description='TestDesc')
  941. expected_url = 'http://test_endpoint/machine?comp=health'
  942. self.assertEqual(
  943. [mock.call(expected_url, data=mock.ANY, extra_headers=mock.ANY)],
  944. self.AzureEndpointHttpClient.return_value.post
  945. .call_args_list)
  946. def test_goal_state_values_used_for_report_ready(self):
  947. shim = wa_shim()
  948. shim.register_with_azure_and_fetch_data()
  949. posted_document = (
  950. self.AzureEndpointHttpClient.return_value.post
  951. .call_args[1]['data']
  952. )
  953. self.assertIn(self.test_incarnation, posted_document)
  954. self.assertIn(self.test_container_id, posted_document)
  955. self.assertIn(self.test_instance_id, posted_document)
  956. def test_goal_state_values_used_for_report_failure(self):
  957. shim = wa_shim()
  958. shim.register_with_azure_and_report_failure(description='TestDesc')
  959. posted_document = (
  960. self.AzureEndpointHttpClient.return_value.post
  961. .call_args[1]['data']
  962. )
  963. self.assertIn(self.test_incarnation, posted_document)
  964. self.assertIn(self.test_container_id, posted_document)
  965. self.assertIn(self.test_instance_id, posted_document)
  966. def test_xml_elems_in_report_ready_post(self):
  967. shim = wa_shim()
  968. shim.register_with_azure_and_fetch_data()
  969. health_document = HEALTH_REPORT_XML_TEMPLATE.format(
  970. incarnation=escape(self.test_incarnation),
  971. container_id=escape(self.test_container_id),
  972. instance_id=escape(self.test_instance_id),
  973. health_status=escape('Ready'),
  974. health_detail_subsection='')
  975. posted_document = (
  976. self.AzureEndpointHttpClient.return_value.post
  977. .call_args[1]['data'])
  978. self.assertEqual(health_document, posted_document)
  979. def test_xml_elems_in_report_failure_post(self):
  980. shim = wa_shim()
  981. shim.register_with_azure_and_report_failure(description='TestDesc')
  982. health_document = HEALTH_REPORT_XML_TEMPLATE.format(
  983. incarnation=escape(self.test_incarnation),
  984. container_id=escape(self.test_container_id),
  985. instance_id=escape(self.test_instance_id),
  986. health_status=escape('NotReady'),
  987. health_detail_subsection=HEALTH_DETAIL_SUBSECTION_XML_TEMPLATE
  988. .format(
  989. health_substatus=escape('ProvisioningFailed'),
  990. health_description=escape('TestDesc')))
  991. posted_document = (
  992. self.AzureEndpointHttpClient.return_value.post
  993. .call_args[1]['data'])
  994. self.assertEqual(health_document, posted_document)
  995. @mock.patch.object(azure_helper, 'GoalStateHealthReporter', autospec=True)
  996. def test_register_with_azure_and_fetch_data_calls_send_ready_signal(
  997. self, m_goal_state_health_reporter):
  998. shim = wa_shim()
  999. shim.register_with_azure_and_fetch_data()
  1000. self.assertEqual(
  1001. 1,
  1002. m_goal_state_health_reporter.return_value.send_ready_signal
  1003. .call_count)
  1004. @mock.patch.object(azure_helper, 'GoalStateHealthReporter', autospec=True)
  1005. def test_register_with_azure_and_report_failure_calls_send_failure_signal(
  1006. self, m_goal_state_health_reporter):
  1007. shim = wa_shim()
  1008. shim.register_with_azure_and_report_failure(description='TestDesc')
  1009. m_goal_state_health_reporter.return_value.send_failure_signal \
  1010. .assert_called_once_with(description='TestDesc')
  1011. def test_register_with_azure_and_report_failure_does_not_need_certificates(
  1012. self):
  1013. shim = wa_shim()
  1014. with mock.patch.object(
  1015. shim, '_fetch_goal_state_from_azure', autospec=True
  1016. ) as m_fetch_goal_state_from_azure:
  1017. shim.register_with_azure_and_report_failure(description='TestDesc')
  1018. m_fetch_goal_state_from_azure.assert_called_once_with(
  1019. need_certificate=False)
  1020. def test_clean_up_can_be_called_at_any_time(self):
  1021. shim = wa_shim()
  1022. shim.clean_up()
  1023. def test_openssl_manager_not_instantiated_by_shim_report_status(self):
  1024. shim = wa_shim()
  1025. shim.register_with_azure_and_fetch_data()
  1026. shim.register_with_azure_and_report_failure(description='TestDesc')
  1027. shim.clean_up()
  1028. self.OpenSSLManager.assert_not_called()
  1029. def test_clean_up_after_report_ready(self):
  1030. shim = wa_shim()
  1031. shim.register_with_azure_and_fetch_data()
  1032. shim.clean_up()
  1033. self.OpenSSLManager.return_value.clean_up.assert_not_called()
  1034. def test_clean_up_after_report_failure(self):
  1035. shim = wa_shim()
  1036. shim.register_with_azure_and_report_failure(description='TestDesc')
  1037. shim.clean_up()
  1038. self.OpenSSLManager.return_value.clean_up.assert_not_called()
  1039. def test_fetch_goalstate_during_report_ready_raises_exc_on_get_exc(self):
  1040. self.AzureEndpointHttpClient.return_value.get \
  1041. .side_effect = SentinelException
  1042. shim = wa_shim()
  1043. self.assertRaises(SentinelException,
  1044. shim.register_with_azure_and_fetch_data)
  1045. def test_fetch_goalstate_during_report_failure_raises_exc_on_get_exc(self):
  1046. self.AzureEndpointHttpClient.return_value.get \
  1047. .side_effect = SentinelException
  1048. shim = wa_shim()
  1049. self.assertRaises(SentinelException,
  1050. shim.register_with_azure_and_report_failure,
  1051. description='TestDesc')
  1052. def test_fetch_goalstate_during_report_ready_raises_exc_on_parse_exc(self):
  1053. self.GoalState.side_effect = SentinelException
  1054. shim = wa_shim()
  1055. self.assertRaises(SentinelException,
  1056. shim.register_with_azure_and_fetch_data)
  1057. def test_fetch_goalstate_during_report_failure_raises_exc_on_parse_exc(
  1058. self):
  1059. self.GoalState.side_effect = SentinelException
  1060. shim = wa_shim()
  1061. self.assertRaises(SentinelException,
  1062. shim.register_with_azure_and_report_failure,
  1063. description='TestDesc')
  1064. def test_failure_to_send_report_ready_health_doc_bubbles_up(self):
  1065. self.AzureEndpointHttpClient.return_value.post \
  1066. .side_effect = SentinelException
  1067. shim = wa_shim()
  1068. self.assertRaises(SentinelException,
  1069. shim.register_with_azure_and_fetch_data)
  1070. def test_failure_to_send_report_failure_health_doc_bubbles_up(self):
  1071. self.AzureEndpointHttpClient.return_value.post \
  1072. .side_effect = SentinelException
  1073. shim = wa_shim()
  1074. self.assertRaises(SentinelException,
  1075. shim.register_with_azure_and_report_failure,
  1076. description='TestDesc')
  1077. class TestGetMetadataGoalStateXMLAndReportReadyToFabric(CiTestCase):
  1078. def setUp(self):
  1079. super(TestGetMetadataGoalStateXMLAndReportReadyToFabric, self).setUp()
  1080. patches = ExitStack()
  1081. self.addCleanup(patches.close)
  1082. self.m_shim = patches.enter_context(
  1083. mock.patch.object(azure_helper, 'WALinuxAgentShim'))
  1084. def test_data_from_shim_returned(self):
  1085. ret = azure_helper.get_metadata_from_fabric()
  1086. self.assertEqual(
  1087. self.m_shim.return_value.register_with_azure_and_fetch_data
  1088. .return_value,
  1089. ret)
  1090. def test_success_calls_clean_up(self):
  1091. azure_helper.get_metadata_from_fabric()
  1092. self.assertEqual(1, self.m_shim.return_value.clean_up.call_count)
  1093. def test_failure_in_registration_propagates_exc_and_calls_clean_up(
  1094. self):
  1095. self.m_shim.return_value.register_with_azure_and_fetch_data \
  1096. .side_effect = SentinelException
  1097. self.assertRaises(SentinelException,
  1098. azure_helper.get_metadata_from_fabric)
  1099. self.assertEqual(1, self.m_shim.return_value.clean_up.call_count)
  1100. def test_calls_shim_register_with_azure_and_fetch_data(self):
  1101. m_pubkey_info = mock.MagicMock()
  1102. azure_helper.get_metadata_from_fabric(pubkey_info=m_pubkey_info)
  1103. self.assertEqual(
  1104. 1,
  1105. self.m_shim.return_value
  1106. .register_with_azure_and_fetch_data.call_count)
  1107. self.assertEqual(
  1108. mock.call(pubkey_info=m_pubkey_info),
  1109. self.m_shim.return_value
  1110. .register_with_azure_and_fetch_data.call_args)
  1111. def test_instantiates_shim_with_kwargs(self):
  1112. m_fallback_lease_file = mock.MagicMock()
  1113. m_dhcp_options = mock.MagicMock()
  1114. azure_helper.get_metadata_from_fabric(
  1115. fallback_lease_file=m_fallback_lease_file,
  1116. dhcp_opts=m_dhcp_options)
  1117. self.assertEqual(1, self.m_shim.call_count)
  1118. self.assertEqual(
  1119. mock.call(
  1120. fallback_lease_file=m_fallback_lease_file,
  1121. dhcp_options=m_dhcp_options),
  1122. self.m_shim.call_args)
  1123. class TestGetMetadataGoalStateXMLAndReportFailureToFabric(CiTestCase):
  1124. def setUp(self):
  1125. super(
  1126. TestGetMetadataGoalStateXMLAndReportFailureToFabric, self).setUp()
  1127. patches = ExitStack()
  1128. self.addCleanup(patches.close)
  1129. self.m_shim = patches.enter_context(
  1130. mock.patch.object(azure_helper, 'WALinuxAgentShim'))
  1131. def test_success_calls_clean_up(self):
  1132. azure_helper.report_failure_to_fabric()
  1133. self.assertEqual(
  1134. 1,
  1135. self.m_shim.return_value.clean_up.call_count)
  1136. def test_failure_in_shim_report_failure_propagates_exc_and_calls_clean_up(
  1137. self):
  1138. self.m_shim.return_value.register_with_azure_and_report_failure \
  1139. .side_effect = SentinelException
  1140. self.assertRaises(SentinelException,
  1141. azure_helper.report_failure_to_fabric)
  1142. self.assertEqual(
  1143. 1,
  1144. self.m_shim.return_value.clean_up.call_count)
  1145. def test_report_failure_to_fabric_with_desc_calls_shim_report_failure(
  1146. self):
  1147. azure_helper.report_failure_to_fabric(description='TestDesc')
  1148. self.m_shim.return_value.register_with_azure_and_report_failure \
  1149. .assert_called_once_with(description='TestDesc')
  1150. def test_report_failure_to_fabric_with_no_desc_calls_shim_report_failure(
  1151. self):
  1152. azure_helper.report_failure_to_fabric()
  1153. # default err message description should be shown to the user
  1154. # if no description is passed in
  1155. self.m_shim.return_value.register_with_azure_and_report_failure \
  1156. .assert_called_once_with(
  1157. description=azure_helper
  1158. .DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGE)
  1159. def test_report_failure_to_fabric_empty_desc_calls_shim_report_failure(
  1160. self):
  1161. azure_helper.report_failure_to_fabric(description='')
  1162. # default err message description should be shown to the user
  1163. # if an empty description is passed in
  1164. self.m_shim.return_value.register_with_azure_and_report_failure \
  1165. .assert_called_once_with(
  1166. description=azure_helper
  1167. .DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGE)
  1168. def test_instantiates_shim_with_kwargs(self):
  1169. m_fallback_lease_file = mock.MagicMock()
  1170. m_dhcp_options = mock.MagicMock()
  1171. azure_helper.report_failure_to_fabric(
  1172. fallback_lease_file=m_fallback_lease_file,
  1173. dhcp_opts=m_dhcp_options)
  1174. self.m_shim.assert_called_once_with(
  1175. fallback_lease_file=m_fallback_lease_file,
  1176. dhcp_options=m_dhcp_options)
  1177. class TestExtractIpAddressFromNetworkd(CiTestCase):
  1178. azure_lease = dedent("""\
  1179. # This is private data. Do not parse.
  1180. ADDRESS=10.132.0.5
  1181. NETMASK=255.255.255.255
  1182. ROUTER=10.132.0.1
  1183. SERVER_ADDRESS=169.254.169.254
  1184. NEXT_SERVER=10.132.0.1
  1185. MTU=1460
  1186. T1=43200
  1187. T2=75600
  1188. LIFETIME=86400
  1189. DNS=169.254.169.254
  1190. NTP=169.254.169.254
  1191. DOMAINNAME=c.ubuntu-foundations.internal
  1192. DOMAIN_SEARCH_LIST=c.ubuntu-foundations.internal google.internal
  1193. HOSTNAME=tribaal-test-171002-1349.c.ubuntu-foundations.internal
  1194. ROUTES=10.132.0.1/32,0.0.0.0 0.0.0.0/0,10.132.0.1
  1195. CLIENTID=ff405663a200020000ab11332859494d7a8b4c
  1196. OPTION_245=624c3620
  1197. """)
  1198. def setUp(self):
  1199. super(TestExtractIpAddressFromNetworkd, self).setUp()
  1200. self.lease_d = self.tmp_dir()
  1201. def test_no_valid_leases_is_none(self):
  1202. """No valid leases should return None."""
  1203. self.assertIsNone(
  1204. wa_shim._networkd_get_value_from_leases(self.lease_d))
  1205. def test_option_245_is_found_in_single(self):
  1206. """A single valid lease with 245 option should return it."""
  1207. populate_dir(self.lease_d, {'9': self.azure_lease})
  1208. self.assertEqual(
  1209. '624c3620', wa_shim._networkd_get_value_from_leases(self.lease_d))
  1210. def test_option_245_not_found_returns_None(self):
  1211. """A valid lease, but no option 245 should return None."""
  1212. populate_dir(
  1213. self.lease_d,
  1214. {'9': self.azure_lease.replace("OPTION_245", "OPTION_999")})
  1215. self.assertIsNone(
  1216. wa_shim._networkd_get_value_from_leases(self.lease_d))
  1217. def test_multiple_returns_first(self):
  1218. """Somewhat arbitrarily return the first address when multiple.
  1219. Most important at the moment is that this is consistent behavior
  1220. rather than changing randomly as in order of a dictionary."""
  1221. myval = "624c3601"
  1222. populate_dir(
  1223. self.lease_d,
  1224. {'9': self.azure_lease,
  1225. '2': self.azure_lease.replace("624c3620", myval)})
  1226. self.assertEqual(
  1227. myval, wa_shim._networkd_get_value_from_leases(self.lease_d))
  1228. # vi: ts=4 expandtab