Example #1
0
 def test_get_tasks_by_playbook(self):
     playbook = factories.PlaybookFactory()
     task = factories.TaskFactory(name="task1", playbook=playbook)
     factories.TaskFactory(name="task2", playbook=playbook)
     request = self.client.get("/api/v1/tasks?playbook=%s" % playbook.id)
     self.assertEqual(2, len(request.data["results"]))
     self.assertEqual(task.name, request.data["results"][0]["name"])
     self.assertEqual("task2", request.data["results"][1]["name"])
Example #2
0
    def test_get_tasks_by_name(self):
        # Create a playbook and two tasks
        playbook = factories.PlaybookFactory()
        task = factories.TaskFactory(name="task1", playbook=playbook)
        factories.TaskFactory(name="task2", playbook=playbook)

        # Query for the first task name and expect one result
        request = self.client.get("/api/v1/tasks?name=%s" % task.name)
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(task.name, request.data["results"][0]["name"])
Example #3
0
    def test_get_task_by_action(self):
        task = factories.TaskFactory(action="debug")
        factories.TaskFactory(action="setup")

        # Confirm we have two objects
        request = self.client.get("/api/v1/tasks")
        self.assertEqual(2, len(request.data["results"]))

        # Expect the correct single result when searching
        request = self.client.get("/api/v1/tasks?action=debug")
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(task.id, request.data["results"][0]["id"])
        self.assertEqual(task.action, request.data["results"][0]["action"])
Example #4
0
 def test_create_result_with_two_delegations(self):
     host = factories.HostFactory(name="original")
     delegated_host = factories.HostFactory(name="delegated")
     another_host = factories.HostFactory(name="another")
     task = factories.TaskFactory()
     self.assertEqual(0, models.Result.objects.count())
     request = self.client.post(
         "/api/v1/results",
         {
             "content": factories.RESULT_CONTENTS,
             "status": "ok",
             "host": host.id,
             "delegated_to": [delegated_host.id, another_host.id],
             "task": task.id,
             "play": task.play.id,
             "playbook": task.playbook.id,
             "changed": True,
             "ignore_errors": False,
         },
     )
     self.assertEqual(201, request.status_code)
     self.assertEqual(1, models.Result.objects.count())
     self.assertEqual(request.data["changed"], True)
     self.assertEqual(request.data["ignore_errors"], False)
     self.assertEqual(request.data["status"], "ok")
     self.assertEqual(request.data["host"], host.id)
     self.assertEqual(request.data["delegated_to"],
                      [delegated_host.id, another_host.id])
     self.assertEqual(request.data["task"], task.id)
     self.assertEqual(request.data["play"], task.play.id)
     self.assertEqual(request.data["playbook"], task.playbook.id)
Example #5
0
 def test_get_task_duration(self):
     started = timezone.now()
     ended = started + datetime.timedelta(hours=1)
     task = factories.TaskFactory(started=started, ended=ended)
     request = self.client.get("/api/v1/tasks/%s" % task.id)
     self.assertEqual(parse_duration(request.data["duration"]),
                      ended - started)
Example #6
0
 def test_result_serializer_with_delegation(self):
     host = factories.HostFactory(name="first")
     anotherhost = factories.HostFactory(name="second")
     task = factories.TaskFactory()
     serializer = serializers.ResultSerializer(
         data={
             "status": "skipped",
             "host": host.id,
             "delegated_to": [anotherhost.id],
             "task": task.id,
             "play": task.play.id,
             "playbook": task.playbook.id,
             "changed": False,
             "ignore_errors": False,
         })
     serializer.is_valid()
     result = serializer.save()
     result.refresh_from_db()
     self.assertEqual(result.status, "skipped")
     self.assertEqual(result.changed, False)
     self.assertEqual(result.ignore_errors, False)
     self.assertEqual(result.host.id, host.id)
     # FIXME: Why does the serializer return a ManyRelatedManager queryset for delegated_to ?
     self.assertEqual(result.delegated_to.values()[0]["id"], anotherhost.id)
     self.assertEqual(result.delegated_to.values()[0]["name"],
                      anotherhost.name)
     self.assertEqual(result.task.id, task.id)
Example #7
0
 def test_update_wrong_task_status(self):
     task = factories.TaskFactory()
     self.assertNotEqual("wrong", task.status)
     request = self.client.patch("/api/v1/tasks/%s" % task.id,
                                 {"status": "wrong"})
     self.assertEqual(400, request.status_code)
     task_updated = models.Task.objects.get(id=task.id)
     self.assertNotEqual("wrong", task_updated.status)
