Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | # not use this file except in compliance with the License. You may obtain |
| 4 | # a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | # License for the specific language governing permissions and limitations |
| 12 | # under the License. |
| 13 | |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 14 | import mock |
| 15 | import subprocess |
| 16 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | from tempest.lib.cli import base as cli_base |
| 18 | from tempest.lib import exceptions |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 19 | from tempest.tests import base |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class TestExecute(base.TestCase): |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 23 | |
| 24 | @mock.patch('subprocess.Popen', autospec=True) |
| 25 | def test_execute_success(self, mock_popen): |
| 26 | mock_popen.return_value.returncode = 0 |
| 27 | mock_popen.return_value.communicate.return_value = ( |
| 28 | "__init__.py", "") |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 29 | result = cli_base.execute("/bin/ls", action="tempest", |
| 30 | flags="-l -a") |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 31 | args, kwargs = mock_popen.call_args |
| 32 | # Check merge_stderr == False |
| 33 | self.assertEqual(subprocess.PIPE, kwargs['stderr']) |
| 34 | # Check action and flags are passed |
| 35 | args = args[0] |
| 36 | # We just tests that all pieces are passed through, we cannot make |
| 37 | # assumptions about the order |
| 38 | self.assertIn("/bin/ls", args) |
| 39 | self.assertIn("-l", args) |
| 40 | self.assertIn("-a", args) |
| 41 | self.assertIn("tempest", args) |
| 42 | # The result is mocked - checking that the mock was invoked correctly |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 43 | self.assertIsInstance(result, str) |
| 44 | self.assertIn("__init__.py", result) |
| 45 | |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 46 | @mock.patch('subprocess.Popen', autospec=True) |
| 47 | def test_execute_failure(self, mock_popen): |
| 48 | mock_popen.return_value.returncode = 1 |
| 49 | mock_popen.return_value.communicate.return_value = ( |
| 50 | "No such option --foobar", "") |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 51 | result = cli_base.execute("/bin/ls", action="tempest.lib", |
| 52 | flags="--foobar", merge_stderr=True, |
| 53 | fail_ok=True) |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 54 | args, kwargs = mock_popen.call_args |
| 55 | # Check the merge_stderr |
| 56 | self.assertEqual(subprocess.STDOUT, kwargs['stderr']) |
| 57 | # Check action and flags are passed |
| 58 | args = args[0] |
| 59 | # We just tests that all pieces are passed through, we cannot make |
| 60 | # assumptions about the order |
| 61 | self.assertIn("/bin/ls", args) |
| 62 | self.assertIn("--foobar", args) |
| 63 | self.assertIn("tempest.lib", args) |
| 64 | # The result is mocked - checking that the mock was invoked correctly |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 65 | self.assertIsInstance(result, str) |
| 66 | self.assertIn("--foobar", result) |
| 67 | |
Andrea Frittoli (andreaf) | 0ed95a8 | 2016-05-18 16:04:37 +0100 | [diff] [blame] | 68 | @mock.patch('subprocess.Popen', autospec=True) |
| 69 | def test_execute_failure_raise_exception(self, mock_popen): |
| 70 | mock_popen.return_value.returncode = 1 |
| 71 | mock_popen.return_value.communicate.return_value = ( |
| 72 | "No such option --foobar", "") |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 73 | self.assertRaises(exceptions.CommandFailed, cli_base.execute, |
| 74 | "/bin/ls", action="tempest", flags="--foobar", |
| 75 | merge_stderr=True) |
Georgy Dyuldin | d95375c | 2016-02-24 22:05:30 +0300 | [diff] [blame] | 76 | |
| 77 | def test_execute_with_prefix(self): |
| 78 | result = cli_base.execute("env", action="", |
| 79 | prefix="env NEW_VAR=1") |
| 80 | self.assertIsInstance(result, str) |
| 81 | self.assertIn("NEW_VAR=1", result) |
| 82 | |
| 83 | |
| 84 | class TestCLIClient(base.TestCase): |
| 85 | |
| 86 | @mock.patch.object(cli_base, 'execute') |
| 87 | def test_execute_with_prefix(self, mock_execute): |
| 88 | cli = cli_base.CLIClient(prefix='env LAC_ALL=C') |
| 89 | cli.glance('action') |
| 90 | self.assertEqual(mock_execute.call_count, 1) |
| 91 | self.assertEqual(mock_execute.call_args[1], |
| 92 | {'prefix': 'env LAC_ALL=C'}) |