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.
15 # pylint: disable=redefined-outer-name
16 from collections import namedtuple
19 from openstackcli import (
22 from . import hostcliuser
23 from .hostcli import HostCli
24 from .hostcliuser import HostCliUser
29 with mock.patch.object(hostcliuser, 'HostCli',
30 mock.create_autospec(HostCli)) as p:
36 with mock.patch.object(hostcliuser,
38 mock.create_autospec(OpenStack)) as p:
44 with mock.patch.object(hostcliuser,
46 mock.create_autospec(Runner)) as p:
50 @pytest.fixture(params=[{},
51 {'envname': 'envname'},
52 {'envname': 'otherenv'}])
53 def envkwargs(request):
58 def examplehostcliuser(mock_remotesession,
61 e = ExampleHostCliUser(mock_runners,
62 mock_remotesession=mock_remotesession,
64 e.initialize(mock_remotesession, **envkwargs)
65 for runner in mock_runners:
66 initialize = runner.return_value.initialize
67 initialize.assert_called_once_with(mock_remotesession, **envkwargs)
72 def mock_runners(mock_hostcli, mock_openstack, mock_runner):
73 return MockRunners(hostcli=mock_hostcli,
74 openstack=mock_openstack,
78 class ExampleHostCliUser(HostCliUser):
80 def __init__(self, mock_runners, mock_remotesession, envkwargs):
81 super(ExampleHostCliUser, self).__init__()
82 self._mock_runners = mock_runners
83 self._mock_remotesession = mock_remotesession
84 self._envkwargs = envkwargs
90 def verify_runs(self):
92 for rm in self._runnermocks:
93 mock_run = rm.mock.return_value.run
94 assert rm.runner.run(self.cmd, target=target) == mock_run.return_value
95 mock_run.assert_called_once_with(self.cmd, target=target)
97 execute = self._mock_remotesession.execute_command_in_target
98 assert self._remotesession.execute_command_in_target(
99 self.cmd, target=target) == execute.return_value
102 def _runnermocks(self):
103 yield RunnerMock(runner=self._hostcli, mock=self._mock_runners.hostcli)
104 yield RunnerMock(runner=self._openstack, mock=self._mock_runners.openstack)
105 yield RunnerMock(runner=self._runner, mock=self._mock_runners.runner)
107 def verify_get_env_target(self):
108 postfix = ('.{}'.format(self._envkwargs['envname'])
109 if self._envkwargs else
111 assert self._get_env_target() == 'default{}'.format(postfix)
112 assert self._get_env_target('target') == 'target{}'.format(postfix)
115 class RunnerMock(namedtuple('RunMock', ['runner', 'mock'])):
119 class MockRunners(namedtuple('MockRunners', ['hostcli', 'openstack', 'runner'])):
123 def test_hostcliuser_runners(examplehostcliuser):
124 examplehostcliuser.verify_runs()
127 def test_get_env_target(examplehostcliuser):
128 examplehostcliuser.verify_get_env_target()