-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_service.py
113 lines (88 loc) · 4.58 KB
/
test_service.py
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
# 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.
import pytest
from adcm_aio_client import Filter
from adcm_aio_client.errors import ObjectDoesNotExistError
from adcm_aio_client.objects import Cluster, Component, Host, Service
pytestmark = [pytest.mark.asyncio]
async def test_service(example_cluster: Cluster, three_hosts: list[Host]) -> None:
"""
Service (`cluster.services`) API Examples:
- adding a service to cluster
- with accepting license
- with dependencies
- retrieval with filtering / all cluster's services
- iteration through all cluster's services
- service removal
- turning on maintenance mode
- refreshing service's data
Component (`service.components`) API Examples:
- retrieval with filtering / all service's components
- retrieval hosts, mapped to component with filtering / all mapped hosts
- turning on maintenance mode
- refreshing component's data
"""
cluster = example_cluster
host_1, host_2, host_3 = sorted(three_hosts, key=lambda host: host.name)
# Add services "example_1" and "example_2" using Filter
await cluster.services.add(filter_=Filter(attr="name", op="iin", value=["Example_1", "example_2"]))
# Add service with all it's dependencies
# Services "service_with_requires_my_service" and "my_service" will be added
await cluster.services.add(
filter_=Filter(attr="name", op="eq", value="service_with_requires_my_service"), with_dependencies=True
)
# Add service and accept license
await cluster.services.add(filter_=Filter(attr="name", op="eq", value="with_license"), accept_license=True)
# Get all cluster's services
all_added_services: list[Service] = await cluster.services.all() # noqa: F841
# iterate through all cluster's services
async for service in cluster.services.iter(): # noqa: B007
pass
# Remove services from cluster. Leave single "example_1" service
for service in await cluster.services.filter(
name__in=["example_2", "service_with_requires_my_service", "my_service", "with_license"]
):
await service.delete()
# Get non-existent (already removed) service "example_2"
none_service: None = await cluster.services.get_or_none(name__eq="example_2") # pyright: ignore[reportAssignmentType] # noqa: F841
# Get "example_1" service using case-insensitive filter by display_name
service = await cluster.services.get(display_name__ieq="first example") # actual display_name is "First Example"
# Turn on service's maintenance mode
service_mm = await service.maintenance_mode
await service_mm.on()
# Sync service's data with remote state
await service.refresh()
# Get all service's components
all_components: list[Component] = await service.components.all() # noqa: F841
# Get specific component
component = await service.components.get(name__eq="first")
# Prepare data: add 3 hosts to cluster, map two of them to component
await cluster.hosts.add(host=[host_1, host_2, host_3])
mapping = await cluster.mapping
await mapping.add(component=component, host=[host_1, host_2])
await mapping.save()
# Get all component's hosts
component_hosts: list[Host] = await component.hosts.all() # noqa: F841
# Iterate through all service's components
async for comp in service.components.iter(): # noqa: B007
pass
# Get all hosts, mapped to component
all_component_hosts: list[Host] = await component.hosts.all() # noqa: F841
# Get specific host, mapped to this component
host_2_from_component = await component.hosts.get(name__eq=host_2.name) # noqa: F841
# Trying to get host_3 from component, get error: host_3 is not mapped to component
with pytest.raises(ObjectDoesNotExistError, match="No objects found with the given filter."):
host_3_from_component = await component.hosts.get(name__eq=host_3.name) # noqa: F841
# Turn on maintenance mode on component
await (await component.maintenance_mode).on()
# Sync component's data with remote state
await component.refresh()