Yuiko Takada | 504aecc | 2014-03-11 17:57:50 +0000 | [diff] [blame] | 1 | # 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 | |
Yuiko Takada | 504aecc | 2014-03-11 17:57:50 +0000 | [diff] [blame] | 15 | import subprocess |
| 16 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 17 | import mock |
| 18 | |
Yuiko Takada | 504aecc | 2014-03-11 17:57:50 +0000 | [diff] [blame] | 19 | from tempest.common import commands |
| 20 | from tempest.tests import base |
| 21 | |
| 22 | |
| 23 | class TestCommands(base.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | super(TestCommands, self).setUp() |
| 27 | self.subprocess_args = {'stdout': subprocess.PIPE, |
| 28 | 'stderr': subprocess.STDOUT} |
| 29 | |
| 30 | @mock.patch('subprocess.Popen') |
| 31 | def test_ip_addr_raw(self, mock): |
| 32 | expected = ['/usr/bin/sudo', '-n', 'ip', 'a'] |
| 33 | commands.ip_addr_raw() |
| 34 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 35 | |
| 36 | @mock.patch('subprocess.Popen') |
| 37 | def test_ip_route_raw(self, mock): |
| 38 | expected = ['/usr/bin/sudo', '-n', 'ip', 'r'] |
| 39 | commands.ip_route_raw() |
| 40 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 41 | |
| 42 | @mock.patch('subprocess.Popen') |
| 43 | def test_ip_ns_raw(self, mock): |
| 44 | expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'list'] |
| 45 | commands.ip_ns_raw() |
| 46 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 47 | |
| 48 | @mock.patch('subprocess.Popen') |
| 49 | def test_iptables_raw(self, mock): |
| 50 | table = 'filter' |
Sean Dague | 8976c9d | 2014-07-02 11:12:26 -0400 | [diff] [blame] | 51 | expected = ['/usr/bin/sudo', '-n', 'iptables', '--line-numbers', |
| 52 | '-L', '-nv', '-t', |
Yuiko Takada | 504aecc | 2014-03-11 17:57:50 +0000 | [diff] [blame] | 53 | '%s' % table] |
| 54 | commands.iptables_raw(table) |
| 55 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 56 | |
| 57 | @mock.patch('subprocess.Popen') |
| 58 | def test_ip_ns_list(self, mock): |
| 59 | expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'list'] |
| 60 | commands.ip_ns_list() |
| 61 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 62 | |
| 63 | @mock.patch('subprocess.Popen') |
| 64 | def test_ip_ns_addr(self, mock): |
| 65 | ns_list = commands.ip_ns_list() |
| 66 | for ns in ns_list: |
| 67 | expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, |
| 68 | 'ip', 'a'] |
| 69 | commands.ip_ns_addr(ns) |
| 70 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 71 | |
| 72 | @mock.patch('subprocess.Popen') |
| 73 | def test_ip_ns_route(self, mock): |
| 74 | ns_list = commands.ip_ns_list() |
| 75 | for ns in ns_list: |
| 76 | expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, |
| 77 | 'ip', 'r'] |
| 78 | commands.ip_ns_route(ns) |
| 79 | mock.assert_called_once_with(expected, **self.subprocess_args) |
| 80 | |
| 81 | @mock.patch('subprocess.Popen') |
| 82 | def test_iptables_ns(self, mock): |
| 83 | table = 'filter' |
| 84 | ns_list = commands.ip_ns_list() |
| 85 | for ns in ns_list: |
| 86 | expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, |
| 87 | 'iptables', '-v', '-S', '-t', table] |
| 88 | commands.iptables_ns(ns, table) |
| 89 | mock.assert_called_once_with(expected, **self.subprocess_args) |