11import cherrypy
22import json
33from _pytest .monkeypatch import MonkeyPatch
4+ from urllib .error import URLError
45from cherrypy .test import helper
56from cephadm .agent import NodeProxyEndpoint
67from unittest .mock import MagicMock , call , patch
@@ -23,6 +24,7 @@ def __init__(self) -> None:
2324 self .agent_cache .agent_ports = {"host01" : 1234 }
2425 self .node_proxy_cache = NodeProxyCache (self )
2526 self .node_proxy_cache .save = MagicMock ()
27+ self .node_proxy = MagicMock ()
2628 self .http_server = MagicMock ()
2729 self .http_server .agent = MagicMock ()
2830 self .http_server .agent .ssl_certs = SSLCerts ()
@@ -35,13 +37,13 @@ def get_mgr_ip(self) -> str:
3537class TestNodeProxyEndpoint (helper .CPWebCase ):
3638 mgr = FakeMgr ()
3739 app = NodeProxyEndpoint (mgr )
38- mgr .node_proxy .keyrings = {"host01" : "fake-secret01" ,
39- "host02" : "fake-secret02" }
40- mgr .node_proxy .oob = {"host01" : {"username" : "oob-user01" ,
41- "password" : "oob-pass01" },
42- "host02" : {"username" : "oob-user02" ,
43- "password" : "oob-pass02" }}
44- mgr .node_proxy .data = node_proxy_data .full_set
40+ mgr .node_proxy_cache .keyrings = {"host01" : "fake-secret01" ,
41+ "host02" : "fake-secret02" }
42+ mgr .node_proxy_cache .oob = {"host01" : {"username" : "oob-user01" ,
43+ "password" : "oob-pass01" },
44+ "host02" : {"username" : "oob-user02" ,
45+ "password" : "oob-pass02" }}
46+ mgr .node_proxy_cache .data = node_proxy_data .full_set
4547
4648 @classmethod
4749 def setup_server (cls ):
@@ -117,11 +119,6 @@ def test_data_raises_alert(self):
117119
118120 assert TestNodeProxyEndpoint .mgr .set_health_warning .mock_calls == calls
119121
120- # @pytest.mark.parametrize("method", ["GET", "PATCH"])
121- # def test_led_no_hostname(self, method):
122- # self.getPage("/led", method=method)
123- # self.assertStatus('501 Not Implemented')
124-
125122 def test_led_GET_no_hostname (self ):
126123 self .getPage ("/led" , method = "GET" )
127124 self .assertStatus ('501 Not Implemented' )
@@ -132,20 +129,16 @@ def test_led_PATCH_no_hostname(self):
132129 ('Content-Length' , str (len (data )))])
133130 self .assertStatus ('501 Not Implemented' )
134131
135- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
136- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
137- def test_set_led_no_type (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
138- data = '{"IndicatorLED": "Blinking"}'
132+ def test_set_led_no_type (self ):
133+ data = '{"state": "on", "keyring": "fake-secret01"}'
139134 self .getPage ("/host01/led" , method = "PATCH" , body = data , headers = [('Content-Type' , 'application/json' ),
140135 ('Content-Length' , str (len (data )))])
141136 self .assertStatus ('400 Bad Request' )
142137
143- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
144- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
145- def test_set_chassis_led (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
146- data = '{"IndicatorLED": "Blinking"}'
147- with patch ('cephadm.agent.AgentMessageThread.get_agent_response' ) as a :
148- a .return_value = '{"http_code": 200}'
138+ def test_set_chassis_led (self ):
139+ data = '{"state": "on", "keyring": "fake-secret01"}'
140+ with patch ('cephadm.agent.http_req' ) as p :
141+ p .return_value = [], '{}' , 200
149142 self .getPage ("/host01/led/chassis" , method = "PATCH" , body = data , headers = [('Content-Type' , 'application/json' ),
150143 ('Content-Length' , str (len (data )))])
151144 self .assertStatus ('200 OK' )
@@ -170,70 +163,43 @@ def test_get_led_type_drive_missing_id(self):
170163 self .getPage ("/host01/led/drive" , method = "GET" )
171164 self .assertStatus ('400 Bad Request' )
172165
173- def test_get_led_type_chassis_answer_invalid_json (self ):
174- self .getPage ("/host01/led/chassis" , method = "GET" )
175- self .assertStatus ('503 Service Unavailable' )
166+ def test_get_led_url_error (self ):
167+ with patch ('cephadm.agent.http_req' ) as p :
168+ p .side_effect = URLError ('fake error' )
169+ self .getPage ("/host02/led/chassis" , method = "GET" )
170+ self .assertStatus ('502 Bad Gateway' )
176171
177- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
178- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
179- def test_get_led_type_chassis_answer_no_http_code (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
180- with patch ('cephadm.agent.AgentMessageThread.get_agent_response' ) as a :
181- a .return_value = '{"foo": "bar"}'
182- self .getPage ("/host01/led/chassis" , method = "GET" )
183- self .assertStatus ('503 Service Unavailable' )
184-
185- def test_get_led_status_not_200 (self ):
186- self .getPage ("/host01/led/chassis" , method = "GET" )
187- self .assertStatus ('503 Service Unavailable' )
188-
189- def test_get_led_key_error (self ):
190- self .getPage ("/host02/led/chassis" , method = "GET" )
191- self .assertStatus ('502 Bad Gateway' )
192-
193- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
194- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
195- def test_get_chassis_led_ok (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
196- with patch ('cephadm.agent.AgentMessageThread.get_agent_response' ) as a :
197- a .return_value = '{"http_code": 200}'
172+ def test_get_chassis_led_ok (self ):
173+ with patch ('cephadm.agent.http_req' , return_value = MagicMock ()) as p :
174+ p .return_value = [], '{}' , 200
198175 self .getPage ("/host01/led/chassis" , method = "GET" )
199176 self .assertStatus ('200 OK' )
200177
201- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
202- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
203- def test_get_drive_led_without_id (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
178+ def test_get_drive_led_without_id (self ):
204179 self .getPage ("/host01/led/drive" , method = "GET" )
205180 self .assertStatus ('400 Bad Request' )
206181
207- @patch ('cephadm.agent.AgentMessageThread.join' , return_value = MagicMock )
208- @patch ('cephadm.agent.AgentMessageThread.start' , return_value = MagicMock )
209- def test_get_drive_led_with_id (self , m_agent_msg_thread_start , m_agent_msg_thread_join ):
210- with patch ('cephadm.agent.AgentMessageThread.get_agent_response' ) as a :
211- a .return_value = '{"http_code": 200}'
182+ def test_get_drive_led_with_id (self ):
183+ with patch ('cephadm.agent.http_req' , return_value = MagicMock ()) as p :
184+ p .return_value = [], '{}' , 200
212185 self .getPage ("/host01/led/drive/123" , method = "GET" )
213186 self .assertStatus ('200 OK' )
214187
215- # def test_led_endpoint_unreachable(self):
216- # TestNodeProxyEndpoint.app.query_endpoint = MagicMock(side_effect=URLError("fake-error"))
217- # self.getPage("/host02/led", method="GET")
218- # calls = [call(addr='10.10.10.12',
219- # data=None,
220- # endpoint='/led',
221- # headers={},
222- # method='GET',
223- # port=8080,
224- # ssl_ctx=TestNodeProxyEndpoint.app.ssl_ctx)]
225- # self.assertStatus('502 Bad Gateway')
226- # assert TestNodeProxyEndpoint.app.query_endpoint.mock_calls == calls
227-
228188 def test_fullreport_with_valid_hostname (self ):
189+ # data = '{"cephx": {"name": "node-proxy.host01", "secret": "fake-secret01"}}'
190+ # self.getPage("/host02/fullreport", method="POST", body=data, headers=[('Content-Type', 'application/json'), ('Content-Length', str(len(data)))])
229191 self .getPage ("/host02/fullreport" , method = "GET" )
230192 self .assertStatus ('200 OK' )
231193
232194 def test_fullreport_no_hostname (self ):
195+ # data = '{"cephx": {"name": "node-proxy.host01", "secret": "fake-secret01"}}'
196+ # self.getPage("/fullreport", method="POST", body=data, headers=[('Content-Type', 'application/json'), ('Content-Length', str(len(data)))])
233197 self .getPage ("/fullreport" , method = "GET" )
234198 self .assertStatus ('200 OK' )
235199
236200 def test_fullreport_with_invalid_hostname (self ):
201+ # data = '{"cephx": {"name": "node-proxy.host03", "secret": "fake-secret03"}}'
202+ # self.getPage("/host03/fullreport", method="POST", body=data, headers=[('Content-Type', 'application/json'), ('Content-Length', str(len(data)))])
237203 self .getPage ("/host03/fullreport" , method = "GET" )
238204 self .assertStatus ('404 Not Found' )
239205
0 commit comments