+ def testGetGroupTags(self):
+ self.rapi.AddResponse("[]")
+ self.assertEqual([], self.client.GetGroupTags("fooGroup"))
+ self.assertHandler(rlib2.R_2_groups_name_tags)
+ self.assertItems(["fooGroup"])
+
+ def testAddGroupTags(self):
+ self.rapi.AddResponse("1234")
+ self.assertEqual(1234,
+ self.client.AddGroupTags("fooGroup", ["awesome"], dry_run=True))
+ self.assertHandler(rlib2.R_2_groups_name_tags)
+ self.assertItems(["fooGroup"])
+ self.assertDryRun()
+ self.assertQuery("tag", ["awesome"])
+
+ def testDeleteGroupTags(self):
+ self.rapi.AddResponse("25826")
+ self.assertEqual(25826, self.client.DeleteGroupTags("foo", ["awesome"],
+ dry_run=True))
+ self.assertHandler(rlib2.R_2_groups_name_tags)
+ self.assertItems(["foo"])
+ self.assertDryRun()
+ self.assertQuery("tag", ["awesome"])
+
+ def testQuery(self):
+ for idx, what in enumerate(constants.QR_VIA_RAPI):
+ for idx2, qfilter in enumerate([None, ["?", "name"]]):
+ job_id = 11010 + (idx << 4) + (idx2 << 16)
+ fields = sorted(query.ALL_FIELDS[what].keys())[:10]
+
+ self.rapi.AddResponse(str(job_id))
+ self.assertEqual(self.client.Query(what, fields, qfilter=qfilter),
+ job_id)
+ self.assertItems([what])
+ self.assertHandler(rlib2.R_2_query)
+ self.assertFalse(self.rapi.GetLastHandler().queryargs)
+ data = serializer.LoadJson(self.rapi.GetLastRequestData())
+ self.assertEqual(data["fields"], fields)
+ if qfilter is None:
+ self.assertTrue("qfilter" not in data)
+ else:
+ self.assertEqual(data["qfilter"], qfilter)
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+ def testQueryFields(self):
+ exp_result = objects.QueryFieldsResponse(fields=[
+ objects.QueryFieldDefinition(name="pnode", title="PNode",
+ kind=constants.QFT_NUMBER),
+ objects.QueryFieldDefinition(name="other", title="Other",
+ kind=constants.QFT_BOOL),
+ ])
+
+ for what in constants.QR_VIA_RAPI:
+ for fields in [None, ["name", "_unknown_"], ["&", "?|"]]:
+ self.rapi.AddResponse(serializer.DumpJson(exp_result.ToDict()))
+ result = self.client.QueryFields(what, fields=fields)
+ self.assertItems([what])
+ self.assertHandler(rlib2.R_2_query_fields)
+ self.assertFalse(self.rapi.GetLastRequestData())
+
+ queryargs = self.rapi.GetLastHandler().queryargs
+ if fields is None:
+ self.assertFalse(queryargs)
+ else:
+ self.assertEqual(queryargs, {
+ "fields": [",".join(fields)],
+ })
+
+ self.assertEqual(objects.QueryFieldsResponse.FromDict(result).ToDict(),
+ exp_result.ToDict())
+
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+ def testWaitForJobCompletionNoChange(self):
+ resp = serializer.DumpJson({
+ "status": constants.JOB_STATUS_WAITING,
+ })
+
+ for retries in [1, 5, 25]:
+ for _ in range(retries):
+ self.rapi.AddResponse(resp)
+
+ self.assertFalse(self.client.WaitForJobCompletion(22789, period=None,
+ retries=retries))
+ self.assertHandler(rlib2.R_2_jobs_id)
+ self.assertItems(["22789"])
+
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+ def testWaitForJobCompletionAlreadyFinished(self):
+ self.rapi.AddResponse(serializer.DumpJson({
+ "status": constants.JOB_STATUS_SUCCESS,
+ }))
+
+ self.assertTrue(self.client.WaitForJobCompletion(22793, period=None,
+ retries=1))
+ self.assertHandler(rlib2.R_2_jobs_id)
+ self.assertItems(["22793"])
+
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+ def testWaitForJobCompletionEmptyResponse(self):
+ self.rapi.AddResponse("{}")
+ self.assertFalse(self.client.WaitForJobCompletion(22793, period=None,
+ retries=10))
+ self.assertHandler(rlib2.R_2_jobs_id)
+ self.assertItems(["22793"])
+
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+ def testWaitForJobCompletionOutOfRetries(self):
+ for retries in [3, 10, 21]:
+ for _ in range(retries):
+ self.rapi.AddResponse(serializer.DumpJson({
+ "status": constants.JOB_STATUS_RUNNING,
+ }))
+
+ self.assertFalse(self.client.WaitForJobCompletion(30948, period=None,
+ retries=retries - 1))
+ self.assertHandler(rlib2.R_2_jobs_id)
+ self.assertItems(["30948"])
+
+ self.assertEqual(self.rapi.CountPending(), 1)
+ self.rapi.ResetResponses()
+
+ def testWaitForJobCompletionSuccessAndFailure(self):
+ for retries in [1, 4, 13]:
+ for (success, end_status) in [(False, constants.JOB_STATUS_ERROR),
+ (True, constants.JOB_STATUS_SUCCESS)]:
+ for _ in range(retries):
+ self.rapi.AddResponse(serializer.DumpJson({
+ "status": constants.JOB_STATUS_RUNNING,
+ }))
+
+ self.rapi.AddResponse(serializer.DumpJson({
+ "status": end_status,
+ }))
+
+ result = self.client.WaitForJobCompletion(3187, period=None,
+ retries=retries + 1)
+ self.assertEqual(result, success)
+ self.assertHandler(rlib2.R_2_jobs_id)
+ self.assertItems(["3187"])
+
+ self.assertEqual(self.rapi.CountPending(), 0)
+
+
+class RapiTestRunner(unittest.TextTestRunner):
+ def run(self, *args):
+ global _used_handlers
+ assert _used_handlers is None
+
+ _used_handlers = set()
+ try:
+ # Run actual tests
+ result = unittest.TextTestRunner.run(self, *args)
+
+ diff = (set(connector.CONNECTOR.values()) - _used_handlers -
+ _KNOWN_UNUSED)
+ if diff:
+ raise AssertionError("The following RAPI resources were not used by the"
+ " RAPI client: %r" % utils.CommaJoin(diff))
+ finally:
+ # Reset global variable
+ _used_handlers = None
+
+ return result
+