1
# Copyright (c) 2013 Mirantis Inc.
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
20
from sahara.conductor import manager
21
from sahara import context
22
from sahara import exceptions as ex
23
import sahara.tests.unit.conductor.base as test_base
27
"plugin_name": "test_plugin",
28
"hadoop_version": "test_version",
29
"tenant_id": "tenant_1",
31
"name": "test_cluster",
32
"user_keypair_id": "my_keypair",
37
"node_processes": ["p1", "p2"],
39
"security_groups": None
44
"node_processes": ["p3", "p4"],
46
"security_groups": ["group1", "group2"]
60
class ClusterTest(test_base.ConductorManagerTestCase):
61
def __init__(self, *args, **kwargs):
62
super(ClusterTest, self).__init__(
64
lambda: SAMPLE_CLUSTER,
65
lambda: manager.CLUSTER_DEFAULTS,
66
lambda: manager.NODE_GROUP_DEFAULTS,
67
lambda: manager.INSTANCE_DEFAULTS,
70
def test_cluster_create_list_delete(self):
72
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
73
self.assertIsInstance(cluster_db_obj, dict)
75
lst = self.api.cluster_get_all(ctx)
76
self.assertEqual(len(lst), 1)
79
self.api.cluster_destroy(ctx, cl_id)
80
lst = self.api.cluster_get_all(ctx)
81
self.assertEqual(len(lst), 0)
83
with testtools.ExpectedException(ex.NotFoundException):
84
self.api.cluster_destroy(ctx, cl_id)
86
def test_duplicate_cluster_create(self):
88
self.api.cluster_create(ctx, SAMPLE_CLUSTER)
90
with testtools.ExpectedException(ex.DBDuplicateEntry):
91
self.api.cluster_create(ctx, SAMPLE_CLUSTER)
93
def test_cluster_fields(self):
95
cl_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
96
self.assertIsInstance(cl_db_obj, dict)
98
for key, val in SAMPLE_CLUSTER.items():
99
if key == 'node_groups':
100
# this will be checked separately
102
self.assertEqual(val, cl_db_obj.get(key),
103
"Key not found %s" % key)
105
for ng in cl_db_obj["node_groups"]:
109
self.assertEqual(ng.pop("cluster_id"), cl_db_obj["id"])
111
self.assertEqual(ng.pop("instances"), [])
112
ng.pop("node_configs")
113
ng.pop("node_group_template_id")
114
ng.pop("volume_mount_prefix")
115
ng.pop("volumes_size")
116
ng.pop("volumes_per_node")
117
ng.pop("floating_ip_pool")
118
ng.pop("image_username")
120
ng.pop("auto_security_group")
123
self.assertEqual(SAMPLE_CLUSTER["node_groups"],
124
cl_db_obj["node_groups"])
126
def test_cluster_no_ng(self):
128
cluster_schema = copy.deepcopy(SAMPLE_CLUSTER)
129
cluster_schema.pop('node_groups')
130
cl_db_obj = self.api.cluster_create(ctx, cluster_schema)
131
self.assertIsInstance(cl_db_obj, dict)
133
for key, val in cluster_schema.items():
134
self.assertEqual(val, cl_db_obj.get(key),
135
"Key not found %s" % key)
137
self.assertEqual(cl_db_obj["node_groups"], [])
139
def test_cluster_update_status(self):
141
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
142
_id = cluster_db_obj["id"]
144
updated_cl = self.api.cluster_update(ctx, _id, {"status": "Active"})
145
self.assertIsInstance(updated_cl, dict)
146
self.assertEqual(updated_cl["status"], "Active")
148
get_cl_obj = self.api.cluster_get(ctx, _id)
149
self.assertEqual(updated_cl, get_cl_obj)
151
with testtools.ExpectedException(ex.NotFoundException):
152
self.api.cluster_update(ctx, "bad_id", {"status": "Active"})
154
def _ng_in_cluster(self, cluster_db_obj, ng_id):
155
for ng in cluster_db_obj["node_groups"]:
156
if ng["id"] == ng_id:
160
def test_add_node_group(self):
162
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
163
_id = cluster_db_obj["id"]
168
"node_processes": ["p3", "p4"],
172
ng_id = self.api.node_group_add(ctx, _id, node_group)
174
cluster_db_obj = self.api.cluster_get(ctx, _id)
175
found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
177
self.assertTrue(found_ng, "New Node Group not found")
179
def test_update_node_group(self):
181
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
182
_id = cluster_db_obj["id"]
184
self.assertEqual(len(cluster_db_obj["node_groups"]), 2)
185
ng_id = cluster_db_obj["node_groups"][-1]["id"]
187
self.api.node_group_update(ctx, ng_id, {"image_id": "test_image"})
189
cluster_db_obj = self.api.cluster_get(ctx, _id)
191
found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
192
self.assertTrue(found_ng, "Updated Node Group not found")
194
for ng in cluster_db_obj["node_groups"]:
195
if ng["id"] != ng_id:
198
self.assertEqual(ng["image_id"], "test_image")
200
def test_delete_node_group(self):
202
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
203
_id = cluster_db_obj["id"]
205
ng_id = cluster_db_obj["node_groups"][-1]["id"]
207
self.api.node_group_remove(ctx, ng_id)
209
cluster_db_obj = self.api.cluster_get(ctx, _id)
210
found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
212
self.assertFalse(found_ng, "Node Group is still in a CLuster")
214
with testtools.ExpectedException(ex.NotFoundException):
215
self.api.node_group_remove(ctx, ng_id)
217
def _add_instance(self, ctx, ng_id):
219
"instance_name": "additional_vm"
221
return self.api.instance_add(ctx, ng_id, instance)
223
def test_add_instance(self):
225
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
226
_id = cluster_db_obj["id"]
228
ng_id = cluster_db_obj["node_groups"][-1]["id"]
229
count = cluster_db_obj["node_groups"][-1]["count"]
231
self._add_instance(ctx, ng_id)
233
cluster_db_obj = self.api.cluster_get(ctx, _id)
234
for ng in cluster_db_obj["node_groups"]:
235
if ng["id"] != ng_id:
239
self.assertEqual(count + 1, ng["count"])
240
self.assertEqual("additional_vm",
241
ng["instances"][0]["instance_name"])
243
def test_update_instance(self):
245
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
246
_id = cluster_db_obj["id"]
248
ng_id = cluster_db_obj["node_groups"][-1]["id"]
250
instance_id = self._add_instance(ctx, ng_id)
252
self.api.instance_update(ctx, instance_id,
253
{"management_ip": "1.1.1.1"})
255
cluster_db_obj = self.api.cluster_get(ctx, _id)
256
for ng in cluster_db_obj["node_groups"]:
257
if ng["id"] != ng_id:
260
self.assertEqual("1.1.1.1", ng["instances"][0]["management_ip"])
262
def test_remove_instance(self):
264
cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
265
_id = cluster_db_obj["id"]
267
ng_id = cluster_db_obj["node_groups"][-1]["id"]
268
count = cluster_db_obj["node_groups"][-1]["count"]
270
instance_id = self._add_instance(ctx, ng_id)
272
cluster_db_obj = self.api.cluster_get(ctx, _id)
273
for ng in cluster_db_obj["node_groups"]:
274
if ng["id"] != ng_id:
277
self.assertEqual(count + 1, ng["count"])
279
self.api.instance_remove(ctx, instance_id)
281
cluster_db_obj = self.api.cluster_get(ctx, _id)
282
for ng in cluster_db_obj["node_groups"]:
283
if ng["id"] != ng_id:
286
self.assertEqual(count, ng["count"])
288
with testtools.ExpectedException(ex.NotFoundException):
289
self.api.instance_remove(ctx, instance_id)