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

« back to all changes in this revision

Viewing changes to sahara/tests/unit/conductor/manager/test_templates.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 testtools
 
17
 
 
18
from sahara.conductor import manager
 
19
from sahara import context
 
20
from sahara import exceptions as ex
 
21
import sahara.tests.unit.conductor.base as test_base
 
22
 
 
23
 
 
24
SAMPLE_NGT = {
 
25
    "plugin_name": "test_plugin",
 
26
    "flavor_id": "42",
 
27
    "tenant_id": "tenant_1",
 
28
    "hadoop_version": "test_version",
 
29
    "name": "ngt_test",
 
30
    "node_processes": ["p1", "p2"],
 
31
    "floating_ip_pool": None,
 
32
    "node_configs": {
 
33
        "service_1": {
 
34
            "config_1": "value_1"
 
35
        },
 
36
        "service_2": {
 
37
            "config_1": "value_1"
 
38
        }
 
39
    }
 
40
}
 
41
 
 
42
SAMPLE_CLT = {
 
43
    "plugin_name": "test_plugin",
 
44
    "tenant_id": "tenant_1",
 
45
    "hadoop_version": "test_version",
 
46
    "name": "clt_test",
 
47
    "cluster_configs": {
 
48
        "service_1": {
 
49
            "config_1": "value_1"
 
50
        },
 
51
        "service_2": {
 
52
            "config_1": "value_1"
 
53
        }
 
54
    },
 
55
    "node_groups": [
 
56
        {
 
57
            "name": "ng_1",
 
58
            "flavor_id": "42",
 
59
            "node_processes": ["p1", "p2"],
 
60
            "count": 1,
 
61
            "floating_ip_pool": None,
 
62
            "security_groups": None,
 
63
        },
 
64
        {
 
65
            "name": "ng_2",
 
66
            "flavor_id": "42",
 
67
            "node_processes": ["p3", "p4"],
 
68
            "count": 3,
 
69
            "floating_ip_pool": None,
 
70
            "security_groups": ["group1", "group2"],
 
71
        }
 
72
 
 
73
    ]
 
74
}
 
75
 
 
76
 
 
77
class NodeGroupTemplates(test_base.ConductorManagerTestCase):
 
78
    def __init__(self, *args, **kwargs):
 
79
        super(NodeGroupTemplates, self).__init__(
 
80
            checks=[
 
81
                lambda: SAMPLE_CLT,
 
82
                lambda: SAMPLE_NGT,
 
83
                lambda: manager.CLUSTER_DEFAULTS,
 
84
                lambda: manager.NODE_GROUP_DEFAULTS,
 
85
                lambda: manager.INSTANCE_DEFAULTS,
 
86
            ], *args, **kwargs)
 
87
 
 
88
    def test_minimal_ngt_create_list_delete(self):
 
89
        ctx = context.ctx()
 
90
        self.api.node_group_template_create(ctx, SAMPLE_NGT)
 
91
 
 
92
        lst = self.api.node_group_template_get_all(ctx)
 
93
        self.assertEqual(len(lst), 1)
 
94
 
 
95
        ngt_id = lst[0]['id']
 
96
        self.api.node_group_template_destroy(ctx, ngt_id)
 
97
 
 
98
        lst = self.api.node_group_template_get_all(ctx)
 
99
        self.assertEqual(len(lst), 0)
 
100
 
 
101
    def test_duplicate_ngt_create(self):
 
102
        ctx = context.ctx()
 
103
        self.api.node_group_template_create(ctx, SAMPLE_NGT)
 
104
        with testtools.ExpectedException(ex.DBDuplicateEntry):
 
105
            self.api.node_group_template_create(ctx, SAMPLE_NGT)
 
106
 
 
107
    def test_ngt_fields(self):
 
108
        ctx = context.ctx()
 
109
        ngt_db_obj_id = self.api.node_group_template_create(
 
110
            ctx, SAMPLE_NGT)['id']
 
111
 
 
112
        ngt_db_obj = self.api.node_group_template_get(ctx, ngt_db_obj_id)
 
113
        self.assertIsInstance(ngt_db_obj, dict)
 
114
 
 
115
        for key, val in SAMPLE_NGT.items():
 
116
            self.assertEqual(val, ngt_db_obj.get(key),
 
117
                             "Key not found %s" % key)
 
