2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from __future__ import absolute_import, print_function
7 from future import standard_library
13 import arvnodeman.status as status
14 import arvnodeman.config as config
17 class TestServer(object):
18 def __init__(self, management_token=None):
19 self.mgmt_token = management_token
22 cfg = config.NodeManagerConfig()
23 cfg.set('Manage', 'port', '0')
24 cfg.set('Manage', 'address', '127.0.0.1')
25 if self.mgmt_token != None:
26 cfg.set('Manage', 'ManagementToken', self.mgmt_token)
27 self.srv = status.Server(cfg)
29 addr, port = self.srv.server_address
30 self.srv_base = 'http://127.0.0.1:'+str(port)
33 def __exit__(self, exc_type, exc_value, traceback):
36 def get_status_response(self):
37 return requests.get(self.srv_base+'/status.json')
40 return self.get_status_response().json()
42 def get_healthcheck_ping(self, auth_header=None):
44 if auth_header != None:
45 headers['Authorization'] = auth_header
46 return requests.get(self.srv_base+'/_health/ping', headers=headers)
48 class StatusServerUpdates(unittest.TestCase):
49 def test_updates(self):
50 with TestServer() as srv:
52 status.tracker.update({'nodes_'+str(n): n})
53 r = srv.get_status_response()
54 self.assertEqual(200, r.status_code)
55 self.assertEqual('application/json', r.headers['content-type'])
57 self.assertEqual(n, resp['nodes_'+str(n)])
58 self.assertEqual(1, resp['nodes_1'])
61 class StatusServerDisabled(unittest.TestCase):
62 def test_config_disabled(self):
63 cfg = config.NodeManagerConfig()
64 cfg.set('Manage', 'port', '-1')
65 cfg.set('Manage', 'address', '127.0.0.1')
66 self.srv = status.Server(cfg)
68 self.assertFalse(self.srv.enabled)
69 self.assertFalse(getattr(self.srv, '_thread', False))
71 class HealthcheckPing(unittest.TestCase):
72 def test_ping_disabled(self):
73 with TestServer() as srv:
74 r = srv.get_healthcheck_ping()
75 self.assertEqual(404, r.status_code)
77 def test_ping_no_auth(self):
78 with TestServer('configuredmanagementtoken') as srv:
79 r = srv.get_healthcheck_ping()
80 self.assertEqual(401, r.status_code)
82 def test_ping_bad_auth_format(self):
83 with TestServer('configuredmanagementtoken') as srv:
84 r = srv.get_healthcheck_ping('noBearer')
85 self.assertEqual(403, r.status_code)
87 def test_ping_bad_auth_token(self):
88 with TestServer('configuredmanagementtoken') as srv:
89 r = srv.get_healthcheck_ping('Bearer badtoken')
90 self.assertEqual(403, r.status_code)
92 def test_ping_success(self):
93 with TestServer('configuredmanagementtoken') as srv:
94 r = srv.get_healthcheck_ping('Bearer configuredmanagementtoken')
95 self.assertEqual(200, r.status_code)
96 self.assertEqual('application/json', r.headers['content-type'])
98 self.assertEqual('{"health": "OK"}', json.dumps(resp))