Fuel UI
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_deployment_graph_object.py 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Mirantis, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from nailgun.objects import deployment_graph
  16. from nailgun.test import base
  17. JSON_TASKS = [
  18. {
  19. 'id': 'post_deployment_end',
  20. 'type': 'stage',
  21. 'requires': ['post_deployment_start']
  22. },
  23. {
  24. 'id': 'primary-controller',
  25. 'parameters': {'strategy': {'type': 'one_by_one'}},
  26. 'required_for': ['deploy_end'],
  27. 'requires': ['deploy_start'],
  28. 'role': ['primary-controller'],
  29. 'type': 'group'
  30. },
  31. {
  32. 'id': 'test-roles',
  33. 'role': '*',
  34. 'type': 'group'
  35. },
  36. {
  37. 'id': 'cross-dep-test',
  38. 'type': 'puppet',
  39. 'cross-depended-by': ['a', 'b'],
  40. 'cross-depends': ['c', 'd'],
  41. },
  42. {
  43. 'id': 'custom-fields-test',
  44. 'type': 'puppet',
  45. 'CUSTOM_FIELD1': 'custom',
  46. 'CUSTOM_FIELD2': ['custom'],
  47. 'CUSTOM_FIELD3': {'custom': 'custom'},
  48. },
  49. {
  50. 'id': 'ssl-keys-saving',
  51. 'type': 'puppet',
  52. 'version': '2.0.0',
  53. 'groups': ['primary-controller', 'controller', 'compute',
  54. 'cinder', 'primary-mongo', 'mongo', 'ceph-osd', 'virt'],
  55. 'requires': ['firewall'],
  56. 'condition': "(settings:public_ssl.horizon.value == true or settings:"
  57. "public_ssl.services.value == true) and settings:"
  58. "public_ssl.cert_source.value == 'user_uploaded'",
  59. 'required_for': ['deploy_end'],
  60. 'parameters': {
  61. 'puppet_manifest': '/etc/puppet/modules/osnailyfacter/'
  62. 'modular/ssl/ssl_keys_saving.pp',
  63. 'puppet_modules': '/etc/puppet/modules',
  64. 'timeout': 3600
  65. },
  66. 'test_pre': {
  67. 'cmd': 'ruby /etc/puppet/modules/osnailyfacter/'
  68. 'modular/ssl/ssl_keys_saving_pre.rb'
  69. }
  70. }
  71. ]
  72. EXPECTED_TASKS = [
  73. {
  74. 'task_name': 'cross-dep-test',
  75. 'id': 'cross-dep-test', # legacy
  76. 'type': 'puppet',
  77. 'version': '1.0.0',
  78. 'cross_depended_by': ['a', 'b'],
  79. 'cross_depends': ['c', 'd'],
  80. 'cross-depended-by': ['a', 'b'], # legacy
  81. 'cross-depends': ['c', 'd'], # legacy
  82. }, {
  83. 'task_name': 'post_deployment_end',
  84. 'id': 'post_deployment_end', # legacy
  85. 'type': 'stage',
  86. 'version': '1.0.0',
  87. 'requires': ['post_deployment_start'],
  88. }, {
  89. 'task_name': 'test-roles',
  90. 'id': 'test-roles',
  91. 'role': '*',
  92. 'roles': '*',
  93. 'version': '1.0.0',
  94. 'type': 'group'
  95. }, {
  96. 'task_name': 'primary-controller',
  97. 'id': 'primary-controller', # legacy
  98. 'type': 'group',
  99. 'version': '1.0.0',
  100. 'required_for': ['deploy_end'],
  101. 'requires': ['deploy_start'],
  102. 'roles': ['primary-controller'],
  103. 'role': ['primary-controller'], # legacy
  104. 'parameters': {'strategy': {'type': 'one_by_one'}},
  105. },
  106. {
  107. 'id': 'custom-fields-test',
  108. 'task_name': 'custom-fields-test',
  109. 'type': 'puppet',
  110. 'version': '1.0.0',
  111. 'CUSTOM_FIELD1': 'custom',
  112. 'CUSTOM_FIELD2': ['custom'],
  113. 'CUSTOM_FIELD3': {'custom': 'custom'},
  114. },
  115. {
  116. 'id': 'ssl-keys-saving',
  117. 'task_name': 'ssl-keys-saving',
  118. 'type': 'puppet',
  119. 'version': '2.0.0',
  120. 'groups': ['primary-controller', 'controller', 'compute',
  121. 'cinder', 'primary-mongo', 'mongo', 'ceph-osd', 'virt'],
  122. 'requires': ['firewall'],
  123. 'condition': "(settings:public_ssl.horizon.value == true or "
  124. "settings:public_ssl.services.value == true) "
  125. "and settings:public_ssl.cert_source.value == "
  126. "'user_uploaded'",
  127. 'required_for': ['deploy_end'],
  128. 'parameters': {
  129. 'puppet_manifest': '/etc/puppet/modules/osnailyfacter/'
  130. 'modular/ssl/ssl_keys_saving.pp',
  131. 'puppet_modules': '/etc/puppet/modules',
  132. 'timeout': 3600
  133. },
  134. 'test_pre': {
  135. 'cmd': 'ruby /etc/puppet/modules/osnailyfacter/'
  136. 'modular/ssl/ssl_keys_saving_pre.rb'
  137. }
  138. }
  139. ]
  140. class TestDeploymentGraphModel(
  141. base.BaseTestCase,
  142. base.DeploymentTasksTestMixin
  143. ):
  144. def test_deployment_graph_creation(self):
  145. dg = deployment_graph.DeploymentGraph.create({
  146. 'tasks': JSON_TASKS,
  147. 'name': 'test_graph',
  148. 'node_filter': '$.pending_deletion',
  149. 'on_success': {'node_attributes': {'status': 'discover'}},
  150. 'on_error': {'node_attributes': {'status': 'error'}},
  151. 'on_stop': {}
  152. })
  153. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  154. self.assertEqual(serialized['name'], 'test_graph')
  155. self.assertEqual('$.pending_deletion', serialized['node_filter'])
  156. self.assertEqual(
  157. {'status': 'discover'}, serialized['on_success']['node_attributes']
  158. )
  159. self.assertEqual(
  160. {'status': 'error'}, serialized['on_error']['node_attributes']
  161. )
  162. self.assertEqual(
  163. {}, serialized['on_stop']
  164. )
  165. self.assertItemsEqual(serialized['tasks'], EXPECTED_TASKS)
  166. def test_create_deployment_graph_with_default_handlers(self):
  167. dg = deployment_graph.DeploymentGraph.create({
  168. 'tasks': JSON_TASKS, 'name': 'test_graph'
  169. })
  170. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  171. self.assertEqual(serialized['name'], 'test_graph')
  172. self.assertItemsEqual(serialized['tasks'], EXPECTED_TASKS)
  173. def test_deployment_graph_update(self):
  174. updated_tasks = [
  175. {
  176. 'task_name': 'updated',
  177. 'type': 'puppet'
  178. }
  179. ]
  180. expected_updated_tasks = [
  181. {
  182. 'task_name': 'updated',
  183. 'type': 'puppet'
  184. }
  185. ]
  186. dg = deployment_graph.DeploymentGraph.create(
  187. {'tasks': JSON_TASKS, 'name': 'test_graph'})
  188. deployment_graph.DeploymentGraph.update(dg, {'tasks': updated_tasks})
  189. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  190. self.assertEqual(serialized['name'], 'test_graph')
  191. self._compare_tasks(
  192. expected_updated_tasks, serialized['tasks'])
  193. def test_deployment_graph_delete(self):
  194. cluster = self.env.create()
  195. deployment_graph.DeploymentGraph.create_for_model(
  196. {'tasks': JSON_TASKS, 'name': 'test_graph'}, cluster, 'test_graph'
  197. )
  198. dg = deployment_graph.DeploymentGraph.get_for_model(
  199. cluster, 'test_graph'
  200. )
  201. self.assertIsNotNone(dg)
  202. deployment_graph.DeploymentGraph.delete(dg)
  203. dg = deployment_graph.DeploymentGraph.get_for_model(
  204. cluster, 'test_graph'
  205. )
  206. self.assertIsNone(dg)
  207. def test_deployment_graph_create_for_model(self):
  208. cluster = self.env.create()
  209. deployment_graph.DeploymentGraph.create_for_model(
  210. {'tasks': JSON_TASKS, 'name': 'test_graph'}, cluster, 'test_graph'
  211. )
  212. dg = deployment_graph.DeploymentGraph.get_for_model(
  213. cluster, 'test_graph'
  214. )
  215. self._compare_tasks(
  216. EXPECTED_TASKS, deployment_graph.DeploymentGraph.get_tasks(dg)
  217. )
  218. def test_create_tasks_with_yaql_condition(self):
  219. task = {
  220. 'task_name': 'test',
  221. 'type': 'puppet',
  222. 'condition': {'yaql_exp': 'changed($.nodes)'},
  223. 'roles': ['/.*/'],
  224. 'requires': ['other_task'],
  225. }
  226. graph = deployment_graph.DeploymentGraph.create(
  227. {'name': 'test_graph', 'tasks': [task]}
  228. )
  229. self._compare_tasks(
  230. [task], deployment_graph.DeploymentGraph.get_tasks(graph)
  231. )
  232. def test_get_metadata(self):
  233. graph = deployment_graph.DeploymentGraph.create({
  234. 'name': 'test_graph', 'tasks': [],
  235. 'node_filter': '$.deleted',
  236. 'on_success': {},
  237. 'on_error': {'node_attributes': {'error_type': 'deploy'}}
  238. })
  239. metadata = deployment_graph.DeploymentGraph.get_metadata(graph)
  240. self.assertEqual(
  241. {
  242. 'node_filter': '$.deleted',
  243. 'on_success': {},
  244. 'on_error': {'node_attributes': {'error_type': 'deploy'}}
  245. },
  246. metadata
  247. )