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
|
# http://www.apache.org/licenses/LICENSE-2.0
"""CPS onboard module."""
import base64
import ssl
from abc import ABC
import pg8000
from kubernetes import client, config
from onapsdk.configuration import settings
from onapsdk.cps import Anchor, Dataspace, SchemaSet
from onaptests.utils.exceptions import (EnvironmentPreparationException,
OnapTestException)
from ..base import BaseStep
class CpsBaseStep(BaseStep, ABC):
"""Abstract CPS base step."""
@property
def component(self) -> str:
"""Component name."""
return "CPS"
class CreateCpsDataspaceStep(CpsBaseStep):
"""Step to create a dataspace."""
def __init__(self) -> None:
"""Initialize step."""
super().__init__(cleanup=settings.CLEANUP_FLAG)
@property
def description(self) -> str:
"""Step description."""
return "Create CPS dataspace."
@BaseStep.store_state
def execute(self) -> None:
"""Create a dataspace.
Use settings values:
- DATASPACE_NAME.
"""
super().execute()
Dataspace.create(settings.DATASPACE_NAME)
@BaseStep.store_state(cleanup=True)
def cleanup(self) -> None:
"""Delete created dataspace."""
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
dataspace.delete()
super().cleanup()
class CreateCpsSchemaSetStep(CpsBaseStep):
"""Step to check schema-set creation."""
def __init__(self) -> None:
"""Initialize step.
Substeps:
- CreateCpsDataspaceStep.
"""
super().__init__(cleanup=settings.CLEANUP_FLAG)
self.add_step(CreateCpsDataspaceStep())
@property
def description(self) -> str:
"""Step description."""
return "Create CPS bookstore schema set"
@BaseStep.store_state
def execute(self) -> None:
"""Get dataspace created on substep and create schema-set.
Use settings values:
- DATASPACE_NAME,
- SCHEMA_SET_NAME.
"""
super().execute()
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
with settings.SCHEMA_SET_FILE.open("rb") as schema_set_file:
dataspace.create_schema_set(settings.SCHEMA_SET_NAME, schema_set_file)
@BaseStep.store_state(cleanup=True)
def cleanup(self) -> None:
"""Delete created schema-set."""
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
schema_set = dataspace.get_schema_set(settings.SCHEMA_SET_NAME)
schema_set.delete()
super().cleanup()
class CreateCpsAnchorStep(CpsBaseStep):
"""Step to create an anchor."""
def __init__(self) -> None:
"""Initialize step.
Substeps:
- CreateCpsSchemaSetStep.
"""
super().__init__(cleanup=settings.CLEANUP_FLAG)
self.add_step(CreateCpsSchemaSetStep())
@property
def description(self) -> str:
"""Step description."""
return "Create CPS anchor"
@BaseStep.store_state
def execute(self) -> None:
"""Create anchor.
Get dataspace and schema-set created substeps and create anchor.
Use settings values:
- DATASPACE_NAME,
- SCHEMA_SET_NAME,
- ANCHOR_DATA.
"""
super().execute()
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
schema_set: SchemaSet = dataspace.get_schema_set(settings.SCHEMA_SET_NAME)
dataspace.create_anchor(schema_set, settings.ANCHOR_NAME)
@BaseStep.store_state(cleanup=True)
def cleanup(self) -> None:
"""Delete an anchor."""
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
anchor: Anchor = dataspace.get_anchor(settings.ANCHOR_NAME)
anchor.delete()
super().cleanup()
class CreateCpsAnchorNodeStep(CpsBaseStep):
"""Step to create anchor node."""
def __init__(self) -> None:
"""Initialize step.
Substeps:
- CreateCpsAnchorStep.
"""
super().__init__(cleanup=settings.CLEANUP_FLAG)
self.add_step(CreateCpsAnchorStep())
@property
def description(self) -> str:
"""Step description."""
return "Create CPS anchor node"
@BaseStep.store_state
def execute(self) -> None:
"""Create a node on an anchor created on substep.
Use settings values:
- DATASPACE_NAME,
- ANCHOR_NAME,
- ANCHOR_DATA.
"""
super().execute()
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
anchor: Anchor = dataspace.get_anchor(settings.ANCHOR_NAME)
anchor.create_node(settings.ANCHOR_DATA)
@BaseStep.store_state(cleanup=True)
def cleanup(self) -> None:
"""Delete nodes."""
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
anchor: Anchor = dataspace.get_anchor(settings.ANCHOR_NAME)
anchor.delete_nodes("/")
super().cleanup()
class UpdateCpsAnchorNodeStep(CpsBaseStep):
"""Step to update node on anchor creation."""
def __init__(self) -> None:
"""Initialize step.
Substeps:
- CreateCpsAnchorNodeStep.
"""
super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
self.add_step(CreateCpsAnchorNodeStep())
@property
def description(self) -> str:
"""Step description."""
return "Update CPS anchor node"
@BaseStep.store_state
def execute(self) -> None:
"""Update a node on an anchor created on substep.
Use settings values:
- DATASPACE_NAME,
- ANCHOR_NAME,
- ANCHOR_DATA_2.
"""
super().execute()
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
anchor: Anchor = dataspace.get_anchor(settings.ANCHOR_NAME)
anchor.update_node("/", settings.ANCHOR_DATA_2)
class QueryCpsAnchorNodeStep(CpsBaseStep):
"""Step to check query on node."""
def __init__(self) -> None:
"""Initialize step.
Substeps:
- UpdateCpsAnchorNodeStep.
"""
super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
self.add_step(UpdateCpsAnchorNodeStep())
@property
def description(self) -> str:
"""Step description."""
return "Query node"
@BaseStep.store_state
def execute(self) -> None:
"""Query on node on an anchor created on substep.
Use settings values:
- DATASPACE_NAME,
- ANCHOR_NAME,
- QUERY_1,
- QUERY_2,
- QUERY_3.
"""
super().execute()
dataspace: Dataspace = Dataspace(settings.DATASPACE_NAME)
anchor: Anchor = dataspace.get_anchor(settings.ANCHOR_NAME)
anchor.query_node(settings.QUERY_1)
anchor.query_node(settings.QUERY_2)
anchor.query_node(settings.QUERY_3)
class CheckPostgressDataBaseConnectionStep(CpsBaseStep):
"""Step to test connection with postgress."""
def __init__(self) -> None:
"""Initialize step."""
super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
self.login = None
self.password = None
@property
def description(self) -> str:
"""Step description."""
return "Establish connection with Postgress and execute the query"
def get_database_credentials(self):
"""Resolve CPS datbase credentials from k8s secret."""
if settings.IN_CLUSTER:
config.load_incluster_config()
else:
config.load_kube_config(config_file=settings.K8S_CONFIG)
api_instance = client.CoreV1Api()
try:
secret = api_instance.read_namespaced_secret(
settings.SECRET_NAME, settings.K8S_TESTS_NAMESPACE)
if secret.data:
if settings.DB_LOGIN in secret.data and settings.DB_PASSWORD in secret.data:
login_base64 = secret.data[settings.DB_LOGIN]
self.login = base64.b64decode(login_base64).decode("utf-8")
password_base64 = secret.data[settings.DB_PASSWORD]
self.password = base64.b64decode(password_base64).decode("utf-8")
else:
raise EnvironmentPreparationException(
"Login key or password key not found in secret")
else:
raise EnvironmentPreparationException("Secret data not found in secret")
except client.rest.ApiException as e:
self.login = None
self.password = None
raise EnvironmentPreparationException("Error accessing secret") from e
def connect_to_postgress(self):
"""Connect to CPS database and execute select query."""
self.get_database_credentials()
if self.login and self.password:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
db_params = {
"user": self.login,
"password": self.password,
"host": settings.DB_PRIMARY_HOST,
"database": settings.DATABASE,
"port": settings.DB_PORT,
"ssl_context": ctx
}
try:
connection = pg8000.connect(**db_params)
cursor = connection.cursor()
select_query = "SELECT * FROM yang_resource LIMIT 1;"
cursor.execute(select_query)
if cursor:
cursor.close()
if connection:
connection.close()
except Exception as e:
self._logger.exception(f"Error while connecting to PostgreSQL: {str(e)}")
raise OnapTestException(e) from e
@BaseStep.store_state
def execute(self) -> None:
"""Establish connection with Postgress and execute the query.
Use settings values:
- DB_PRIMARY_HOST,
- DATABASE,
- DB_PORT,
- K8S_TESTS_NAMESPACE,
- SECRET_NAME,
- DB_LOGIN,
- DB_PASSWORD.
"""
super().execute()
self.connect_to_postgress()
|