def test_get_details_calls_correct_api_and_parses_result(self): client = self.fake_client() data = { "system-1": { "lshw": b"<lshw><data1 /></lshw>", "lldp": b"<lldp><data1 /></lldp>", }, "system-2": { "lshw": b"<lshw><data2 /></lshw>", "lldp": b"<lldp><data2 /></lldp>", }, } response1 = factory.make_response( http.client.OK, bson.BSON.encode(data["system-1"]), "application/bson", ) response2 = factory.make_response( http.client.OK, bson.BSON.encode(data["system-2"]), "application/bson", ) get = self.patch(client, "get") get.side_effect = [response1, response2] result = tags.get_details_for_nodes(client, ["system-1", "system-2"]) self.assertEqual(data, result) self.assertThat( get, MockCallsMatch( call("/MAAS/api/2.0/nodes/system-1/", op="details"), call("/MAAS/api/2.0/nodes/system-2/", op="details"), ), )
def test_process_node_tags_integration(self): self.useFixture( ClusterConfigurationFixture( maas_url=factory.make_simple_http_url())) get_hw_system1 = factory.make_response( http.client.OK, bson.BSON.encode({"lshw": b"<node />"}), "application/bson", ) get_hw_system2 = factory.make_response( http.client.OK, bson.BSON.encode({"lshw": b"<not-node />"}), "application/bson", ) mock_get = self.patch(MAASClient, "get") mock_get.side_effect = [get_hw_system1, get_hw_system2] mock_post = self.patch(MAASClient, "post") mock_post.return_value = factory.make_response( http.client.OK, b'{"added": 1, "removed": 1}', "application/json") tag_name = factory.make_name("tag") tag_definition = "//lshw:node" tag_nsmap = {"lshw": "lshw"} rack_id = factory.make_name("rack") tags.process_node_tags( rack_id, [{ "system_id": "system-id1" }, { "system_id": "system-id2" }], tag_name, tag_definition, tag_nsmap, self.fake_client(), ) tag_url = "/MAAS/api/2.0/tags/%s/" % (tag_name, ) self.assertThat( mock_post, MockCalledOnceWith( tag_url, as_json=True, op="update_nodes", rack_controller=rack_id, definition=tag_definition, add=["system-id1"], remove=["system-id2"], ), )
def test_post_updated_nodes_calls_correct_api_and_parses_result(self): client = self.fake_client() content = b'{"added": 1, "removed": 2}' response = factory.make_response(http.client.OK, content, "application/json") post_mock = MagicMock(return_value=response) self.patch(client, "post", post_mock) name = factory.make_name("tag") rack_id = factory.make_name("rack") tag_definition = factory.make_name("//") result = tags.post_updated_nodes( client, rack_id, name, tag_definition, ["add-system-id"], ["remove-1", "remove-2"], ) self.assertEqual({"added": 1, "removed": 2}, result) url = "/MAAS/api/2.0/tags/%s/" % (name, ) post_mock.assert_called_once_with( url, op="update_nodes", as_json=True, rack_controller=rack_id, definition=tag_definition, add=["add-system-id"], remove=["remove-1", "remove-2"], )
def test_process_OK_response_with_JSON_content(self): data = {"abc": 123} response = factory.make_response( http.client.OK, json.dumps(data).encode("ascii"), "application/json", ) self.assertEqual(data, tags.process_response(response))
def test_process_node_tags_integration(self): self.useFixture( ClusterConfigurationFixture( maas_url=factory.make_simple_http_url())) get_hw_system1 = factory.make_response( http.client.OK, bson.BSON.encode({'lshw': b'<node />'}), 'application/bson', ) get_hw_system2 = factory.make_response( http.client.OK, bson.BSON.encode({'lshw': b'<not-node />'}), 'application/bson', ) mock_get = self.patch(MAASClient, 'get') mock_get.side_effect = [get_hw_system1, get_hw_system2] mock_post = self.patch(MAASClient, 'post') mock_post.return_value = factory.make_response( http.client.OK, b'{"added": 1, "removed": 1}', 'application/json', ) tag_name = factory.make_name('tag') tag_definition = '//lshw:node' tag_nsmap = {"lshw": "lshw"} rack_id = factory.make_name('rack') tags.process_node_tags(rack_id, [{ "system_id": "system-id1" }, { "system_id": "system-id2" }], tag_name, tag_definition, tag_nsmap, self.fake_client()) tag_url = '/MAAS/api/2.0/tags/%s/' % (tag_name, ) self.assertThat( mock_post, MockCalledOnceWith(tag_url, as_json=True, op='update_nodes', rack_controller=rack_id, definition=tag_definition, add=['system-id1'], remove=['system-id2']))
def test_process_not_OK_response(self): response = factory.make_response(http.client.NOT_FOUND, b"", "application/json") response.url = factory.make_string() error = self.assertRaises(urllib.error.HTTPError, tags.process_response, response) self.assertThat( error, MatchesStructure.byEquality(url=response.url, code=response.code, msg="Not Found, expected 200 OK", headers=response.headers, fp=response.fp))
def test_post_updated_nodes_calls_correct_api_and_parses_result(self): client = self.fake_client() content = b'{"added": 1, "removed": 2}' response = factory.make_response(http.client.OK, content, 'application/json') post_mock = MagicMock(return_value=response) self.patch(client, 'post', post_mock) name = factory.make_name('tag') rack_id = factory.make_name('rack') tag_definition = factory.make_name('//') result = tags.post_updated_nodes(client, rack_id, name, tag_definition, ['add-system-id'], ['remove-1', 'remove-2']) self.assertEqual({'added': 1, 'removed': 2}, result) url = '/MAAS/api/2.0/tags/%s/' % (name, ) post_mock.assert_called_once_with(url, op='update_nodes', as_json=True, rack_controller=rack_id, definition=tag_definition, add=['add-system-id'], remove=['remove-1', 'remove-2'])
def test_process_OK_response_with_other_content(self): data = factory.make_bytes() response = factory.make_response(http.client.OK, data, "application/octet-stream") self.assertEqual(data, tags.process_response(response))
def test_process_OK_response_with_BSON_content(self): data = {"abc": 123} response = factory.make_response(http.client.OK, bson.BSON.encode(data), "application/bson") self.assertEqual(data, tags.process_response(response))