Enforce unique names for disk config objects

This commit is contained in:
Brian Cain 2019-11-22 12:32:51 -08:00
parent b5b59a4eee
commit 734aad1ede
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 20 additions and 7 deletions

View File

@ -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)

View File

@ -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