def test_missing_agent(self): self.jc = JolokiaClient('http://google.com') resp = self._prepare_response(self.responses['method_not_allowed'], 405, False) self.jc.session.request = Mock(return_value=resp) resp = self.jc.get_attribute(mbean='java.lang:Memory', attribute='HeapMemoryUsage') assert resp['status'] == 405
def test_not_found(self): # Assuming there is no Jolokia agent at /foo self.jc = JolokiaClient('http://localhost:8080/foo') resp = self._prepare_response(self.responses['valid_exec_response'], 404, False) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data.status_code == 404
def test_illegal_argument_exception(self): # Assuming a Jolokia agent at /jolokia self.jc = JolokiaClient('http://localhost:8080/jolokia/foo') resp = self._prepare_response(self.responses['illegal_argument'], 400, False) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data['status'] == 400 assert resp_data['error_type'] == 'java.lang.IllegalArgumentException'
def test_not_found(self): # Assuming there is no Jolokia agent at /foo self.jc = JolokiaClient('http://{0}:8080/foo'.format( self.jolokia_host)) if self.mock: resp = self._prepare_response(self.responses['generic_404_page'], 404, False) LOGGER.debug(resp) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) LOGGER.debug(resp_data) else: resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data.status_code == 404
class TestGetAttribute(TestCase): def __init__(self, *args, **kwargs): super(TestGetAttribute, self).__init__(*args, **kwargs) self.jc = JolokiaClient('http://localhost:8080/jolokia') def test_valid_request(self): setattr(self.jc.session, 'request', mock_get_heap_memory_usage) resp_data = self.jc.get_attribute(mbean='java.lang:Memory', attribute='HeapMemoryUsage') assert resp_data['status'] == 200 assert type(resp_data['value']) is int def test_empty_request_body(self): pytest.raises(IllegalArgumentException, self.jc.get_attribute) def test_valid_bulk_request(self, *args, **kwargs): setattr(self.jc.session, 'request', mock_bulk_read) attributes = ['HeapMemoryUsage', 'NonHeapMemoryUsage'] resp_data = self.jc.get_attribute(mbean='java.lang:Memory', attribute=attributes) assert type(resp_data) is list assert len(resp_data) == 2 def test_missing_mbean(self): kwargs = {'attribute': 'HeapMemoryUsage'} pytest.raises(IllegalArgumentException, self.jc.get_attribute, **kwargs) def test_missing_attribute(self): kwargs = {'mbean': 'java.lang:type=Memory'} pytest.raises(IllegalArgumentException, self.jc.get_attribute, **kwargs)
class TestExecute(JolokiaTestCase): def test_valid_execute(self): resp = self._prepare_response(self.responses['valid_exec_response'], 200, True) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data['status'] == 200 assert isinstance(resp_data['value'], list) def test_empty_body(self): pytest.raises(IllegalArgumentException, self.jc.execute) def test_not_found(self): # Assuming there is no Jolokia agent at /foo self.jc = JolokiaClient('http://localhost:8080/foo') resp = self._prepare_response(self.responses['valid_exec_response'], 404, False) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data.status_code == 404 def test_illegal_argument_exception(self): # Assuming a Jolokia agent at /jolokia self.jc = JolokiaClient('http://localhost:8080/jolokia/foo') resp = self._prepare_response(self.responses['illegal_argument'], 400, False) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data['status'] == 400 assert resp_data['error_type'] == 'java.lang.IllegalArgumentException'
class TestExecute(JolokiaTestCase): def test_valid_execute(self): if self.mock: resp = self._prepare_response( self.responses['valid_exec_response'], 200, True) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) else: resp_data = self.jc.execute(**self.requests['valid_exec']) LOGGER.debug(self.requests['valid_exec']) assert resp_data['status'] == 200 assert isinstance(resp_data['value'], list) def test_empty_body(self): pytest.raises(IllegalArgumentException, self.jc.execute) def test_not_found(self): # Assuming there is no Jolokia agent at /foo self.jc = JolokiaClient('http://{0}:8080/foo'.format( self.jolokia_host)) if self.mock: resp = self._prepare_response(self.responses['generic_404_page'], 404, False) LOGGER.debug(resp) self.jc.session.request = Mock(return_value=resp) resp_data = self.jc.execute(**self.requests['valid_exec']) LOGGER.debug(resp_data) else: resp_data = self.jc.execute(**self.requests['valid_exec']) assert resp_data.status_code == 404
class TestExecute(TestCase): def __init__(self, *args, **kwargs): super(TestExecute, self).__init__(*args, **kwargs) self.jc = JolokiaClient('http://localhost:8080/jolokia') def test_valid_execute(self): setattr(self.jc.session, 'request', mock_valid_exec) resp_data = self.jc.execute(**VALID_EXEC) assert resp_data['status'] == 200 assert type(resp_data['value']) is list def test_empty_body(self): pytest.raises(IllegalArgumentException, self.jc.execute)
def setUp(self): try: self.jolokia_host = os.environ['JOLOKIA_HOST'] except KeyError: self.mock = True self.jolokia_host = 'localhost' try: self.agent_version = os.environ['JOLOKIA_AGENT_VERSION'] except KeyError: self.agent_version = '1.5.0' try: self.protocol_version = os.environ['JOLOKIA_PROTOCOL_VERSION'] except KeyError: self.protocol_version = '7.2' self.jc = JolokiaClient('http://{0}:8080/jolokia'.format( self.jolokia_host))
class TestSearch(TestCase): def __init__(self, *args, **kwargs): super(TestSearch, self).__init__(*args, **kwargs) self.jc = JolokiaClient('http://localhost:8080/jolokia') def test_valid_search(self): resp_json = json.dumps(VALID_SEARCH_RESPONSE) resp = JolokiaResponse() setattr(JolokiaResponse, 'content', resp_json.encode('utf-8')) resp.status_code = 200 setattr(JolokiaResponse, 'ok', True) mock = Mock(return_value=resp) mock.__name__ = 'test_valid_search' setattr(self.jc.session, 'request', mock) resp_data = self.jc.search(**VALID_SEARCH) assert resp_data['status'] == 200 assert type(resp_data['value']) is list
class JolokiaTestCase(TestCase): @classmethod def setUpClass(cls): f_requests = open('tests/fixtures/requests.json', 'r') cls.requests = json.load(f_requests) f_requests.close() f_responses = open('tests/fixtures/responses.json', 'r') cls.responses = json.load(f_responses) f_responses.close() def setUp(self): self.jc = JolokiaClient('http://localhost:8080/jolokia-1.0.0') def _prepare_response(self, response_json, status_code, ok): resp_string = json.dumps(response_json) resp_obj = JolokiaResponse() setattr(JolokiaResponse, 'content', resp_string.encode('utf-8')) resp_obj.status_code = status_code setattr(JolokiaResponse, 'ok', ok) return resp_obj def test_missing_agent(self): self.jc = JolokiaClient('http://google.com') resp = self._prepare_response(self.responses['method_not_allowed'], 405, False) self.jc.session.request = Mock(return_value=resp) resp = self.jc.get_attribute(mbean='java.lang:Memory', attribute='HeapMemoryUsage') assert resp['status'] == 405
def __init__(self, *args, **kwargs): super(TestGetAttribute, self).__init__(*args, **kwargs) self.jc = JolokiaClient('http://localhost:8080/jolokia')
def setUp(self): self.jc = JolokiaClient('http://localhost:8080/jolokia-1.0.0')
class TestSetAttribute(TestCase): def __init__(self, *args, **kwargs): super(TestSetAttribute, self).__init__(*args, **kwargs) self.jc = JolokiaClient('http://localhost:8080/jolokia') def test_empty_body(self): pytest.raises(IllegalArgumentException, self.jc.set_attribute) def test_missing_mbean(self): args = ['Verbose', True] pytest.raises(IllegalArgumentException, self.jc.set_attribute, *args) def test_missing_attribute(self): kwargs = {'mbean': 'java.lang:type=ClassLoading', 'value': True} pytest.raises(IllegalArgumentException, self.jc.set_attribute, **kwargs) def test_missing_value(self): kwargs = { 'mbean': 'java.lang:type=ClassLoading', 'attribute': 'Verbose' } pytest.raises(IllegalArgumentException, self.jc.set_attribute, **kwargs) def test_valid_request(self): setattr(self.jc.session, 'request', mock_valid_write) resp_data = self.jc.set_attribute('java.lang:type=ClassLoading', 'Verbose', True) assert not resp_data['value'] def test_bulk_write(self): attr_map = {'HistoryMaxEntries': 20, 'MaxDebugEntries': 200} setattr(self.jc.session, 'request', mock_bulk_write) resp_data = self.jc.set_attribute( mbean='jolokia:type=Config', attribute=['HistoryMaxEntries', 'MaxDebugEntries'], value=attr_map) for obj in resp_data: assert obj['status'] == 200 def test_incorrect_attribute_type(self): kwargs = { 'value': { 'foo': 1, 'bar': 2 }, 'attribute': 'baz', 'mbean': 'bang' } pytest.raises(IllegalArgumentException, self.jc.set_attribute, **kwargs)