azimuth-robotframework 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,386 @@
1
+ import contextlib
2
+ import dataclasses
3
+ import enum
4
+ import os
5
+ import subprocess
6
+ import tempfile
7
+ import typing as t
8
+
9
+ from robot.api import logger
10
+ from robot.api.deco import keyword
11
+
12
+ from . import util
13
+
14
+
15
+ @dataclasses.dataclass(frozen = True)
16
+ class NodeGroupConfig:
17
+ """
18
+ Type for a node group config.
19
+ """
20
+ name: str
21
+ machine_size: str
22
+ autoscale: bool = False
23
+ count: t.Optional[int] = None
24
+ min_count: t.Optional[int] = None
25
+ max_count: t.Optional[int] = None
26
+
27
+ def __post_init__(self):
28
+ if self.autoscale:
29
+ assert self.min_count is not None and self.max_count is not None, \
30
+ "min_count and max_count are required for autoscaling groups"
31
+ else:
32
+ assert self.count is not None, \
33
+ "count is required for non-autoscaling groups"
34
+
35
+
36
+ @dataclasses.dataclass(frozen = True)
37
+ class KubernetesClusterConfig:
38
+ """
39
+ Type for a Kubernetes cluster config.
40
+ """
41
+ name: str
42
+ template: str
43
+ control_plane_size: str
44
+ node_groups: t.List[NodeGroupConfig] = dataclasses.field(default_factory = list)
45
+ autohealing_enabled: bool = True
46
+ dashboard_enabled: bool = False
47
+ ingress_enabled: bool = False
48
+ ingress_controller_load_balancer_ip: t.Optional[str] = None
49
+ monitoring_enabled: bool = False
50
+
51
+
52
+ @enum.unique
53
+ class SonobuoyMode(enum.Enum):
54
+ """
55
+ Enumeration of Sonobuoy modes.
56
+ """
57
+ CERTIFIED_CONFORMANCE = "certified-conformance"
58
+ CONFORMANCE_LITE = "conformance-lite"
59
+ NON_DISRUPTIVE_CONFORMANCE = "non-disruptive-conformance"
60
+ QUICK = "quick"
61
+
62
+
63
+ class KubernetesClusterKeywords:
64
+ """
65
+ Keywords for interacting with Kubernetes clusters.
66
+ """
67
+ def __init__(self, ctx):
68
+ self._ctx = ctx
69
+
70
+ @property
71
+ def _resource(self):
72
+ return self._ctx.client.kubernetes_clusters()
73
+
74
+ @keyword
75
+ def list_kubernetes_clusters(self) -> t.List[t.Dict[str, t.Any]]:
76
+ """
77
+ Lists Kubernetes clusters using the active client.
78
+ """
79
+ return list(self._resource.list())
80
+
81
+ @keyword
82
+ def fetch_kubernetes_cluster_by_id(self, id: str) -> t.Dict[str, t.Any]:
83
+ """
84
+ Fetches a Kubernetes cluster by id using the active client.
85
+ """
86
+ return self._resource.fetch(id)
87
+
88
+ @keyword
89
+ def find_kubernetes_cluster_by_name(self, name: str) -> t.Dict[str, t.Any]:
90
+ """
91
+ Finds a Kubernetes cluster by name using the active client.
92
+ """
93
+ try:
94
+ return next(c for c in self._resource.list() if c.name == name)
95
+ except StopIteration:
96
+ raise ValueError(f"no Kubernetes cluster with name '{name}'")
97
+
98
+ @keyword
99
+ def new_kubernetes_config(
100
+ self,
101
+ *,
102
+ name: str,
103
+ template: str,
104
+ control_plane_size: str
105
+ ) -> KubernetesClusterConfig:
106
+ """
107
+ Initialises a new Kubernetes config.
108
+ """
109
+ return KubernetesClusterConfig(name, template, control_plane_size)
110
+
111
+ @keyword
112
+ def change_template_for_kubernetes_config(
113
+ self,
114
+ config: KubernetesClusterConfig,
115
+ *,
116
+ template: str
117
+ ) -> KubernetesClusterConfig:
118
+ """
119
+ Changes the template for the current Kubernetes config.
120
+ """
121
+ return dataclasses.replace(config, template = template)
122
+
123
+ @keyword
124
+ def add_node_group_to_kubernetes_config(
125
+ self,
126
+ config: KubernetesClusterConfig,
127
+ *,
128
+ name: str,
129
+ machine_size: str,
130
+ autoscale: bool = False,
131
+ count: t.Optional[int] = None,
132
+ min_count: t.Optional[int] = None,
133
+ max_count: t.Optional[int] = None
134
+ ) -> KubernetesClusterConfig:
135
+ """
136
+ Adds a node group to the current Kubernetes config.
137
+ """
138
+ return dataclasses.replace(
139
+ config,
140
+ node_groups = [
141
+ *config.node_groups,
142
+ NodeGroupConfig(name, machine_size, autoscale, count, min_count, max_count),
143
+ ]
144
+ )
145
+
146
+ @keyword
147
+ def enable_autohealing_for_kubernetes_config(
148
+ self,
149
+ config: KubernetesClusterConfig
150
+ ) -> KubernetesClusterConfig:
151
+ """
152
+ Enables auto-healing for the current Kubernetes config.
153
+ """
154
+ return dataclasses.replace(config, autohealing_enabled = True)
155
+
156
+ @keyword
157
+ def disable_autohealing_for_kubernetes_config(
158
+ self,
159
+ config: KubernetesClusterConfig
160
+ ) -> KubernetesClusterConfig:
161
+ """
162
+ Disables auto-healing for the current Kubernetes config.
163
+ """
164
+ return dataclasses.replace(config, autohealing_enabled = False)
165
+
166
+ @keyword
167
+ def enable_dashboard_for_kubernetes_config(
168
+ self,
169
+ config: KubernetesClusterConfig
170
+ ) -> KubernetesClusterConfig:
171
+ """
172
+ Enables the Kubernetes dashboard for the current Kubernetes config.
173
+ """
174
+ return dataclasses.replace(config, dashboard_enabled = True)
175
+
176
+ @keyword
177
+ def disable_dashboard_for_kubernetes_config(
178
+ self,
179
+ config: KubernetesClusterConfig
180
+ ) -> KubernetesClusterConfig:
181
+ """
182
+ Disables the Kubernetes dashboard for the current Kubernetes config.
183
+ """
184
+ return dataclasses.replace(config, dashboard_enabled = False)
185
+
186
+ @keyword
187
+ def enable_ingress_for_kubernetes_config(
188
+ self,
189
+ config: KubernetesClusterConfig,
190
+ ip: str
191
+ ) -> KubernetesClusterConfig:
192
+ """
193
+ Enables ingress for the current Kubernetes config.
194
+ """
195
+ return dataclasses.replace(
196
+ config,
197
+ ingress_enabled = True,
198
+ ingress_controller_load_balancer_ip = ip
199
+ )
200
+
201
+ @keyword
202
+ def disable_ingress_for_kubernetes_config(
203
+ self,
204
+ config: KubernetesClusterConfig
205
+ ) -> KubernetesClusterConfig:
206
+ """
207
+ Disables ingress for the current Kubernetes config.
208
+ """
209
+ return dataclasses.replace(
210
+ config,
211
+ ingress_enabled = False,
212
+ ingress_controller_load_balancer_ip = None
213
+ )
214
+
215
+ @keyword
216
+ def enable_monitoring_for_kubernetes_config(
217
+ self,
218
+ config: KubernetesClusterConfig
219
+ ) -> KubernetesClusterConfig:
220
+ """
221
+ Enables monitoring for the current Kubernetes config.
222
+ """
223
+ return dataclasses.replace(config, monitoring_enabled = True)
224
+
225
+ @keyword
226
+ def disable_monitoring_for_kubernetes_config(
227
+ self,
228
+ config: KubernetesClusterConfig
229
+ ) -> KubernetesClusterConfig:
230
+ """
231
+ Disables monitoring for the current Kubernetes config.
232
+ """
233
+ return dataclasses.replace(config, monitoring_enabled = False)
234
+
235
+ @keyword
236
+ def create_kubernetes_cluster(self, config: KubernetesClusterConfig) -> t.Dict[str, t.Any]:
237
+ """
238
+ Creates a Kubernetes cluster using the active client.
239
+ """
240
+ return self._resource.create(dataclasses.asdict(config))
241
+
242
+ @keyword
243
+ def delete_kubernetes_cluster(self, id: str, interval: int = 15):
244
+ """
245
+ Deletes the specified Kubernetes cluster and waits for it to be deleted.
246
+ """
247
+ util.delete_resource(self._resource, id, interval)
248
+
249
+ @keyword
250
+ def get_kubeconfig_for_kubernetes_cluster(self, id: str) -> str:
251
+ """
252
+ Returns the kubeconfig for the specified Kubernetes cluster.
253
+ """
254
+ data = self._resource.action(id, "kubeconfig")
255
+ return data["kubeconfig"]
256
+
257
+ @keyword
258
+ def wait_for_kubernetes_cluster_status(
259
+ self,
260
+ id: str,
261
+ target_status: str,
262
+ interval: int = 15
263
+ ) -> t.Dict[str, t.Any]:
264
+ """
265
+ Waits for the specified cluster to reach the target status before returning it.
266
+ """
267
+ return util.wait_for_resource_property(
268
+ self._resource,
269
+ id,
270
+ "status",
271
+ target_status,
272
+ {"Reconciling", "Upgrading", "Deleting", "Unhealthy", "Unknown"},
273
+ interval
274
+ )
275
+
276
+ @keyword
277
+ def wait_for_kubernetes_cluster_ready(self, id: str, interval: int = 15) -> t.Dict[str, t.Any]:
278
+ """
279
+ Waits for the cluster status to be ready before returning it.
280
+ """
281
+ return self.wait_for_kubernetes_cluster_status(id, "Ready", interval)
282
+
283
+ @keyword
284
+ def get_kubernetes_cluster_service_url(self, cluster: t.Dict[str, t.Any], name: str) -> str:
285
+ """
286
+ Returns the Zenith FQDN for the specified cluster service.
287
+ """
288
+ # Allow some shortcut names
289
+ names = {name}
290
+ if name in {"dashboard", "kubernetes-dashboard"}:
291
+ names.add("kubernetes-dashboard-client")
292
+ if name == "monitoring":
293
+ names.add("monitoring-system-kube-prometheus-stack-client")
294
+ try:
295
+ return next(
296
+ service["fqdn"]
297
+ for service in cluster["services"]
298
+ if service["name"] in names
299
+ )
300
+ except StopIteration:
301
+ raise ValueError(f"no such service - {name}")
302
+
303
+ @contextlib.contextmanager
304
+ def _kubeconfig_for_cluster(self, id: str):
305
+ kubeconfig = self.get_kubeconfig_for_kubernetes_cluster(id)
306
+ with tempfile.NamedTemporaryFile("w") as file:
307
+ file.write(kubeconfig)
308
+ file.flush()
309
+ yield file.name
310
+
311
+ def _run_sonobuoy_cmd(self, executable, cmd, *args):
312
+ proc = subprocess.run(
313
+ [executable, cmd, *args],
314
+ capture_output = True
315
+ )
316
+ if proc.returncode != 0:
317
+ logger.info(f"sonobuoy {cmd} command failed")
318
+ logger.info(proc.stderr)
319
+ return proc
320
+
321
+ @keyword
322
+ def run_sonobuoy_for_kubernetes_cluster(
323
+ self,
324
+ id: str,
325
+ *,
326
+ executable = "sonobuoy",
327
+ mode: SonobuoyMode = SonobuoyMode.QUICK,
328
+ run_extra_args: t.Optional[t.List[str]] = None,
329
+ results_extra_args: t.Optional[t.List[str]] = None,
330
+ delete_extra_args: t.Optional[t.List[str]] = None
331
+ ):
332
+ """
333
+ Runs Sonobuoy conformance tests for the specified cluster.
334
+ """
335
+ with self._kubeconfig_for_cluster(id) as kubeconfig:
336
+ run_proc = self._run_sonobuoy_cmd(
337
+ executable,
338
+ "run",
339
+ "--kubeconfig",
340
+ kubeconfig,
341
+ "--wait",
342
+ "--mode",
343
+ mode.value,
344
+ *(run_extra_args or [])
345
+ )
346
+ # Even if the run failed, we still want to run the retrieve
347
+ # The retrieve command has a known failure mode that is solved by retrying
348
+ while True:
349
+ retrieve_proc = self._run_sonobuoy_cmd(
350
+ executable,
351
+ "retrieve",
352
+ "--kubeconfig",
353
+ kubeconfig,
354
+ )
355
+ if (
356
+ retrieve_proc.returncode == 0 or
357
+ "unexpected EOF" not in retrieve_proc.stderr.decode()
358
+ ):
359
+ break
360
+ # Even if the run and/or retrieve failed, we delete the run
361
+ delete_proc = self._run_sonobuoy_cmd(
362
+ executable,
363
+ "delete",
364
+ "--kubeconfig",
365
+ kubeconfig,
366
+ "--wait",
367
+ *(delete_extra_args or [])
368
+ )
369
+
370
+ # If anything failed so far, we are done
371
+ assert run_proc.returncode == 0, "sonobuoy run command failed"
372
+ assert retrieve_proc.returncode == 0, "sonobuoy retrieve command failed"
373
+ assert delete_proc.returncode == 0, "sonobuoy delete command failed"
374
+
375
+ # Process and return the results
376
+ results_file = retrieve_proc.stdout.strip()
377
+ results_proc = self._run_sonobuoy_cmd(
378
+ executable,
379
+ "results",
380
+ results_file,
381
+ *(results_extra_args or [])
382
+ )
383
+ os.remove(results_file)
384
+ assert results_proc.returncode == 0, "sonobuoy results command failed"
385
+ logger.info(results_proc.stdout)
386
+ return results_proc.stdout
Azimuth/main.py ADDED
@@ -0,0 +1,140 @@
1
+ import secrets
2
+ import string
3
+ import typing as t
4
+
5
+ from robotlibcore import DynamicCore, keyword
6
+
7
+ from azimuth_sdk import Configuration, SyncClient
8
+
9
+ from .cluster_types import ClusterTypeKeywords
10
+ from .clusters import ClusterKeywords
11
+ from .kubernetes_app_templates import KubernetesAppTemplateKeywords
12
+ from .kubernetes_apps import KubernetesAppKeywords
13
+ from .kubernetes_cluster_templates import KubernetesClusterTemplateKeywords
14
+ from .kubernetes_clusters import KubernetesClusterKeywords
15
+ from .sizes import SizeKeywords
16
+ from .zenith import ZenithKeywords
17
+
18
+
19
+ class Azimuth(DynamicCore):
20
+ """
21
+ Robot Framework library for testing Azimuth.
22
+ """
23
+ # This ensures that one instance of the library is shared everywhere
24
+ # This is important to get the client sharing working correctly
25
+ ROBOT_LIBRARY_SCOPE = "GLOBAL"
26
+
27
+ def __init__(self):
28
+ # The Azimuth client instance
29
+ self.client: SyncClient = None
30
+ super().__init__([
31
+ ClusterTypeKeywords(self),
32
+ ClusterKeywords(self),
33
+ KubernetesAppTemplateKeywords(self),
34
+ KubernetesAppKeywords(self),
35
+ KubernetesClusterTemplateKeywords(self),
36
+ KubernetesClusterKeywords(self),
37
+ SizeKeywords(self),
38
+ ZenithKeywords(self),
39
+ ])
40
+
41
+ def _create_client(self, config):
42
+ # Clean up any old clients first
43
+ if self.client:
44
+ self.client.__exit__()
45
+ self.client = config.sync_client()
46
+ self.client.__enter__()
47
+
48
+ @keyword
49
+ def create_client(
50
+ self,
51
+ base_url: str,
52
+ *,
53
+ auth_data: t.Dict[str, t.Any],
54
+ authenticator: t.Optional[str] = None,
55
+ authenticator_type: t.Optional[str] = None,
56
+ default_tenancy_id: t.Optional[str] = None,
57
+ **kwargs
58
+ ):
59
+ """
60
+ Creates an Azimuth SDK client using the given authentication.
61
+ """
62
+ self._create_client(
63
+ Configuration.create(
64
+ base_url,
65
+ auth_data = auth_data,
66
+ authenticator = authenticator,
67
+ authenticator_type = authenticator_type,
68
+ default_tenancy_id = default_tenancy_id,
69
+ **kwargs
70
+ )
71
+ )
72
+
73
+ @keyword
74
+ def create_client_from_openstack_clouds_file(
75
+ self,
76
+ base_url: str,
77
+ path: str,
78
+ cloud: str = "openstack",
79
+ *,
80
+ default_tenancy_id: t.Optional[str] = None,
81
+ **kwargs
82
+ ):
83
+ """
84
+ Creates an Azimuth SDK client using the specified OpenStack clouds.yaml file.
85
+ """
86
+ self._create_client(
87
+ Configuration.from_openstack_clouds_file(
88
+ base_url,
89
+ path,
90
+ cloud = cloud,
91
+ default_tenancy_id = default_tenancy_id,
92
+ **kwargs
93
+ )
94
+ )
95
+
96
+ @keyword
97
+ def create_client_from_environment(
98
+ self,
99
+ base_url: str,
100
+ *,
101
+ default_tenancy_id: t.Optional[str] = None,
102
+ **kwargs
103
+ ):
104
+ """
105
+ Creates an Azimuth SDK client from the environment variables.
106
+ """
107
+ self._create_client(
108
+ Configuration.from_environment(
109
+ base_url,
110
+ default_tenancy_id = default_tenancy_id,
111
+ **kwargs
112
+ )
113
+ )
114
+
115
+ @keyword
116
+ def switch_tenancy(self, tenancy_id: str):
117
+ """
118
+ Switches the current client to a different tenancy.
119
+ """
120
+ assert self.client is not None
121
+ self.client.switch_tenancy(tenancy_id)
122
+
123
+ @keyword
124
+ def close_client(self):
125
+ """
126
+ Closes the active client.
127
+ """
128
+ if self.client:
129
+ self.client.__exit__()
130
+ self.client = None
131
+
132
+ @keyword
133
+ def generate_name(self, prefix: str, suffix_length = 5, suffix_chars = None) -> str:
134
+ """
135
+ Given a prefix, generates a name with a random suffix.
136
+ """
137
+ suffix_chars = suffix_chars or (string.ascii_lowercase + string.digits)
138
+ suffix = "".join(secrets.choice(suffix_chars) for _ in range(suffix_length))
139
+ return f"{prefix}-{suffix}"
140
+
Azimuth/sizes.py ADDED
@@ -0,0 +1,39 @@
1
+ import typing as t
2
+
3
+ from robot.api.deco import keyword
4
+
5
+
6
+ class SizeKeywords:
7
+ """
8
+ Keywords for interacting with sizes.
9
+ """
10
+ def __init__(self, ctx):
11
+ self._ctx = ctx
12
+
13
+ @property
14
+ def _resource(self):
15
+ return self._ctx.client.sizes()
16
+
17
+ @keyword
18
+ def list_sizes(self) -> t.List[t.Dict[str, t.Any]]:
19
+ """
20
+ Lists available sizes using the active client.
21
+ """
22
+ return list(self._resource.list())
23
+
24
+ @keyword
25
+ def fetch_size(self, id: str) -> t.Dict[str, t.Any]:
26
+ """
27
+ Fetches a size by id using the active client.
28
+ """
29
+ return self._resource.fetch(id)
30
+
31
+ @keyword
32
+ def find_size_by_name(self, name: str) -> t.Dict[str, t.Any]:
33
+ """
34
+ Finds a size by name using the active client.
35
+ """
36
+ try:
37
+ return next(c for c in self._resource.list() if c.name == name)
38
+ except StopIteration:
39
+ raise ValueError(f"no size with name '{name}'")
Azimuth/util.py ADDED
@@ -0,0 +1,70 @@
1
+ import time
2
+ import typing as t
3
+
4
+ import azimuth_sdk
5
+
6
+
7
+ def wait_for_resource(
8
+ resource,
9
+ id: str,
10
+ predicate: t.Callable[[t.Dict[str, t.Any]], bool],
11
+ interval: int
12
+ ) -> t.Dict[str, t.Any]:
13
+ """
14
+ Waits for the specified predicate to become True for the instance with the given ID.
15
+ """
16
+ while True:
17
+ instance = resource.fetch(id)
18
+ if predicate(instance):
19
+ return instance
20
+ else:
21
+ time.sleep(interval)
22
+
23
+
24
+ def wait_for_resource_property(
25
+ resource,
26
+ id: str,
27
+ property: str,
28
+ target_value: t.Any,
29
+ working_values: t.Collection[t.Any],
30
+ interval: int
31
+ ) -> t.Dict[str, t.Any]:
32
+ """
33
+ Waits for the specified property on the instance with the given ID to reach a target
34
+ value. It will only continue while the property is in the working values.
35
+ """
36
+ def predicate(instance):
37
+ property_value = getattr(instance, property)
38
+ if property_value == target_value:
39
+ return True
40
+ elif property_value in working_values:
41
+ return False
42
+ else:
43
+ raise AssertionError(f"unexpected {property} - {property_value}")
44
+ return wait_for_resource(resource, id, predicate, interval)
45
+
46
+
47
+ def delete_resource(resource, id: str, interval: int):
48
+ """
49
+ Deletes the instance with the specified ID and waits for it to be deleted.
50
+ """
51
+ # Keep trying to delete the resource until we don't get a 409
52
+ # This allows it to be used as a teardown
53
+ while True:
54
+ try:
55
+ resource.delete(id)
56
+ except azimuth_sdk.APIError as exc:
57
+ if exc.status_code != 409:
58
+ raise
59
+ else:
60
+ break
61
+ time.sleep(interval)
62
+ while True:
63
+ try:
64
+ _ = resource.fetch(id)
65
+ except azimuth_sdk.APIError as exc:
66
+ if exc.status_code == 404:
67
+ return
68
+ else:
69
+ raise
70
+ time.sleep(interval)