blob: 01304544860418f973212187e94fddac97020f15 [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 subprocess
15
Masayuki Igawa134d9f72017-02-10 18:05:26 +090016import mock
17
Matthew Treinish9e26ca82016-02-23 11:43:20 -050018from tempest.lib.cli import base as cli_base
19from tempest.lib import exceptions
Matthew Treinishffad78a2016-04-16 14:39:52 -040020from tempest.tests import base
Matthew Treinish9e26ca82016-02-23 11:43:20 -050021
22
23class TestExecute(base.TestCase):
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010024
25 @mock.patch('subprocess.Popen', autospec=True)
26 def test_execute_success(self, mock_popen):
27 mock_popen.return_value.returncode = 0
28 mock_popen.return_value.communicate.return_value = (
29 "__init__.py", "")
Matthew Treinish9e26ca82016-02-23 11:43:20 -050030 result = cli_base.execute("/bin/ls", action="tempest",
31 flags="-l -a")
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010032 args, kwargs = mock_popen.call_args
33 # Check merge_stderr == False
34 self.assertEqual(subprocess.PIPE, kwargs['stderr'])
35 # Check action and flags are passed
36 args = args[0]
37 # We just tests that all pieces are passed through, we cannot make
38 # assumptions about the order
39 self.assertIn("/bin/ls", args)
40 self.assertIn("-l", args)
41 self.assertIn("-a", args)
42 self.assertIn("tempest", args)
43 # The result is mocked - checking that the mock was invoked correctly
Matthew Treinish9e26ca82016-02-23 11:43:20 -050044 self.assertIsInstance(result, str)
45 self.assertIn("__init__.py", result)
46
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010047 @mock.patch('subprocess.Popen', autospec=True)
48 def test_execute_failure(self, mock_popen):
49 mock_popen.return_value.returncode = 1
50 mock_popen.return_value.communicate.return_value = (
51 "No such option --foobar", "")
Matthew Treinish9e26ca82016-02-23 11:43:20 -050052 result = cli_base.execute("/bin/ls", action="tempest.lib",
53 flags="--foobar", merge_stderr=True,
54 fail_ok=True)
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010055 args, kwargs = mock_popen.call_args
56 # Check the merge_stderr
57 self.assertEqual(subprocess.STDOUT, kwargs['stderr'])
58 # Check action and flags are passed
59 args = args[0]
60 # We just tests that all pieces are passed through, we cannot make
61 # assumptions about the order
62 self.assertIn("/bin/ls", args)
63 self.assertIn("--foobar", args)
64 self.assertIn("tempest.lib", args)
65 # The result is mocked - checking that the mock was invoked correctly
Matthew Treinish9e26ca82016-02-23 11:43:20 -050066 self.assertIsInstance(result, str)
67 self.assertIn("--foobar", result)
68
Andrea Frittoli (andreaf)0ed95a82016-05-18 16:04:37 +010069 @mock.patch('subprocess.Popen', autospec=True)
70 def test_execute_failure_raise_exception(self, mock_popen):
71 mock_popen.return_value.returncode = 1
72 mock_popen.return_value.communicate.return_value = (
73 "No such option --foobar", "")
Matthew Treinish9e26ca82016-02-23 11:43:20 -050074 self.assertRaises(exceptions.CommandFailed, cli_base.execute,
75 "/bin/ls", action="tempest", flags="--foobar",
76 merge_stderr=True)
Georgy Dyuldind95375c2016-02-24 22:05:30 +030077
78 def test_execute_with_prefix(self):
79 result = cli_base.execute("env", action="",
80 prefix="env NEW_VAR=1")
81 self.assertIsInstance(result, str)
82 self.assertIn("NEW_VAR=1", result)
83
84
85class TestCLIClient(base.TestCase):
86
87 @mock.patch.object(cli_base, 'execute')
88 def test_execute_with_prefix(self, mock_execute):
89 cli = cli_base.CLIClient(prefix='env LAC_ALL=C')
90 cli.glance('action')
91 self.assertEqual(mock_execute.call_count, 1)
92 self.assertEqual(mock_execute.call_args[1],
93 {'prefix': 'env LAC_ALL=C'})