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.
17 from cmframework.agent.cmagent import CMAgent
18 from cmframework.apis.cmerror import CMError
21 class CMAgentTest(unittest.TestCase):
22 @mock.patch('cmframework.agent.cmagent.cmmanage.CMManage')
23 @mock.patch('cmframework.agent.cmagent.cmlogger.CMLogger')
24 @mock.patch('cmframework.agent.cmagent.socket.gethostbyname')
25 @mock.patch('cmframework.agent.cmagent.CMAgent._reboot_node')
26 @mock.patch('cmframework.agent.cmagent.VerboseLogger')
27 @mock.patch('cmframework.agent.cmagent.logging')
28 def test_activate_default_args_and_reboot(self, mock_logging, mock_verboselogger,
29 mock_reboot_node, mock_socket_get_hostbyname,
30 mock_cmlogger, mock_cmmanage):
31 mock_cmmanage.return_value.activate_node = mock.MagicMock(return_value=True)
32 mock_reboot_node.return_value = 0
40 mock_socket_get_hostbyname.assert_called_once_with('config-manager')
42 mock_cmmanage.assert_called_once_with(mock_socket_get_hostbyname.return_value, 61100,
43 'cmframework.lib.CMClientImpl',
44 mock_verboselogger.return_value)
45 mock_cmmanage.return_value.activate_node.assert_called_once()
46 mock_reboot_node.assert_called_once()
48 @mock.patch('cmframework.agent.cmagent.cmmanage.CMManage')
49 @mock.patch('cmframework.agent.cmagent.cmlogger.CMLogger')
50 @mock.patch('cmframework.agent.cmagent.socket.gethostbyname')
51 @mock.patch('cmframework.agent.cmagent.CMAgent._reboot_node')
52 @mock.patch('cmframework.agent.cmagent.VerboseLogger')
53 @mock.patch('cmframework.agent.cmagent.logging')
54 def test_activate_no_reboot(self, mock_logging, mock_verboselogger, mock_reboot_node,
55 mock_socket_get_hostbyname, mock_cmlogger, mock_cmmanage):
56 mock_cmmanage.return_value.activate_node = mock.MagicMock(return_value=False)
61 mock_cmmanage.return_value.activate_node.assert_called_once()
62 mock_reboot_node.assert_not_called()
64 @mock.patch('cmframework.agent.cmagent.cmmanage.CMManage')
65 @mock.patch('cmframework.agent.cmagent.cmlogger.CMLogger')
66 @mock.patch('cmframework.agent.cmagent.socket.gethostbyname')
67 @mock.patch('cmframework.agent.cmagent.CMAgent._reboot_node')
68 @mock.patch('cmframework.agent.cmagent.VerboseLogger')
69 @mock.patch('cmframework.agent.cmagent.logging')
70 def test_activate_custom_args(self, mock_logging, mock_verboselogger, mock_reboot_node,
71 mock_socket_get_hostbyname, mock_cmlogger, mock_cmmanage):
72 mock_cmmanage.return_value.activate_node = mock.MagicMock(return_value=True)
74 args = ['--ip', 'abc.com', '--port', '1234', '--client-lib', 'abc']
78 mock_socket_get_hostbyname.assert_called_once_with('abc.com')
80 mock_cmmanage.assert_called_once_with(mock_socket_get_hostbyname.return_value, 1234, 'abc',
81 mock_verboselogger.return_value)
82 mock_cmmanage.return_value.activate_node.assert_called_once()
84 @mock.patch('cmframework.agent.cmagent.cmmanage.CMManage')
85 @mock.patch('cmframework.agent.cmagent.cmlogger.CMLogger')
86 @mock.patch('cmframework.agent.cmagent.socket.gethostbyname')
87 @mock.patch('cmframework.agent.cmagent.CMAgent._reboot_node')
88 @mock.patch('cmframework.agent.cmagent.VerboseLogger')
89 @mock.patch('cmframework.agent.cmagent.logging')
90 def test_activate_localhost(self, mock_logging, mock_verboselogger, mock_reboot_node,
91 mock_socket_get_hostbyname, mock_cmlogger, mock_cmmanage):
92 mock_cmmanage.return_value.activate_node = mock.MagicMock(return_value=True)
95 mock_socket_get_hostbyname.side_effect = socket.gaierror()
101 mock_socket_get_hostbyname.assert_called_once_with('config-manager')
102 mock_verboselogger.assert_called_once()
104 mock_cmmanage.assert_called_once_with('127.0.0.1', 61100,
105 'cmframework.lib.CMClientImpl',
106 mock_verboselogger.return_value)
107 mock_cmmanage.return_value.activate_node.assert_called_once()
109 @mock.patch('cmframework.agent.cmagent.cmmanage.CMManage')
110 @mock.patch('cmframework.agent.cmagent.cmlogger.CMLogger')
111 @mock.patch('cmframework.agent.cmagent.socket.gethostbyname')
112 @mock.patch('cmframework.agent.cmagent.CMAgent._reboot_node')
113 @mock.patch('cmframework.agent.cmagent.VerboseLogger')
114 @mock.patch('cmframework.agent.cmagent.logging')
115 def test_activate_fails(self, mock_logging, mock_verboselogger, mock_reboot_node,
116 mock_socket_get_hostbyname, mock_cmlogger, mock_cmmanage):
117 mock_cmmanage.return_value.activate_node = mock.MagicMock()
118 mock_cmmanage.return_value.activate_node.side_effect = CMError('Test error')
128 # TODO assert that alarm was raised
131 if __name__ == '__main__':