Add tests for mutex util module
This commit is contained in:
parent
b91a5d5576
commit
f153996b2d
@ -22,7 +22,7 @@ module Vagrant
|
||||
end
|
||||
|
||||
def unlock
|
||||
File.delete(@mutex_path)
|
||||
File.delete(@mutex_path) if File.file?(@mutex_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -102,29 +102,6 @@ describe Vagrant::Action::Builtin::BoxAdd, :skip_windows, :bsdtar do
|
||||
end
|
||||
|
||||
context "with box file directly" do
|
||||
it "creates and cleans up a lock file" do
|
||||
box_path = iso_env.box2_file(:virtualbox)
|
||||
|
||||
env[:box_name] = "foo"
|
||||
env[:box_url] = box_path.to_s
|
||||
|
||||
|
||||
expect(box_collection).to receive(:add).with(any_args) { |path, name, version, opts|
|
||||
expect(checksum(path)).to eq(checksum(box_path))
|
||||
expect(name).to eq("foo")
|
||||
expect(version).to eq("0")
|
||||
expect(opts[:metadata_url]).to be_nil
|
||||
true
|
||||
}.and_return(box)
|
||||
|
||||
expect(app).to receive(:call).with(env)
|
||||
|
||||
mutex_path = env[:tmp_path].join("box" + Digest::SHA1.hexdigest("file://" + box_path.to_s) + ".lock").to_s
|
||||
expect(File).to receive(:write).with(mutex_path, "")
|
||||
expect(File).to receive(:delete).with(mutex_path)
|
||||
subject.call(env)
|
||||
end
|
||||
|
||||
it "adds it" do
|
||||
box_path = iso_env.box2_file(:virtualbox)
|
||||
|
||||
|
||||
49
test/unit/vagrant/util/file_mutex_test.rb
Normal file
49
test/unit/vagrant/util/file_mutex_test.rb
Normal file
@ -0,0 +1,49 @@
|
||||
require File.expand_path("../../../base", __FILE__)
|
||||
require 'vagrant/util/file_mutex'
|
||||
|
||||
describe Vagrant::Util::FileMutex do
|
||||
include_context "unit"
|
||||
|
||||
let(:temp_dir) { Dir.mktmpdir("vagrant-test-util-mutex_test") }
|
||||
|
||||
after do
|
||||
FileUtils.rm_rf(temp_dir)
|
||||
end
|
||||
|
||||
it "should create a lock file" do
|
||||
mutex_path = temp_dir + "test.lock"
|
||||
instance = described_class.new(mutex_path)
|
||||
instance.lock
|
||||
expect(File).to exist(mutex_path)
|
||||
end
|
||||
|
||||
it "should create and delete lock file" do
|
||||
mutex_path = temp_dir + "test.lock"
|
||||
instance = described_class.new(mutex_path)
|
||||
instance.lock
|
||||
instance.unlock
|
||||
expect(File).to_not exist(mutex_path)
|
||||
end
|
||||
|
||||
it "should not raise an error if the lock file does not exist" do
|
||||
mutex_path = temp_dir + "test.lock"
|
||||
instance = described_class.new(mutex_path)
|
||||
instance.unlock
|
||||
expect(File).to_not exist(mutex_path)
|
||||
end
|
||||
|
||||
it "should run a function with a lock" do
|
||||
mutex_path = temp_dir + "test.lock"
|
||||
instance = described_class.new(mutex_path)
|
||||
instance.with_lock { true }
|
||||
expect(File).to_not exist(mutex_path)
|
||||
end
|
||||
|
||||
it "should fail running a function when locked" do
|
||||
mutex_path = temp_dir + "test.lock"
|
||||
instance = described_class.new(mutex_path)
|
||||
instance.lock
|
||||
expect {instance.with_lock { true }}.
|
||||
to raise_error(Vagrant::Errors::VagrantLocked)
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user