From 24f8f40a15443441ecf564a99460da8f756d4d41 Mon Sep 17 00:00:00 2001 From: Luca Zaninotto Date: Tue, 19 Nov 2024 18:51:12 +0100 Subject: [PATCH] test(containers): availablability triggers test happy path for triggers in `Available*` astarte interfaces Signed-off-by: Luca Zaninotto --- .../astarte_trigger_controller_test.exs | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs b/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs index aa4f2366d..311b1f432 100644 --- a/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs +++ b/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs @@ -26,6 +26,14 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do import Edgehog.DevicesFixtures import Edgehog.OSManagementFixtures + alias Edgehog.Astarte.Device.AvailableContainers.ContainerStatus + alias Edgehog.Astarte.Device.AvailableContainersMock + alias Edgehog.Astarte.Device.AvailableDeployments.DeploymentStatus + alias Edgehog.Astarte.Device.AvailableDeploymentsMock + alias Edgehog.Astarte.Device.AvailableImages.ImageStatus + alias Edgehog.Astarte.Device.AvailableImagesMock + alias Edgehog.Astarte.Device.AvailableNetworks.NetworkStatus + alias Edgehog.Astarte.Device.AvailableNetworksMock alias Edgehog.Astarte.Device.DeviceStatusMock alias Edgehog.Containers.Deployment alias Edgehog.Devices.Device @@ -475,6 +483,243 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do assert deployment.message == nil end + test "AvailableImages triggers update deployment status", context do + %{conn: conn, realm: realm, device: device, tenant: tenant} = context + + release = release_fixture(containers: 1, tenant: tenant) + + assert {:ok, release} = Ash.load(release, containers: [:image, :networks]) + assert [container] = release.containers + assert [network] = container.networks + + deployment = + [tenant: tenant, device_id: device.id, release_id: release.id] + |> deployment_fixture() + |> Ash.Changeset.for_update(:set_status, %{status: :sent}, tenant: tenant) + |> Ash.update!() + + deployment_event = %{ + device_id: device.device_id, + event: %{ + type: "incoming_data", + interface: "io.edgehog.devicemanager.apps.AvailableImages", + path: "/" <> container.image.id <> "/pulled", + value: true + }, + timestamp: DateTime.to_iso8601(DateTime.utc_now()) + } + + expect(AvailableImagesMock, :get, fn _, _ -> + {:ok, + [ + %ImageStatus{ + id: container.image.id, + pulled: true + } + ]} + end) + + expect(AvailableNetworksMock, :get, fn _, _ -> + {:ok, + [ + %NetworkStatus{ + id: network.id, + created: true + } + ]} + end) + + expect(AvailableContainersMock, :get, fn _, _ -> + {:ok, + [ + %ContainerStatus{ + id: container.id, + status: "Created" + } + ]} + end) + + expect(AvailableDeploymentsMock, :get, fn _, _ -> + {:ok, + [ + %DeploymentStatus{ + id: deployment.id, + status: :stopped + } + ]} + end) + + path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug) + + conn = + conn + |> put_req_header("astarte-realm", realm.name) + |> post(path, deployment_event) + + assert response(conn, 200) + + {:ok, deployment} = Ash.get(Deployment, deployment.id, tenant: tenant) + assert deployment.status == :ready + end + + test "AvailableContainers triggers update deployment status", context do + %{conn: conn, realm: realm, device: device, tenant: tenant} = context + + release = release_fixture(containers: 1, tenant: tenant) + + assert {:ok, release} = Ash.load(release, containers: [:image, :networks]) + assert [container] = release.containers + assert [network] = container.networks + + deployment = + [tenant: tenant, device_id: device.id, release_id: release.id] + |> deployment_fixture() + |> Ash.Changeset.for_update(:set_status, %{status: :sent}, tenant: tenant) + |> Ash.update!() + + deployment_event = %{ + device_id: device.device_id, + event: %{ + type: "incoming_data", + interface: "io.edgehog.devicemanager.apps.AvailableContainers", + path: "/" <> container.id <> "/status", + value: "Created" + }, + timestamp: DateTime.to_iso8601(DateTime.utc_now()) + } + + expect(AvailableImagesMock, :get, fn _, _ -> + {:ok, + [ + %ImageStatus{ + id: container.image.id, + pulled: true + } + ]} + end) + + expect(AvailableNetworksMock, :get, fn _, _ -> + {:ok, + [ + %NetworkStatus{ + id: network.id, + created: true + } + ]} + end) + + expect(AvailableContainersMock, :get, fn _, _ -> + {:ok, + [ + %ContainerStatus{ + id: container.id, + status: "Created" + } + ]} + end) + + expect(AvailableDeploymentsMock, :get, fn _, _ -> + {:ok, + [ + %DeploymentStatus{ + id: deployment.id, + status: :stopped + } + ]} + end) + + path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug) + + conn = + conn + |> put_req_header("astarte-realm", realm.name) + |> post(path, deployment_event) + + assert response(conn, 200) + + {:ok, deployment} = Ash.get(Deployment, deployment.id, tenant: tenant) + assert deployment.status == :ready + end + + test "AvailableDeployments triggers update deployment status", context do + %{conn: conn, realm: realm, device: device, tenant: tenant} = context + + release = release_fixture(containers: 1, tenant: tenant) + + assert {:ok, release} = Ash.load(release, containers: [:image, :networks]) + assert [container] = release.containers + assert [network] = container.networks + + deployment = + [tenant: tenant, device_id: device.id, release_id: release.id] + |> deployment_fixture() + |> Ash.Changeset.for_update(:set_status, %{status: :sent}, tenant: tenant) + |> Ash.update!() + + deployment_event = %{ + device_id: device.device_id, + event: %{ + type: "incoming_data", + interface: "io.edgehog.devicemanager.apps.AvailableDeployments", + path: "/" <> deployment.id <> "/status", + value: "Stopped" + }, + timestamp: DateTime.to_iso8601(DateTime.utc_now()) + } + + expect(AvailableImagesMock, :get, fn _, _ -> + {:ok, + [ + %ImageStatus{ + id: container.image.id, + pulled: true + } + ]} + end) + + expect(AvailableNetworksMock, :get, fn _, _ -> + {:ok, + [ + %NetworkStatus{ + id: network.id, + created: true + } + ]} + end) + + expect(AvailableContainersMock, :get, fn _, _ -> + {:ok, + [ + %ContainerStatus{ + id: container.id, + status: "Created" + } + ]} + end) + + expect(AvailableDeploymentsMock, :get, fn _, _ -> + {:ok, + [ + %DeploymentStatus{ + id: deployment.id, + status: :stopped + } + ]} + end) + + path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug) + + conn = + conn + |> put_req_header("astarte-realm", realm.name) + |> post(path, deployment_event) + + assert response(conn, 200) + + {:ok, deployment} = Ash.get(Deployment, deployment.id, tenant: tenant) + assert deployment.status == :stopped + end + test "unset AvailableDeployments deletes an existing deployment", context do %{conn: conn, realm: realm, device: device, tenant: tenant} = context