1 # Copyright 2016 Red Hat Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 from tools import tasks
24 _LOGGER = logging.getLogger(__name__)
27 def add_veth_port(port, peer_port):
30 :param port:port name for the first port
31 :param peer_port: port name for the peer port
34 # touch some files in a tmp area so we can track them. This allows us to
35 # track VSPerf created veth ports so they can be cleaned up if needed.
36 if not os.path.isdir('/tmp/veth'):
40 # OK don't crash but cleanup may be an issue
41 _LOGGER.error('Unable to create veth temp folder.')
43 'Veth ports may not be removed on testcase completion')
44 if os.path.isdir('/tmp/veth'):
45 with open('/tmp/veth/{}-{}'.format(port, peer_port), 'a'):
46 os.utime('/tmp/veth/{}-{}'.format(port, peer_port), None)
47 tasks.run_task(['sudo', 'ip', 'link', 'add',
48 port, 'type', 'veth', 'peer', 'name', peer_port],
49 _LOGGER, 'Adding veth port {} with peer port {}...'.format(
50 port, peer_port), False)
53 def bring_up_eth_port(eth_port, namespace=None):
56 :param eth_port: string of eth port to bring up
57 :param namespace: Namespace eth port it located if needed
61 tasks.run_task(['sudo', 'ip', 'netns', 'exec', namespace,
62 'ip', 'link', 'set', eth_port, 'up'],
64 'Bringing up port {} in namespace {}...'.format(
65 eth_port, namespace), False)
67 tasks.run_task(['sudo', 'ip', 'link', 'set', eth_port, 'up'],
68 _LOGGER, 'Bringing up port...', False)
71 def del_veth_port(port, peer_port):
73 Delete the veth ports, the peer will automatically be deleted on deletion
74 of the first port param
75 :param port: port name to delete
76 :param port: peer port name
79 # delete the file if it exists in the temp area
80 if os.path.exists('/tmp/veth/{}-{}'.format(port, peer_port)):
81 os.remove('/tmp/veth/{}-{}'.format(port, peer_port))
82 tasks.run_task(['sudo', 'ip', 'link', 'del', port],
83 _LOGGER, 'Deleting veth port {} with peer {}...'.format(
84 port, peer_port), False)
87 # pylint: disable=unused-argument
88 def validate_add_veth_port(result, port, peer_port):
90 Validation function for integration testcases
92 devs = os.listdir('/sys/class/net')
93 return all([port in devs, peer_port in devs])
96 def validate_bring_up_eth_port(result, eth_port, namespace=None):
98 Validation function for integration testcases
102 command += ['ip', 'netns', 'exec', namespace]
103 command += ['cat', '/sys/class/net/{}/operstate'.format(eth_port)]
104 out = tasks.run_task(command, _LOGGER, 'Validating port up...', False)
106 # since different types of ports may report different status the best way
107 # we can do this for now is to just make sure it doesn't say down
113 def validate_del_veth_port(result, port, peer_port):
115 Validation function for integration testcases
117 devs = os.listdir('/sys/class/net')
118 return not any([port in devs, peer_port in devs])