Newer
Older
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
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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 copy, grpc, logging, pytest
from common.tests.PytestGenerateTests import pytest_generate_tests # (required) pylint: disable=unused-import
from common.tools.grpc.Tools import grpc_message_to_json_string
from context.client.ContextClient import ContextClient
from context.proto.context_pb2 import Context, ContextId, DeviceId, Link, LinkId, Topology, Device, TopologyId
from device.client.DeviceClient import DeviceClient
from service.client.ServiceClient import ServiceClient
from service.proto.context_pb2 import Service, ServiceId
from .PrepareTestScenario import ( # pylint: disable=unused-import
# be careful, order of symbols is important here!
mock_service, service_service, context_client, device_client, service_client)
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
try:
from .ServiceHandlersToTest import SERVICE_HANDLERS_TO_TEST
except ImportError:
LOGGER.exception('Unable to load service handlers, nothing will be tested.')
SERVICE_HANDLERS_TO_TEST = []
class TestServiceHandlers:
scenarios = SERVICE_HANDLERS_TO_TEST
def test_prepare_environment(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
for context in contexts: context_client.SetContext(Context(**context))
for topology in topologies: context_client.SetTopology(Topology(**topology))
for device in devices: device_client.AddDevice(Device(**device))
for link in links: context_client.SetLink(Link(**link))
def test_service_create_error_cases(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
with pytest.raises(grpc.RpcError) as e:
service_with_endpoints = copy.deepcopy(service_descriptor)
service_with_endpoints['service_endpoint_ids'].extend(service_endpoint_ids)
service_client.CreateService(Service(**service_with_endpoints))
assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
msg_head = 'service.service_endpoint_ids(['
msg_tail = ']) is invalid; RPC method CreateService does not accept Endpoints. '\
'Endpoints should be configured after creating the service.'
except_msg = str(e.value.details())
assert except_msg.startswith(msg_head) and except_msg.endswith(msg_tail)
with pytest.raises(grpc.RpcError) as e:
service_with_config_rules = copy.deepcopy(service_descriptor)
service_with_config_rules['service_config']['config_rules'].extend(service_config_rules)
service_client.CreateService(Service(**service_with_config_rules))
assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
msg_head = 'service.service_config.config_rules(['
msg_tail = ']) is invalid; RPC method CreateService does not accept Config Rules. '\
'Config Rules should be configured after creating the service.'
except_msg = str(e.value.details())
assert except_msg.startswith(msg_head) and except_msg.endswith(msg_tail)
with pytest.raises(grpc.RpcError) as e:
service_with_constraints = copy.deepcopy(service_descriptor)
service_with_constraints['service_constraints'].extend(service_constraints)
service_client.CreateService(Service(**service_with_constraints))
assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
msg_head = 'service.service_constraints(['
msg_tail = ']) is invalid; RPC method CreateService does not accept Constraints. '\
'Constraints should be configured after creating the service.'
except_msg = str(e.value.details())
assert except_msg.startswith(msg_head) and except_msg.endswith(msg_tail)
def test_service_create_correct(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_client.CreateService(Service(**service_descriptor))
def test_service_get_created(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_data = context_client.GetService(ServiceId(**service_id))
LOGGER.info('service_data = {:s}'.format(grpc_message_to_json_string(service_data)))
def test_service_update_configure(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_with_settings = copy.deepcopy(service_descriptor)
service_with_settings['service_endpoint_ids'].extend(service_endpoint_ids)
service_with_settings['service_config']['config_rules'].extend(service_config_rules)
service_with_settings['service_constraints'].extend(service_constraints)
service_client.UpdateService(Service(**service_with_settings))
for endpoint_id in service_endpoint_ids:
device_id = endpoint_id['device_id']
device_data = context_client.GetDevice(DeviceId(**device_id))
for i,config_rule in enumerate(device_data.device_config.config_rules):
LOGGER.info('device_data[{:s}][#{:d}] => {:s}'.format(
str(device_id), i, grpc_message_to_json_string(config_rule)))
def test_service_update_deconfigure(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_with_settings = copy.deepcopy(service_descriptor)
service_with_settings['service_endpoint_ids'].extend([]) # remove endpoints
service_client.UpdateService(Service(**service_with_settings))
for endpoint_id in service_endpoint_ids:
device_id = endpoint_id['device_id']
device_data = context_client.GetDevice(DeviceId(**device_id))
for i,config_rule in enumerate(device_data.device_config.config_rules):
LOGGER.info('device_data[{:s}][#{:d}] => {:s}'.format(
str(device_id), i, grpc_message_to_json_string(config_rule)))
def test_service_get_updated(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_data = context_client.GetService(ServiceId(**service_id))
LOGGER.info('service_data = {:s}'.format(grpc_message_to_json_string(service_data)))
def test_service_delete(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
service_client.DeleteService(ServiceId(**service_id))
def test_cleanup_environment(
self, service_id, service_descriptor, service_endpoint_ids, service_config_rules, service_constraints,
contexts, topologies, devices, links,
context_client : ContextClient, # pylint: disable=redefined-outer-name
device_client : DeviceClient, # pylint: disable=redefined-outer-name
service_client : ServiceClient): # pylint: disable=redefined-outer-name
for link in links: context_client.RemoveLink(LinkId(**link['link_id']))
for device in devices: device_client.DeleteDevice(DeviceId(**device['device_id']))
for topology in topologies: context_client.RemoveTopology(TopologyId(**topology['topology_id']))
for context in contexts: context_client.RemoveContext(ContextId(**context['context_id']))