3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 @six.add_metaclass(abc.ABCMeta)
20 class CliWrapperBase(object):
21 """Test class base for CLI (openstackcli).
23 def __init__(self, clicls):
25 self._remotesession = None
26 self._exec_func = None
27 self._expected_cmd_postfix = ''
33 def set_remotesession(self, remotesession):
34 """Set *RemoteSession* mock class instance.
36 self._remotesession = remotesession
39 def remotesession(self):
40 return self._remotesession
42 def set_expected_cmd_postfix(self, expected_cmd_postfix):
43 """Set expected cmd postfix for *RemoteSession* *exec_func* call.
45 self._expected_cmd_postfix = expected_cmd_postfix
47 def set_exec_func_and_return_value(self, exec_func, return_value):
48 """Set expected *RunnerSession* execution function and
49 set mock return value for this call.
51 self._exec_func = exec_func
52 self._exec_func.return_value = return_value
54 def run_with_verify(self, run_method, cmd):
55 """Run *run_method* of CLI with *cmd* and *_target_kwargs* kwargs. Then
56 verify the *RemoteSession* *_exec_func* call.
59 *run_method* return value.
61 ret = run_method(cmd, **self._target_kwargs)
62 self._exec_func.assert_called_once_with(
63 self._get_expected_cmd(cmd + self._expected_cmd_postfix),
64 target=self._expected_target)
68 def _expected_target(self):
69 """Return expected target for RemoteSession call.
72 def _get_expected_cmd(self, cmd):
73 return '{pre_cmd}{clistr}{expected_cmd_args}{cmd}'.format(
74 pre_cmd=self._pre_cmd,
76 expected_cmd_args=self._expected_cmd_args,
81 n = self._cli.__class__.__name__
82 return '' if n == 'Runner' else n.lower()
89 def _target_kwargs(self):
90 """Return target kwargs for *RemoteSession* method call.
94 def _expected_cmd_args(self):
95 """Return args string after cli.
99 """Initialize CLI with mock RemoteSession instance *remotesession*.
101 self._cli.initialize(self.remotesession)