Juju Charm - Nova Cloud Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_nova_cc_hooks.py 50KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. # Copyright 2016 Canonical Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import tempfile
  16. import yaml
  17. from mock import MagicMock, patch, call
  18. from test_utils import CharmTestCase
  19. with patch('charmhelpers.core.hookenv.config') as config:
  20. with patch('charmhelpers.contrib.openstack.utils.get_os_codename_package'):
  21. config.return_value = 'neutron'
  22. import nova_cc_utils as utils
  23. _reg = utils.register_configs
  24. _map = utils.restart_map
  25. utils.register_configs = MagicMock()
  26. utils.restart_map = MagicMock()
  27. with patch('charmhelpers.contrib.hardening.harden.harden') as mock_dec:
  28. mock_dec.side_effect = (lambda *dargs, **dkwargs: lambda f:
  29. lambda *args, **kwargs: f(*args, **kwargs))
  30. with patch('nova_cc_utils.guard_map') as gmap:
  31. with patch('charmhelpers.core.hookenv.config') as config:
  32. config.return_value = False
  33. gmap.return_value = {}
  34. import nova_cc_hooks as hooks
  35. utils.register_configs = _reg
  36. utils.restart_map = _map
  37. TO_PATCH = [
  38. 'apt_update',
  39. 'apt_install',
  40. 'configure_installation_source',
  41. 'charm_dir',
  42. 'do_openstack_upgrade',
  43. 'openstack_upgrade_available',
  44. 'cmd_all_services',
  45. 'config',
  46. 'config_value_changed',
  47. 'determine_endpoints',
  48. 'determine_packages',
  49. 'determine_ports',
  50. 'disable_services',
  51. 'enable_services',
  52. 'NovaCellContext',
  53. 'open_port',
  54. 'is_relation_made',
  55. 'local_unit',
  56. 'log',
  57. 'os_release',
  58. 'related_units',
  59. 'relation_get',
  60. 'relation_set',
  61. 'relation_ids',
  62. 'placement_api_enabled',
  63. 'ssh_compute_add',
  64. 'ssh_known_hosts_lines',
  65. 'ssh_authorized_keys_lines',
  66. 'save_script_rc',
  67. 'service_reload',
  68. 'services',
  69. 'execd_preinstall',
  70. 'network_manager',
  71. 'unit_get',
  72. 'uuid',
  73. 'is_leader',
  74. 'keystone_ca_cert_b64',
  75. 'migrate_nova_databases',
  76. 'uuid',
  77. 'get_hacluster_config',
  78. 'get_iface_for_address',
  79. 'get_netmask_for_address',
  80. 'update_nrpe_config',
  81. 'git_install',
  82. 'git_install_requested',
  83. 'status_set',
  84. 'update_dns_ha_resource_params',
  85. 'serial_console_settings',
  86. 'get_relation_ip',
  87. 'is_clustered',
  88. ]
  89. FAKE_KS_AUTH_CFG = {
  90. 'auth_host': 'kshost',
  91. 'auth_port': '5000',
  92. 'service_port': 'token',
  93. 'service_username': 'admin_user',
  94. 'service_password': 'admin_passwd',
  95. 'service_tenant_name': 'admin_tenant',
  96. 'auth_uri': 'http://kshost:5000/v2',
  97. # quantum-gateway interface deviates a bit.
  98. 'keystone_host': 'kshost',
  99. 'service_tenant': 'service_tenant',
  100. }
  101. class NovaCCHooksTests(CharmTestCase):
  102. def setUp(self):
  103. super(NovaCCHooksTests, self).setUp(hooks, TO_PATCH)
  104. (tmpfd, hooks.NOVA_CONSOLEAUTH_OVERRIDE) = tempfile.mkstemp()
  105. self.config.side_effect = self.test_config.get
  106. self.relation_get.side_effect = self.test_relation.get
  107. self.charm_dir.return_value = '/var/lib/juju/charms/nova/charm'
  108. def tearDown(self):
  109. try:
  110. os.remove(hooks.NOVA_CONSOLEAUTH_OVERRIDE)
  111. except OSError:
  112. pass
  113. super(NovaCCHooksTests, self).tearDown()
  114. def test_install_hook(self):
  115. self.determine_packages.return_value = [
  116. 'nova-scheduler', 'nova-api-ec2']
  117. self.determine_ports.return_value = [80, 81, 82]
  118. hooks.install()
  119. self.apt_install.assert_called_with(
  120. ['nova-scheduler', 'nova-api-ec2'], fatal=True)
  121. self.assertTrue(self.execd_preinstall.called)
  122. self.assertTrue(self.disable_services.called)
  123. self.cmd_all_services.assert_called_with('stop')
  124. def test_install_hook_git(self):
  125. self.git_install_requested.return_value = True
  126. self.determine_packages.return_value = ['foo', 'bar']
  127. self.determine_ports.return_value = [80, 81, 82]
  128. repo = 'cloud:trusty-juno'
  129. openstack_origin_git = {
  130. 'repositories': [
  131. {'name': 'requirements',
  132. 'repository': 'git://git.openstack.org/openstack/requirements', # noqa
  133. 'branch': 'stable/juno'},
  134. {'name': 'nova',
  135. 'repository': 'git://git.openstack.org/openstack/nova',
  136. 'branch': 'stable/juno'}
  137. ],
  138. 'directory': '/mnt/openstack-git',
  139. }
  140. projects_yaml = yaml.dump(openstack_origin_git)
  141. self.test_config.set('openstack-origin', repo)
  142. self.test_config.set('openstack-origin-git', projects_yaml)
  143. hooks.install()
  144. self.git_install.assert_called_with(projects_yaml)
  145. self.apt_install.assert_called_with(['foo', 'bar'], fatal=True)
  146. self.assertTrue(self.execd_preinstall.called)
  147. self.assertTrue(self.disable_services.called)
  148. self.cmd_all_services.assert_called_with('stop')
  149. @patch.object(hooks, 'update_aws_compat_services')
  150. @patch.object(hooks, 'update_nova_consoleauth_config')
  151. @patch.object(hooks, 'is_db_initialised')
  152. @patch.object(hooks, 'determine_packages')
  153. @patch.object(utils, 'service_resume')
  154. @patch.object(utils, 'config')
  155. @patch.object(hooks, 'filter_installed_packages')
  156. @patch.object(hooks, 'configure_https')
  157. def test_config_changed_no_upgrade(self, conf_https, mock_filter_packages,
  158. utils_config, mock_service_resume,
  159. mock_determine_packages,
  160. mock_is_db_initialised,
  161. mock_update_nova_consoleauth_config,
  162. mock_update_aws_compat_services):
  163. mock_determine_packages.return_value = []
  164. utils_config.side_effect = self.test_config.get
  165. self.test_config.set('console-access-protocol', 'dummy')
  166. self.git_install_requested.return_value = False
  167. self.openstack_upgrade_available.return_value = False
  168. mock_is_db_initialised.return_value = False
  169. self.os_release.return_value = 'diablo'
  170. hooks.config_changed()
  171. self.assertTrue(self.save_script_rc.called)
  172. mock_filter_packages.assert_called_with([])
  173. self.assertTrue(mock_update_nova_consoleauth_config.called)
  174. self.assertTrue(mock_update_aws_compat_services.called)
  175. @patch.object(hooks, 'update_aws_compat_services')
  176. @patch.object(hooks, 'update_nova_consoleauth_config')
  177. @patch.object(hooks, 'is_db_initialised')
  178. @patch.object(utils, 'service_resume')
  179. @patch.object(hooks, 'configure_https')
  180. def test_config_changed_git(self, configure_https, mock_service_resume,
  181. mock_is_db_initialised,
  182. mock_update_nova_consoleauth_config,
  183. mock_update_aws_compat_services):
  184. self.git_install_requested.return_value = True
  185. repo = 'cloud:trusty-juno'
  186. openstack_origin_git = {
  187. 'repositories': [
  188. {'name': 'requirements',
  189. 'repository':
  190. 'git://git.openstack.org/openstack/requirements',
  191. 'branch': 'stable/juno'},
  192. {'name': 'nova',
  193. 'repository': 'git://git.openstack.org/openstack/nova',
  194. 'branch': 'stable/juno'}
  195. ],
  196. 'directory': '/mnt/openstack-git',
  197. }
  198. projects_yaml = yaml.dump(openstack_origin_git)
  199. self.test_config.set('openstack-origin', repo)
  200. self.test_config.set('openstack-origin-git', projects_yaml)
  201. mock_is_db_initialised.return_value = False
  202. self.os_release.return_value = 'diablo'
  203. hooks.config_changed()
  204. self.git_install.assert_called_with(projects_yaml)
  205. self.assertFalse(self.do_openstack_upgrade.called)
  206. self.assertTrue(mock_update_nova_consoleauth_config.called)
  207. self.assertTrue(mock_update_aws_compat_services.called)
  208. @patch.object(hooks, 'update_aws_compat_services')
  209. @patch.object(hooks, 'update_nova_consoleauth_config')
  210. @patch.object(hooks, 'is_db_initialised')
  211. @patch.object(hooks, 'quantum_joined')
  212. @patch.object(hooks, 'determine_packages')
  213. @patch.object(utils, 'service_resume')
  214. @patch('charmhelpers.contrib.openstack.ip.unit_get')
  215. @patch('charmhelpers.contrib.hahelpers.cluster.relation_ids')
  216. @patch.object(utils, 'config')
  217. @patch.object(hooks, 'db_joined')
  218. @patch.object(hooks, 'filter_installed_packages')
  219. @patch('charmhelpers.contrib.openstack.ip.service_name',
  220. lambda *args: 'nova-cloud-controller')
  221. @patch.object(hooks, 'cluster_joined')
  222. @patch.object(hooks, 'identity_joined')
  223. @patch.object(hooks, 'neutron_api_relation_joined')
  224. @patch.object(hooks, 'configure_https')
  225. def test_config_changed_with_upgrade(self, conf_https, neutron_api_joined,
  226. identity_joined, cluster_joined,
  227. mock_filter_packages, db_joined,
  228. utils_config, mock_relids,
  229. mock_unit_get,
  230. mock_service_resume,
  231. mock_determine_packages,
  232. mock_quantum_joined,
  233. mock_is_db_initialised,
  234. mock_update_nova_consoleauth_config,
  235. mock_update_aws_compat_services):
  236. mock_determine_packages.return_value = []
  237. mock_is_db_initialised.return_value = False
  238. self.git_install_requested.return_value = False
  239. self.openstack_upgrade_available.return_value = True
  240. self.relation_ids.return_value = ['generic_rid']
  241. utils_config.side_effect = self.test_config.get
  242. self.test_config.set('console-access-protocol', 'dummy')
  243. mock_relids.return_value = []
  244. mock_unit_get.return_value = '127.0.0.1'
  245. self.os_release.return_value = 'diablo'
  246. hooks.config_changed()
  247. self.assertTrue(self.do_openstack_upgrade.called)
  248. self.assertTrue(neutron_api_joined.called)
  249. self.assertTrue(identity_joined.called)
  250. self.assertTrue(cluster_joined.called)
  251. self.assertTrue(db_joined.called)
  252. self.assertTrue(self.save_script_rc.called)
  253. mock_filter_packages.assert_called_with([])
  254. self.assertTrue(mock_quantum_joined.called)
  255. self.assertTrue(mock_update_nova_consoleauth_config.called)
  256. self.assertTrue(mock_update_aws_compat_services.called)
  257. @patch.object(hooks, 'update_aws_compat_services')
  258. @patch.object(hooks, 'update_nova_consoleauth_config')
  259. @patch.object(hooks, 'is_db_initialised')
  260. @patch.object(utils, 'service_resume')
  261. @patch.object(hooks, 'filter_installed_packages')
  262. @patch.object(hooks, 'configure_https')
  263. @patch.object(hooks, 'compute_changed')
  264. def test_config_changed_region_change(self, mock_compute_changed,
  265. mock_config_https,
  266. mock_filter_packages,
  267. mock_service_resume,
  268. mock_is_db_initialised,
  269. mock_update_nova_consoleauth_config,
  270. mock_update_aws_compat_services):
  271. self.git_install_requested.return_value = False
  272. self.openstack_upgrade_available.return_value = False
  273. self.config_value_changed.return_value = True
  274. self.related_units.return_value = ['unit/0']
  275. self.relation_ids.side_effect = \
  276. lambda x: ['generic_rid'] if x == 'cloud-compute' else []
  277. mock_is_db_initialised.return_value = False
  278. self.os_release.return_value = 'diablo'
  279. hooks.config_changed()
  280. mock_compute_changed.assert_has_calls([call('generic_rid', 'unit/0')])
  281. self.assertTrue(mock_update_nova_consoleauth_config.called)
  282. self.assertTrue(mock_update_aws_compat_services.called)
  283. @patch.object(hooks, 'is_cellv2_init_ready')
  284. @patch.object(hooks, 'is_db_initialised')
  285. @patch.object(hooks, 'nova_api_relation_joined')
  286. def test_compute_changed_nova_api_trigger(self, api_joined,
  287. mock_is_db_initialised,
  288. mock_is_cellv2_init_ready):
  289. self.relation_ids.return_value = ['nova-api/0']
  290. mock_is_db_initialised.return_value = False
  291. mock_is_cellv2_init_ready.return_value = False
  292. hooks.compute_changed()
  293. api_joined.assert_called_with(rid='nova-api/0')
  294. @patch.object(hooks, 'is_cellv2_init_ready')
  295. @patch.object(hooks, 'is_db_initialised')
  296. def test_compute_changed_ssh_migration(self, mock_is_db_initialised,
  297. mock_is_cellv2_init_ready):
  298. self.test_relation.set({
  299. 'migration_auth_type': 'ssh', 'ssh_public_key': 'fookey',
  300. 'private-address': '10.0.0.1', 'region': 'RegionOne'})
  301. self.ssh_known_hosts_lines.return_value = [
  302. 'k_h_0', 'k_h_1', 'k_h_2']
  303. self.ssh_authorized_keys_lines.return_value = [
  304. 'auth_0', 'auth_1', 'auth_2']
  305. mock_is_db_initialised.return_value = False
  306. mock_is_cellv2_init_ready.return_value = False
  307. hooks.compute_changed()
  308. self.ssh_compute_add.assert_called_with('fookey', rid=None, unit=None)
  309. expected_relations = [
  310. call(relation_settings={'authorized_keys_0': 'auth_0'},
  311. relation_id=None),
  312. call(relation_settings={'authorized_keys_1': 'auth_1'},
  313. relation_id=None),
  314. call(relation_settings={'authorized_keys_2': 'auth_2'},
  315. relation_id=None),
  316. call(relation_settings={'known_hosts_0': 'k_h_0'},
  317. relation_id=None),
  318. call(relation_settings={'known_hosts_1': 'k_h_1'},
  319. relation_id=None),
  320. call(relation_settings={'known_hosts_2': 'k_h_2'},
  321. relation_id=None),
  322. call(authorized_keys_max_index=3, relation_id=None),
  323. call(known_hosts_max_index=3, relation_id=None)]
  324. self.assertEqual(sorted(self.relation_set.call_args_list),
  325. sorted(expected_relations))
  326. @patch.object(hooks, 'is_cellv2_init_ready')
  327. @patch.object(hooks, 'is_db_initialised')
  328. def test_compute_changed_nova_public_key(self, mock_is_db_initialised,
  329. mock_is_cellv2_init_ready):
  330. self.test_relation.set({
  331. 'migration_auth_type': 'sasl', 'nova_ssh_public_key': 'fookey',
  332. 'private-address': '10.0.0.1', 'region': 'RegionOne'})
  333. self.ssh_known_hosts_lines.return_value = [
  334. 'k_h_0', 'k_h_1', 'k_h_2']
  335. self.ssh_authorized_keys_lines.return_value = [
  336. 'auth_0', 'auth_1', 'auth_2']
  337. mock_is_db_initialised.return_value = False
  338. mock_is_cellv2_init_ready.return_value = False
  339. hooks.compute_changed()
  340. self.ssh_compute_add.assert_called_with('fookey', user='nova',
  341. rid=None, unit=None)
  342. expected_relations = [
  343. call(relation_settings={'nova_authorized_keys_0': 'auth_0'},
  344. relation_id=None),
  345. call(relation_settings={'nova_authorized_keys_1': 'auth_1'},
  346. relation_id=None),
  347. call(relation_settings={'nova_authorized_keys_2': 'auth_2'},
  348. relation_id=None),
  349. call(relation_settings={'nova_known_hosts_0': 'k_h_0'},
  350. relation_id=None),
  351. call(relation_settings={'nova_known_hosts_1': 'k_h_1'},
  352. relation_id=None),
  353. call(relation_settings={'nova_known_hosts_2': 'k_h_2'},
  354. relation_id=None),
  355. call(relation_settings={'nova_known_hosts_max_index': 3},
  356. relation_id=None),
  357. call(relation_settings={'nova_authorized_keys_max_index': 3},
  358. relation_id=None)]
  359. self.assertEqual(sorted(self.relation_set.call_args_list),
  360. sorted(expected_relations))
  361. @patch.object(hooks, 'canonical_url')
  362. @patch.object(utils, 'config')
  363. @patch.object(hooks, '_auth_config')
  364. def test_compute_joined_neutron(self, auth_config, _util_config,
  365. _canonical_url):
  366. _util_config.return_value = None
  367. self.is_relation_made.return_value = False
  368. self.network_manager.return_value = 'neutron'
  369. self.is_leader = True
  370. self.keystone_ca_cert_b64.return_value = 'foocert64'
  371. self.unit_get.return_value = 'nova-cc-host1'
  372. self.serial_console_settings.return_value = {
  373. 'enable_serial_console': 'false',
  374. 'serial_console_base_url': 'ws://controller:6803',
  375. }
  376. _canonical_url.return_value = 'http://nova-cc-host1'
  377. auth_config.return_value = FAKE_KS_AUTH_CFG
  378. hooks.compute_joined()
  379. self.relation_set.assert_called_with(
  380. relation_id=None,
  381. ca_cert='foocert64',
  382. region='RegionOne',
  383. volume_service='cinder',
  384. ec2_host='nova-cc-host1',
  385. network_manager='neutron',
  386. enable_serial_console='false',
  387. serial_console_base_url='ws://controller:6803',
  388. **FAKE_KS_AUTH_CFG)
  389. @patch.object(hooks, 'canonical_url')
  390. @patch.object(utils, 'config')
  391. @patch.object(hooks, 'NeutronAPIContext')
  392. @patch.object(hooks, '_auth_config')
  393. def test_compute_joined_neutron_api_rel(self, auth_config, napi,
  394. _util_config, _canonical_url):
  395. def mock_NeutronAPIContext():
  396. return {
  397. 'neutron_plugin': 'bob',
  398. 'neutron_security_groups': 'yes',
  399. 'neutron_url': 'http://nova-cc-host1:9696',
  400. }
  401. _util_config.return_value = None
  402. napi.return_value = mock_NeutronAPIContext
  403. self.is_relation_made.return_value = True
  404. self.network_manager.return_value = 'neutron'
  405. self.is_leader = True
  406. self.keystone_ca_cert_b64.return_value = 'foocert64'
  407. self.unit_get.return_value = 'nova-cc-host1'
  408. self.serial_console_settings.return_value = {
  409. 'enable_serial_console': 'false',
  410. 'serial_console_base_url': 'ws://controller:6803',
  411. }
  412. _canonical_url.return_value = 'http://nova-cc-host1'
  413. auth_config.return_value = FAKE_KS_AUTH_CFG
  414. hooks.compute_joined()
  415. self.relation_set.assert_called_with(
  416. relation_id=None,
  417. quantum_url='http://nova-cc-host1:9696',
  418. ca_cert='foocert64',
  419. quantum_port=9696,
  420. quantum_host='nova-cc-host1',
  421. quantum_security_groups='yes',
  422. region='RegionOne',
  423. volume_service='cinder',
  424. ec2_host='nova-cc-host1',
  425. quantum_plugin='bob',
  426. network_manager='neutron',
  427. enable_serial_console='false',
  428. serial_console_base_url='ws://controller:6803',
  429. **FAKE_KS_AUTH_CFG)
  430. @patch.object(hooks, 'canonical_url')
  431. @patch.object(hooks, '_auth_config')
  432. def test_nova_vmware_joined(self, auth_config, _canonical_url):
  433. auth_config.return_value = FAKE_KS_AUTH_CFG
  434. self.is_relation_made.return_value = False
  435. self.network_manager.return_value = 'neutron'
  436. _canonical_url.return_value = 'http://nova-cc-host1'
  437. hooks.nova_vmware_relation_joined()
  438. self.relation_set.assert_called_with(
  439. network_manager='neutron',
  440. relation_id=None,
  441. **FAKE_KS_AUTH_CFG)
  442. def test_db_joined(self):
  443. self.get_relation_ip.return_value = '10.10.10.10'
  444. self.is_relation_made.return_value = False
  445. self.os_release.return_value = 'diablo'
  446. hooks.db_joined()
  447. self.relation_set.assert_called_with(nova_database='nova',
  448. nova_username='nova',
  449. nova_hostname='10.10.10.10',
  450. relation_id=None)
  451. self.get_relation_ip.assert_called_with('shared-db',
  452. cidr_network=None)
  453. def test_db_joined_spaces(self):
  454. self.get_relation_ip.return_value = '192.168.20.1'
  455. self.unit_get.return_value = 'nova.foohost.com'
  456. self.is_relation_made.return_value = False
  457. self.os_release.return_value = 'diablo'
  458. hooks.db_joined()
  459. self.relation_set.assert_called_with(nova_database='nova',
  460. nova_username='nova',
  461. nova_hostname='192.168.20.1',
  462. relation_id=None)
  463. def test_db_joined_mitaka(self):
  464. self.get_relation_ip.return_value = '10.10.10.10'
  465. self.os_release.return_value = 'mitaka'
  466. self.is_relation_made.return_value = False
  467. hooks.db_joined()
  468. self.relation_set.assert_has_calls([
  469. call(nova_database='nova',
  470. nova_username='nova',
  471. nova_hostname='10.10.10.10',
  472. relation_id=None),
  473. call(novaapi_database='nova_api',
  474. novaapi_username='nova',
  475. novaapi_hostname='10.10.10.10',
  476. relation_id=None),
  477. ])
  478. self.get_relation_ip.assert_called_with('shared-db',
  479. cidr_network=None)
  480. @patch('charmhelpers.contrib.openstack.ip.service_name',
  481. lambda *args: 'nova-cloud-controller')
  482. @patch('charmhelpers.contrib.openstack.ip.unit_get')
  483. @patch('charmhelpers.contrib.openstack.ip.is_clustered')
  484. @patch('charmhelpers.contrib.openstack.ip.config')
  485. def test_identity_joined(self, _ip_config, _is_clustered, _unit_get):
  486. _is_clustered.return_value = False
  487. _unit_get.return_value = '127.0.0.1'
  488. _ip_config.side_effect = self.test_config.get
  489. self.test_config.set('os-public-hostname', 'ncc.example.com')
  490. hooks.identity_joined()
  491. self.determine_endpoints.asssert_called_with(
  492. public_url='http://ncc.example.com',
  493. internal_url='http://127.0.0.1',
  494. admin_url='http://127.0.0.1'
  495. )
  496. def test_identity_joined_partial_cluster(self):
  497. self.is_clustered.return_value = False
  498. self.test_config.set('vip', '10.0.0.10')
  499. hooks.identity_joined()
  500. self.assertFalse(self.relation_set.called)
  501. def test_postgresql_nova_db_joined(self):
  502. self.is_relation_made.return_value = False
  503. hooks.pgsql_nova_db_joined()
  504. self.relation_set.assert_called_with(database='nova')
  505. def test_db_joined_with_postgresql(self):
  506. self.is_relation_made.return_value = True
  507. with self.assertRaises(Exception) as context:
  508. hooks.db_joined()
  509. self.assertEqual(context.exception.message,
  510. 'Attempting to associate a mysql database when'
  511. ' there is already associated a postgresql one')
  512. def test_postgresql_nova_joined_with_db(self):
  513. self.is_relation_made.return_value = True
  514. with self.assertRaises(Exception) as context:
  515. hooks.pgsql_nova_db_joined()
  516. self.assertEqual(context.exception.message,
  517. 'Attempting to associate a postgresql database when'
  518. ' there is already associated a mysql one')
  519. @patch.object(hooks, 'CONFIGS')
  520. def test_db_changed_missing_relation_data(self, configs):
  521. configs.complete_contexts = MagicMock()
  522. configs.complete_contexts.return_value = []
  523. hooks.db_changed()
  524. self.log.assert_called_with(
  525. 'shared-db relation incomplete. Peer not ready?'
  526. )
  527. @patch.object(hooks, 'CONFIGS')
  528. def test_postgresql_nova_db_changed_missing_relation_data(self, configs):
  529. configs.complete_contexts = MagicMock()
  530. configs.complete_contexts.return_value = []
  531. hooks.postgresql_nova_db_changed()
  532. self.log.assert_called_with(
  533. 'pgsql-nova-db relation incomplete. Peer not ready?'
  534. )
  535. def _shared_db_test(self, configs):
  536. configs.complete_contexts = MagicMock()
  537. configs.complete_contexts.return_value = ['shared-db']
  538. configs.write = MagicMock()
  539. hooks.db_changed()
  540. def _postgresql_db_test(self, configs):
  541. configs.complete_contexts = MagicMock()
  542. configs.complete_contexts.return_value = ['pgsql-nova-db']
  543. configs.write = MagicMock()
  544. hooks.postgresql_nova_db_changed()
  545. @patch.object(hooks, 'nova_api_relation_joined')
  546. @patch.object(hooks, 'is_db_initialised')
  547. @patch.object(hooks, 'CONFIGS')
  548. def test_db_changed(self, configs,
  549. mock_is_db_initialised, api_joined):
  550. self.relation_ids.return_value = ['nova-api/0']
  551. mock_is_db_initialised.return_value = False
  552. 'No database migration is attempted when ACL list is not present'
  553. self.os_release.return_value = 'diablo'
  554. self._shared_db_test(configs)
  555. self.assertTrue(configs.write_all.called)
  556. self.assertFalse(self.migrate_nova_databases.called)
  557. api_joined.asert_called_with(rid='nova-api/0')
  558. @patch.object(utils, 'is_leader')
  559. @patch.object(utils, 'os_release')
  560. @patch.object(hooks, 'is_db_initialised')
  561. @patch.object(hooks, 'CONFIGS')
  562. def test_db_changed_allowed(self, configs, mock_is_db_initialised,
  563. utils_os_release, utils_is_leader):
  564. mock_is_db_initialised.return_value = False
  565. allowed_units = 'nova-cloud-controller/0 nova-cloud-controller/3'
  566. self.test_relation.set({
  567. 'nova_allowed_units': allowed_units,
  568. })
  569. self.local_unit.return_value = 'nova-cloud-controller/3'
  570. self.os_release.return_value = 'diablo'
  571. utils_os_release.return_value = 'diablo'
  572. utils_is_leader.return_value = False
  573. self._shared_db_test(configs)
  574. self.assertTrue(configs.write_all.called)
  575. self.migrate_nova_databases.assert_called_with()
  576. @patch.object(hooks, 'is_db_initialised')
  577. @patch.object(hooks, 'CONFIGS')
  578. def test_db_changed_not_allowed(self, configs, mock_is_db_initialised):
  579. mock_is_db_initialised.return_value = False
  580. allowed_units = 'nova-cloud-controller/0 nova-cloud-controller/3'
  581. self.test_relation.set({
  582. 'nova_allowed_units': allowed_units,
  583. })
  584. self.local_unit.return_value = 'nova-cloud-controller/1'
  585. self.os_release.return_value = 'diablo'
  586. self._shared_db_test(configs)
  587. self.assertTrue(configs.write_all.called)
  588. self.assertFalse(self.migrate_nova_databases.called)
  589. @patch.object(utils, 'is_leader')
  590. @patch.object(utils, 'os_release')
  591. @patch.object(hooks, 'quantum_joined')
  592. @patch.object(hooks, 'nova_api_relation_joined')
  593. @patch.object(hooks, 'is_db_initialised')
  594. @patch.object(hooks, 'CONFIGS')
  595. def test_postgresql_db_changed(self, configs, mock_is_db_initialised,
  596. api_joined, quantum_joined,
  597. utils_os_release, utils_is_leader):
  598. self.relation_ids.side_effect = [
  599. [],
  600. ['neutron-gateway/0'],
  601. ['nova-api/0']]
  602. mock_is_db_initialised.return_value = False
  603. self.os_release.return_value = 'diablo'
  604. utils_os_release.return_value = 'diablo'
  605. utils_is_leader.return_value = True
  606. self._postgresql_db_test(configs)
  607. self.assertTrue(configs.write_all.called)
  608. self.migrate_nova_databases.assert_called_with()
  609. api_joined.assert_called_with(rid='nova-api/0')
  610. @patch.object(utils, 'is_leader')
  611. @patch.object(utils, 'os_release')
  612. @patch.object(hooks, 'quantum_joined')
  613. @patch.object(hooks, 'is_db_initialised')
  614. @patch.object(hooks, 'nova_cell_relation_joined')
  615. @patch.object(hooks, 'compute_joined')
  616. @patch.object(hooks, 'CONFIGS')
  617. def test_db_changed_remote_restarts(self, configs, comp_joined,
  618. cell_joined, mock_is_db_initialised,
  619. quantum_joined, utils_os_release,
  620. utils_is_leader):
  621. mock_is_db_initialised.return_value = False
  622. def _relation_ids(rel):
  623. relid = {
  624. 'cloud-compute': ['nova-compute/0'],
  625. 'cell': ['nova-cell-api/0'],
  626. 'neutron-api': ['neutron-api/0'],
  627. 'quantum-network-service': ['neutron-gateway/0']
  628. }
  629. return relid[rel]
  630. self.relation_ids.side_effect = _relation_ids
  631. allowed_units = 'nova-cloud-controller/0'
  632. self.test_relation.set({
  633. 'nova_allowed_units': allowed_units,
  634. })
  635. self.local_unit.return_value = 'nova-cloud-controller/0'
  636. self.os_release.return_value = 'diablo'
  637. utils_os_release.return_value = 'diablo'
  638. utils_is_leader.return_value = False
  639. self._shared_db_test(configs)
  640. comp_joined.assert_called_with(remote_restart=True,
  641. rid='nova-compute/0')
  642. cell_joined.assert_called_with(remote_restart=True,
  643. rid='nova-cell-api/0')
  644. quantum_joined.assert_called_with(remote_restart=True,
  645. rid='neutron-gateway/0')
  646. self.migrate_nova_databases.assert_called_with()
  647. @patch.object(hooks, 'nova_cell_relation_joined')
  648. @patch.object(hooks, 'CONFIGS')
  649. def test_amqp_relation_broken(self, configs, cell_joined):
  650. configs.write = MagicMock()
  651. self.relation_ids.return_value = ['nova-cell-api/0']
  652. hooks.relation_broken()
  653. self.assertTrue(configs.write_all.called)
  654. cell_joined.assert_called_with(rid='nova-cell-api/0')
  655. @patch.object(hooks, 'leader_init_db_if_ready_allowed_units')
  656. @patch.object(hooks, 'update_cell_db_if_ready_allowed_units')
  657. @patch.object(hooks, 'is_db_initialised')
  658. @patch.object(hooks, 'quantum_joined')
  659. @patch.object(hooks, 'nova_api_relation_joined')
  660. @patch.object(hooks, 'nova_cell_relation_joined')
  661. @patch.object(hooks, 'CONFIGS')
  662. def test_amqp_changed_api_rel(self, configs, cell_joined, api_joined,
  663. quantum_joined, mock_is_db_initialised,
  664. update_db_allowed, init_db_allowed):
  665. self.relation_ids.side_effect = [
  666. ['nova-cell-api/0'],
  667. ['nova-api/0'],
  668. ['quantum-service/0'],
  669. ]
  670. mock_is_db_initialised.return_value = False
  671. configs.complete_contexts = MagicMock()
  672. configs.complete_contexts.return_value = ['amqp']
  673. configs.write = MagicMock()
  674. self.os_release.return_value = 'diablo'
  675. self.is_relation_made.return_value = True
  676. hooks.amqp_changed()
  677. self.assertEqual(configs.write.call_args_list,
  678. [call('/etc/nova/nova.conf')])
  679. cell_joined.assert_called_with(rid='nova-cell-api/0')
  680. api_joined.assert_called_with(rid='nova-api/0')
  681. quantum_joined.assert_called_with(rid='quantum-service/0',
  682. remote_restart=True)
  683. @patch.object(hooks, 'leader_init_db_if_ready_allowed_units')
  684. @patch.object(hooks, 'update_cell_db_if_ready_allowed_units')
  685. @patch.object(hooks, 'is_db_initialised')
  686. @patch.object(hooks, 'quantum_joined')
  687. @patch.object(hooks, 'nova_api_relation_joined')
  688. @patch.object(hooks, 'nova_cell_relation_joined')
  689. @patch.object(hooks, 'CONFIGS')
  690. def test_amqp_changed_noapi_rel(self, configs, cell_joined, api_joined,
  691. quantum_joined, mock_is_db_initialised,
  692. update_db_allowed, init_db_allowed):
  693. mock_is_db_initialised.return_value = False
  694. configs.complete_contexts = MagicMock()
  695. configs.complete_contexts.return_value = ['amqp']
  696. configs.write = MagicMock()
  697. self.relation_ids.side_effect = [
  698. ['nova-cell-api/0'],
  699. ['nova-api/0'],
  700. ['quantum-service/0'],
  701. ]
  702. self.is_relation_made.return_value = False
  703. self.network_manager.return_value = 'neutron'
  704. self.os_release.return_value = 'diablo'
  705. hooks.amqp_changed()
  706. self.assertEqual(configs.write.call_args_list,
  707. [call('/etc/nova/nova.conf')])
  708. cell_joined.assert_called_with(rid='nova-cell-api/0')
  709. api_joined.assert_called_with(rid='nova-api/0')
  710. quantum_joined.assert_called_with(rid='quantum-service/0',
  711. remote_restart=True)
  712. @patch.object(hooks, 'canonical_url')
  713. def test_nova_cell_relation_joined(self, _canonical_url):
  714. self.uuid.uuid4.return_value = 'bob'
  715. _canonical_url.return_value = 'http://novaurl'
  716. hooks.nova_cell_relation_joined(rid='rid',
  717. remote_restart=True)
  718. self.relation_set.assert_called_with(restart_trigger='bob',
  719. nova_url='http://novaurl:8774/v2',
  720. relation_id='rid')
  721. @patch.object(hooks, 'CONFIGS')
  722. def test_nova_cell_relation_changed(self, configs):
  723. hooks.nova_cell_relation_changed()
  724. configs.write.assert_called_with('/etc/nova/nova.conf')
  725. def test_get_cell_type(self):
  726. self.NovaCellContext().return_value = {
  727. 'cell_type': 'parent',
  728. 'cell_name': 'api',
  729. }
  730. self.assertEqual(hooks.get_cell_type(), 'parent')
  731. @patch.object(hooks, 'canonical_url')
  732. @patch.object(os, 'rename')
  733. @patch.object(os.path, 'isfile')
  734. @patch.object(hooks, 'CONFIGS')
  735. @patch.object(hooks, 'get_cell_type')
  736. def test_neutron_api_relation_joined(self, get_cell_type, configs, isfile,
  737. rename, _canonical_url):
  738. nova_url = 'http://novaurl:8774/v2'
  739. isfile.return_value = True
  740. _identity_joined = self.patch('identity_joined')
  741. self.relation_ids.return_value = ['relid']
  742. _canonical_url.return_value = 'http://novaurl'
  743. get_cell_type.return_value = 'parent'
  744. self.uuid.uuid4.return_value = 'bob'
  745. hooks.neutron_api_relation_joined(remote_restart=True)
  746. self.assertTrue(_identity_joined.called)
  747. self.relation_set.assert_called_with(relation_id=None,
  748. cell_type='parent',
  749. nova_url=nova_url,
  750. restart_trigger='bob')
  751. @patch.object(hooks, 'CONFIGS')
  752. def test_neutron_api_relation_changed(self, configs):
  753. self.relation_ids.return_value = ['relid']
  754. _compute_joined = self.patch('compute_joined')
  755. _quantum_joined = self.patch('quantum_joined')
  756. hooks.neutron_api_relation_changed()
  757. self.assertTrue(configs.write.called_with('/etc/nova/nova.conf'))
  758. self.assertTrue(_compute_joined.called)
  759. self.assertTrue(_quantum_joined.called)
  760. @patch.object(os, 'remove')
  761. @patch.object(os.path, 'isfile')
  762. @patch.object(hooks, 'CONFIGS')
  763. def test_neutron_api_relation_broken(self, configs, isfile, remove):
  764. isfile.return_value = True
  765. self.relation_ids.return_value = ['relid']
  766. _compute_joined = self.patch('compute_joined')
  767. _quantum_joined = self.patch('quantum_joined')
  768. hooks.neutron_api_relation_broken()
  769. self.assertTrue(configs.write_all.called)
  770. self.assertTrue(_compute_joined.called)
  771. self.assertTrue(_quantum_joined.called)
  772. @patch.object(hooks, 'canonical_url')
  773. @patch.object(utils, 'config')
  774. def test_console_settings_vnc(self, _utils_config, _canonical_url):
  775. _utils_config.return_value = 'vnc'
  776. _cc_host = "nova-cc-host1"
  777. _canonical_url.return_value = 'http://' + _cc_host
  778. _con_sets = hooks.console_settings()
  779. console_settings = {
  780. 'console_proxy_novnc_address': 'http://%s:6080/vnc_auto.html' %
  781. (_cc_host),
  782. 'console_proxy_novnc_port': 6080,
  783. 'console_access_protocol': 'vnc',
  784. 'console_proxy_novnc_host': _cc_host,
  785. 'console_proxy_xvpvnc_port': 6081,
  786. 'console_proxy_xvpvnc_host': _cc_host,
  787. 'console_proxy_xvpvnc_address': 'http://%s:6081/console' %
  788. (_cc_host),
  789. 'console_keymap': 'en-us'
  790. }
  791. self.assertEqual(_con_sets, console_settings)
  792. @patch.object(hooks, 'canonical_url')
  793. @patch.object(utils, 'config')
  794. def test_console_settings_xvpvnc(self, _utils_config, _canonical_url):
  795. _utils_config.return_value = 'xvpvnc'
  796. _cc_host = "nova-cc-host1"
  797. _canonical_url.return_value = 'http://' + _cc_host
  798. _con_sets = hooks.console_settings()
  799. console_settings = {
  800. 'console_access_protocol': 'xvpvnc',
  801. 'console_keymap': 'en-us',
  802. 'console_proxy_xvpvnc_port': 6081,
  803. 'console_proxy_xvpvnc_host': _cc_host,
  804. 'console_proxy_xvpvnc_address': 'http://%s:6081/console' %
  805. (_cc_host),
  806. }
  807. self.assertEqual(_con_sets, console_settings)
  808. @patch.object(hooks, 'canonical_url')
  809. @patch.object(utils, 'config')
  810. def test_console_settings_novnc(self, _utils_config, _canonical_url):
  811. _utils_config.return_value = 'novnc'
  812. _cc_host = "nova-cc-host1"
  813. _canonical_url.return_value = 'http://' + _cc_host
  814. _con_sets = hooks.console_settings()
  815. console_settings = {
  816. 'console_proxy_novnc_address': 'http://%s:6080/vnc_auto.html' %
  817. (_cc_host),
  818. 'console_proxy_novnc_port': 6080,
  819. 'console_access_protocol': 'novnc',
  820. 'console_proxy_novnc_host': _cc_host,
  821. 'console_keymap': 'en-us'
  822. }
  823. self.assertEqual(_con_sets, console_settings)
  824. @patch.object(hooks, 'canonical_url')
  825. @patch.object(utils, 'config')
  826. def test_console_settings_spice(self, _utils_config, _canonical_url):
  827. _utils_config.return_value = 'spice'
  828. _cc_host = "nova-cc-host1"
  829. _canonical_url.return_value = 'http://' + _cc_host
  830. _con_sets = hooks.console_settings()
  831. console_settings = {
  832. 'console_proxy_spice_address': 'http://%s:6082/spice_auto.html' %
  833. (_cc_host),
  834. 'console_proxy_spice_host': _cc_host,
  835. 'console_proxy_spice_port': 6082,
  836. 'console_access_protocol': 'spice',
  837. 'console_keymap': 'en-us'
  838. }
  839. self.assertEqual(_con_sets, console_settings)
  840. @patch.object(hooks, 'https')
  841. @patch.object(utils, 'config')
  842. def test_console_settings_explicit_ip(self, _utils_config, _https):
  843. _utils_config.return_value = 'spice'
  844. _https.return_value = False
  845. _cc_public_host = "public-host"
  846. self.test_config.set('console-proxy-ip', _cc_public_host)
  847. _con_sets = hooks.console_settings()
  848. console_settings = {
  849. 'console_proxy_spice_address': 'http://%s:6082/spice_auto.html' %
  850. (_cc_public_host),
  851. 'console_proxy_spice_host': _cc_public_host,
  852. 'console_proxy_spice_port': 6082,
  853. 'console_access_protocol': 'spice',
  854. 'console_keymap': 'en-us'
  855. }
  856. self.assertEqual(_con_sets, console_settings)
  857. @patch.object(hooks, 'https')
  858. @patch.object(utils, 'config')
  859. def test_console_settings_explicit_ip_with_https(self, _utils_config,
  860. _https):
  861. _utils_config.return_value = 'spice'
  862. _https.return_value = True
  863. _cc_public_host = "public-host"
  864. self.test_config.set('console-proxy-ip', _cc_public_host)
  865. _con_sets = hooks.console_settings()
  866. console_settings = {
  867. 'console_proxy_spice_address': 'https://%s:6082/spice_auto.html' %
  868. (_cc_public_host),
  869. 'console_proxy_spice_host': _cc_public_host,
  870. 'console_proxy_spice_port': 6082,
  871. 'console_access_protocol': 'spice',
  872. 'console_keymap': 'en-us'
  873. }
  874. self.assertEqual(_con_sets, console_settings)
  875. @patch('nova_cc_utils.config')
  876. def test_ha_relation_joined_no_bound_ip(self, config):
  877. self.get_hacluster_config.return_value = {
  878. 'ha-bindiface': 'em0',
  879. 'ha-mcastport': '8080',
  880. 'vip': '10.10.10.10',
  881. }
  882. self.test_config.set('vip_iface', 'eth120')
  883. self.test_config.set('vip_cidr', '21')
  884. config.return_value = None
  885. self.get_iface_for_address.return_value = None
  886. self.get_netmask_for_address.return_value = None
  887. hooks.ha_joined()
  888. args = {
  889. 'relation_id': None,
  890. 'corosync_bindiface': 'em0',
  891. 'corosync_mcastport': '8080',
  892. 'init_services': {'res_nova_haproxy': 'haproxy'},
  893. 'resources': {'res_nova_eth120_vip': 'ocf:heartbeat:IPaddr2',
  894. 'res_nova_haproxy': 'lsb:haproxy'},
  895. 'resource_params': {
  896. 'res_nova_eth120_vip': 'params ip="10.10.10.10"'
  897. ' cidr_netmask="21" nic="eth120"',
  898. 'res_nova_haproxy': 'op monitor interval="5s"'},
  899. 'colocations': {},
  900. 'clones': {'cl_nova_haproxy': 'res_nova_haproxy'}
  901. }
  902. self.relation_set.assert_has_calls([
  903. call(groups={'grp_nova_vips': 'res_nova_eth120_vip'}),
  904. call(**args),
  905. ])
  906. def test_ha_joined_dns_ha(self):
  907. def _fake_update(resources, resource_params, relation_id=None):
  908. resources.update({'res_nova_public_hostname': 'ocf:maas:dns'})
  909. resource_params.update({'res_nova_public_hostname':
  910. 'params fqdn="nova.maas" '
  911. 'ip_address="10.0.0.1"'})
  912. self.test_config.set('dns-ha', True)
  913. self.get_hacluster_config.return_value = {
  914. 'vip': None,
  915. 'ha-bindiface': 'em0',
  916. 'ha-mcastport': '8080',
  917. 'os-admin-hostname': None,
  918. 'os-internal-hostname': None,
  919. 'os-public-hostname': 'nova.maas',
  920. }
  921. args = {
  922. 'relation_id': None,
  923. 'corosync_bindiface': 'em0',
  924. 'corosync_mcastport': '8080',
  925. 'init_services': {'res_nova_haproxy': 'haproxy'},
  926. 'resources': {'res_nova_public_hostname': 'ocf:maas:dns',
  927. 'res_nova_haproxy': 'lsb:haproxy'},
  928. 'resource_params': {
  929. 'res_nova_public_hostname': 'params fqdn="nova.maas" '
  930. 'ip_address="10.0.0.1"',
  931. 'res_nova_haproxy': 'op monitor interval="5s"'},
  932. 'clones': {'cl_nova_haproxy': 'res_nova_haproxy'},
  933. 'colocations': {},
  934. }
  935. self.update_dns_ha_resource_params.side_effect = _fake_update
  936. hooks.ha_joined()
  937. self.assertTrue(self.update_dns_ha_resource_params.called)
  938. self.relation_set.assert_called_with(**args)
  939. @patch('nova_cc_utils.config')
  940. def test_ha_relation_multi_consoleauth(self, config):
  941. self.get_hacluster_config.return_value = {
  942. 'ha-bindiface': 'em0',
  943. 'ha-mcastport': '8080',
  944. 'vip': '10.10.10.10',
  945. }
  946. self.test_config.set('vip_iface', 'eth120')
  947. self.test_config.set('vip_cidr', '21')
  948. self.test_config.set('single-nova-consoleauth', False)
  949. config.return_value = 'novnc'
  950. self.get_iface_for_address.return_value = None
  951. self.get_netmask_for_address.return_value = None
  952. hooks.ha_joined()
  953. args = {
  954. 'relation_id': None,
  955. 'corosync_bindiface': 'em0',
  956. 'corosync_mcastport': '8080',
  957. 'init_services': {'res_nova_haproxy': 'haproxy'},
  958. 'resources': {'res_nova_eth120_vip': 'ocf:heartbeat:IPaddr2',
  959. 'res_nova_haproxy': 'lsb:haproxy'},
  960. 'resource_params': {
  961. 'res_nova_eth120_vip': 'params ip="10.10.10.10"'
  962. ' cidr_netmask="21" nic="eth120"',
  963. 'res_nova_haproxy': 'op monitor interval="5s"'},
  964. 'colocations': {},
  965. 'clones': {'cl_nova_haproxy': 'res_nova_haproxy'}
  966. }
  967. self.relation_set.assert_has_calls([
  968. call(groups={'grp_nova_vips': 'res_nova_eth120_vip'}),
  969. call(**args),
  970. ])
  971. @patch('nova_cc_utils.config')
  972. def test_ha_relation_single_consoleauth(self, config):
  973. self.get_hacluster_config.return_value = {
  974. 'ha-bindiface': 'em0',
  975. 'ha-mcastport': '8080',
  976. 'vip': '10.10.10.10',
  977. }
  978. self.test_config.set('vip_iface', 'eth120')
  979. self.test_config.set('vip_cidr', '21')
  980. config.return_value = 'novnc'
  981. self.get_iface_for_address.return_value = None
  982. self.get_netmask_for_address.return_value = None
  983. hooks.ha_joined()
  984. args = {
  985. 'relation_id': None,
  986. 'corosync_bindiface': 'em0',
  987. 'corosync_mcastport': '8080',
  988. 'init_services': {'res_nova_haproxy': 'haproxy',
  989. 'res_nova_consoleauth': 'nova-consoleauth'},
  990. 'resources': {'res_nova_eth120_vip': 'ocf:heartbeat:IPaddr2',
  991. 'res_nova_haproxy': 'lsb:haproxy',
  992. 'res_nova_consoleauth':
  993. 'ocf:openstack:nova-consoleauth'},
  994. 'resource_params': {
  995. 'res_nova_eth120_vip': 'params ip="10.10.10.10"'
  996. ' cidr_netmask="21" nic="eth120"',
  997. 'res_nova_haproxy': 'op monitor interval="5s"',
  998. 'res_nova_consoleauth': 'op monitor interval="5s"'},
  999. 'colocations': {
  1000. 'vip_consoleauth': 'inf: res_nova_consoleauth grp_nova_vips'
  1001. },
  1002. 'clones': {'cl_nova_haproxy': 'res_nova_haproxy'}
  1003. }
  1004. self.relation_set.assert_has_calls([
  1005. call(groups={'grp_nova_vips': 'res_nova_eth120_vip'}),
  1006. call(**args),
  1007. ])
  1008. @patch.object(hooks, 'update_aws_compat_services')
  1009. @patch.object(hooks, 'is_db_initialised')
  1010. @patch.object(hooks, 'determine_packages')
  1011. @patch.object(hooks, 'service_pause')
  1012. @patch.object(hooks, 'filter_installed_packages')
  1013. @patch('nova_cc_hooks.configure_https')
  1014. @patch('nova_cc_utils.config')
  1015. def test_config_changed_single_consoleauth(self, mock_config,
  1016. mock_configure_https,
  1017. mock_filter_packages,
  1018. mock_service_pause,
  1019. mock_determine_packages,
  1020. mock_is_db_initialised,
  1021. mock_update_aws_compat_svcs):
  1022. mock_determine_packages.return_value = []
  1023. mock_is_db_initialised.return_value = False
  1024. self.config_value_changed.return_value = False
  1025. self.git_install_requested.return_value = False
  1026. self.os_release.return_value = 'diablo'
  1027. def cfg(k, v):
  1028. if k == "single-nova-authconsole":
  1029. return True
  1030. return 'novnc'
  1031. config.side_effect = cfg
  1032. rids = {'ha': ['ha:1']}
  1033. def f(r):
  1034. return rids.get(r, [])
  1035. self.relation_ids.side_effect = f
  1036. hooks.config_changed()
  1037. args = {
  1038. 'delete_resources': [],
  1039. 'init_services': {'res_nova_consoleauth': 'nova-consoleauth'},
  1040. 'resources': {'res_nova_consoleauth':
  1041. 'ocf:openstack:nova-consoleauth'},
  1042. 'resource_params': {
  1043. 'res_nova_consoleauth': 'op monitor interval="5s"'},
  1044. 'colocations': {
  1045. 'vip_consoleauth': 'inf: res_nova_consoleauth grp_nova_vips'
  1046. }
  1047. }
  1048. self.relation_set.assert_has_calls([
  1049. call(v, **args) for v in rids['ha']
  1050. ])
  1051. mock_service_pause.assert_has_calls([
  1052. call('nova-consoleauth')]
  1053. )
  1054. mock_filter_packages.assert_called_with([])
  1055. self.assertTrue(mock_update_aws_compat_svcs.called)
  1056. @patch.object(hooks, 'is_api_ready')
  1057. def _test_nova_api_relation_joined(self, tgt, is_api_ready):
  1058. is_api_ready.return_value = tgt
  1059. exp = 'yes' if tgt else 'no'
  1060. hooks.nova_api_relation_joined(rid='foo')
  1061. self.relation_set.assert_called_with(
  1062. 'foo', **{'nova-api-ready': exp})
  1063. def test_nova_api_relation_joined_ready(self):
  1064. self._test_nova_api_relation_joined(True)
  1065. def test_nova_api_relation_joined_not_ready(self):
  1066. self._test_nova_api_relation_joined(False)
  1067. @patch.object(hooks, 'memcached_common')
  1068. def test_memcache_joined(self, _memcached_common):
  1069. self.get_relation_ip.return_value = 'foo'
  1070. hooks.memcached_joined()
  1071. self.get_relation_ip.assert_called_once_with('memcache')
  1072. self.relation_set.assert_called_once_with(
  1073. relation_id=None,
  1074. relation_settings={'private-address': 'foo'})
  1075. hooks.memcached_joined()