From 96a19aa00c431bc2fa6bfcf9d07c51f684002e38 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 21 Mar 2019 11:15:41 -0700 Subject: [PATCH] Fix how options to cli args are handled Since options could also be defined as strings, convert it all to string and compare those instead --- .../docker/action/prepare_networks.rb | 6 ++-- .../docker/action/prepare_networks_test.rb | 32 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/plugins/providers/docker/action/prepare_networks.rb b/plugins/providers/docker/action/prepare_networks.rb index 4740ae0d2..e1ff59992 100644 --- a/plugins/providers/docker/action/prepare_networks.rb +++ b/plugins/providers/docker/action/prepare_networks.rb @@ -20,13 +20,13 @@ module VagrantPlugins # Generate CLI arguments for creating the docker network. # # @param [Hash] options Options from the network config - # @returns[Array Network create arguments + # @returns[Array] Network create arguments def generate_create_cli_arguments(options) options.map do |key, value| # If value is false, option is not set - next if value == false + next if value.to_s == "false" # If value is true, consider feature flag with no value - opt = value == true ? [] : [value] + opt = value.to_s == "true" ? [] : [value] opt.unshift("--#{key.to_s.tr("_", "-")}") end.flatten.compact end diff --git a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb index d12fe391f..84b5c829c 100644 --- a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb +++ b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb @@ -163,22 +163,6 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do end end - describe "#generate_connect_cli_arguments" do - let(:network_options) { - {:ip=>"172.20.128.2", - :subnet=>"172.20.0.0/16", - :driver=>"bridge", - :internal=>"true", - :alias=>"mynetwork", - :protocol=>"tcp", - :id=>"80e017d5-388f-4a2f-a3de-f8dce8156a58"} } - - it "returns an array of cli arguments" do - cli_args = subject.generate_connect_cli_arguments(network_options) - expect(cli_args).to eq(["--ip", "172.20.128.2", "--alias=mynetwork"]) - end - end - describe "#generate_create_cli_arguments" do let(:network_options) { {:ip=>"172.20.128.2", @@ -189,9 +173,23 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do :protocol=>"tcp", :id=>"80e017d5-388f-4a2f-a3de-f8dce8156a58"} } + let(:false_network_options) { + {:ip=>"172.20.128.2", + :subnet=>"172.20.0.0/16", + :driver=>"bridge", + :internal=>"false", + :alias=>"mynetwork", + :protocol=>"tcp", + :id=>"80e017d5-388f-4a2f-a3de-f8dce8156a58"} } + it "returns an array of cli arguments" do cli_args = subject.generate_create_cli_arguments(network_options) - expect(cli_args).to eq(["--subnet=172.20.0.0/16", "--driver=bridge", "--internal=true"]) + expect(cli_args).to eq( ["--ip", "172.20.128.2", "--subnet", "172.20.0.0/16", "--driver", "bridge", "--internal", "--alias", "mynetwork", "--protocol", "tcp", "--id", "80e017d5-388f-4a2f-a3de-f8dce8156a58"]) + end + + it "removes option if set to false" do + cli_args = subject.generate_create_cli_arguments(false_network_options) + expect(cli_args).to eq( ["--ip", "172.20.128.2", "--subnet", "172.20.0.0/16", "--driver", "bridge", "--alias", "mynetwork", "--protocol", "tcp", "--id", "80e017d5-388f-4a2f-a3de-f8dce8156a58"]) end end end