118
 
 
119
    def test_ngt_delete(self):
 
120
        ctx = context.ctx()
 
121
        db_obj_ngt = self.api.node_group_template_create(ctx, SAMPLE_NGT)
 
122
        _id = db_obj_ngt['id']
 
123
 
 
124
        self.api.node_group_template_destroy(ctx, _id)
 
125
 
 
126
        with testtools.ExpectedException(ex.NotFoundException):
 
127
            self.api.node_group_template_destroy(ctx, _id)
 
128
 
 
129
 
 
130
class ClusterTemplates(test_base.ConductorManagerTestCase):
 
131
    def __init__(self, *args, **kwargs):
 
132
        super(ClusterTemplates, self).__init__(
 
133
            checks=[
 
134
                lambda: SAMPLE_CLT,
 
135
                lambda: SAMPLE_NGT,
 
136
                lambda: manager.CLUSTER_DEFAULTS,
 
137
                lambda: manager.NODE_GROUP_DEFAULTS,
 
138
                lambda: manager.INSTANCE_DEFAULTS,
 
139
            ], *args, **kwargs)
 
140
 
 
141
    def test_minimal_clt_create_list_delete(self):
 
142
        ctx = context.ctx()
 
143
        self.api.cluster_template_create(ctx, SAMPLE_CLT)
 
144
 
 
145
        lst = self.api.cluster_template_get_all(ctx)
 
146
        self.assertEqual(len(lst), 1)
 
147
 
 
148
        clt_id = lst[0]['id']
 
149
        self.api.cluster_template_destroy(ctx, clt_id)
 
150
 
 
151
        lst = self.api.cluster_template_get_all(ctx)
 
152
        self.assertEqual(len(lst), 0)
 
153
 
 
154
        with testtools.ExpectedException(ex.NotFoundException):
 
155
            self.api.cluster_template_destroy(ctx, clt_id)
 
156
 
 
157
    def test_duplicate_clt_create(self):
 
158
        ctx = context.ctx()
 
159
        self.api.cluster_template_create(ctx, SAMPLE_CLT)
 
160
        with testtools.ExpectedException(ex.DBDuplicateEntry):
 
161
            self.api.cluster_template_create(ctx, SAMPLE_CLT)
 
162
 
 
163
    def test_clt_fields(self):
 
164
        ctx = context.ctx()
 
165
        clt_db_obj_id = self.api.cluster_template_create(ctx, SAMPLE_CLT)['id']
 
166
 
 
167
        clt_db_obj = self.api.cluster_template_get(ctx, clt_db_obj_id)
 
168
        self.assertIsInstance(clt_db_obj, dict)
 
169
 
 
170
        for key, val in SAMPLE_CLT.items():
 
171
            if key == 'node_groups':
 
172
                # this will be checked separately
 
173
                continue
 
174
            self.assertEqual(val, clt_db_obj.get(key),
 
175
                             "Key not found %s" % key)
 
176
 
 
177
        for ng in clt_db_obj["node_groups"]:
 
178
            ng.pop("created_at")
 
179
            ng.pop("updated_at")
 
180
            ng.pop("id")
 
181
            ng.pop("tenant_id")
 
182
            self.assertEqual(ng.pop("cluster_template_id"), clt_db_obj_id)
 
183
            ng.pop("image_id")
 
184
            ng.pop("node_configs")
 
185
            ng.pop("node_group_template_id")
 
186
            ng.pop("volume_mount_prefix")
 
187
            ng.pop("volumes_size")
 
188
            ng.pop("volumes_per_node")
 
189
            ng.pop("auto_security_group")
 
190
 
 
191
        self.assertEqual(SAMPLE_CLT["node_groups"],
 
192
                         clt_db_obj["node_groups"])
 
193
 
 
194
    def test_clt_delete(self):
 
195
        ctx = context.ctx()
 
196
        db_obj_clt = self.api.cluster_template_create(ctx, SAMPLE_CLT)
 
197
        _id = db_obj_clt['id']
 
198
 
 
199
        self.api.cluster_template_destroy(ctx, _id)
 
200
 
 
201
        with testtools.ExpectedException(ex.NotFoundException):
 
202
            self.api.cluster_template_destroy(ctx, _id)