Example #8
0
 def test_partial_update_task(self):
     task = factories.TaskFactory()
     self.assertNotEqual("update", task.name)
     request = self.client.patch("/api/v1/tasks/%s" % task.id,
                                 {"name": "update"})
     self.assertEqual(200, request.status_code)
     task_updated = models.Task.objects.get(id=task.id)
     self.assertEqual("update", task_updated.name)
Example #9
0
    def test_expired_task(self):
        task = factories.TaskFactory(status="running")
        self.assertEqual("running", task.status)

        request = self.client.patch("/api/v1/tasks/%s" % task.id,
                                    {"status": "expired"})
        self.assertEqual(200, request.status_code)
        task_updated = models.Task.objects.get(id=task.id)
        self.assertEqual("expired", task_updated.status)
Example #10
0
    def test_get_task_by_status(self):
        task = factories.TaskFactory(status="running")
        factories.TaskFactory(status="completed")
        factories.TaskFactory(status="unknown")

        # Confirm we have three objects
        request = self.client.get("/api/v1/tasks")
        self.assertEqual(3, len(request.data["results"]))

        # Test single status
        request = self.client.get("/api/v1/tasks?status=running")
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(task.status, request.data["results"][0]["status"])

        # Test multiple status
        request = self.client.get(
            "/api/v1/tasks?status=running&status=completed")
        self.assertEqual(2, len(request.data["results"]))
Example #11
0
    def test_get_plays_by_name(self):
        # Create a playbook and two plays
        playbook = factories.PlaybookFactory()
        play = factories.PlayFactory(name="first_play", playbook=playbook)
        factories.TaskFactory(name="second_play", playbook=playbook)

        # Query for the first play name and expect one result
        request = self.client.get("/api/v1/plays?name=%s" % play.name)
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(play.name, request.data["results"][0]["name"])
Example #12
0
    def test_get_task_by_path(self):
        # Create two files with different paths
        first_file = factories.FileFactory(
            path="/root/roles/foo/tasks/main.yml")
        second_file = factories.FileFactory(
            path="/root/roles/bar/tasks/main.yml")

        # Create two tasks using these files
        task = factories.TaskFactory(file=first_file)
        factories.TaskFactory(file=second_file)

        # Test exact match
        request = self.client.get(
            "/api/v1/tasks?path=/root/roles/foo/tasks/main.yml")
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(task.file.path, request.data["results"][0]["path"])

        # Test partial match
        request = self.client.get("/api/v1/tasks?path=main.yml")
        self.assertEqual(len(request.data["results"]), 2)
Example #13
0
    def test_get_tasks_by_play(self):
        playbook = factories.PlaybookFactory()
        # Create two plays
        play = factories.PlayFactory(name="first_play", playbook=playbook)
        second_play = factories.PlayFactory(name="second_play",
                                            playbook=playbook)
        request = self.client.get("/api/v1/plays")
        self.assertEqual(2, len(request.data["results"]))

        # Add tasks for the first play and validate
        task = factories.TaskFactory(name="inside_first", play=play)
        factories.TaskFactory(name="inside_second", play=play)

        request = self.client.get("/api/v1/tasks?play=%s" % play.id)
        self.assertEqual(2, len(request.data["results"]))
        self.assertEqual(task.name, request.data["results"][1]["name"])
        self.assertEqual("inside_second", request.data["results"][0]["name"])

        request = self.client.get("/api/v1/tasks?play=%s" % second_play.id)
        self.assertEqual(0, len(request.data["results"]))
Example #14
0
    def test_get_task_order(self):
        old_started = timezone.now() - datetime.timedelta(hours=12)
        old_ended = old_started + datetime.timedelta(minutes=30)
        old_task = factories.TaskFactory(started=old_started, ended=old_ended)
        new_started = timezone.now() - datetime.timedelta(hours=6)
        new_ended = new_started + datetime.timedelta(hours=1)
        new_task = factories.TaskFactory(started=new_started, ended=new_ended)

        # Ensure we have two objects
        request = self.client.get("/api/v1/tasks")
        self.assertEqual(2, len(request.data["results"]))

        order_fields = [
            "id", "created", "updated", "started", "ended", "duration"
        ]
        # Ascending order
        for field in order_fields:
            request = self.client.get("/api/v1/tasks?order=%s" % field)
            self.assertEqual(request.data["results"][0]["id"], old_task.id)

        # Descending order
        for field in order_fields:
            request = self.client.get("/api/v1/tasks?order=-%s" % field)
            self.assertEqual(request.data["results"][0]["id"], new_task.id)
