Add cloudtaf framework
[ta/cloudtaf.git] / libraries / openstackcli / cliwrapperbase.py
1 # Copyright 2019 Nokia
2 #
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
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,
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.
14
15 import abc
16 import six
17
18
19 @six.add_metaclass(abc.ABCMeta)
20 class CliWrapperBase(object):
21     """Test class base for CLI (openstackcli).
22     """
23     def __init__(self, clicls):
24         self._cli = clicls()
25         self._remotesession = None
26         self._exec_func = None
27         self._expected_cmd_postfix = ''
28
29     @property
30     def cli(self):
31         return self._cli
32
33     def set_remotesession(self, remotesession):
34         """Set *RemoteSession* mock class instance.
35         """
36         self._remotesession = remotesession
37
38     @property
39     def remotesession(self):
40         return self._remotesession
41
42     def set_expected_cmd_postfix(self, expected_cmd_postfix):
43         """Set expected cmd postfix for *RemoteSession* *exec_func* call.
44         """
45         self._expected_cmd_postfix = expected_cmd_postfix
46
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.
50         """
51         self._exec_func = exec_func
52         self._exec_func.return_value = return_value
53
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.
57
58         Return:
59             *run_method* return value.
60         """
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)
65         return ret
66
67     @abc.abstractproperty
68     def _expected_target(self):
69         """Return expected target for RemoteSession call.
70         """
71
72     def _get_expected_cmd(self, cmd):
73         return '{pre_cmd}{clistr}{expected_cmd_args}{cmd}'.format(
74             pre_cmd=self._pre_cmd,
75             clistr=self._clistr,
76             expected_cmd_args=self._expected_cmd_args,
77             cmd=cmd)
78
79     @property
80     def _clistr(self):
81         n = self._cli.__class__.__name__
82         return '' if n == 'Runner' else n.lower()
83
84     @property
85     def _pre_cmd(self):
86         return ''
87
88     @abc.abstractproperty
89     def _target_kwargs(self):
90         """Return target kwargs for *RemoteSession* method call.
91         """
92
93     @abc.abstractproperty
94     def _expected_cmd_args(self):
95         """Return args string after cli.
96         """
97
98     def initialize(self):
99         """Initialize CLI with mock RemoteSession instance *remotesession*.
100         """
101         self._cli.initialize(self.remotesession)