Skip to content

Commit

Permalink
test(containers): availablability triggers
Browse files Browse the repository at this point in the history
test happy path for triggers in `Available*` astarte interfaces

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Nov 22, 2024
1 parent 8a26cdc commit 24f8f40
Showing 1 changed file with 245 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 24f8f40

Please sign in to comment.