def test_render(self): expected_location = 'http://mynode:1234/foo/bar' expected_server = 'vault-redirector/%s/TwistedWeb/16.1.0' % _VERSION if sys.version_info[0] < 3: type(self.mock_request).uri = '/foo/bar' type(self.mock_request).path = '/foo/bar' else: # in py3 these are byte literals type(self.mock_request).uri = b'/foo/bar' type(self.mock_request).path = b'/foo/bar' self.mock_request.reset_mock() with patch('%s.logger' % pbm) as mock_logger: resp = self.cls.render(self.mock_request) assert self.mock_request.mock_calls == [ call.setHeader('server', expected_server), call.setResponseCode(307), call.setHeader('Location', expected_location), call.setHeader("Content-Type", "text/html; charset=UTF-8") ] assert resp == self.empty_resp assert mock_logger.mock_calls == [ call.info('RESPOND 307 to %s for %s%s request for %s from %s:%s', expected_location, '', 'GET', '/foo/bar', '1.2.3.4', 12345) ]
def test_healthcheck_no_active_log_disabled(self): type(self.mock_redir).active_node_ip_port = None type(self.mock_redir).log_enabled = False if sys.version_info[0] < 3: type(self.mock_request).uri = '/vault-redirector-health' type(self.mock_request).path = '/vault-redirector-health' expected = 'foobar' expected_msg = 'No Active Vault' else: # in py3 these are byte literals type(self.mock_request).uri = b'/vault-redirector-health' type(self.mock_request).path = b'/vault-redirector-health' expected = b'foobar' expected_msg = b'No Active Vault' with patch('%s.logger' % pbm) as mock_logger: with patch('%s.VaultRedirectorSite.status_response' % pbm) as mock_status: mock_status.return_value = 'foobar' resp = self.cls.healthcheck(self.mock_request) assert self.mock_request.mock_calls == [ call.setResponseCode(503, message=expected_msg), call.setHeader("Content-Type", "application/json") ] assert resp == expected assert mock_logger.mock_calls == []
def test_healthcheck_queued(self): type(self.mock_redir).log_enabled = True type(self.mock_request).queued = 1 if sys.version_info[0] < 3: type(self.mock_request).uri = '/vault-redirector-health' type(self.mock_request).path = '/vault-redirector-health' expected = 'foobar' expected_msg = 'OK' else: # in py3 these are byte literals type(self.mock_request).uri = b'/vault-redirector-health' type(self.mock_request).path = b'/vault-redirector-health' expected = b'foobar' expected_msg = b'OK' with patch('%s.logger' % pbm) as mock_logger: with patch('%s.VaultRedirectorSite.status_response' % pbm) as mock_status: mock_status.return_value = 'foobar' resp = self.cls.healthcheck(self.mock_request) assert self.mock_request.mock_calls == [ call.setResponseCode(200, message=expected_msg), call.setHeader("Content-Type", "application/json") ] assert resp == expected assert mock_logger.mock_calls == [ call.info( 'RESPOND %d for %s%s request for /vault-redirector-health' ' from %s:%s', 200, 'QUEUED ', 'GET', '1.2.3.4', 12345) ]
def _make_request( self, method, postpath, reqbody, resreadsreq, rescode, resbody): m_request = MagicMock(name='Request') m_request.method = method m_request.postpath = postpath if reqbody is None: readrv = '' elif reqbody == 'mangled JSON': readrv = reqbody else: readrv = json.dumps(reqbody, indent=2) m_request.content.read.return_value = readrv r = self.tar.render(m_request) self.assertEqual(r, server.NOT_DONE_YET) expected = [ call.setResponseCode(rescode), call.setHeader('Content-Type', 'application/json'), call.write(json.dumps(resbody, indent=2)), call.finish(), ] if resreadsreq: expected.insert(0, call.content.read()) check_mock(self, m_request, expected)
def test_healthcheck_no_active_log_disabled(self): type(self.mock_redir).active_node_ip_port = None type(self.mock_redir).log_enabled = False if sys.version_info[0] < 3: type(self.mock_request).uri = '/vault-redirector-health' type(self.mock_request).path = '/vault-redirector-health' expected = 'foobar' expected_msg = 'No Active Vault' else: # in py3 these are byte literals type(self.mock_request).uri = b'/vault-redirector-health' type(self.mock_request).path = b'/vault-redirector-health' expected = b'foobar' expected_msg = b'No Active Vault' with patch('%s.logger' % pbm) as mock_logger: with patch('%s.VaultRedirectorSite.status_response' % pbm ) as mock_status: mock_status.return_value = 'foobar' resp = self.cls.healthcheck(self.mock_request) assert self.mock_request.mock_calls == [ call.setResponseCode(503, message=expected_msg), call.setHeader("Content-Type", "application/json") ] assert resp == expected assert mock_logger.mock_calls == []
def test_healthcheck_queued(self): type(self.mock_redir).log_enabled = True type(self.mock_request).queued = 1 if sys.version_info[0] < 3: type(self.mock_request).uri = '/vault-redirector-health' type(self.mock_request).path = '/vault-redirector-health' expected = 'foobar' expected_msg = 'OK' else: # in py3 these are byte literals type(self.mock_request).uri = b'/vault-redirector-health' type(self.mock_request).path = b'/vault-redirector-health' expected = b'foobar' expected_msg = b'OK' with patch('%s.logger' % pbm) as mock_logger: with patch('%s.VaultRedirectorSite.status_response' % pbm ) as mock_status: mock_status.return_value = 'foobar' resp = self.cls.healthcheck(self.mock_request) assert self.mock_request.mock_calls == [ call.setResponseCode(200, message=expected_msg), call.setHeader("Content-Type", "application/json") ] assert resp == expected assert mock_logger.mock_calls == [ call.info('RESPOND %d for %s%s request for /vault-redirector-health' ' from %s:%s', 200, 'QUEUED ', 'GET', '1.2.3.4', 12345) ]
def test_render_log_disabled(self): expected_location = 'http://mynode:1234/foo/bar' expected_server = 'vault-redirector/%s/TwistedWeb/16.1.0' % _VERSION type(self.mock_request).uri = '/foo/bar' type(self.mock_request).path = '/foo/bar' self.mock_request.reset_mock() type(self.mock_redir).log_enabled = False with patch('%s.logger' % pbm) as mock_logger: resp = self.cls.render(self.mock_request) assert self.mock_request.mock_calls == [ call.setHeader('server', expected_server), call.setResponseCode(307), call.setHeader('Location', expected_location), call.setHeader("Content-Type", "text/html; charset=UTF-8") ] assert resp == self.empty_resp assert mock_logger.mock_calls == []
def test_render_503_log_disabled(self): type(self.mock_redir).active_node_ip_port = None expected_server = 'vault-redirector/%s/TwistedWeb/16.1.0' % _VERSION type(self.mock_request).uri = '/foo/bar' type(self.mock_request).path = '/foo/bar' self.mock_request.reset_mock() type(self.mock_redir).log_enabled = False with patch('%s.logger' % pbm) as mock_logger: resp = self.cls.render(self.mock_request) assert self.mock_request.mock_calls == [ call.setHeader('server', expected_server) ] assert isinstance(resp, resource.ErrorPage) assert resp.code == SERVICE_UNAVAILABLE assert resp.brief == 'No Active Node' assert resp.detail == 'No active Vault leader could be determined ' \ 'from Consul API' assert mock_logger.mock_calls == []
def test_render_healthcheck(self): expected_server = 'vault-redirector/%s/TwistedWeb/16.1.0' % _VERSION if sys.version_info[0] < 3: type(self.mock_request).uri = '/vault-redirector-health' type(self.mock_request).path = '/vault-redirector-health' else: # in py3 these are byte literals type(self.mock_request).uri = b'/vault-redirector-health' type(self.mock_request).path = b'/vault-redirector-health' self.mock_request.reset_mock() with patch('%s.logger' % pbm) as mock_logger: with patch('%s.VaultRedirectorSite.healthcheck' % pbm) as mock_hc: mock_hc.return_value = 'myresult' resp = self.cls.render(self.mock_request) assert self.mock_request.mock_calls == [ call.setHeader('server', expected_server) ] assert resp == 'myresult' assert mock_logger.mock_calls == [] assert mock_hc.mock_calls == [call(self.mock_request)]
def test_render_503(self): type(self.mock_redir).active_node_ip_port = None expected_server = 'vault-redirector/%s/TwistedWeb/16.1.0' % _VERSION type(self.mock_request).uri = '/foo/bar' type(self.mock_request).path = '/foo/bar' self.mock_request.reset_mock() with patch('%s.logger' % pbm) as mock_logger: resp = self.cls.render(self.mock_request) assert self.mock_request.mock_calls == [ call.setHeader('server', expected_server) ] assert isinstance(resp, resource.ErrorPage) assert resp.code == SERVICE_UNAVAILABLE assert resp.brief == 'No Active Node' assert resp.detail == 'No active Vault leader could be determined ' \ 'from Consul API' assert mock_logger.mock_calls == [ call.warning('RESPOND 503 for %s%s request for %s from %s:%s', '', 'GET', '/foo/bar', '1.2.3.4', 12345) ]