~ubuntu-branches/ubuntu/vivid/sahara/vivid-proposed

« back to all changes in this revision

Viewing changes to sahara/tests/unit/conductor/manager/test_clusters.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-24 16:34:46 UTC
  • Revision ID: package-import@ubuntu.com-20140924163446-8gu3zscu5e3n9lr2
Tags: upstream-2014.2~b3
ImportĀ upstreamĀ versionĀ 2014.2~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2013 Mirantis Inc.
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
import copy
 
17
 
 
18
import testtools
 
19
 
 
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
 
24
 
 
25
 
 
26
SAMPLE_CLUSTER = {
 
27
    "plugin_name": "test_plugin",
 
28
    "hadoop_version": "test_version",
 
29
    "tenant_id": "tenant_1",
 
30
    "is_transient": True,
 
31
    "name": "test_cluster",
 
32
    "user_keypair_id": "my_keypair",
 
33
    "node_groups": [
 
34
        {
 
35
            "name": "ng_1",
 
36
            "flavor_id": "42",
 
37
            "node_processes": ["p1", "p2"],
 
38
            "count": 1,
 
39
            "security_groups": None
 
40
        },
 
41
        {
 
42
            "name": "ng_2",
 
43
            "flavor_id": "42",
 
44
            "node_processes": ["p3", "p4"],
 
45
            "count": 3,
 
46
            "security_groups": ["group1", "group2"]
 
47
        }
 
48
    ],
 
49
    "cluster_configs": {
 
50
        "service_1": {
 
51
            "config_2": "value_2"
 
52
        },
 
53
        "service_2": {
 
54
            "config_1": "value_1"
 
55
        }
 
56
    },
 
57
}
 
58
 
 
59
 
 
60
class ClusterTest(test_base.ConductorManagerTestCase):
 
61
    def __init__(self, *args, **kwargs):
 
62
        super(ClusterTest, self).__init__(
 
63
            checks=[
 
64
                lambda: SAMPLE_CLUSTER,
 
65
                lambda: manager.CLUSTER_DEFAULTS,
 
66
                lambda: manager.NODE_GROUP_DEFAULTS,
 
67
                lambda: manager.INSTANCE_DEFAULTS,
 
68
            ], *args, **kwargs)
 
69
 
 
70
    def test_cluster_create_list_delete(self):
 
71
        ctx = context.ctx()
 
72
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
73
        self.assertIsInstance(cluster_db_obj, dict)
 
74
 
 
75
        lst = self.api.cluster_get_all(ctx)
 
76
        self.assertEqual(len(lst), 1)
 
77
        cl_id = lst[0]["id"]
 
78
 
 
79
        self.api.cluster_destroy(ctx, cl_id)
 
80
        lst = self.api.cluster_get_all(ctx)
 
81
        self.assertEqual(len(lst), 0)
 
82
 
 
83
        with testtools.ExpectedException(ex.NotFoundException):
 
84
            self.api.cluster_destroy(ctx, cl_id)
 
85
 
 
86
    def test_duplicate_cluster_create(self):
 
87
        ctx = context.ctx()
 
88
        self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
89
 
 
90
        with testtools.ExpectedException(ex.DBDuplicateEntry):
 
91
            self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
92
 
 
93
    def test_cluster_fields(self):
 
94
        ctx = context.ctx()
 
95
        cl_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
96
        self.assertIsInstance(cl_db_obj, dict)
 
97
 
 
98
        for key, val in SAMPLE_CLUSTER.items():
 
99
            if key == 'node_groups':
 
100
                # this will be checked separately
 
101
                continue
 
102
            self.assertEqual(val, cl_db_obj.get(key),
 
103
                             "Key not found %s" % key)
 
104
 
 
105
        for ng in cl_db_obj["node_groups"]:
 
106
            ng.pop("created_at")
 
107
            ng.pop("updated_at")
 
108
            ng.pop("id")
 
109
            self.assertEqual(ng.pop("cluster_id"), cl_db_obj["id"])
 
110
            ng.pop("image_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")
 
119
            ng.pop("open_ports")
 
120
            ng.pop("auto_security_group")
 
121
            ng.pop("tenant_id")
 
122
 
 
123
        self.assertEqual(SAMPLE_CLUSTER["node_groups"],
 
124
                         cl_db_obj["node_groups"])
 
125
 
 
126
    def test_cluster_no_ng(self):
 
127
        ctx = context.ctx()
 
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)
 
132
 
 
133
        for key, val in cluster_schema.items():
 
134
            self.assertEqual(val, cl_db_obj.get(key),
 
135
                             "Key not found %s" % key)
 
136
 
 
137
        self.assertEqual(cl_db_obj["node_groups"], [])
 
138
 
 
139
    def test_cluster_update_status(self):
 
140
        ctx = context.ctx()
 
141
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
142
        _id = cluster_db_obj["id"]
 
143
 
 
144
        updated_cl = self.api.cluster_update(ctx, _id, {"status": "Active"})
 
145
        self.assertIsInstance(updated_cl, dict)
 
146
        self.assertEqual(updated_cl["status"], "Active")
 
147
 
 
148
        get_cl_obj = self.api.cluster_get(ctx, _id)
 
