aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_msb_k8s.py
blob: 37be1890ccfee3a163e84118de8439bdae13644c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#   Copyright 2022 Orange, Deutsche Telekom AG
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from unittest import mock

from onapsdk.msb.k8s import Definition, ConnectivityInfo, Instance


CONNECTIVITY_INFO = {
    "cloud-region": "test_cloud_region",
    "cloud-owner": "test_cloud_owner",
    "other-connectivity-list": {},
    "kubeconfig": "test_kubeconfig"
}


DEFINITION = {
    "rb-name": "test_rb_name_0",
    "rb-version": "test_rb_version_0"
}


DEFINITIONS = [
    DEFINITION,
    {
        "rb-name": "test_rb_name_1",
        "rb-version": "test_rb_version_1",
        "chart-name": "test_chart_name_1",
        "description": "test_description_1",
        "labels": {}
    }
]


PROFILE = {
    "rb-name": "test_rb_name",
    "rb-version": "test_rb_version",
    "profile-name": "test_profile_name",
    "namespace": "test_namespace"
}


PROFILES = [
    PROFILE,
    {
        "rb-name": "test_rb_name_1",
        "rb-version": "test_rb_version_1",
        "profile-name": "test_profile_name_1",
        "namespace": "test_namespace_1"
    }
]


CONFIGURATION_TEMPLATE = {
    "template-name": "test_configuration_template_name",
    "description": "test_configuration_template_description"
}


CONFIGURATION_TEMPLATES = [
    CONFIGURATION_TEMPLATE,
    {
        "template-name": "test_configuration_template_name_0"
    }
]


INSTANCE = {
  "id": "ID_GENERATED_BY_K8SPLUGIN",
  "namespace": "NAMESPACE_WHERE_INSTANCE_HAS_BEEN_DEPLOYED_AS_DERIVED_FROM_PROFILE",
  "release-name": "RELEASE_NAME_AS_COMPUTED_BASED_ON_INSTANTIATION_REQUEST_AND_PROFILE_DEFAULT",
  "request": {
    "rb-name": "test-rbdef",
    "rb-version": "v1",
    "profile-name": "p1",
    "release-name": "release-x",
    "cloud-region": "krd",
    "override-values": {
        "optionalDictOfParameters": "andTheirValues, like",
        "global.name": "dummy-name"
    },
    "labels": {
        "optionalLabelForInternalK8spluginInstancesMetadata": "dummy-value"
    },
  },
  "resources": [
        {
            "GVK": {
                "Group": "",
                "Kind": "ConfigMap",
                "Version": "v1"
            },
            "Name": "test-cm"
        },
        {
            "GVK": {
                "Group": "",
                "Kind": "Service",
                "Version": "v1"
            },
            "Name": "test-svc"
        },
        {
            "GVK": {
                "Group": "apps",
                "Kind": "Deployment",
                "Version": "v1"
            },
            "Name": "test-dep"
        }
  ]
}


INSTANCES = [
    INSTANCE
]


@mock.patch.object(ConnectivityInfo, "send_message_json")
def test_get_connectivity_info_by_region_id(mock_send_message_json):
    mock_send_message_json.return_value = CONNECTIVITY_INFO
    conn_info: ConnectivityInfo = ConnectivityInfo.get_connectivity_info_by_region_id("test_cloud_region_id")
    assert conn_info.cloud_region_id == "test_cloud_region"
    assert conn_info.cloud_owner == "test_cloud_owner"
    assert conn_info.other_connectivity_list == {}
    assert conn_info.kubeconfig == "test_kubeconfig"


@mock.patch.object(ConnectivityInfo, "send_message")
@mock.patch.object(ConnectivityInfo, "send_message_json")
def test_connectivity_info_create_delete(mock_send_message_json, mock_send_message):
    mock_send_message_json.return_value = CONNECTIVITY_INFO
    conn_info: ConnectivityInfo = ConnectivityInfo.create("test_cloud_region", "test_cloud_owner", b"kubeconfig")
    assert conn_info.cloud_region_id == "test_cloud_region"
    assert conn_info.cloud_owner == "test_cloud_owner"
    assert conn_info.other_connectivity_list == {}
    assert conn_info.kubeconfig == "test_kubeconfig"
    conn_info.delete()


