def test_get_playbook_order(self): old_started = timezone.now() - datetime.timedelta(hours=12) old_ended = old_started + datetime.timedelta(minutes=30) old_playbook = factories.PlaybookFactory(started=old_started, ended=old_ended) new_started = timezone.now() - datetime.timedelta(hours=6) new_ended = new_started + datetime.timedelta(hours=1) new_playbook = factories.PlaybookFactory(started=new_started, ended=new_ended) # Ensure we have two objects request = self.client.get("/api/v1/playbooks") 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/playbooks?order=%s" % field) self.assertEqual(request.data["results"][0]["id"], old_playbook.id) # Descending order for field in order_fields: request = self.client.get("/api/v1/playbooks?order=-%s" % field) self.assertEqual(request.data["results"][0]["id"], new_playbook.id)
def test_delete_and_update_distincthost(self): # Create two playbooks and two hosts first_playbook = factories.PlaybookFactory() second_playbook = factories.PlaybookFactory() first_host = self.client.post("/api/v1/hosts", { "name": "localhost", "playbook": first_playbook.id }) # Slow this down a bit so we have a second host that is more noticeably "latest" time.sleep(0.5) second_host = self.client.post("/api/v1/hosts", { "name": "localhost", "playbook": second_playbook.id }) self.assertEqual(2, models.Host.objects.count()) self.assertEqual(1, models.DistinctHost.objects.count()) self.assertEqual(second_host.data["id"], models.DistinctHost.objects.first().latest.id) # Deleting the second host should update the distincthost to point to the first host delete = self.client.delete("/api/v1/hosts/%s" % second_host.data["id"]) self.assertEqual(204, delete.status_code) self.assertEqual(1, models.Host.objects.count()) self.assertEqual(first_host.data["id"], models.DistinctHost.objects.first().latest.id)
def test_get_playbook_by_name(self): playbook = factories.PlaybookFactory(name="playbook1") factories.PlaybookFactory(name="playbook2") # Test exact match request = self.client.get("/api/v1/playbooks?name=playbook1") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.name, request.data["results"][0]["name"]) # Test partial match request = self.client.get("/api/v1/playbooks?name=playbook") self.assertEqual(len(request.data["results"]), 2)
def test_get_playbook_by_path(self): playbook = factories.PlaybookFactory(path="/root/playbook.yml") factories.PlaybookFactory(path="/home/playbook.yml") # Test exact match request = self.client.get("/api/v1/playbooks?path=/root/playbook.yml") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.path, request.data["results"][0]["path"]) # Test partial match request = self.client.get("/api/v1/playbooks?path=playbook.yml") self.assertEqual(len(request.data["results"]), 2)
def test_get_playbook_by_ansible_version(self): playbook = factories.PlaybookFactory(name="playbook1", ansible_version="2.9.1") factories.PlaybookFactory(name="playbook2", ansible_version="2.8.2") # Test exact match request = self.client.get("/api/v1/playbooks?ansible_version=2.9.1") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.name, request.data["results"][0]["name"]) # Test partial match request = self.client.get("/api/v1/playbooks?ansible_version=2.9") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.name, request.data["results"][0]["name"])
def test_get_playbook_by_controller(self): playbook = factories.PlaybookFactory(name="playbook1", controller="controller-one") factories.PlaybookFactory(name="playbook2", controller="controller-two") # Test exact match request = self.client.get("/api/v1/playbooks?controller=controller-one") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.name, request.data["results"][0]["name"]) self.assertEqual(playbook.controller, request.data["results"][0]["controller"]) # Test partial match request = self.client.get("/api/v1/playbooks?controller=controller") self.assertEqual(len(request.data["results"]), 2)
def test_get_playbook_by_label(self): # Create two playbooks, one with labels and one without playbook = factories.PlaybookFactory() self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"labels": ["test-label"]}) factories.PlaybookFactory() # Ensure we have two objects when searching without labels request = self.client.get("/api/v1/playbooks") self.assertEqual(2, len(request.data["results"])) # Search with label and ensure we have the right one request = self.client.get("/api/v1/playbooks?label=%s" % "test-label") self.assertEqual(1, len(request.data["results"])) self.assertEqual(request.data["results"][0]["labels"][0]["name"], "test-label")
def test_update_wrong_playbook_status(self): playbook = factories.PlaybookFactory() self.assertNotEqual("wrong", playbook.status) request = self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"status": "wrong"}) self.assertEqual(400, request.status_code) playbook_updated = models.Playbook.objects.get(id=playbook.id) self.assertNotEqual("wrong", playbook_updated.status)
def test_get_playbooks(self): expected_playbook = factories.PlaybookFactory() request = self.client.get("/api/v1/playbooks") self.assertEqual(1, len(request.data["results"])) self.assertEqual(1, request.data["count"]) playbook = request.data["results"][0] self.assertEqual(playbook["ansible_version"], expected_playbook.ansible_version)
def test_partial_update_playbook(self): playbook = factories.PlaybookFactory() self.assertNotEqual("completed", playbook.status) request = self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"status": "completed"}) self.assertEqual(200, request.status_code) playbook_updated = models.Playbook.objects.get(id=playbook.id) self.assertEqual("completed", playbook_updated.status)
def test_prune_with_bad_authentication_http_client(self): # Create a user self.user = User.objects.create_superuser("prune", "*****@*****.**", "password") # Create a playbook with an old start date old_timestamp = datetime.datetime.now() - datetime.timedelta(days=60) factories.PlaybookFactory(started=old_timestamp) self.assertEqual(1, models.Playbook.objects.all().count()) # Set up arguments with a wrong password args = [ "--confirm", "--client", "http", "--endpoint", self.live_server_url, "--username", "prune", "--password", "somethingelse", ] with self.assertRaises(SystemExit): run_prune_command(*args) # TODO: the assertRaises prevents us from looking at the output # output = run_prune_command(*args) # self.assertIn("Client failed to retrieve results, see logs for ara.clients.offline or ara.clients.http.", output) # noqa # Nothing should have been deleted because the command failed self.assertEqual(1, models.Playbook.objects.all().count())
def test_prune_with_authenticated_http_client(self): # Create a user self.user = User.objects.create_superuser("prune", "*****@*****.**", "password") # Create a playbook with an old start date old_timestamp = datetime.datetime.now() - datetime.timedelta(days=60) factories.PlaybookFactory(started=old_timestamp) self.assertEqual(1, models.Playbook.objects.all().count()) args = [ "--confirm", "--client", "http", "--endpoint", self.live_server_url, "--username", "prune", "--password", "password", ] output = self.run_prune_command(*args) self.assertIn( "INFO:ara.api.management.commands.prune:Found 1 playbooks matching query", output) self.assertIn( "INFO:ara.api.management.commands.prune:1 playbooks deleted", output) self.assertEqual(0, models.Playbook.objects.all().count())
def test_get_playbook_duration(self): started = timezone.now() ended = started + datetime.timedelta(hours=1) playbook = factories.PlaybookFactory(started=started, ended=ended) request = self.client.get("/api/v1/playbooks/%s" % playbook.id) self.assertEqual(parse_duration(request.data["duration"]), ended - started)
def test_host_serializer(self): playbook = factories.PlaybookFactory() serializer = serializers.HostSerializer(data={"name": "serializer", "playbook": playbook.id}) serializer.is_valid() host = serializer.save() host.refresh_from_db() self.assertEqual(host.name, "serializer") self.assertEqual(host.playbook.id, playbook.id)
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"])
def test_create_file(self): self.assertEqual(0, models.File.objects.count()) playbook = factories.PlaybookFactory() request = self.client.post( "/api/v1/files", {"path": "/path/playbook.yml", "content": factories.FILE_CONTENTS, "playbook": playbook.id} ) self.assertEqual(201, request.status_code) self.assertEqual(1, models.File.objects.count())
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"])
def test_get_playbook_by_status(self): playbook = factories.PlaybookFactory(status="failed") factories.PlaybookFactory(status="completed") factories.PlaybookFactory(status="running") # Confirm we have three objects request = self.client.get("/api/v1/playbooks") self.assertEqual(3, len(request.data["results"])) # Test single status request = self.client.get("/api/v1/playbooks?status=failed") self.assertEqual(1, len(request.data["results"])) self.assertEqual(playbook.status, request.data["results"][0]["status"]) # Test multiple status request = self.client.get("/api/v1/playbooks?status=failed&status=completed") self.assertEqual(2, len(request.data["results"]))
def test_patch_playbook_labels(self): playbook = factories.PlaybookFactory() labels = ["test-label", "another-test-label"] self.assertNotEqual(playbook.labels, labels) request = self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"labels": labels}) self.assertEqual(200, request.status_code) playbook_updated = models.Playbook.objects.get(id=playbook.id) self.assertEqual([label.name for label in playbook_updated.labels.all()], labels)
def test_expired_playbook(self): playbook = factories.PlaybookFactory(status="running") self.assertEqual("running", playbook.status) request = self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"status": "expired"}) self.assertEqual(200, request.status_code) playbook_updated = models.Playbook.objects.get(id=playbook.id) self.assertEqual("expired", playbook_updated.status)
def test_patch_playbook_name(self): playbook = factories.PlaybookFactory() new_name = "foo" self.assertNotEqual(playbook.name, new_name) request = self.client.patch("/api/v1/playbooks/%s" % playbook.id, {"name": new_name}) self.assertEqual(200, request.status_code) playbook_updated = models.Playbook.objects.get(id=playbook.id) self.assertEqual(playbook_updated.name, new_name)
def test_file_serializer(self): playbook = factories.PlaybookFactory() serializer = serializers.FileSerializer( data={"path": "/path/playbook.yml", "content": factories.FILE_CONTENTS, "playbook": playbook.id} ) serializer.is_valid() file = serializer.save() file.refresh_from_db() self.assertEqual(file.content.sha1, utils.sha1(factories.FILE_CONTENTS))
def test_host_serializer_compress_facts(self): playbook = factories.PlaybookFactory() serializer = serializers.HostSerializer( data={"name": "compress", "facts": factories.HOST_FACTS, "playbook": playbook.id} ) serializer.is_valid() host = serializer.save() host.refresh_from_db() self.assertEqual(host.facts, utils.compressed_obj(factories.HOST_FACTS))
def test_create_host(self): playbook = factories.PlaybookFactory() self.assertEqual(0, models.Host.objects.count()) request = self.client.post("/api/v1/hosts", { "name": "create", "playbook": playbook.id }) self.assertEqual(201, request.status_code) self.assertEqual(1, models.Host.objects.count())
def test_prune_with_no_matching_playbook(self): # Create a playbook with start date as of now factories.PlaybookFactory() self.assertEqual(1, models.Playbook.objects.all().count()) args = ["--confirm"] output = run_prune_command(*args) self.assertIn("Found 0 playbooks matching query", output) self.assertIn("0 playbooks deleted", output) self.assertEqual(1, models.Playbook.objects.all().count())
def test_prune_with_no_matching_playbook_with_http_client(self): # Create a playbook with start date as of now factories.PlaybookFactory() self.assertEqual(1, models.Playbook.objects.all().count()) args = ["--confirm", "--client", "http", "--endpoint", self.live_server_url] output = run_prune_command(*args) self.assertIn("Found 0 playbooks matching query", output) self.assertIn("0 playbooks deleted", output) self.assertEqual(1, models.Playbook.objects.all().count())
def test_get_records_by_key(self): playbook = factories.PlaybookFactory() record = factories.RecordFactory(playbook=playbook, key="by_key") factories.RecordFactory(key="another_record") request = self.client.get("/api/v1/records?key=%s" % record.key) self.assertEqual(2, models.Record.objects.all().count()) self.assertEqual(1, len(request.data["results"])) self.assertEqual(record.key, request.data["results"][0]["key"]) self.assertEqual(record.playbook.id, request.data["results"][0]["playbook"])
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"])
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"])
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"])