robot tcs, test charts, robot container added
[ta/cloudtaf.git] / hostcli / test_hostcliuser.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 # pylint: disable=redefined-outer-name
16 from collections import namedtuple
17 import mock
18 import pytest
19 from openstackcli import (
20     OpenStack,
21     Runner)
22 from . import hostcliuser
23 from .hostcli import HostCli
24 from .hostcliuser import HostCliUser
25
26
27 @pytest.fixture
28 def mock_hostcli():
29     with mock.patch.object(hostcliuser, 'HostCli',
30                            mock.create_autospec(HostCli)) as p:
31         yield p
32
33
34 @pytest.fixture
35 def mock_openstack():
36     with mock.patch.object(hostcliuser,
37                            'OpenStack',
38                            mock.create_autospec(OpenStack)) as p:
39         yield p
40
41
42 @pytest.fixture
43 def mock_runner():
44     with mock.patch.object(hostcliuser,
45                            'Runner',
46                            mock.create_autospec(Runner)) as p:
47         yield p
48
49
50 @pytest.fixture(params=[{},
51                         {'envname': 'envname'},
52                         {'envname': 'otherenv'}])
53 def envkwargs(request):
54     return request.param
55
56
57 @pytest.fixture
58 def examplehostcliuser(mock_remotesession,
59                        mock_runners,
60                        envkwargs):
61     e = ExampleHostCliUser(mock_runners,
62                            mock_remotesession=mock_remotesession,
63                            envkwargs=envkwargs)
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)
68     return e
69
70
71 @pytest.fixture
72 def mock_runners(mock_hostcli, mock_openstack, mock_runner):
73     return MockRunners(hostcli=mock_hostcli,
74                        openstack=mock_openstack,
75                        runner=mock_runner)
76
77
78 class ExampleHostCliUser(HostCliUser):
79
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
85
86     @property
87     def cmd(self):
88         return 'cmd'
89
90     def verify_runs(self):
91         target = 'target'
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)
96
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
100
101     @property
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)
106
107     def verify_get_env_target(self):
108         postfix = ('.{}'.format(self._envkwargs['envname'])
109                    if self._envkwargs else
110                    '')
111         assert self._get_env_target() == 'default{}'.format(postfix)
112         assert self._get_env_target('target') == 'target{}'.format(postfix)
113
114
115 class RunnerMock(namedtuple('RunMock', ['runner', 'mock'])):
116     pass
117
118
119 class MockRunners(namedtuple('MockRunners', ['hostcli', 'openstack', 'runner'])):
120     pass
121
122
123 def test_hostcliuser_runners(examplehostcliuser):
124     examplehostcliuser.verify_runs()
125
126
127 def test_get_env_target(examplehostcliuser):
128     examplehostcliuser.verify_get_env_target()