Removed obsolete url
[ta/config-manager.git] / cmframework / test / cmagent_test.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 unittest
16 import mock
17 from cmframework.agent.cmagent import CMAgent
18 from cmframework.apis.cmerror import CMError
19
20
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
33         args = []
34         agent = CMAgent()
35         agent(args)
36
37         # import pdb
38         # pdb.set_trace()
39
40         mock_socket_get_hostbyname.assert_called_once_with('config-manager')
41
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()
47
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)
57         args = []
58         agent = CMAgent()
59         agent(args)
60
61         mock_cmmanage.return_value.activate_node.assert_called_once()
62         mock_reboot_node.assert_not_called()
63
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)
73
74         args = ['--ip', 'abc.com', '--port', '1234', '--client-lib', 'abc']
75         agent = CMAgent()
76         agent(args)
77
78         mock_socket_get_hostbyname.assert_called_once_with('abc.com')
79
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()
83
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)
93
94         import socket
95         mock_socket_get_hostbyname.side_effect = socket.gaierror()
96
97         args = []
98         agent = CMAgent()
99         agent(args)
100
101         mock_socket_get_hostbyname.assert_called_once_with('config-manager')
102         mock_verboselogger.assert_called_once()
103
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()
108
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')
119
120         args = []
121         agent = CMAgent()
122         try:
123             agent(args)
124             assert False
125         except CMError:
126             pass
127
128         # TODO assert that alarm was raised
129
130
131 if __name__ == '__main__':
132     unittest.main()