149
        self.assertEqual(updated_cl, get_cl_obj)
 
150
 
 
151
        with testtools.ExpectedException(ex.NotFoundException):
 
152
            self.api.cluster_update(ctx, "bad_id", {"status": "Active"})
 
153
 
 
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:
 
157
                return ng
 
158
        return None
 
159
 
 
160
    def test_add_node_group(self):
 
161
        ctx = context.ctx()
 
162
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
163
        _id = cluster_db_obj["id"]
 
164
 
 
165
        node_group = {
 
166
            "name": "ng_3",
 
167
            "flavor_id": "42",
 
168
            "node_processes": ["p3", "p4"],
 
169
            "count": 5
 
170
        }
 
171
 
 
172
        ng_id = self.api.node_group_add(ctx, _id, node_group)
 
173
 
 
174
        cluster_db_obj = self.api.cluster_get(ctx, _id)
 
175
        found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
 
176
 
 
177
        self.assertTrue(found_ng, "New Node Group not found")
 
178
 
 
179
    def test_update_node_group(self):
 
180
        ctx = context.ctx()
 
181
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
182
        _id = cluster_db_obj["id"]
 
183
 
 
184
        self.assertEqual(len(cluster_db_obj["node_groups"]), 2)
 
185
        ng_id = cluster_db_obj["node_groups"][-1]["id"]
 
186
 
 
187
        self.api.node_group_update(ctx, ng_id, {"image_id": "test_image"})
 
188
 
 
189
        cluster_db_obj = self.api.cluster_get(ctx, _id)
 
190
 
 
191
        found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
 
192
        self.assertTrue(found_ng, "Updated Node Group not found")
 
193
 
 
194
        for ng in cluster_db_obj["node_groups"]:
 
195
            if ng["id"] != ng_id:
 
196
                continue
 
197
 
 
198
            self.assertEqual(ng["image_id"], "test_image")
 
199
 
 
200
    def test_delete_node_group(self):
 
201
        ctx = context.ctx()
 
202
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
203
        _id = cluster_db_obj["id"]
 
204
 
 
205
        ng_id = cluster_db_obj["node_groups"][-1]["id"]
 
206
 
 
207
        self.api.node_group_remove(ctx, ng_id)
 
208
 
 
209
        cluster_db_obj = self.api.cluster_get(ctx, _id)
 
210
        found_ng = self._ng_in_cluster(cluster_db_obj, ng_id)
 
211
 
 
212
        self.assertFalse(found_ng, "Node Group is still in a CLuster")
 
213
 
 
214
        with testtools.ExpectedException(ex.NotFoundException):
 
215
            self.api.node_group_remove(ctx, ng_id)
 
216
 
 
217
    def _add_instance(self, ctx, ng_id):
 
218
        instance = {
 
219
            "instance_name": "additional_vm"
 
220
        }
 
221
        return self.api.instance_add(ctx, ng_id, instance)
 
222
 
 
223
    def test_add_instance(self):
 
224
        ctx = context.ctx()
 
225
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
226
        _id = cluster_db_obj["id"]
 
227
 
 
228
        ng_id = cluster_db_obj["node_groups"][-1]["id"]
 
229
        count = cluster_db_obj["node_groups"][-1]["count"]
 
230
 
 
231
        self._add_instance(ctx, ng_id)
 
232
 
 
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:
 
236
                continue
 
237
 
 
238
            ng.pop('tenant_id')
 
239
            self.assertEqual(count + 1, ng["count"])
 
240
            self.assertEqual("additional_vm",
 
241
                             ng["instances"][0]["instance_name"])
 
242
 
 
243
    def test_update_instance(self):
 
244
        ctx = context.ctx()
 
245
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
246
        _id = cluster_db_obj["id"]
 
247
 
 
248
        ng_id = cluster_db_obj["node_groups"][-1]["id"]
 
249
 
 
250
        instance_id = self._add_instance(ctx, ng_id)
 
251
 
 
252
        self.api.instance_update(ctx, instance_id,
 
253
                                 {"management_ip": "1.1.1.1"})
 
254
 
 
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:
 
258
                continue
 
259
 
 
260
            self.assertEqual("1.1.1.1", ng["instances"][0]["management_ip"])
 
261
 
 
262
    def test_remove_instance(self):
 
263
        ctx = context.ctx()
 
264
        cluster_db_obj = self.api.cluster_create(ctx, SAMPLE_CLUSTER)
 
265
        _id = cluster_db_obj["id"]
 
266
 
 
267
        ng_id = cluster_db_obj["node_groups"][-1]["id"]
 
268
        count = cluster_db_obj["node_groups"][-1]["count"]
 
269
 
 
270
        instance_id = self._add_instance(ctx, ng_id)
 
271
 
 
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:
 
275
                continue
 
276
 
 
277
            self.assertEqual(count + 1, ng["count"])
 
278
 
 
279
        self.api.instance_remove(ctx, instance_id)
 
280
 
 
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:
 
284
                continue
 
285
 
 
286
            self.assertEqual(count, ng["count"])
 
287
 
 
288
        with testtools.ExpectedException(ex.NotFoundException):
 
289
            self.api.instance_remove(ctx, instance_id)