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:
Chris Roberts 2024-01-23 13:57:04 -08:00
parent f2092af607
commit c239e37b2f
2 changed files with 53 additions and 7 deletions

View File

@ -348,9 +348,8 @@ module VagrantPlugins
network_info = inspect_network(all_networks)
network_info.each do |network|
config = network["IPAM"]["Config"]
if (defined?(config.size) &&
config.size > 0 &&
config = Array(network["IPAM"]["Config"])
if (config.size > 0 &&
config.first["Subnet"] == subnet_string)
@logger.debug("Found existing network #{network["Name"]} already configured with #{subnet_string}")
return network["Name"]

View File

@ -667,21 +667,68 @@ describe VagrantPlugins::DockerProvider::Driver do
let(:subnet_string) { "172.20.0.0/16" }
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(:inspect_network).and_return(JSON.load(docker_network_struct))
end
it "returns network name if defined" do
network_name = subject.network_defined?(subnet_string)
expect(network_name).to eq("vagrant_network_172.20.0.0/16")
end
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")
expect(network_name).to eq(nil)
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
describe '#network_containing_address' do