Beispiel #1
0
 def test_process_node_tags_integration(self):
     self.set_secrets()
     get_nodes = FakeMethod(result=make_response(
         httplib.OK,
         b'["system-id1", "system-id2"]',
         'application/json',
     ))
     post_hw_details = FakeMethod(result=make_response(
         httplib.OK,
         bson.BSON.encode({
             'system-id1': {
                 'lshw': b'<node />'
             },
             'system-id2': {
                 'lshw': b'<not-node />'
             },
         }),
         'application/bson',
     ))
     get_fake = MultiFakeMethod([get_nodes])
     post_update_fake = FakeMethod(result=make_response(
         httplib.OK,
         b'{"added": 1, "removed": 1}',
         'application/json',
     ))
     post_fake = MultiFakeMethod([post_hw_details, post_update_fake])
     self.patch(MAASClient, 'get', get_fake)
     self.patch(MAASClient, 'post', post_fake)
     tag_name = factory.make_name('tag')
     nodegroup_uuid = get_recorded_nodegroup_uuid()
     tag_definition = '//lshw:node'
     tag_nsmap = {"lshw": "lshw"}
     tags.process_node_tags(tag_name, tag_definition, tag_nsmap=tag_nsmap)
     nodegroup_url = '/api/1.0/nodegroups/%s/' % (nodegroup_uuid, )
     tag_url = '/api/1.0/tags/%s/' % (tag_name, )
     self.assertEqual([((nodegroup_url, ), {
         'op': 'list_nodes'
     })], get_nodes.calls)
     self.assertEqual([
         ((nodegroup_url, ), {
             'op': 'details',
             'system_ids': ['system-id1', 'system-id2'],
         }),
     ], post_hw_details.calls)
     self.assertEqual([
         ((tag_url, ), {
             'as_json': True,
             'op': 'update_nodes',
             'nodegroup': nodegroup_uuid,
             'definition': tag_definition,
             'add': ['system-id1'],
             'remove': ['system-id2'],
         }),
     ], post_update_fake.calls)
Beispiel #2
0
 def test_call_calls_all_given_methods(self):
     methods = FakeMethod(), FakeMethod()
     method = MultiFakeMethod(methods)
     call1_args = "input 1"
     call2_args = "input 2"
     method(call1_args)
     method(call2_args)
     self.assertEqual(
         [[('input 1',)], [('input 2',)]],
         [methods[0].extract_args(), methods[1].extract_args()])
Beispiel #3
0
 def test_rndc_command_can_be_retried(self):
     # The rndc_command task can be retried.
     # Simulate a temporary failure.
     number_of_failures = RNDC_COMMAND_MAX_RETRY
     raised_exception = CalledProcessError(factory.make_name('exception'),
                                           random.randint(100, 200))
     simulate_failures = MultiFakeMethod(
         [FakeMethod(failure=raised_exception)] * number_of_failures +
         [FakeMethod()])
     self.patch(tasks, 'execute_rndc_command', simulate_failures)
     command = factory.getRandomString()
     result = rndc_command.delay(command, retry=True)
     self.assertTrue(result.successful())
Beispiel #4
0
 def test_rndc_command_is_retried_a_limited_number_of_times(self):
     # If we simulate RNDC_COMMAND_MAX_RETRY + 1 failures, the
     # task fails.
     number_of_failures = RNDC_COMMAND_MAX_RETRY + 1
     raised_exception = utils.ExternalProcessError(
         random.randint(100, 200), factory.make_name('exception'))
     simulate_failures = MultiFakeMethod(
         [FakeMethod(failure=raised_exception)] * number_of_failures +
         [FakeMethod()])
     self.patch(tasks, 'execute_rndc_command', simulate_failures)
     command = factory.getRandomString()
     self.assertRaises(utils.ExternalProcessError,
                       rndc_command.delay,
                       command,
                       retry=True)
Beispiel #5
0
 def test_update_node_tags_can_be_retried(self):
     self.set_secrets()
     # The update_node_tags task can be retried.
     # Simulate a temporary failure.
     number_of_failures = UPDATE_NODE_TAGS_MAX_RETRY
     raised_exception = MissingCredentials(factory.make_name('exception'),
                                           random.randint(100, 200))
     simulate_failures = MultiFakeMethod(
         [FakeMethod(failure=raised_exception)] * number_of_failures +
         [FakeMethod()])
     self.patch(tags, 'process_node_tags', simulate_failures)
     tag = factory.getRandomString()
     result = update_node_tags.delay(tag,
                                     '//node',
                                     tag_nsmap=None,
                                     retry=True)
     self.assertTrue(result.successful())
Beispiel #6
0
 def test_update_node_tags_is_retried_a_limited_number_of_times(self):
     self.set_secrets()
     # If we simulate UPDATE_NODE_TAGS_MAX_RETRY + 1 failures, the
     # task fails.
     number_of_failures = UPDATE_NODE_TAGS_MAX_RETRY + 1
     raised_exception = MissingCredentials(factory.make_name('exception'),
                                           random.randint(100, 200))
     simulate_failures = MultiFakeMethod(
         [FakeMethod(failure=raised_exception)] * number_of_failures +
         [FakeMethod()])
     self.patch(tags, 'process_node_tags', simulate_failures)
     tag = factory.getRandomString()
     self.assertRaises(MissingCredentials,
                       update_node_tags.delay,
                       tag,
                       '//node',
                       tag_nsmap=None,
                       retry=True)
Beispiel #7
0
 def test_process_node_tags_requests_details_in_batches(self):
     client = object()
     uuid = factory.make_name('nodegroupuuid')
     self.patch(tags, 'get_cached_knowledge',
                MagicMock(return_value=(client, uuid)))
     self.patch(tags, 'get_nodes_for_node_group',
                MagicMock(return_value=['a', 'b', 'c']))
     fake_first = FakeMethod(
         result={
             'a': {
                 'lshw': b'<node />'
             },
             'c': {
                 'lshw': b'<parent><node /></parent>'
             },
         })
     fake_second = FakeMethod(result={
         'b': {
             'lshw': b'<not-node />'
         },
     })
     self.patch(tags, 'get_details_for_nodes',
                MultiFakeMethod([fake_first, fake_second]))
     self.patch(tags, 'post_updated_nodes')
     tag_name = factory.make_name('tag')
     tag_definition = '//node'
     tags.process_node_tags(tag_name,
                            tag_definition,
                            tag_nsmap=None,
                            batch_size=2)
     tags.get_cached_knowledge.assert_called_once_with()
     tags.get_nodes_for_node_group.assert_called_once_with(client, uuid)
     self.assertEqual([((client, uuid, ['a', 'c']), {})], fake_first.calls)
     self.assertEqual([((client, uuid, ['b']), {})], fake_second.calls)
     tags.post_updated_nodes.assert_called_once_with(
         client, tag_name, tag_definition, uuid, ['a', 'c'], ['b'])
Beispiel #8
0
 def test_raises_if_called_one_time_too_many(self):
     method = MultiFakeMethod([FakeMethod()])
     method()
     self.assertRaises(ValueError, method)