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_cluster_collection_handlers.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 mock import patch
  16. from oslo_serialization import jsonutils
  17. from sqlalchemy.sql import not_
  18. from nailgun import objects
  19. from nailgun import consts
  20. from nailgun.db.sqlalchemy.models import Cluster
  21. from nailgun.db.sqlalchemy.models import NetworkGroup
  22. from nailgun.db.sqlalchemy.models import Release
  23. from nailgun.extensions.network_manager.managers.nova_network import \
  24. NovaNetworkManager
  25. from nailgun.test.base import BaseIntegrationTest
  26. from nailgun.utils import reverse
  27. class TestHandlers(BaseIntegrationTest):
  28. def _get_cluster_networks(self, cluster_id):
  29. nets = self.app.get(
  30. reverse('NovaNetworkConfigurationHandler',
  31. {"cluster_id": cluster_id}),
  32. headers=self.default_headers,
  33. ).json_body["networks"]
  34. return nets
  35. def test_cluster_list_empty(self):
  36. resp = self.app.get(
  37. reverse('ClusterCollectionHandler'),
  38. headers=self.default_headers
  39. )
  40. self.assertEqual(200, resp.status_code)
  41. self.assertEqual([], resp.json_body)
  42. def test_cluster_create(self):
  43. release_id = self.env.create_release(api=False).id
  44. resp = self.app.post(
  45. reverse('ClusterCollectionHandler'),
  46. jsonutils.dumps({
  47. 'name': 'cluster-name',
  48. 'release': release_id,
  49. }),
  50. headers=self.default_headers
  51. )
  52. self.assertEqual(201, resp.status_code)
  53. def test_cluster_create_no_ip_addresses(self):
  54. """Two clusters having same networks updated to use full CIDR is ok"""
  55. cluster = self.env.create_cluster(
  56. api=True,
  57. net_provider=consts.CLUSTER_NET_PROVIDERS.nova_network)
  58. cluster_db = self.db.query(Cluster).get(cluster["id"])
  59. cluster2 = self.env.create_cluster(
  60. api=True,
  61. net_provider=consts.CLUSTER_NET_PROVIDERS.nova_network,
  62. release_id=cluster_db.release.id)
  63. cluster2_db = self.db.query(Cluster).get(cluster2["id"])
  64. for clstr in (cluster_db, cluster2_db):
  65. management_net = self.db.query(NetworkGroup).filter_by(
  66. name="management",
  67. group_id=objects.Cluster.get_default_group(clstr).id
  68. ).first()
  69. NovaNetworkManager.update(
  70. clstr,
  71. {
  72. "networks": [
  73. {
  74. "name": "management",
  75. "ip_ranges": [
  76. ["192.168.0.2", "192.168.255.254"]
  77. ],
  78. "id": management_net.id,
  79. "cluster_id": clstr.id,
  80. "vlan_start": 101,
  81. "cidr": "192.168.0.0/16",
  82. "gateway": "192.168.0.1"
  83. }
  84. ]
  85. }
  86. )
  87. cluster1_nets = self._get_cluster_networks(cluster["id"])
  88. cluster2_nets = self._get_cluster_networks(cluster2["id"])
  89. for net1, net2 in zip(cluster1_nets, cluster2_nets):
  90. for f in ('group_id', 'id'):
  91. del net1[f]
  92. del net2[f]
  93. cluster1_nets = sorted(cluster1_nets, key=lambda n: n['name'])
  94. cluster2_nets = sorted(cluster2_nets, key=lambda n: n['name'])
  95. self.assertEquals(cluster1_nets, cluster2_nets)
  96. def test_cluster_creation_same_networks(self):
  97. cluster1_id = self.env.create_cluster(
  98. api=True,
  99. net_provider=consts.CLUSTER_NET_PROVIDERS.nova_network)["id"]
  100. cluster2_id = self.env.create_cluster(
  101. api=True,
  102. net_provider=consts.CLUSTER_NET_PROVIDERS.nova_network)["id"]
  103. cluster1_nets = self._get_cluster_networks(cluster1_id)
  104. cluster2_nets = self._get_cluster_networks(cluster2_id)
  105. for net1, net2 in zip(cluster1_nets, cluster2_nets):
  106. for f in ('group_id', 'id'):
  107. del net1[f]
  108. del net2[f]
  109. cluster1_nets = sorted(cluster1_nets, key=lambda n: n['name'])
  110. cluster2_nets = sorted(cluster2_nets, key=lambda n: n['name'])
  111. self.assertEqual(cluster1_nets, cluster2_nets)
  112. def test_if_cluster_creates_correct_networks(self):
  113. release = Release()
  114. release.version = "1111-6.0"
  115. release.name = u"release_name_" + str(release.version)
  116. release.description = u"release_desc" + str(release.version)
  117. release.operating_system = consts.RELEASE_OS.ubuntu
  118. release.state = consts.RELEASE_STATES.available
  119. release.networks_metadata = self.env.get_default_networks_metadata()
  120. release.attributes_metadata = {
  121. "editable": {
  122. "keystone": {
  123. "admin_tenant": "admin"
  124. }
  125. },
  126. "generated": {
  127. "mysql": {
  128. "root_password": ""
  129. }
  130. }
  131. }
  132. self.db.add(release)
  133. self.db.commit()
  134. resp = self.app.post(
  135. reverse('ClusterCollectionHandler'),
  136. jsonutils.dumps({
  137. 'name': 'cluster-name',
  138. 'release': release.id,
  139. 'net_provider': consts.CLUSTER_NET_PROVIDERS.nova_network,
  140. }),
  141. headers=self.default_headers
  142. )
  143. self.assertEqual(201, resp.status_code)
  144. nets = self.db.query(NetworkGroup).filter(
  145. not_(NetworkGroup.name == "fuelweb_admin")
  146. ).all()
  147. obtained = []
  148. for net in nets:
  149. obtained.append({
  150. 'release': net.release,
  151. 'name': net.name,
  152. 'vlan_id': net.vlan_start,
  153. 'cidr': net.cidr,
  154. 'gateway': net.gateway
  155. })
  156. expected = [
  157. {
  158. 'release': release.id,
  159. 'name': u'public',
  160. 'vlan_id': None,
  161. 'cidr': '172.16.0.0/24',
  162. 'gateway': '172.16.0.1'
  163. },
  164. {
  165. 'release': release.id,
  166. 'name': u'fixed',
  167. 'vlan_id': None,
  168. 'cidr': None,
  169. 'gateway': None
  170. },
  171. {
  172. 'release': release.id,
  173. 'name': u'storage',
  174. 'vlan_id': 102,
  175. 'cidr': '192.168.1.0/24',
  176. 'gateway': None
  177. },
  178. {
  179. 'release': release.id,
  180. 'name': u'management',
  181. 'vlan_id': 101,
  182. 'cidr': '192.168.0.0/24',
  183. 'gateway': None
  184. }
  185. ]
  186. self.assertItemsEqual(expected, obtained)
  187. @patch('nailgun.rpc.cast')
  188. def test_verify_networks(self, mocked_rpc):
  189. cluster = self.env.create_cluster(
  190. api=True,
  191. net_provider=consts.CLUSTER_NET_PROVIDERS.nova_network)
  192. nets = self.env.nova_networks_get(cluster['id']).json_body
  193. resp = self.env.nova_networks_put(cluster['id'], nets)
  194. self.assertEqual(200, resp.status_code)