blob: aaeb6f458ef8b8a93110d717760f6c50f1dadcbf [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001#
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)0ed95a82016-05-18 16:04:37 +010014import mock
15import subprocess
16
Matthew Treinish9e26ca82016-02-23 11:43:20 -050017from tempest.lib.cli import base as cli_base
18from tempest.lib import exceptions
Matthew Treinishffad78a2016-04-16 14:39:52 -040019from tempest.tests import base
Matthew Treinish9e26ca82016-02-23 11:43:20 -050020
21
22class TestExecute(base.TestCase):
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010023
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 Treinish9e26ca82016-02-23 11:43:20 -050029 result = cli_base.execute("/bin/ls", action="tempest",
30 flags="-l -a")
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010031 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 Treinish9e26ca82016-02-23 11:43:20 -050043 self.assertIsInstance(result, str)
44 self.assertIn("__init__.py", result)
45
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010046 @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 Treinish9e26ca82016-02-23 11:43:20 -050051 result = cli_base.execute("/bin/ls", action="tempest.lib",
52 flags="--foobar", merge_stderr=True,
53 fail_ok=True)
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010054 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 Treinish9e26ca82016-02-23 11:43:20 -050065 self.assertIsInstance(result, str)
66 self.assertIn("--foobar", result)
67
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010068 @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 Treinish9e26ca82016-02-23 11:43:20 -050073 self.assertRaises(exceptions.CommandFailed, cli_base.execute,
74 "/bin/ls", action="tempest", flags="--foobar",
75 merge_stderr=True)
Georgy Dyuldind95375c2016-02-24 22:05:30 +030076
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
84class 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'})