@mock.patch.object(Definition, "send_message_json")
def test_definition_get_all(mock_send_message_json):
    mock_send_message_json.return_value = []
    assert len(list(Definition.get_all())) == 0

    mock_send_message_json.return_value = DEFINITIONS
    definitions = list(Definition.get_all())
    assert len(definitions) == 2

    def_0, def_1 = definitions
    assert def_0.rb_name == "test_rb_name_0"
    assert def_0.rb_version == "test_rb_version_0"
    assert def_0.chart_name is None
    assert def_0.description is None
    assert def_0.labels is None

    assert def_1.rb_name == "test_rb_name_1"
    assert def_1.rb_version == "test_rb_version_1"
    assert def_1.chart_name == "test_chart_name_1"
    assert def_1.description == "test_description_1"
    assert def_1.labels == {}


@mock.patch.object(Definition, "send_message_json")
def test_get_definition_by_name_version(mock_send_message_json):
    mock_send_message_json.return_value = DEFINITION
    def_0 = Definition.get_definition_by_name_version("rb_name", "rb_version")
    assert def_0.rb_name == "test_rb_name_0"
    assert def_0.rb_version == "test_rb_version_0"
    assert def_0.chart_name is None
    assert def_0.description is None
    assert def_0.labels is None


@mock.patch.object(Definition, "send_message_json")
@mock.patch.object(Definition, "send_message")
def test_create_definition(mock_send_message, mock_send_message_json):
    mock_send_message_json.return_value = DEFINITION
    def_0 = Definition.create(
        rb_name="test_rb_name_0",
        rb_version="test_rb_version_0"
    )
    assert def_0.rb_name == "test_rb_name_0"
    assert def_0.rb_version == "test_rb_version_0"
    assert def_0.chart_name is None
    assert def_0.description is None
    assert def_0.labels is None


@mock.patch.object(Definition, "send_message_json")
@mock.patch.object(Definition, "send_message")
def test_definition_create_profile(mock_send_message, mock_send_message_json):
    mock_send_message_json.return_value = PROFILE
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    profile = deff.create_profile(
        profile_name="test_profile_name",
        namespace="test_namespace",
        kubernetes_version="test_k8s_version"
    )
    assert profile.rb_name == "test_rb_name"
    assert profile.rb_version == "test_rb_version"
    assert profile.profile_name == "test_profile_name"
    assert profile.namespace == "test_namespace"
    assert profile.kubernetes_version is None
    assert profile.labels == {}
    assert profile.release_name == "test_profile_name"


@mock.patch.object(Definition, "send_message_json")
def test_definition_get_profile_by_name(mock_send_message_json):
    mock_send_message_json.return_value = PROFILE
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    profile = deff.get_profile_by_name("test_profile_name")
    assert profile.rb_name == "test_rb_name"
    assert profile.rb_version == "test_rb_version"
    assert profile.profile_name == "test_profile_name"
    assert profile.namespace == "test_namespace"
    assert profile.kubernetes_version is None
    assert profile.labels == {}
    assert profile.release_name == "test_profile_name"


@mock.patch.object(Definition, "send_message_json")
def test_definition_get_all_profiles(mock_send_message_json):
    mock_send_message_json.return_value = []
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    assert len(list(deff.get_all_profiles())) == 0

    mock_send_message_json.return_value = PROFILES
    profiles = list(deff.get_all_profiles())
    assert len(profiles) == 2
    prof_0, prof_1 = profiles

    assert prof_0.rb_name == "test_rb_name"
    assert prof_0.rb_version == "test_rb_version"
    assert prof_0.profile_name == "test_profile_name"
    assert prof_0.namespace == "test_namespace"
    assert prof_0.kubernetes_version is None
    assert prof_0.labels == {}
    assert prof_0.release_name == "test_profile_name"

    assert prof_1.rb_name == "test_rb_name_1"
    assert prof_1.rb_version == "test_rb_version_1"
    assert prof_1.profile_name == "test_profile_name_1"
    assert prof_1.namespace == "test_namespace_1"
    assert prof_1.kubernetes_version is None
    assert prof_1.labels == {}
    assert prof_1.release_name == "test_profile_name_1"


