Skip to content

Commit

Permalink
chore(containers): test refactor
Browse files Browse the repository at this point in the history
some tests had no more reason to exist

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Dec 4, 2024
1 parent 5943661 commit 1a9f4cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,71 +411,6 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
{:ok, cluster: cluster, realm: realm, device: device}
end

test "updates the deployment status", context do
%{
conn: conn,
realm: realm,
device: device,
tenant: tenant
} = context

deployment = deployment_fixture(tenant: tenant, device_id: device.id)

deployment_event = %{
device_id: device.device_id,
event: %{
type: "incoming_data",
interface: "io.edgehog.devicemanager.apps.DeploymentEvent",
path: "/" <> deployment.id,
value: %{
"status" => "Error",
"message" => "error message"
}
},
timestamp: DateTime.to_iso8601(DateTime.utc_now())
}

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

# Deployment must be reloaded from the db
deployment = Ash.get!(Edgehog.Containers.Release.Deployment, deployment.id, tenant: tenant)
deployment = Ash.get!(Edgehog.Containers.Release.Deployment, deployment.id, tenant: tenant)

assert deployment.status == :error
assert deployment.message == "error message"

deployment_event = %{
device_id: device.device_id,
event: %{
type: "incoming_data",
interface: "io.edgehog.devicemanager.apps.DeploymentEvent",
path: "/" <> deployment.id,
value: %{
"status" => "Starting",
"message" => ""
}
},
timestamp: DateTime.to_iso8601(DateTime.utc_now())
}

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

deployment = Ash.get!(Edgehog.Containers.Release.Deployment, deployment.id, tenant: tenant)

assert deployment.status == :starting
assert deployment.message == nil
end

test "Starting status does not update a Started deployment", context do
%{
conn: conn,
Expand All @@ -484,7 +419,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
tenant: tenant
} = context

deployment = deployment_fixture(tenant: tenant, device_id: device.id, status: :started)
deployment = deployment_fixture(tenant: tenant, device_id: device.id, state: :started)

deployment_event = %{
device_id: device.device_id,
Expand All @@ -510,8 +445,8 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
# Deployment must be reloaded from the db
deployment = Ash.get!(Edgehog.Containers.Release.Deployment, deployment.id, tenant: tenant)

assert deployment.status == :started
assert deployment.message == nil
assert deployment.state == :started
assert deployment.last_message == nil
end

test "Stopping status does not update a Stopped deployment", context do
Expand All @@ -522,7 +457,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
tenant: tenant
} = context

deployment = deployment_fixture(tenant: tenant, device_id: device.id, status: :stopped)
deployment = deployment_fixture(tenant: tenant, device_id: device.id, state: :stopped)

deployment_event = %{
device_id: device.device_id,
Expand All @@ -548,126 +483,8 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
# Deployment must be reloaded from the db
deployment = Ash.get!(Edgehog.Containers.Release.Deployment, deployment.id, tenant: tenant)

assert deployment.status == :stopped
assert deployment.message == nil
end

test "AvailableImages triggers update deployment status", context do
%{conn: conn, realm: realm, device: device, tenant: tenant} = context

release =
[containers: 1, tenant: tenant]
|> release_fixture()
|> Ash.load!(containers: [:image, :networks])

[container] = release.containers

deployment =
deployment_fixture(
tenant: tenant,
device_id: device.id,
release_id: release.id,
status: :sent
)

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())
}

set_resource_expectations([deployment])

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

deployment = Ash.get!(Deployment, deployment.id, tenant: tenant)
assert deployment.status == :stopped
end

test "AvailableContainers triggers update deployment status", context do
%{conn: conn, realm: realm, device: device, tenant: tenant} = context

release =
[containers: 1, tenant: tenant]
|> release_fixture()
|> Ash.load!(containers: [:image, :networks])

[container] = release.containers

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())
}

set_resource_expectations([deployment])

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

deployment = Ash.get!(Deployment, deployment.id, tenant: tenant)
assert deployment.status == :stopped
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)

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())
}

set_resource_expectations([deployment])

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

deployment = Ash.get!(Deployment, deployment.id, tenant: tenant)
assert deployment.status == :stopped
assert deployment.state == :stopped
assert deployment.last_message == nil
end

test "unset AvailableDeployments deletes an existing deployment", context do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ defmodule EdgehogWeb.Schema.Mutation.SendDeploymentUpgradeTest do

alias Edgehog.Astarte.Device.CreateDeploymentRequestMock
alias Edgehog.Astarte.Device.DeploymentUpdateMock
alias Edgehog.Containers
alias Edgehog.Containers.Release.Deployment
alias Edgehog.Containers.Release

describe "sendDeploymentUpgrade" do
setup %{tenant: tenant} do
Expand Down Expand Up @@ -64,7 +63,7 @@ defmodule EdgehogWeb.Schema.Mutation.SendDeploymentUpgradeTest do

{:ok, %{id: deployment_id}} = AshGraphql.Resource.decode_relay_id(result["id"])

assert Edgehog.Containers.Release.Deployment.ReadyAction
assert Release.Deployment.ReadyAction
|> Ash.read_first!(tenant: tenant)
|> Map.fetch!(:deployment_id) == deployment_id
end
Expand All @@ -83,10 +82,8 @@ defmodule EdgehogWeb.Schema.Mutation.SendDeploymentUpgradeTest do

{:ok, %{id: deployment_id}} = AshGraphql.Resource.decode_relay_id(result["id"])

deployment = Ash.get!(Deployment, deployment_id, tenant: tenant)
deployment = Ash.get!(Release.Deployment, deployment_id, tenant: tenant)
set_resource_expectations([deployment_0_0_1, deployment])

Containers.deployment_update_status!(deployment)
end

test "fails if the deployments do not belong to the same application", args do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeploymentStartTest do

describe "startDeployment mutation tests" do
test "start on an existing deployment", %{tenant: tenant} do
deployment = deployment_fixture(tenant: tenant)
deployment = deployment_fixture(tenant: tenant, state: :stopped)

expect(DeploymentCommandMock, :send_deployment_command, 1, fn _, _, _ -> :ok end)

Expand Down

0 comments on commit 1a9f4cc

Please sign in to comment.