Skip to content

Commit

Permalink
Don't apply update by default if the UUID is the same
Browse files Browse the repository at this point in the history
This is to help handle an edge case where (for whatever reason) the NervesHub
server may send a second update request despite the device already having
updated. In that case, if the request is the same UUID of the currently running
firmware, then we ignore the request and log it
  • Loading branch information
jjcarstens committed Aug 8, 2022
1 parent d458dd9 commit 2550f22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/nerves_hub_link/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule NervesHubLink.Client do
require Logger

@typedoc "Update that comes over a socket."
@type update_data :: map()
@type update_data :: NervesHubLinkCommon.Message.UpdateInfo.t()

@typedoc "Supported responses from `update_available/1`"
@type update_response :: :apply | :ignore | {:reschedule, pos_integer()}
Expand Down
14 changes: 13 additions & 1 deletion lib/nerves_hub_link/client/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ defmodule NervesHubLink.Client.Default do
require Logger

@impl NervesHubLink.Client
def update_available(_), do: :apply
def update_available(update_info) do
if update_info.firmware_meta.uuid == Nerves.Runtime.KV.get_active("nerves_fw_uuid") do
Logger.info("""
[NervesHubLink.Client] Ignoring request to update to the same firmware
#{inspect(update_info)}
""")

:ignore
else
:apply
end
end

@impl NervesHubLink.Client
def handle_fwup_message({:progress, percent}) do
Expand Down
17 changes: 16 additions & 1 deletion test/nerves_hub_link/client/default_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
defmodule NervesHubLink.Client.DefaultTest do
use ExUnit.Case, async: true
alias NervesHubLink.Client.Default
alias NervesHubLinkCommon.Message.{FirmwareMetadata, UpdateInfo}

import ExUnit.CaptureIO

doctest Default

@update_info %UpdateInfo{
firmware_url: "https://nerves-hub.org/firmware/1234",
firmware_meta: %FirmwareMetadata{}
}

test "update_available/1" do
assert Default.update_available(-1) == :apply
assert Default.update_available(@update_info) == :apply
end

test "update_avialable with same uuid" do
update_info =
put_in(@update_info.firmware_meta.uuid, Nerves.Runtime.KV.get_active("nerves_fw_uuid"))

assert Default.update_available(update_info) == :ignore
end

describe "handle_fwup_message/1" do
Expand Down

0 comments on commit 2550f22

Please sign in to comment.