示例#1
0
    def test_get_host_order(self):
        first_host = factories.HostFactory(name="alpha")
        second_host = factories.HostFactory(name="beta",
                                            changed=10,
                                            failed=10,
                                            ok=10,
                                            skipped=10,
                                            unreachable=10)

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

        order_fields = [
            "id", "created", "updated", "name", "changed", "failed", "ok",
            "skipped", "unreachable"
        ]
        # Ascending order
        for field in order_fields:
            request = self.client.get("/api/v1/hosts?order=%s" % field)
            self.assertEqual(request.data["results"][0]["id"], first_host.id)

        # Descending order
        for field in order_fields:
            request = self.client.get("/api/v1/hosts?order=-%s" % field)
            self.assertEqual(request.data["results"][0]["id"], second_host.id)
示例#2
0
    def test_get_hosts_by_stats(self):
        # Create two hosts with different stats
        first_host = factories.HostFactory(name="first_host",
                                           changed=2,
                                           failed=2,
                                           ok=2,
                                           skipped=2,
                                           unreachable=2)
        second_host = factories.HostFactory(name="second_host",
                                            changed=0,
                                            failed=0,
                                            ok=0,
                                            skipped=0,
                                            unreachable=0)

        # There must be two distinct hosts
        request = self.client.get("/api/v1/hosts")
        self.assertEqual(2, request.data["count"])
        self.assertEqual(2, len(request.data["results"]))

        statuses = ["changed", "failed", "ok", "skipped", "unreachable"]

        # Searching for > should only return the first host
        for status in statuses:
            request = self.client.get("/api/v1/hosts?%s__gt=1" % status)
            self.assertEqual(1, request.data["count"])
            self.assertEqual(1, len(request.data["results"]))
            self.assertEqual(first_host.id, request.data["results"][0]["id"])

        # Searching for < should only return the second host
        for status in statuses:
            request = self.client.get("/api/v1/hosts?%s__lt=1" % status)
            self.assertEqual(1, request.data["count"])
            self.assertEqual(1, len(request.data["results"]))
            self.assertEqual(second_host.id, request.data["results"][0]["id"])
示例#3
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)
示例#4
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)
示例#5
0
 def test_get_hosts_by_playbook(self):
     playbook = factories.PlaybookFactory()
     host = factories.HostFactory(name="host1", playbook=playbook)
     factories.HostFactory(name="host2", playbook=playbook)
     request = self.client.get("/api/v1/hosts?playbook=%s" % playbook.id)
     self.assertEqual(2, len(request.data["results"]))
     self.assertEqual(host.name, request.data["results"][0]["name"])
     self.assertEqual("host2", request.data["results"][1]["name"])
示例#6
0
 def test_get_result_by_playbook(self):
     playbook = factories.PlaybookFactory()
     host_one = factories.HostFactory(name="one")
     host_two = factories.HostFactory(name="two")
     result = factories.ResultFactory(playbook=playbook, host=host_one, status="failed")
     factories.ResultFactory(playbook=playbook, host=host_two, status="skipped")
     request = self.client.get("/api/v1/results?playbook=%s" % playbook.id)
     self.assertEqual(2, len(request.data["results"]))
     self.assertEqual(result.status, request.data["results"][1]["status"])
     self.assertEqual("skipped", request.data["results"][0]["status"])
示例#7
0
    def test_get_hosts_by_name(self):
        # Create a playbook and two hosts
        playbook = factories.PlaybookFactory()
        host = factories.HostFactory(name="host1", playbook=playbook)
        factories.HostFactory(name="host2", playbook=playbook)

        # Query for the first host name and expect one result
        request = self.client.get("/api/v1/hosts?name=%s" % host.name)
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(host.name, request.data["results"][0]["name"])
示例#8
0
 def test_partial_update_host(self):
     host = factories.HostFactory()
     self.assertNotEqual("foo", host.name)
     request = self.client.patch("/api/v1/hosts/%s" % host.id, {"name": "foo"})
     self.assertEqual(200, request.status_code)
     host_updated = models.Host.objects.get(id=host.id)
     self.assertEqual("foo", host_updated.name)
示例#9
0
 def test_distincthost_factory(self):
     # TODO: Why doesn't the name propagate to "latest" in factories.DistinctHostFactory(name="testhost") ?
     host = factories.HostFactory(name="testhost")
     latest_host = factories.DistinctHostFactory(name="testhost",
                                                 latest=host)
     self.assertEqual(host.name, "testhost")
     self.assertEqual(latest_host.latest.name, "testhost")
示例#10
0
 def test_get_latesthosts(self):
     # TODO: Why doesn't the name propagate to "latest" in factories.LatestHostFactory(name="testhost") ?
     host = factories.HostFactory(name="gethosts")
     latest_host = factories.LatestHostFactory(name="gethosts", host=host)
     request = self.client.get("/api/v1/latesthosts")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(latest_host.name, request.data["results"][0]["name"])
     self.assertEqual(latest_host.name, request.data["results"][0]["host"]["name"])
示例#11
0
    def test_get_host_by_date(self):
        host = factories.HostFactory()

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

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

        # Expect a host when searching after it was created
        for field in positive_date_fields:
            request = self.client.get("/api/v1/hosts?%s=%s" % (field, past.isoformat()))
            self.assertEqual(request.data["count"], 1)
            self.assertEqual(request.data["results"][0]["id"], host.id)
示例#12
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))
示例#13
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)
示例#14
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())
示例#15
0
 def test_delete_host(self):
     host = factories.HostFactory()
     self.assertEqual(1, models.Host.objects.all().count())
     request = self.client.delete("/api/v1/hosts/%s" % host.id)
     self.assertEqual(204, request.status_code)
     self.assertEqual(0, models.Host.objects.all().count())
示例#16
0
 def test_get_hosts(self):
     host = factories.HostFactory()
     request = self.client.get("/api/v1/hosts")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(host.name, request.data["results"][0]["name"])
示例#17
0
 def test_host_serializer_decompress_facts(self):
     host = factories.HostFactory(
         facts=utils.compressed_obj(factories.HOST_FACTS))
     serializer = serializers.HostSerializer(instance=host)
     self.assertEqual(serializer.data["facts"], factories.HOST_FACTS)
示例#18
0
 def test_host_factory(self):
     host = factories.HostFactory(name="testhost")
     self.assertEqual(host.name, "testhost")
示例#19
0
 def test_get_host(self):
     host = factories.HostFactory()
     request = self.client.get("/api/v1/hosts/%s" % host.id)
     self.assertEqual(host.name, request.data["name"])
示例#20
0
 def test_get_playbook_arguments(self):
     host = factories.HostFactory()
     request = self.client.get("/api/v1/hosts/%s" % host.id)
     self.assertIn("inventory", request.data["playbook"]["arguments"])