From 734aad1ede74cfcfb0588b2f94552ddd69309d01 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 22 Nov 2019 12:32:51 -0800 Subject: [PATCH] Enforce unique names for disk config objects --- plugins/kernel_v2/config/vm.rb | 6 +++++- test/unit/plugins/kernel_v2/config/vm_test.rb | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index e4fff571c..595cdff54 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -812,7 +812,11 @@ module VagrantPlugins name: machine.name) end - # TODO: Check for duplicate disk names? + disk_names = @disks.map { |d| d.name } + duplicate_names = disk_names.detect{ |d| disk_names.count(d) > 1 } + if duplicate_names && duplicate_names.size + errors << "Duplicate disk names found: #{duplicate_names}. Please use unique names" + end @disks.each do |d| error = d.validate(machine) diff --git a/test/unit/plugins/kernel_v2/config/vm_test.rb b/test/unit/plugins/kernel_v2/config/vm_test.rb index 92f7d7e3c..43cb2fcfc 100644 --- a/test/unit/plugins/kernel_v2/config/vm_test.rb +++ b/test/unit/plugins/kernel_v2/config/vm_test.rb @@ -551,10 +551,12 @@ describe VagrantPlugins::Kernel_V2::VMConfig do describe "#disk" do it "stores the disks" do - subject.disk("disk", size: 100) - subject.disk("disk", size: 1000, primary: false, name: "storage") + subject.disk(:disk, size: 100) + subject.disk(:disk, size: 1000, primary: false, name: "storage") subject.finalize! + assert_valid + d = subject.disks expect(d.length).to eql(2) expect(d[0].size).to eql(100) @@ -562,9 +564,16 @@ describe VagrantPlugins::Kernel_V2::VMConfig do expect(d[1].name).to eql("storage") end + it "raises an error with duplicate names" do + subject.disk(:disk, size: 100, name: "foo") + subject.disk(:disk, size: 1000, name: "foo", primary: false) + subject.finalize! + assert_invalid + end + it "does not merge duplicate disks" do - subject.disk("disk", size: 1000, primary: false, name: "storage") - subject.disk("disk", size: 1000, primary: false, name: "backup") + subject.disk(:disk, size: 1000, primary: false, name: "storage") + subject.disk(:disk, size: 1000, primary: false, name: "backup") merged = subject.merge(subject) merged_disks = merged.disks @@ -573,10 +582,10 @@ describe VagrantPlugins::Kernel_V2::VMConfig do end it "ignores non-overriding runs" do - subject.disk("disk", name: "foo") + subject.disk(:disk, name: "foo") other = described_class.new - other.disk("disk", name: "bar", primary: false) + other.disk(:disk, name: "bar", primary: false) merged = subject.merge(other) merged_disks = merged.disks