Skip to content

Commit c239e37

Browse files
committed
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.
1 parent f2092af commit c239e37

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

plugins/providers/docker/driver.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ def network_defined?(subnet_string)
348348

349349
network_info = inspect_network(all_networks)
350350
network_info.each do |network|
351-
config = network["IPAM"]["Config"]
352-
if (defined?(config.size) &&
353-
config.size > 0 &&
351+
config = Array(network["IPAM"]["Config"])
352+
if (config.size > 0 &&
354353
config.first["Subnet"] == subnet_string)
355354
@logger.debug("Found existing network #{network["Name"]} already configured with #{subnet_string}")
356355
return network["Name"]

test/unit/plugins/providers/docker/driver_test.rb

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,21 +667,68 @@
667667
let(:subnet_string) { "172.20.0.0/16" }
668668
let(:network_names) { ["vagrant_network_172.20.0.0/16", "bridge", "null" ] }
669669

670-
it "returns network name if defined" do
670+
before do
671671
allow(subject).to receive(:list_network_names).and_return(network_names)
672672
allow(subject).to receive(:inspect_network).and_return(JSON.load(docker_network_struct))
673+
end
673674

675+
it "returns network name if defined" do
674676
network_name = subject.network_defined?(subnet_string)
675677
expect(network_name).to eq("vagrant_network_172.20.0.0/16")
676678
end
677679

678680
it "returns nil name if not defined" do
679-
allow(subject).to receive(:list_network_names).and_return(network_names)
680-
allow(subject).to receive(:inspect_network).and_return(JSON.load(docker_network_struct))
681-
682681
network_name = subject.network_defined?("120.20.0.0/24")
683682
expect(network_name).to eq(nil)
684683
end
684+
685+
context "when config information is missing" do
686+
let(:docker_network_struct) do
687+
[
688+
{
689+
"Name": "bridge",
690+
"Id": "ae74f6cc18bbcde86326937797070b814cc71bfc4a6d8e3e8cf3b2cc5c7f4a7d",
691+
"Created": "2019-03-20T14:10:06.313314662-07:00",
692+
"Scope": "local",
693+
"Driver": "bridge",
694+
"EnableIPv6": false,
695+
"IPAM": {
696+
"Driver": "default",
697+
"Options": nil,
698+
},
699+
"Internal": false,
700+
"Attachable": false,
701+
"Ingress": false,
702+
"ConfigFrom": {
703+
"Network": ""
704+
},
705+
"ConfigOnly": false,
706+
"Containers": {
707+
"a1ee9b12bcea8268495b1f43e8d1285df1925b7174a695075f6140adb9415d87": {
708+
"Name": "vagrant-sandbox_docker-1_1553116237",
709+
"EndpointID": "fc1b0ed6e4f700cf88bb26a98a0722655191542e90df3e3492461f4d1f3c0cae",
710+
"MacAddress": "02:42:ac:11:00:02",
711+
"IPv4Address": "172.17.0.2/16",
712+
"IPv6Address": ""
713+
},
714+
"Options": {
715+
"com.docker.network.bridge.default_bridge": "true",
716+
"com.docker.network.bridge.enable_icc": "true",
717+
"com.docker.network.bridge.enable_ip_masquerade": "true",
718+
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
719+
"com.docker.network.bridge.name": "docker0",
720+
"com.docker.network.driver.mtu": "1500"
721+
},
722+
"Labels": {}
723+
},
724+
}
725+
].to_json
726+
end
727+
728+
it "should not raise an error" do
729+
expect { subject.network_defined?(subnet_string) }.not_to raise_error
730+
end
731+
end
685732
end
686733

687734
describe '#network_containing_address' do

0 commit comments

Comments
 (0)