blob: bdb92691f86ad845013076f4512697de36bb83a3 [file] [log] [blame]
Yuiko Takada504aecc2014-03-11 17:57:50 +00001# Copyright 2014 NEC Corporation. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import mock
16import subprocess
17
18from tempest.common import commands
19from tempest.tests import base
20
21
22class TestCommands(base.TestCase):
23
24 def setUp(self):
25 super(TestCommands, self).setUp()
26 self.subprocess_args = {'stdout': subprocess.PIPE,
27 'stderr': subprocess.STDOUT}
28
29 @mock.patch('subprocess.Popen')
30 def test_ip_addr_raw(self, mock):
31 expected = ['/usr/bin/sudo', '-n', 'ip', 'a']
32 commands.ip_addr_raw()
33 mock.assert_called_once_with(expected, **self.subprocess_args)
34
35 @mock.patch('subprocess.Popen')
36 def test_ip_route_raw(self, mock):
37 expected = ['/usr/bin/sudo', '-n', 'ip', 'r']
38 commands.ip_route_raw()
39 mock.assert_called_once_with(expected, **self.subprocess_args)
40
41 @mock.patch('subprocess.Popen')
42 def test_ip_ns_raw(self, mock):
43 expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'list']
44 commands.ip_ns_raw()
45 mock.assert_called_once_with(expected, **self.subprocess_args)
46
47 @mock.patch('subprocess.Popen')
48 def test_iptables_raw(self, mock):
49 table = 'filter'
50 expected = ['/usr/bin/sudo', '-n', 'iptables', '-v', '-S', '-t',
51 '%s' % table]
52 commands.iptables_raw(table)
53 mock.assert_called_once_with(expected, **self.subprocess_args)
54
55 @mock.patch('subprocess.Popen')
56 def test_ip_ns_list(self, mock):
57 expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'list']
58 commands.ip_ns_list()
59 mock.assert_called_once_with(expected, **self.subprocess_args)
60
61 @mock.patch('subprocess.Popen')
62 def test_ip_ns_addr(self, mock):
63 ns_list = commands.ip_ns_list()
64 for ns in ns_list:
65 expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns,
66 'ip', 'a']
67 commands.ip_ns_addr(ns)
68 mock.assert_called_once_with(expected, **self.subprocess_args)
69
70 @mock.patch('subprocess.Popen')
71 def test_ip_ns_route(self, mock):
72 ns_list = commands.ip_ns_list()
73 for ns in ns_list:
74 expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns,
75 'ip', 'r']
76 commands.ip_ns_route(ns)
77 mock.assert_called_once_with(expected, **self.subprocess_args)
78
79 @mock.patch('subprocess.Popen')
80 def test_iptables_ns(self, mock):
81 table = 'filter'
82 ns_list = commands.ip_ns_list()
83 for ns in ns_list:
84 expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns,
85 'iptables', '-v', '-S', '-t', table]
86 commands.iptables_ns(ns, table)
87 mock.assert_called_once_with(expected, **self.subprocess_args)