4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 @file test_trafgen_data.py
20 @author Karun Ganesharatnam (karun.ganesharatnam@riftio.com)
22 @brief Scriptable load-balancer test with multi-vm VNFs
31 import rift
.auto
.session
33 from gi
.repository
import (
42 @pytest.fixture(scope
='session')
43 def trafgen_vnfr(request
, rwvnfr_proxy
, session_type
):
44 vnfr
= "/vnfr-catalog/vnfr"
45 vnfrs
= rwvnfr_proxy
.get(vnfr
, list_obj
=True)
46 for vnfr
in vnfrs
.vnfr
:
47 if 'trafgen' in vnfr
.short_name
:
49 assert False, "Not found the VNFR with name 'trafgen'"
51 @pytest.fixture(scope
='session')
52 def trafgen_session(request
, trafgen_vnfr
, session_type
):
53 trafgen_host
= trafgen_vnfr
.vnf_configuration
.config_access
.mgmt_ip_address
54 if session_type
== 'netconf':
55 tg_session
= rift
.auto
.session
.NetconfSession(host
=trafgen_host
)
56 elif session_type
== 'restconf':
57 tg_session
= rift
.auto
.session
.RestconfSession(host
=trafgen_host
)
60 rift
.vcs
.vcs
.wait_until_system_started(tg_session
, 900)
63 @pytest.fixture(scope
='session')
64 def trafgen_ports(request
, trafgen_vnfr
, session_type
):
65 return [cp
.name
for cp
in trafgen_vnfr
.connection_point
]
67 @pytest.fixture(scope
='module')
68 def tgdata_proxy(trafgen_session
):
69 '''fixture that returns a proxy to RwTrafgenDataYang'''
70 return trafgen_session
.proxy(RwTrafgenDataYang
)
73 @pytest.fixture(scope
='module')
74 def tgcfg_proxy(trafgen_session
):
75 '''fixture that returns a proxy to RwTrafgenYang'''
76 return trafgen_session
.proxy(RwTrafgenYang
)
79 @pytest.fixture(scope
='module')
80 def vnfdata_proxy(trafgen_session
):
81 '''fixture that returns a proxy to RwVnfBaseOpdataYang'''
82 return trafgen_session
.proxy(RwVnfBaseOpdataYang
)
85 @pytest.fixture(scope
='module')
86 def vnfcfg_proxy(trafgen_session
):
87 '''fixture that returns a proxy to RwVnfBaseConfigYang'''
88 return trafgen_session
.proxy(RwVnfBaseConfigYang
)
91 def confirm_config(tgcfg_proxy
, vnf_name
):
92 '''To ensure the configuration is present for the given VNF
95 vnf_name - vnf name of configuration
97 xpath
= "/vnf-config/vnf[name='%s'][instance='0']" % vnf_name
99 tg_config
= tgcfg_proxy
.get_config(xpath
)
100 if tg_config
is not None:
104 assert False, "Configuration check timeout"
107 def start_traffic(tgdata_proxy
, tgcfg_proxy
, port_name
):
108 '''Start traffic on the port with the specified name.
111 port_name - name of port on which to start traffic
113 confirm_config(tgcfg_proxy
, 'trafgen')
114 rpc_input
= RwTrafgenDataYang
.RwStartTrafgenTraffic
.from_dict({
115 'vnf_name':'trafgen',
117 'port_name':port_name
119 rpc_output
= RwVnfBaseOpdataYang
.YangOutput_RwVnfBaseOpdata_Start_VnfOutput()
120 tgdata_proxy
.rpc(rpc_input
, rpc_name
='start', output_obj
=rpc_output
)
123 def stop_traffic(tgdata_proxy
, port_name
):
124 '''Stop traffic on the port with the specified name.
127 port_name - name of port on which to stop traffic
129 rpc_input
= RwTrafgenDataYang
.RwStopTrafgenTraffic
.from_dict({
130 'vnf_name':'trafgen',
132 'port_name':port_name
134 rpc_output
= RwVnfBaseOpdataYang
.YangOutput_RwVnfBaseOpdata_Stop_VnfOutput()
135 tgdata_proxy
.rpc(rpc_input
, rpc_name
='stop', output_obj
=rpc_output
)
138 def wait_for_traffic_started(vnfdata_proxy
, vnf_name
, port_name
, timeout
=120, interval
=2, threshold
=60):
139 '''Wait for traffic to be started on the specified port
141 Traffic is determined to be started if the input/output packets on the port
142 increment during the specified interval
145 port_name - name of the port being monitored
146 timeout - time allowed for traffic to start
147 interval - interval at which the counters should be checked
148 threhsold - values under the threshold treated as 0
150 def value_incremented(previous_sample
, current_sample
):
151 '''Comparison that returns True if the the sampled counter increased
152 beyond the specified threshold during the sampling interval
153 otherwise returns false
155 return (int(current_sample
) - int(previous_sample
)) > threshold
157 xpath
= "/vnf-opdata/vnf[name='{}'][instance='0']/port-state[portname='{}']/counters/{}"
158 vnfdata_proxy
.wait_for_interval(xpath
.format(vnf_name
, port_name
, 'input-packets'),
159 value_incremented
, timeout
=timeout
, interval
=interval
)
162 def wait_for_traffic_stopped(vnfdata_proxy
, vnf_name
, port_name
, timeout
=60, interval
=2, threshold
=60):
163 '''Wait for traffic to be stopped on the specified port
165 Traffic is determined to be stopped if the input/output packets on the port
166 remain unchanged during the specified interval
169 port_name - name of the port being monitored
170 timeout - time allowed for traffic to start
171 interval - interval at which the counters should be checked
172 threshold - values under the threshold treated as 0
174 def value_unchanged(previous_sample
, current_sample
):
175 '''Comparison that returns True if the the sampled counter increased
176 less than the specified threshold during the sampling interval
177 otherwise returns False
179 return (int(current_sample
) - int(previous_sample
)) < threshold
181 xpath
= "/vnf-opdata/vnf[name='{}'][instance='0']/port-state[portname='{}']/counters/{}"
182 vnfdata_proxy
.wait_for_interval(xpath
.format(vnf_name
, port_name
, 'input-packets'), value_unchanged
, timeout
=timeout
, interval
=interval
)
184 @pytest.mark
.depends('multivmvnf')
185 @pytest.mark
.incremental
186 class TestMVVSlbDataFlow
:
188 def test_start_stop_traffic(self
, vnfdata_proxy
, tgdata_proxy
, tgcfg_proxy
, trafgen_ports
):
189 ''' This test verfies that traffic can be stopped and started on
193 vnfdata_proxy - proxy to retrieve vnf operational data
194 tgdata_proxy - proxy to retrieve trafgen operational data
195 tgcfg_proxy - proxy to retrieve trafgen configuration
196 trafgen_ports - list of port names on which traffic can be started
199 for port
in trafgen_ports
:
200 start_traffic(tgdata_proxy
, tgcfg_proxy
, port
)
201 wait_for_traffic_started(vnfdata_proxy
, 'trafgen', port
)
202 stop_traffic(tgdata_proxy
, port
)
203 wait_for_traffic_stopped(vnfdata_proxy
, 'trafgen', port
)
206 def test_start_traffic(self
, vnfdata_proxy
, tgdata_proxy
, tgcfg_proxy
, trafgen_ports
):
207 ''' This test starts traffic on all trafgen ports in preperation for
211 vnfdata_proxy - proxy to retrieve vnf operational data
212 tgdata_proxy - proxy to retrieve trafgen operational data
213 tgcfg_proxy - proxy to retrieve trafgen configuration
214 trafgen_ports - list of port names on which traffic can be started
216 for port
in trafgen_ports
:
217 start_traffic(tgdata_proxy
, tgcfg_proxy
, port
)
218 wait_for_traffic_started(vnfdata_proxy
, 'trafgen', port
)