Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a device when calling Deployments.verify_deployment_membership/1 #1894

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ defmodule NervesHub.Deployments do
|> Repo.update!()

DeploymentTemplates.audit_deployment_mismatch(device, deployment, reason)

device
else
device
end
Expand Down
43 changes: 43 additions & 0 deletions test/nerves_hub/deployments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule NervesHub.DeploymentsTest do

import Phoenix.ChannelTest

alias NervesHub.AuditLogs
alias NervesHub.Deployments
alias NervesHub.Devices
alias NervesHub.Devices.Device
alias NervesHub.Fixtures

Expand Down Expand Up @@ -294,4 +296,45 @@ defmodule NervesHub.DeploymentsTest do
assert [] == Deployments.matching_deployments(%Device{firmware_metadata: nil}, [false])
end
end

describe "verify_deployment_membership/1" do
setup %{org: org, product: product, firmware: firmware} = context do
Map.merge(context, %{
device: Fixtures.device_fixture(org, product, firmware, %{tags: ["beta", "rpi"]})
})
end

test "does nothing when device has no deployment", %{device: device} do
refute device.deployment_id
device = Deployments.verify_deployment_membership(device)
refute device.deployment_id
end

test "does nothing when device has deployment and meets matching conditions", %{
device: device,
deployment: deployment
} do
device = Devices.update_deployment(device, deployment)
assert device.deployment_id

device = Deployments.verify_deployment_membership(device)
assert device.deployment_id
end

test "removes device from deployment and creates audit log when conditions aren't met", %{
device: device,
deployment: deployment
} do
{:ok, device} =
device
|> Devices.update_deployment(deployment)
|> Devices.update_firmware_metadata(%{"platform" => "foobar"})

device = Deployments.verify_deployment_membership(device)
refute device.deployment_id

[audit_log] = AuditLogs.logs_for(deployment)
assert audit_log.description =~ "no longer matches deployment"
end
end
end
Loading