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_changes_model.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 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 oslo_serialization import jsonutils
  16. from nailgun import consts
  17. from nailgun import objects
  18. from nailgun.db.sqlalchemy.models import Cluster
  19. from nailgun.db.sqlalchemy.models import ClusterChanges
  20. from nailgun.test.base import BaseIntegrationTest
  21. from nailgun.test.base import fake_tasks
  22. from nailgun.test.base import mock_rpc
  23. from nailgun.utils import reverse
  24. class TestClusterChanges(BaseIntegrationTest):
  25. def test_cluster_creation_adds_pending_changes(self):
  26. self.env.create_cluster(api=True)
  27. attributes_changes = self.db.query(ClusterChanges).filter_by(
  28. name="attributes"
  29. ).all()
  30. self.assertEqual(len(attributes_changes), 1)
  31. networks_changes = self.db.query(ClusterChanges).filter_by(
  32. name="networks"
  33. ).all()
  34. self.assertEqual(len(networks_changes), 1)
  35. all_changes = self.db.query(ClusterChanges).all()
  36. self.assertEqual(len(all_changes), 2)
  37. def test_node_volumes_modification_adds_pending_changes(self):
  38. cluster = self.env.create_cluster(api=True)
  39. self.env.create_node(
  40. api=True,
  41. cluster_id=cluster["id"]
  42. )
  43. node_db = self.env.nodes[0]
  44. node_disks_changes = self.db.query(ClusterChanges).filter_by(
  45. name="disks",
  46. node_id=node_db.id
  47. ).all()
  48. self.assertEqual(len(node_disks_changes), 1)
  49. resp = self.app.get(
  50. reverse(
  51. 'ClusterHandler',
  52. kwargs={'obj_id': cluster['id']}),
  53. headers=self.default_headers
  54. )
  55. self.assertIn(
  56. {"name": "disks", "node_id": node_db.id},
  57. resp.json_body["changes"]
  58. )
  59. def test_node_volumes_clears_after_deletion_from_cluster(self):
  60. cluster = self.env.create_cluster(api=True)
  61. self.env.create_node(
  62. api=True,
  63. cluster_id=cluster["id"]
  64. )
  65. node_db = self.env.nodes[0]
  66. node_disks_changes = self.db.query(ClusterChanges).filter_by(
  67. name="disks",
  68. node_id=node_db.id
  69. ).all()
  70. self.assertEqual(len(node_disks_changes), 1)
  71. self.app.put(
  72. reverse('NodeCollectionHandler'),
  73. jsonutils.dumps([{"id": node_db.id, "cluster_id": None}]),
  74. headers=self.default_headers
  75. )
  76. self.env.refresh_clusters()
  77. node_disks_changes = self.db.query(ClusterChanges).filter_by(
  78. name="disks",
  79. node_id=node_db.id
  80. ).all()
  81. self.assertEqual(len(node_disks_changes), 0)
  82. def test_attributes_changing_adds_pending_changes(self):
  83. cluster = self.env.create_cluster(api=True)
  84. objects.Cluster.clear_pending_changes(cluster)
  85. all_changes = self.db.query(ClusterChanges).all()
  86. self.assertEqual(len(all_changes), 0)
  87. self.app.put(
  88. reverse(
  89. 'ClusterAttributesHandler',
  90. kwargs={'cluster_id': cluster['id']}),
  91. jsonutils.dumps({
  92. 'editable': {
  93. 'foo': {'bar': None}
  94. }
  95. }),
  96. headers=self.default_headers
  97. )
  98. pending_changes = self.db.query(ClusterChanges).filter_by(
  99. name="attributes"
  100. ).all()
  101. self.assertEqual(len(pending_changes), 1)
  102. def test_default_attributes_adds_pending_changes(self):
  103. cluster = self.env.create_cluster(api=True)
  104. objects.Cluster.clear_pending_changes(cluster)
  105. all_changes = self.db.query(ClusterChanges).all()
  106. self.assertEqual(len(all_changes), 0)
  107. self.app.put(
  108. reverse(
  109. 'ClusterAttributesDefaultsHandler',
  110. kwargs={'cluster_id': cluster['id']}),
  111. headers=self.default_headers
  112. )
  113. pending_changes = self.db.query(ClusterChanges).filter_by(
  114. name="attributes"
  115. ).all()
  116. self.assertEqual(len(pending_changes), 1)
  117. @fake_tasks(override_state={"progress": 100, "status": "ready"})
  118. def test_successful_deployment_drops_all_changes(self):
  119. cluster = self.env.create(
  120. nodes_kwargs=[
  121. {"api": True, "pending_addition": True}
  122. ]
  123. )
  124. supertask = self.env.launch_deployment()
  125. self.assertEqual(supertask.status, consts.TASK_STATUSES.ready)
  126. cluster_db = self.db.query(Cluster).get(cluster.id)
  127. self.assertEqual(list(cluster_db.changes), [])
  128. @fake_tasks(error="deployment", error_msg="Terrible error")
  129. def test_failed_deployment_does_nothing_with_changes(self):
  130. cluster = self.env.create_cluster(api=True)
  131. self.env.create_node(
  132. cluster_id=cluster["id"],
  133. status="error",
  134. error_type="provision",
  135. pending_roles=["controller"]
  136. )
  137. supertask = self.env.launch_deployment()
  138. self.assertEqual(supertask.status, consts.TASK_STATUSES.error)
  139. attributes_changes = self.db.query(ClusterChanges).filter_by(
  140. name="attributes"
  141. ).all()
  142. self.assertEqual(len(attributes_changes), 1)
  143. networks_changes = self.db.query(ClusterChanges).filter_by(
  144. name="networks"
  145. ).all()
  146. self.assertEqual(len(networks_changes), 1)
  147. disks_changes = self.db.query(ClusterChanges).filter_by(
  148. name="disks"
  149. ).all()
  150. self.assertEqual(len(disks_changes), 1)
  151. all_changes = self.db.query(ClusterChanges).all()
  152. self.assertEqual(len(all_changes), 4)
  153. @mock_rpc()
  154. def test_role_unassignment_drops_changes(self):
  155. cluster = self.env.create(
  156. nodes_kwargs=[
  157. {"pending_addition": True, "api": True}
  158. ]
  159. )
  160. supertask = self.env.launch_deployment()
  161. self.assertNotEqual(supertask.status, consts.TASK_STATUSES.error)
  162. new_node = self.env.create_node(
  163. cluster_id=cluster.id,
  164. pending_addition=True,
  165. api=True
  166. )
  167. self.app.put(
  168. reverse("NodeHandler",
  169. kwargs={"obj_id": new_node["id"]}),
  170. jsonutils.dumps({
  171. "cluster": None,
  172. "pending_addition": False,
  173. "pending_roles": []
  174. }),
  175. headers=self.default_headers
  176. )
  177. all_changes = self.db.query(ClusterChanges).filter_by(
  178. cluster_id=cluster.id,
  179. node_id=new_node["id"]
  180. ).all()
  181. self.assertEqual(all_changes, [])