def test_patch_handler(self): with h.MultiHandlerSingleThreadHTTPServer() as server: host, port = server.server_address print(host, port) # Create Variables to export to server int_var = util.MutableVariable(123) bool_var = util.MutableVariable(True) string_var = util.MutableVariable('Test Case') list_var = util.MutableVariable([1, 2, 3]) dict_var = util.MutableVariable( {'key1': 'value1', 'key2': 'value2'}) # Create a prober on path /probes/vars prober = h.ProbeResource('/vars') prober.add_probe('int_var', int_var) prober.add_probe('bool_var', bool_var) prober.add_probe('string_var', string_var) prober.add_probe('list_var', list_var) prober.add_probe('dict_var', dict_var) # Export prober to the server h.export_probe_resource_to_server(server, prober) # Start serving server.start_serving_async() # Send PUT request to /probes/vars connection = http.client.HTTPConnection(host, port) connection.request(h.HTTPMethod.PATCH.name, "/probes/vars", body="{\"bool_var\":{\"_value\":false,\"py\/object\":\"omnilib.util.container.MutableVariable\"},\"dict_var\":{\"_value\":{\"key3\":\"value3\",\"key4\":\"value4\"},\"py\/object\":\"omnilib.util.container.MutableVariable\"},\"int_var\":{\"_value\":456,\"py\/object\":\"omnilib.util.container.MutableVariable\"},\"list_var\":{\"_value\":[4,5,6],\"py\/object\":\"omnilib.util.container.MutableVariable\"},\"string_var\":{\"_value\":\"Case Test\",\"py\/object\":\"omnilib.util.container.MutableVariable\"}}") response = connection.getresponse() self.assertEqual(response.status, http.HTTPStatus.OK) # Validate response self.assertEqual( prober.get_probe_value('int_var'), 456) self.assertEqual( prober.get_probe_value('bool_var'), False) self.assertEqual(prober.get_probe_value( 'string_var'), string_var) self.assertEqual( prober.get_probe_value('list_var'), [4, 5, 6]) self.assertEqual( prober.get_probe_value('dict_var'), {'key3': 'value3', 'key4': 'value4'}) # Close connection connection.close()
def test_set_probe_value(self): prober = h.ProbeResource('prober') int_var = util.MutableVariable(123) int_var_desc = 'Integer Variable' prober.add_probe('int_var', int_var, desc=int_var_desc) new_int_var = util.MutableVariable(456) prober.set_probe_value('int_var', new_int_var) self.assertEqual(prober.get_probe_value('int_var'), new_int_var) with self.assertRaises(ValueError): prober.set_probe_value('int_var', 123)
def test_getters(self): prober = h.ProbeResource('prober') int_var = util.MutableVariable(123) int_var_desc = 'Test Integer Variable' bool_var = util.MutableVariable(False) bool_var_desc = 'Test Boolean Variable' prober.add_probe('int_var', int_var, desc=int_var_desc) prober.add_probe('bool_var', bool_var, desc=bool_var_desc) self.assertEqual(sorted(prober.get_probe_labels()), sorted(['int_var', 'bool_var'])) self.assertDictEqual(prober.get_probe_descriptions(), { 'int_var': int_var_desc, 'bool_var': bool_var_desc}) self.assertDictEqual(prober.get_probe_values(), { 'int_var': int_var, 'bool_var': bool_var})
def test_get_handler(self): with h.MultiHandlerSingleThreadHTTPServer() as server: host, port = server.server_address # Create Variables to export to server mutvar_var = util.MutableVariable(util.MutableVariable(123)) int_var = util.MutableVariable(32416190071) bool_var = util.MutableVariable(True) string_var = util.MutableVariable( '9bd3aaa3-cc7c-4aac-a6df-2dc5ae1e68a2') list_var = util.MutableVariable( [15487039, '2c7e5965-e6d6-412c-93f0-b652687ccd12', 105653]) dict_var = util.MutableVariable( {'key1': '08bc2431-8ed6-4bcf-a3a9-79bef3e00c4d', 'key2': '0999f3ab-7521-446d-94ce-f659dea8183a'}) # Create a prober on path /probers/vars prober = h.ProbeResource( '/vars', desc='Test Probe Resource Description') prober.add_probe('mutvar_var', mutvar_var) prober.add_probe('int_var', int_var) prober.add_probe('bool_var', bool_var) prober.add_probe('string_var', string_var) prober.add_probe('list_var', list_var) prober.add_probe('dict_var', dict_var) # Export prober to the server h.export_probe_resource_to_server(server, prober) # Start serving server.start_serving_async() # Read GET response at /probers/vars connection = http.client.HTTPConnection(host, port) connection.request(h.HTTPMethod.GET.name, "/probes/vars") response = connection.getresponse() self.assertEqual(response.status, http.HTTPStatus.OK) content_length = int(response.getheader('Content-Length')) response_string = response.read(content_length) # Validate response self.assertTrue(b'32416190071' in response_string) self.assertTrue( b'9bd3aaa3-cc7c-4aac-a6df-2dc5ae1e68a2' in response_string) self.assertTrue(b'15487039' in response_string) self.assertTrue( b'2c7e5965-e6d6-412c-93f0-b652687ccd12' in response_string) self.assertTrue(b'105653' in response_string) self.assertTrue( b'08bc2431-8ed6-4bcf-a3a9-79bef3e00c4d' in response_string) self.assertTrue( b'0999f3ab-7521-446d-94ce-f659dea8183a' in response_string) # Close connection connection.close()
def test_set_value(self): var = util.MutableVariable(1) self.assertEqual(var.get_value(), 1) var.set_value(2) self.assertEqual(var.get_value(), 2)
def test_get_value(self): self.assertEqual(util.MutableVariable(1).get_value(), 1)
def test_iterator(self): test_list = [1, 2, 3] var = util.MutableVariable([1, 2, 3]) for i in range(len(var)): self.assertEqual(test_list[i], var[i])
def test_bool(self): self.assertTrue(util.MutableVariable(True)) self.assertFalse(util.MutableVariable(False)) self.assertFalse(util.MutableVariable(None))
def test_rich_comparison_methods(self): # Test __eq__ self.assertEqual(util.MutableVariable(1), 1) self.assertTrue(util.MutableVariable(1) == 1) self.assertFalse(util.MutableVariable(1) == 2) self.assertEqual(util.MutableVariable("Test"), "Test") self.assertTrue(util.MutableVariable("Test") == "Test") self.assertFalse(util.MutableVariable("Case") == "Test") self.assertEqual(util.MutableVariable(1), util.MutableVariable(1)) self.assertTrue(util.MutableVariable(1) == util.MutableVariable(1)) self.assertFalse(util.MutableVariable(1) == util.MutableVariable(2)) # Test __lt__, __gt__ self.assertTrue(util.MutableVariable(1) < util.MutableVariable(2)) self.assertTrue(util.MutableVariable(1) > util.MutableVariable(0)) self.assertFalse(util.MutableVariable(1) > util.MutableVariable(2)) self.assertFalse(util.MutableVariable(1) < util.MutableVariable(0)) # Test __ge__, __le__ self.assertTrue(util.MutableVariable(1) <= util.MutableVariable(2)) self.assertTrue(util.MutableVariable(1) >= util.MutableVariable(0)) self.assertFalse(util.MutableVariable(1) >= util.MutableVariable(2)) self.assertFalse(util.MutableVariable(1) <= util.MutableVariable(0)) # Test __ne__ self.assertTrue(util.MutableVariable(1) != util.MutableVariable(2))
def test_reversed(self): var = util.MutableVariable([1, 2, 3]) for t in zip(reversed(var), reversed([1, 2, 3])): self.assertEqual(t[0], t[1])
def test_getitem(self): var = util.MutableVariable([1, 2, 3]) self.assertEqual(var[0], 1) self.assertEqual(var[1], 2) self.assertEqual(var[2], 3)
def test_len(self): var = util.MutableVariable("test") self.assertEqual(len("test"), len(var)) var = util.MutableVariable([1, 2, 3]) self.assertEqual(len(var), 3)