@mock.patch.object(Definition, "send_message_json")
def test_definition_get_configuration_template_by_name(mock_send_message_json):
    mock_send_message_json.return_value = CONFIGURATION_TEMPLATE
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    configuration_tmpl = deff.get_configuration_template_by_name(
        template_name="test_configuration_template_name"
    )
    assert configuration_tmpl.rb_name == deff.rb_name
    assert configuration_tmpl.rb_version == deff.rb_version
    assert configuration_tmpl.template_name == "test_configuration_template_name"
    assert configuration_tmpl.description == "test_configuration_template_description"


@mock.patch.object(Definition, "send_message_json")
@mock.patch.object(Definition, "send_message")
def test_definition_create_configuration_template(mock_send_message, mock_send_message_json):
    mock_send_message_json.return_value = CONFIGURATION_TEMPLATE
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    configuration_tmpl = deff.create_configuration_template(
        template_name="test_configuration_template_name",
        description="test_configuration_template_description"
    )
    assert configuration_tmpl.rb_name == deff.rb_name
    assert configuration_tmpl.rb_version == deff.rb_version
    assert configuration_tmpl.template_name == "test_configuration_template_name"
    assert configuration_tmpl.description == "test_configuration_template_description"
    assert configuration_tmpl.url == f"{deff.base_url}/{deff.rb_name}/{deff.rb_version}/config-template/test_configuration_template_name"


@mock.patch.object(Definition, "send_message_json")
def test_definition_get_all_configuration_templates(mock_send_message_json):
    mock_send_message_json.return_value = []
    deff = Definition(
        rb_name="test_rb_name",
        rb_version="test_rb_version",
        chart_name="test_chart_name",
        description="test_description",
        labels={}
    )
    assert len(list(deff.get_all_configuration_templates())) == 0

    mock_send_message_json.return_value = CONFIGURATION_TEMPLATES
    configuration_tmplts = list(deff.get_all_configuration_templates())
    assert len(configuration_tmplts) == 2

    tmpl_0, tmpl_1 = configuration_tmplts
    assert tmpl_0.rb_name == deff.rb_name
    assert tmpl_0.rb_version == deff.rb_version
    assert tmpl_0.template_name == "test_configuration_template_name"
    assert tmpl_0.description == "test_configuration_template_description"

    assert tmpl_1.rb_name == deff.rb_name
    assert tmpl_1.rb_version == deff.rb_version
    assert tmpl_1.template_name == "test_configuration_template_name_0"
    assert tmpl_1.description is None


@mock.patch.object(Instance, "send_message_json")
def test_instance_get_all(mock_send_message_json):
    mock_send_message_json.return_value = []
    assert len(list(Instance.get_all())) == 0

    mock_send_message_json.return_value = INSTANCES
    assert len(list(Instance.get_all())) == 1


@mock.patch.object(Instance, "send_message_json")
def test_instance_create(mock_send_message_json):
    mock_send_message_json.return_value = INSTANCE
    instance = Instance.create(
        "test_cloud_region_id",
        "test_profile_name",
        "test_rb_name",
        "test_rb_version"
    )
    assert instance.instance_id == "ID_GENERATED_BY_K8SPLUGIN"
    assert instance.namespace == "NAMESPACE_WHERE_INSTANCE_HAS_BEEN_DEPLOYED_AS_DERIVED_FROM_PROFILE"


@mock.patch.object(Instance, "send_message_json")
@mock.patch.object(Instance, "send_message")
def test_instance_get_by_id(mock_send_message, mock_send_message_json):
    mock_send_message_json.return_value = INSTANCE
    instance = Instance.get_by_id("ID_GENERATED_BY_K8SPLUGIN")
    assert instance.instance_id == "ID_GENERATED_BY_K8SPLUGIN"
    assert instance.namespace == "NAMESPACE_WHERE_INSTANCE_HAS_BEEN_DEPLOYED_AS_DERIVED_FROM_PROFILE"
    instance.delete()