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
32 import rift
.auto
.session
34 from gi
.repository
import (
41 gi
.require_version('RwKeyspec', '1.0')
42 from gi
.repository
.RwKeyspec
import quoted_key
45 @pytest.fixture(scope
='session')
46 def trafgen_vnfr(request
, rwvnfr_proxy
, session_type
):
47 vnfr
= "/rw-project:project[rw-project:name='default']/vnfr-catalog/vnfr"
48 vnfrs
= rwvnfr_proxy
.get(vnfr
, list_obj
=True)
49 for vnfr
in vnfrs
.vnfr
:
50 if 'trafgen' in vnfr
.short_name
:
52 assert False, "Not found the VNFR with name 'trafgen'"
54 @pytest.fixture(scope
='session')
55 def trafgen_session(request
, trafgen_vnfr
, session_type
):
56 trafgen_host
= trafgen_vnfr
.vnf_configuration
.config_access
.mgmt_ip_address
57 if session_type
== 'netconf':
58 tg_session
= rift
.auto
.session
.NetconfSession(host
=trafgen_host
)
59 elif session_type
== 'restconf':
60 tg_session
= rift
.auto
.session
.RestconfSession(host
=trafgen_host
)
63 rift
.vcs
.vcs
.wait_until_system_started(tg_session
, 900)
66 @pytest.fixture(scope
='session')
67 def trafgen_ports(request
, trafgen_vnfr
, session_type
):
68 return [cp
.name
for cp
in trafgen_vnfr
.connection_point
]
70 @pytest.fixture(scope
='module')
71 def tgdata_proxy(trafgen_session
):
72 '''fixture that returns a proxy to RwTrafgenDataYang'''
73 return trafgen_session
.proxy(RwTrafgenDataYang
)
76 @pytest.fixture(scope
='module')
77 def tgcfg_proxy(trafgen_session
):
78 '''fixture that returns a proxy to RwTrafgenYang'''
79 return trafgen_session
.proxy(RwTrafgenYang
)
82 @pytest.fixture(scope
='module')
83 def vnfdata_proxy(trafgen_session
):
84 '''fixture that returns a proxy to RwVnfBaseOpdataYang'''
85 return trafgen_session
.proxy(RwVnfBaseOpdataYang
)
88 @pytest.fixture(scope
='module')
89 def vnfcfg_proxy(trafgen_session
):
90 '''fixture that returns a proxy to RwVnfBaseConfigYang'''
91 return trafgen_session
.proxy(RwVnfBaseConfigYang
)
94 def confirm_config(tgcfg_proxy
, vnf_name
):
95 '''To ensure the configuration is present for the given VNF
98 vnf_name - vnf name of configuration
100 xpath
= "/rw-project:project[rw-project:name='default']/vnf-config/vnf[name=%s][instance='0']" % quoted_key(vnf_name
)
102 tg_config
= tgcfg_proxy
.get_config(xpath
)
103 if tg_config
is not None:
107 assert False, "Configuration check timeout"
110 def start_traffic(tgdata_proxy
, tgcfg_proxy
, port_name
):
111 '''Start traffic on the port with the specified name.
114 port_name - name of port on which to start traffic
116 confirm_config(tgcfg_proxy
, 'trafgen')
117 rpc_input
= RwTrafgenDataYang
.RwStartTrafgenTraffic
.from_dict({
118 'vnf_name':'trafgen',
120 'port_name':port_name
122 rpc_output
= RwVnfBaseOpdataYang
.YangOutput_RwVnfBaseOpdata_Start_VnfOutput()
123 tgdata_proxy
.rpc(rpc_input
, rpc_name
='start', output_obj
=rpc_output
)
126 def stop_traffic(tgdata_proxy
, port_name
):
127 '''Stop traffic on the port with the specified name.
130 port_name - name of port on which to stop traffic
132 rpc_input
= RwTrafgenDataYang
.RwStopTrafgenTraffic
.from_dict({
133 'vnf_name':'trafgen',
135 'port_name':port_name
137 rpc_output
= RwVnfBaseOpdataYang
.YangOutput_RwVnfBaseOpdata_Stop_VnfOutput()
138 tgdata_proxy
.rpc(rpc_input
, rpc_name
='stop', output_obj
=rpc_output
)
141 def wait_for_traffic_started(vnfdata_proxy
, vnf_name
, port_name
, timeout
=120, interval
=2, threshold
=60):
142 '''Wait for traffic to be started on the specified port
144 Traffic is determined to be started if the input/output packets on the port
145 increment during the specified interval
148 port_name - name of the port being monitored
149 timeout - time allowed for traffic to start
150 interval - interval at which the counters should be checked
151 threhsold - values under the threshold treated as 0
153 def value_incremented(previous_sample
, current_sample
):
154 '''Comparison that returns True if the the sampled counter increased
155 beyond the specified threshold during the sampling interval
156 otherwise returns false
158 return (int(current_sample
) - int(previous_sample
)) > threshold
160 xpath
= "/rw-project:project[rw-project:name='default']/vnf-opdata/vnf[name={}][instance='0']/port-state[portname={}]/counters/{}"
161 vnfdata_proxy
.wait_for_interval(xpath
.format(quoted_key(vnf_name
), quoted_key(port_name
), quoted_key('input-packets')),
162 value_incremented
, timeout
=timeout
, interval
=interval
)
165 def wait_for_traffic_stopped(vnfdata_proxy
, vnf_name
, port_name
, timeout
=60, interval
=2, threshold
=60):
166 '''Wait for traffic to be stopped on the specified port
168 Traffic is determined to be stopped if the input/output packets on the port
169 remain unchanged during the specified interval
172 port_name - name of the port being monitored
173 timeout - time allowed for traffic to start
174 interval - interval at which the counters should be checked
175 threshold - values under the threshold treated as 0
177 def value_unchanged(previous_sample
, current_sample
):
178 '''Comparison that returns True if the the sampled counter increased
179 less than the specified threshold during the sampling interval
180 otherwise returns False
182 return (int(current_sample
) - int(previous_sample
)) < threshold
184 xpath
= "/rw-project:project[rw-project:name='default']/vnf-opdata/vnf[name={}][instance='0']/port-state[portname={}]/counters/{}"
185 vnfdata_proxy
.wait_for_interval(xpath
.format(quoted_key(vnf_name
), quoted_key(port_name
), quoted_key('input-packets')), value_unchanged
, timeout
=timeout
, interval
=interval
)
187 @pytest.mark
.depends('multivmvnf')
188 @pytest.mark
.incremental
189 class TestMVVSlbDataFlow
:
191 def test_start_stop_traffic(self
, vnfdata_proxy
, tgdata_proxy
, tgcfg_proxy
, trafgen_ports
):
192 ''' This test verfies that traffic can be stopped and started on
196 vnfdata_proxy - proxy to retrieve vnf operational data
197 tgdata_proxy - proxy to retrieve trafgen operational data
198 tgcfg_proxy - proxy to retrieve trafgen configuration
199 trafgen_ports - list of port names on which traffic can be started
202 for port
in trafgen_ports
:
203 start_traffic(tgdata_proxy
, tgcfg_proxy
, port
)
204 wait_for_traffic_started(vnfdata_proxy
, 'trafgen', port
)
205 stop_traffic(tgdata_proxy
, port
)
206 wait_for_traffic_stopped(vnfdata_proxy
, 'trafgen', port
)
209 def test_start_traffic(self
, vnfdata_proxy
, tgdata_proxy
, tgcfg_proxy
, trafgen_ports
):
210 ''' This test starts traffic on all trafgen ports in preperation for
214 vnfdata_proxy - proxy to retrieve vnf operational data
215 tgdata_proxy - proxy to retrieve trafgen operational data
216 tgcfg_proxy - proxy to retrieve trafgen configuration
217 trafgen_ports - list of port names on which traffic can be started
219 for port
in trafgen_ports
:
220 start_traffic(tgdata_proxy
, tgcfg_proxy
, port
)
221 wait_for_traffic_started(vnfdata_proxy
, 'trafgen', port
)