Example #15
0
 def test_result_serializer_compress_content(self):
     host = factories.HostFactory()
     task = factories.TaskFactory()
     serializer = serializers.ResultSerializer(
         data={
             "content": factories.RESULT_CONTENTS,
             "status": "ok",
             "host": host.id,
             "task": task.id,
             "play": task.play.id,
             "playbook": task.playbook.id,
         })
     serializer.is_valid()
     result = serializer.save()
     result.refresh_from_db()
     self.assertEqual(result.content,
                      utils.compressed_obj(factories.RESULT_CONTENTS))
Example #16
0
 def test_create_result(self):
     host = factories.HostFactory()
     task = factories.TaskFactory()
     self.assertEqual(0, models.Result.objects.count())
     request = self.client.post(
         "/api/v1/results",
         {
             "content": factories.RESULT_CONTENTS,
             "status": "ok",
             "host": host.id,
             "task": task.id,
             "play": task.play.id,
             "playbook": task.playbook.id,
             "changed": True,
             "ignore_errors": False,
         },
     )
     self.assertEqual(201, request.status_code)
     self.assertEqual(request.data["changed"], True)
     self.assertEqual(request.data["ignore_errors"], False)
     self.assertEqual(1, models.Result.objects.count())
Example #17
0
 def test_result_serializer(self):
     host = factories.HostFactory()
     task = factories.TaskFactory()
     serializer = serializers.ResultSerializer(
         data={
             "status": "skipped",
             "host": host.id,
             "task": task.id,
             "play": task.play.id,
             "playbook": task.playbook.id,
             "changed": False,
             "ignore_errors": False,
         })
     serializer.is_valid()
     result = serializer.save()
     result.refresh_from_db()
     self.assertEqual(result.status, "skipped")
     self.assertEqual(result.changed, False)
     self.assertEqual(result.ignore_errors, False)
     self.assertEqual(result.host.id, host.id)
     self.assertEqual(result.task.id, task.id)
Example #18
0
    def test_get_task_by_date(self):
        task = factories.TaskFactory()

        past = datetime.datetime.now() - datetime.timedelta(hours=12)
        negative_date_fields = [
            "created_before", "started_before", "updated_before"
        ]
        positive_date_fields = [
            "created_after", "started_after", "updated_after"
        ]

        # Expect no task when searching before it was created
        for field in negative_date_fields:
            request = self.client.get("/api/v1/tasks?%s=%s" %
                                      (field, past.isoformat()))
            self.assertEqual(request.data["count"], 0)

        # Expect a task when searching after it was created
        for field in positive_date_fields:
            request = self.client.get("/api/v1/tasks?%s=%s" %
                                      (field, past.isoformat()))
            self.assertEqual(request.data["count"], 1)
            self.assertEqual(request.data["results"][0]["id"], task.id)
Example #19
0
 def test_delete_task(self):
     task = factories.TaskFactory()
     self.assertEqual(1, models.Task.objects.all().count())
     request = self.client.delete("/api/v1/tasks/%s" % task.id)
     self.assertEqual(204, request.status_code)
     self.assertEqual(0, models.Task.objects.all().count())
Example #20
0
 def test_get_tasks(self):
     task = factories.TaskFactory()
     request = self.client.get("/api/v1/tasks")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(task.name, request.data["results"][0]["name"])
Example #21
0
 def test_task_serializer_decompress_tags(self):
     task = factories.TaskFactory(
         tags=utils.compressed_obj(factories.TASK_TAGS))
     serializer = serializers.TaskSerializer(instance=task)
     self.assertEqual(serializer.data["tags"], factories.TASK_TAGS)
Example #22
0
 def test_task_factory(self):
     task = factories.TaskFactory(name="factory")
     self.assertEqual(task.name, "factory")
Example #23
0
 def test_get_playbook_arguments(self):
     task = factories.TaskFactory()
     request = self.client.get("/api/v1/tasks/%s" % task.id)
     self.assertIn("inventory", request.data["playbook"]["arguments"])
Example #24
0
 def test_get_task(self):
     task = factories.TaskFactory()
     request = self.client.get("/api/v1/tasks/%s" % task.id)
     self.assertEqual(task.name, request.data["name"])