nixos/tests/meilisearch: fix race condition when creating index

previously, when creating the index, there is a race condition between
the task being created and when we check if the index was successfully
created. if it is created too slow, the test fails.

it already polled/awaited when adding documents, so i generalized this
logic (and made it terminate anytime the task finishes, even if that
wasn't success). not ideal, since meilisearch supports proper webhook
callbacks, but whatever, we're writing tests, not production code.

sodiboo 75f55966 97234ec2

Changed files
+11 -12
nixos
+11 -12
nixos/tests/meilisearch.nix
···
machine.wait_for_unit("meilisearch")
machine.wait_for_open_port(7700)
with subtest("check version"):
version = json.loads(machine.succeed("curl ${apiUrl}/version"))
assert version["pkgVersion"] == "${pkgs.meilisearch.version}"
with subtest("create index"):
-
machine.succeed(
-
"curl -X POST -H 'Content-Type: application/json' ${apiUrl}/indexes --data @${indexJSON}"
-
)
indexes = json.loads(machine.succeed("curl ${apiUrl}/indexes"))
assert indexes["total"] == 1, "index wasn't created"
with subtest("add documents"):
-
response = json.loads(
-
machine.succeed(
-
"curl -X POST -H 'Content-Type: application/json' ${apiUrl}/indexes/${uid}/documents --data-binary @${moviesJSON}"
-
)
-
)
-
task_uid = response["taskUid"]
-
machine.wait_until_succeeds(
-
f"curl ${apiUrl}/tasks/{task_uid} | jq -e '.status == \"succeeded\"'"
-
)
with subtest("search"):
response = json.loads(
···
machine.wait_for_unit("meilisearch")
machine.wait_for_open_port(7700)
+
def wait_task(cmd):
+
response = json.loads(machine.succeed(cmd))
+
task_uid = response["taskUid"]
+
machine.wait_until_succeeds(
+
f"curl ${apiUrl}/tasks/{task_uid} | jq -e '.status | IN(\"succeeded\", \"failed\", \"canceled\")'"
+
)
+
machine.succeed(f"curl ${apiUrl}/tasks/{task_uid} | jq -e '.status == \"succeeded\"'")
+
return response
+
with subtest("check version"):
version = json.loads(machine.succeed("curl ${apiUrl}/version"))
assert version["pkgVersion"] == "${pkgs.meilisearch.version}"
with subtest("create index"):
+
wait_task("curl -X POST -H 'Content-Type: application/json' ${apiUrl}/indexes --data @${indexJSON}")
indexes = json.loads(machine.succeed("curl ${apiUrl}/indexes"))
assert indexes["total"] == 1, "index wasn't created"
with subtest("add documents"):
+
wait_task("curl -X POST -H 'Content-Type: application/json' ${apiUrl}/indexes/${uid}/documents --data-binary @${moviesJSON}")
with subtest("search"):
response = json.loads(