Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #371 from renatomassaro/fix-370
Browse files Browse the repository at this point in the history
Make ComponentHenforcer handle empty components on MotherboardUpdateRequest
  • Loading branch information
renatomassaro committed Jan 11, 2018
2 parents a393f98 + ea6ff89 commit aeb2d52
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/server/henforcer/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ defmodule Helix.Server.Henforcer.Component do
components
|> Enum.reduce_while(init, fn {slot_id, comp_id}, {{true, acc}, cache} ->
with \
true <- not is_nil(comp_id) || :empty_component,
{true, r1} <- component_exists?(comp_id),
component = r1.component,

Expand All @@ -184,6 +185,11 @@ defmodule Helix.Server.Henforcer.Component do

{:cont, {{true, new_acc}, r2.owned_components}}
else
# Component may be empty, which is a valid input that does not need to
# go through the standard verifications above
:empty_component ->
{:cont, {{true, acc}, cache}}

error ->
{:halt, {error, cache}}
end
Expand Down Expand Up @@ -253,9 +259,6 @@ defmodule Helix.Server.Henforcer.Component do
{true, _} <- has_public_nip?(r2.network_connections)
do
reply_ok(relay([r0, r1, r2]))
else
error ->
error
end
end

Expand Down
56 changes: 55 additions & 1 deletion test/server/websocket/requests/motherboard_update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,61 @@ defmodule Helix.Server.Websocket.Requests.MotherboardUpdateTest do
assert request.meta.entity_network_connections
end

test "rejects when something is wrong (update)" do
@tag :regression
test "rejects when it's missing the minimum components" do
{server, %{entity: entity}} = ServerSetup.server()

{cpu, _} = ComponentSetup.component(type: :cpu)
{ram, _} = ComponentSetup.component(type: :ram)
{nic, _} = ComponentSetup.component(type: :nic)
{hdd, _} = ComponentSetup.component(type: :hdd)

EntityAction.link_component(entity, cpu)
EntityAction.link_component(entity, ram)
EntityAction.link_component(entity, nic)
EntityAction.link_component(entity, hdd)

# Create a new NC
{:ok, new_nc} =
NetworkAction.Connection.create(@internet_id, Random.ipv4(), entity)

random_component = Enum.random(["cpu_1", "ram_1", "nic_1", "hdd_1"])

params =
%{
"motherboard_id" => to_string(server.motherboard_id),
"slots" => %{
"cpu_1" => to_string(cpu.component_id),
"ram_1" => to_string(ram.component_id),
"hdd_1" => to_string(hdd.component_id),
"nic_1" => to_string(nic.component_id)
},
"network_connections" => %{
to_string(nic.component_id) => %{
"ip" => Random.ipv4(), # This IP does not belong to me!!11!
"network_id" => to_string(new_nc.network_id)
}
}
}
# This will ensure one of the slots will be missing its component
|> put_in(["slots", random_component], nil)

socket =
ChannelSetup.mock_server_socket(
gateway_id: server.server_id,
gateway_entity_id: entity.entity_id,
access: :local
)

request = MotherboardUpdateRequest.new(params)
assert {:ok, request} = Requestable.check_params(request, socket)
assert {:error, %{message: reason}, _} =
Requestable.check_permissions(request, socket)

assert reason == "motherboard_missing_initial_components"
end

test "rejects when something nip is invalid" do
{server, %{entity: entity}} = ServerSetup.server()

{cpu, _} = ComponentSetup.component(type: :cpu)
Expand Down

0 comments on commit aeb2d52

Please sign in to comment.