diff --git a/plugins/providers/docker/action/create.rb b/plugins/providers/docker/action/create.rb index 3d2808917..770003579 100644 --- a/plugins/providers/docker/action/create.rb +++ b/plugins/providers/docker/action/create.rb @@ -91,9 +91,7 @@ module VagrantPlugins def create_params container_name = @provider_config.name if !container_name - container_name = "#{@env[:root_path].basename.to_s}_#{@machine.name}" - container_name.gsub!(/[^-a-z0-9_]/i, "") - container_name << "_#{Time.now.to_i}" + container_name = generate_container_name end image = @env[:create_image] @@ -156,6 +154,15 @@ module VagrantPlugins result end + + def generate_container_name + container_name = "#{@env[:root_path].basename.to_s}_#{@machine.name}" + # Remove leading -/_ and any non-alphanumeric, non-hyphen/underscore characters + container_name.gsub!(/\A[^a-zA-Z0-9]+|[^-a-z0-9_]/i, "") + container_name << "_#{Time.now.to_i}" + container_name + end + end end end diff --git a/test/unit/plugins/providers/docker/action/create_test.rb b/test/unit/plugins/providers/docker/action/create_test.rb index 9a35ff3f4..855af811e 100644 --- a/test/unit/plugins/providers/docker/action/create_test.rb +++ b/test/unit/plugins/providers/docker/action/create_test.rb @@ -55,4 +55,62 @@ describe VagrantPlugins::DockerProvider::Action::Create do expect(result).to eq(["8125:8125", "8125:8125/udp"]) end end + + describe "#generate_container_name" do + let(:env) {{root_path: root_path}} + let(:root_path) {Pathname.new("/path/to/root")} + + before do + subject.instance_variable_set(:@env, env) + subject.instance_variable_set(:@machine, machine) + end + + it "should not remove any characters" do + expect(subject.generate_container_name).to start_with("root") + end + + context "when root path starts with underscores" do + let(:root_path) {Pathname.new("/path/to/__root")} + it "should remove underscores" do + expect(subject.generate_container_name).to start_with("root") + end + end + + context "when root path starts with dashes" do + let(:root_path) {Pathname.new("/path/to/--root")} + it "should remove dashes" do + expect(subject.generate_container_name).to start_with("root") + end + end + + context "when root path starts with combination of invalid starting characters" do + let(:root_path) {Pathname.new("/path/to/_.-_-root")} + it "should remove invalid starting characters" do + expect(subject.generate_container_name).to start_with("root") + end + end + + context "when root path ends with underscores" do + let(:root_path) {Pathname.new("/path/to/root__")} + it "should not remove trailing underscroes" do + expect(subject.generate_container_name).to start_with("root_") + end + end + + context "when root path ends with invalid characters" do + let(:root_path) {Pathname.new("/path/to/root_.")} + it "should remove invalid trailing characters" do + expect(subject.generate_container_name).to start_with("root_") + end + end + + context "when root path contains invalid characters" do + let(:root_path) {Pathname.new("/path/to/root-.path_.")} + it "should only remove invalid characters" do + expect(subject.generate_container_name).to start_with("root-path_") + end + end + + end + end