Skip to content

Commit

Permalink
Fixes #13966 - newer version of apipie validates types for arrays
Browse files Browse the repository at this point in the history
Therefore we no longer can send network interfaces in hashes even though
the server would cope with it.
  • Loading branch information
Tomas Strachota committed Mar 15, 2016
1 parent 361b8e8 commit b414187
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/hammer_cli_foreman/host.rb
Expand Up @@ -134,8 +134,10 @@ def nested_attributes(attrs)
end

def interfaces_attributes
return {} if option_interface_list.empty?

# move each attribute starting with "compute_" to compute_attributes
nic_list = option_interface_list.collect do |nic|
option_interface_list.collect do |nic|
compute_attributes = {}
nic.keys.each do |key|
if key.start_with? 'compute_'
Expand All @@ -145,7 +147,6 @@ def interfaces_attributes
nic['compute_attributes'] = compute_attributes unless compute_attributes.empty?
nic
end
nested_attributes(nic_list)
end

def check_mandatory_interfaces(nics)
Expand Down
10 changes: 6 additions & 4 deletions lib/hammer_cli_foreman/testing/api_expectations.rb
Expand Up @@ -9,11 +9,13 @@ def initialize(resource=nil, action=nil, &block)
end

def matches?(actual_parameters)
resource, action, params, headers, options = actual_parameters.shift(5)
action, params, headers, options = actual_parameters.shift(4)
action_name = action.name.to_s
resource_name = action.resource.to_s

result = true
result &&= (resource.to_s == @expected_resource.to_s) unless @expected_resource.nil?
result &&= (action.to_s == @expected_action.to_s) unless @expected_action.nil?
result &&= (resource_name == @expected_resource.to_s) unless @expected_resource.nil?
result &&= (action_name == @expected_action.to_s) unless @expected_action.nil?
result &&= @block.call(params) if @block
result
end
Expand All @@ -37,7 +39,7 @@ def set_note(note)
end

def api_expects(resource=nil, action=nil, note=nil, &block)
ex = ApipieBindings::API.any_instance.expects(:call)
ex = ApipieBindings::API.any_instance.expects(:call_action)
ex.extend(ExpectationExtensions)
ex.with(BlockMatcher.new(resource, action, &block))
ex.set_note(note)
Expand Down
1 change: 1 addition & 0 deletions test/data/1.11/foreman_api.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions test/functional/host_test.rb
@@ -0,0 +1,38 @@
require File.join(File.dirname(__FILE__), 'test_helper')


describe "host create" do
let(:cmd) { ["host", "create"] }
let(:minimal_params) { ['--hostgroup-id=1', '--location-id=1', '--organization-id=1', '--name=test'] }

it "passes interface attributes to server" do
params = ['--interface', 'identifier=eth0,ip=10.0.0.4,primary=true,provision=true']

api_expects(:hosts, :create, 'Create host with interfaces params') do |par|
ifces = par['host']['interfaces_attributes']

ifces.length == 1 &&
ifces[0]['ip'] == '10.0.0.4' &&
ifces[0]['identifier'] == 'eth0'
end.returns({})

expected_result = success_result("Host created\n")

result = run_cmd(cmd + minimal_params + params)
assert_cmd(expected_result, result)
end

it "sends empty hash for no interfaces" do
# For some reason the Foreman replaces empty interfaces_attributes to nil,
# which causes failure in nested attributes assignment in the host model

api_expects(:hosts, :create, 'Create host with empty interfaces') do |par|
par['host']['interfaces_attributes'] == {}
end.returns({})

expected_result = success_result("Host created\n")

result = run_cmd(cmd + minimal_params)
assert_cmd(expected_result, result)
end
end
4 changes: 2 additions & 2 deletions test/unit/host_test.rb
Expand Up @@ -227,8 +227,8 @@
it_should_call_action_and_test_params(:create) { |par| par["host"]["build"] == true }
it_should_call_action_and_test_params(:create) { |par| par["host"]["enabled"] == true }
it_should_call_action_and_test_params(:create) { |par| par["host"]["provision_method"] == "build" }
it_should_call_action_and_test_params(:create) { |par| par["host"]["interfaces_attributes"]["0"]["primary"] == "true" }
it_should_call_action_and_test_params(:create) { |par| par["host"]["interfaces_attributes"]["0"]["provision"] == "true" }
it_should_call_action_and_test_params(:create) { |par| par["host"]["interfaces_attributes"][0]["primary"] == "true" }
it_should_call_action_and_test_params(:create) { |par| par["host"]["interfaces_attributes"][0]["provision"] == "true" }
end

with_params ["--name=host", "--hostgroup-id=1", "--interface=primary=true,provision=true", "--parameters=servers=[pool.ntp.org,ntp.time.org],password=secret"] do
Expand Down

0 comments on commit b414187

Please sign in to comment.