def test_post_updated_nodes_handles_conflict(self): # If a worker started processing a node late, it might try to post # an updated list with an out-of-date definition. It gets a CONFLICT in # that case, which should be handled. client = self.fake_client() name = factory.make_name('tag') rack_id = factory.make_name('rack') right_tag_defintion = factory.make_name('//') wrong_tag_definition = factory.make_name('//') content = ("Definition supplied '%s' doesn't match" " current definition '%s'" % (wrong_tag_definition, right_tag_defintion)) err = urllib.error.HTTPError('url', http.client.CONFLICT, content, {}, None) post_mock = MagicMock(side_effect=err) self.patch(client, 'post', post_mock) result = tags.post_updated_nodes(client, rack_id, name, wrong_tag_definition, ['add-system-id'], ['remove-1', 'remove-2']) # self.assertEqual({'added': 1, 'removed': 2}, result) url = '/MAAS/api/2.0/tags/%s/' % (name, ) self.assertEqual({}, result) post_mock.assert_called_once_with(url, op='update_nodes', as_json=True, rack_controller=rack_id, definition=wrong_tag_definition, add=['add-system-id'], remove=['remove-1', 'remove-2'])
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_post_updated_nodes_calls_correct_api_and_parses_result(self): client, uuid = self.fake_cached_knowledge() content = b'{"added": 1, "removed": 2}' response = make_response(httplib.OK, content, 'application/json') post_mock = MagicMock(return_value=response) self.patch(client, 'post', post_mock) name = factory.make_name('tag') tag_definition = factory.make_name('//') result = tags.post_updated_nodes(client, name, tag_definition, uuid, ['add-system-id'], ['remove-1', 'remove-2']) self.assertEqual({'added': 1, 'removed': 2}, result) url = '/api/1.0/tags/%s/' % (name, ) post_mock.assert_called_once_with(url, op='update_nodes', as_json=True, nodegroup=uuid, definition=tag_definition, add=['add-system-id'], remove=['remove-1', 'remove-2'])