Force config value type and add test coverage
When extracting the config value from the data, force it to an Array type for the size check. Include a test case that includes missing configuration information to verify it does not produce an error.
This commit is contained in:
parent
f2092af607
commit
c239e37b2f
@ -348,9 +348,8 @@ module VagrantPlugins
|
|||||||
|
|
||||||
network_info = inspect_network(all_networks)
|
network_info = inspect_network(all_networks)
|
||||||
network_info.each do |network|
|
network_info.each do |network|
|
||||||
config = network["IPAM"]["Config"]
|
config = Array(network["IPAM"]["Config"])
|
||||||
if (defined?(config.size) &&
|
if (config.size > 0 &&
|
||||||
config.size > 0 &&
|
|
||||||
config.first["Subnet"] == subnet_string)
|
config.first["Subnet"] == subnet_string)
|
||||||
@logger.debug("Found existing network #{network["Name"]} already configured with #{subnet_string}")
|
@logger.debug("Found existing network #{network["Name"]} already configured with #{subnet_string}")
|
||||||
return network["Name"]
|
return network["Name"]
|
||||||
|
|||||||
@ -667,21 +667,68 @@ describe VagrantPlugins::DockerProvider::Driver do
|
|||||||
let(:subnet_string) { "172.20.0.0/16" }
|
let(:subnet_string) { "172.20.0.0/16" }
|
||||||
let(:network_names) { ["vagrant_network_172.20.0.0/16", "bridge", "null" ] }
|
let(:network_names) { ["vagrant_network_172.20.0.0/16", "bridge", "null" ] }
|
||||||
|
|
||||||
it "returns network name if defined" do
|
before do
|
||||||
allow(subject).to receive(:list_network_names).and_return(network_names)
|
allow(subject).to receive(:list_network_names).and_return(network_names)
|
||||||
allow(subject).to receive(:inspect_network).and_return(JSON.load(docker_network_struct))
|
allow(subject).to receive(:inspect_network).and_return(JSON.load(docker_network_struct))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns network name if defined" do
|
||||||
network_name = subject.network_defined?(subnet_string)
|
network_name = subject.network_defined?(subnet_string)
|
||||||
expect(network_name).to eq("vagrant_network_172.20.0.0/16")
|
expect(network_name).to eq("vagrant_network_172.20.0.0/16")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil name if not defined" do
|
it "returns nil name if not defined" do
|
||||||
allow(subject).to receive(:list_network_names).and_return(network_names)
|
|
||||||
allow(subject).to receive(:inspect_network).and_return(JSON.load(docker_network_struct))
|
|
||||||
|
|
||||||
network_name = subject.network_defined?("120.20.0.0/24")
|
network_name = subject.network_defined?("120.20.0.0/24")
|
||||||
expect(network_name).to eq(nil)
|
expect(network_name).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when config information is missing" do
|
||||||
|
let(:docker_network_struct) do
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"Name": "bridge",
|
||||||
|
"Id": "ae74f6cc18bbcde86326937797070b814cc71bfc4a6d8e3e8cf3b2cc5c7f4a7d",
|
||||||
|
"Created": "2019-03-20T14:10:06.313314662-07:00",
|
||||||
|
"Scope": "local",
|
||||||
|
"Driver": "bridge",
|
||||||
|
"EnableIPv6": false,
|
||||||
|
"IPAM": {
|
||||||
|
"Driver": "default",
|
||||||
|
"Options": nil,
|
||||||
|
},
|
||||||
|
"Internal": false,
|
||||||
|
"Attachable": false,
|
||||||
|
"Ingress": false,
|
||||||
|
"ConfigFrom": {
|
||||||
|
"Network": ""
|
||||||
|
},
|
||||||
|
"ConfigOnly": false,
|
||||||
|
"Containers": {
|
||||||
|
"a1ee9b12bcea8268495b1f43e8d1285df1925b7174a695075f6140adb9415d87": {
|
||||||
|
"Name": "vagrant-sandbox_docker-1_1553116237",
|
||||||
|
"EndpointID": "fc1b0ed6e4f700cf88bb26a98a0722655191542e90df3e3492461f4d1f3c0cae",
|
||||||
|
"MacAddress": "02:42:ac:11:00:02",
|
||||||
|
"IPv4Address": "172.17.0.2/16",
|
||||||
|
"IPv6Address": ""
|
||||||
|
},
|
||||||
|
"Options": {
|
||||||
|
"com.docker.network.bridge.default_bridge": "true",
|
||||||
|
"com.docker.network.bridge.enable_icc": "true",
|
||||||
|
"com.docker.network.bridge.enable_ip_masquerade": "true",
|
||||||
|
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
|
||||||
|
"com.docker.network.bridge.name": "docker0",
|
||||||
|
"com.docker.network.driver.mtu": "1500"
|
||||||
|
},
|
||||||
|
"Labels": {}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
].to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not raise an error" do
|
||||||
|
expect { subject.network_defined?(subnet_string) }.not_to raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#network_containing_address' do
|
describe '#network_containing_address' do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user