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

Fixes #27353 - use vmware network id instead of name #6918

Merged
merged 1 commit into from Oct 2, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/models/compute_resources/foreman/model/vmware.rb
Expand Up @@ -460,10 +460,11 @@ def parse_networks(args)
args = args.deep_dup
dc_networks = networks
args["interfaces_attributes"]&.each do |key, interface|
# Convert network id into name
net = dc_networks.detect { |n| [n.id, n.name].include?(interface['network']) }
# Consolidate network to network id
net = dc_networks.detect { |n| n.id == interface['network'] }
net ||= dc_networks.detect { |n| n.name == interface['network'] }
raise "Unknown Network ID: #{interface['network']}" if net.nil?
interface["network"] = net.name
interface["network"] = net.id
interface["virtualswitch"] = net.virtualswitch
end
args
Expand Down
54 changes: 39 additions & 15 deletions test/models/compute_resources/vmware_test.rb
Expand Up @@ -396,30 +396,40 @@ class Foreman::Model::VmwareTest < ActiveSupport::TestCase
end

describe "#parse_networks" do
def mock_network(id, name, virtualswitch = nil)
mock_network = mock('network')
mock_network.stubs('id').returns(id)
mock_network.stubs('name').returns(name)
mock_network.stubs('virtualswitch').returns(virtualswitch)
mock_network
end

setup do
@mock_network = mock('network')
@mock_network.stubs('id').returns('network-17')
@mock_network.stubs('name').returns('Test network')
@mock_network.stubs('virtualswitch').returns(nil)
@cr = FactoryBot.build_stubbed(:vmware_cr)
@cr.stubs(:networks).returns([@mock_network])
@cr.stubs(:networks).returns(
[
mock_network('network-17', 'Test network'),
mock_network('network-11', 'network-14'),
mock_network('network-14', 'Network name'),
]
)
end

test "converts empty hash" do
assert_equal({}, @cr.parse_networks(HashWithIndifferentAccess.new))
end

test "converts form network ID to network name" do
test "converts form network name to network ID" do
attrs_in = HashWithIndifferentAccess.new(
"interfaces_attributes" => {
"new_interfaces" => {
"type" => "VirtualE1000",
"network" => "network-17",
"network" => "Test network",
"_delete" => "",
},
"0" => {
"type" => "VirtualVmxnet3",
"network" => "network-17",
"network" => "Test network",
"_delete" => "",
},
}
Expand All @@ -428,13 +438,13 @@ class Foreman::Model::VmwareTest < ActiveSupport::TestCase
"interfaces_attributes" => {
"new_interfaces" => {
"type" => "VirtualE1000",
"network" => "Test network",
"network" => "network-17",
"virtualswitch" => nil,
"_delete" => "",
},
"0" => {
"type" => "VirtualVmxnet3",
"network" => "Test network",
"network" => "network-17",
"virtualswitch" => nil,
"_delete" => "",
},
Expand All @@ -443,18 +453,32 @@ class Foreman::Model::VmwareTest < ActiveSupport::TestCase
assert_equal attrs_out, @cr.parse_networks(attrs_in)
end

test "ignores existing network names" do
test "matches the network id, before name lookup" do
attrs = HashWithIndifferentAccess.new(
"interfaces_attributes" => {
"0" => {
"type" => "VirtualVmxnet3",
"network" => "network-14",
"virtualswitch" => nil,
"_delete" => "",
},
}
)
assert_equal attrs, @cr.parse_networks(attrs)
end

test "ignores existing network IDs" do
attrs = HashWithIndifferentAccess.new(
"interfaces_attributes" => {
"new_interfaces" => {
"type" => "VirtualE1000",
"network" => "Test network",
"network" => "network-17",
"virtualswitch" => nil,
"_delete" => "",
},
"0" => {
"type" => "VirtualVmxnet3",
"network" => "Test network",
"network" => "network-17",
"virtualswitch" => nil,
"_delete" => "",
},
Expand All @@ -465,9 +489,9 @@ class Foreman::Model::VmwareTest < ActiveSupport::TestCase

test "doesn't modify input hash" do
# else compute profiles won't save properly
attrs_in = HashWithIndifferentAccess.new("interfaces_attributes" => {"0" => {"network" => "network-17"}})
attrs_in = HashWithIndifferentAccess.new("interfaces_attributes" => {"0" => {"network" => "Test network"}})
@cr.parse_args(attrs_in)
assert_equal "network-17", attrs_in["interfaces_attributes"]["0"]["network"]
assert_equal "Test network", attrs_in["interfaces_attributes"]["0"]["network"]
end
end

Expand Down