Skip to content

Commit

Permalink
no longer required to send host_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
msawka committed Aug 7, 2015
1 parent 97698f3 commit c4d0cbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
23 changes: 21 additions & 2 deletions lib/fleet_manager/actions/restart_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule OpenAperture.FleetManager.FleetAction.RestartUnit do

alias OpenAperture.FleetManager.Request, as: FleetRequest

alias OpenAperture.Fleet.EtcdCluster

@doc """
Method to execute the following FleetManager action: :restart_unit
Expand All @@ -22,9 +24,26 @@ defmodule OpenAperture.FleetManager.FleetAction.RestartUnit do
@spec execute(FleetRequest.t) :: {:ok, Map} | {:error, String.t}
def execute(fleet_request) do
cond do
fleet_request.action_parameters[:host_ip] == nil -> {:error, "An invalid 'host_ip' parameter was provided!"}
fleet_request.action_parameters[:unit_name] == nil -> {:error, "An invalid 'unit_name' parameter was provided!"}
true -> execute_script(fleet_request.action_parameters[:host_ip], fleet_request.action_parameters[:unit_name])
true ->
hosts = EtcdCluster.get_hosts(fleet_request.etcd_token)
if hosts == nil || length(hosts) == 0 do
{:error, "Unable to find a valid host - No hosts are available in cluster #{fleet_request.etcd_token}!"}
else
cur_hosts_cnt = length(hosts)
if cur_hosts_cnt == 1 do
host = List.first(hosts)
else
:random.seed(:os.timestamp)
host = List.first(Enum.shuffle(hosts))
end

if (host != nil && host.primaryIP != nil) do
execute_script(host.primaryIP, fleet_request.action_parameters[:unit_name])
else
{:error, "Host does not have a valid primaryIP: #{inspect host}"}
end
end
end
end

Expand Down
33 changes: 26 additions & 7 deletions test/fleet_manager/actions/restart_unit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,40 @@ defmodule OpenAperture.FleetManager.FleetAction.RestartUnitTest do
alias OpenAperture.FleetManager.Request, as: FleetRequest
alias OpenAperture.FleetManager.FleetAction.RestartUnit

alias OpenAperture.Fleet.EtcdCluster

# ===================================
# execute tests

test "execute - invalid host_ip" do
{status, response} = RestartUnit.execute(%FleetRequest{})
test "execute - no unit_name" do
{status, response} = RestartUnit.execute(%FleetRequest{
action_parameters: %{
host_ip: "123.234.456.789"
}
})
assert status == :error
assert response != nil
end

test "execute - no unit_name" do
test "execute - no hosts available" do
:meck.new(EtcdCluster, [:passthrough])
:meck.expect(EtcdCluster, :get_hosts, fn _ -> [] end)

{status, response} = RestartUnit.execute(%FleetRequest{
action_parameters: %{
host_ip: "123.234.456.789"
unit_name: "[email protected]"
}
})
assert status == :error
assert response != nil
end
after
:meck.unload(EtcdCluster)
end

test "execute - success" do
:meck.new(EtcdCluster, [:passthrough])
:meck.expect(EtcdCluster, :get_hosts, fn _ -> [%FleetApi.Machine{primaryIP: "123.234.456.789"}, %FleetApi.Machine{primaryIP: "234.456.789.123"}] end)

test "execute - nodes" do
template = File.read!("#{System.cwd!()}/templates/node-info.sh.eex")

:meck.new(File, [:unstick])
Expand Down Expand Up @@ -51,11 +65,15 @@ defmodule OpenAperture.FleetManager.FleetAction.RestartUnitTest do
assert status == :ok
assert response != nil
after
:meck.unload(EtcdCluster)
:meck.unload(File)
:meck.unload(System)
end

test "execute - node info fails" do
test "execute - fails" do
:meck.new(EtcdCluster, [:passthrough])
:meck.expect(EtcdCluster, :get_hosts, fn _ -> [%FleetApi.Machine{primaryIP: "123.234.456.789"}, %FleetApi.Machine{primaryIP: "234.456.789.123"}] end)

template = File.read!("#{System.cwd!()}/templates/node-info.sh.eex")

:meck.new(File, [:unstick])
Expand Down Expand Up @@ -83,6 +101,7 @@ defmodule OpenAperture.FleetManager.FleetAction.RestartUnitTest do
assert status == :error
assert response != nil
after
:meck.unload(EtcdCluster)
:meck.unload(File)
:meck.unload(System)
end
Expand Down

0 comments on commit c4d0cbe

Please sign in to comment.