Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Make export dir cleanliness checking more granular
Browse files Browse the repository at this point in the history
ExportRepo now only fails if there are files in cookbooks/ or
data_bags/policyfiles/. Fixes an issue where export would not work with
test kitchen when other kitchen directories were created first.
  • Loading branch information
danielsdeleo committed Dec 5, 2014
1 parent 9de839f commit b98117e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
31 changes: 23 additions & 8 deletions lib/chef-dk/policyfile_services/export_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def policy_name

def run
assert_lockfile_exists!
assert_export_dir_empty!
assert_export_dir_clean!

validate_lockfile
write_updated_lockfile
Expand Down Expand Up @@ -86,10 +86,12 @@ def export
private

def create_repo_structure
FileUtils.rm_rf(export_dir)
FileUtils.rm_rf(cookbooks_dir)
FileUtils.rm_rf(policyfiles_data_bag_dir)

FileUtils.mkdir_p(export_dir)
FileUtils.mkdir_p(File.join(export_dir, "cookbooks"))
FileUtils.mkdir_p(File.join(export_dir, "data_bags", "policyfiles"))
FileUtils.mkdir_p(cookbooks_dir)
FileUtils.mkdir_p(policyfiles_data_bag_dir)
end

def copy_cookbooks
Expand Down Expand Up @@ -161,17 +163,30 @@ def assert_lockfile_exists!
end
end

def assert_export_dir_empty!
entries = Dir.glob(File.join(export_dir, "*"))
if !force_export? && !entries.empty?
raise ExportDirNotEmpty, "Export dir (#{export_dir}) not empty. Refusing to export."
def assert_export_dir_clean!
if !force_export? && !conflicting_fs_entries.empty?
msg = "Export dir (#{export_dir}) not clean. Refusing to export. (Conflicting files: #{conflicting_fs_entries.join(', ')})"
raise ExportDirNotEmpty, msg
end
end

def force_export?
@force_export
end

def conflicting_fs_entries
Dir.glob(File.join(cookbooks_dir, "*")) +
Dir.glob(File.join(policyfiles_data_bag_dir, "*"))
end

def cookbooks_dir
File.join(export_dir, "cookbooks")
end

def policyfiles_data_bag_dir
File.join(export_dir, "data_bags", "policyfiles")
end

end

end
Expand Down
3 changes: 1 addition & 2 deletions lib/kitchen/provisioner/policyfile_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def prepare_cookbooks
#
# @api private
def policy_exporter
return @policy_exporter if @policy_exporter
@policy_exporter = ChefDK::PolicyfileServices::ExportRepo.new(export_dir: sandbox_path)
@policy_exporter ||= ChefDK::PolicyfileServices::ExportRepo.new(export_dir: sandbox_path)
end

# Writes a fake (but valid) validation.pem into the sandbox directory.
Expand Down
54 changes: 48 additions & 6 deletions spec/unit/policyfile_services/export_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,59 @@

end

context "When the export dir is already populated" do
context "When the export dir has non-conflicting content" do

let(:file_in_export_dir) { File.join(export_dir, "some_random_cruft") }

let(:extra_data_bag_dir) { File.join(export_dir, "data_bags", "extraneous") }

let(:extra_data_bag_item) { File.join(extra_data_bag_dir, "an_item.json") }

before do
FileUtils.mkdir_p(export_dir)
File.open(file_in_export_dir, "wb+") { |f| f.print "some random cruft" }
FileUtils.mkdir_p(extra_data_bag_dir)
File.open(extra_data_bag_item, "wb+") { |f| f.print "some random cruft" }
end

it "ignores the non-conflicting content and exports" do
export_service.run

expect(File).to exist(file_in_export_dir)
expect(File).to exist(extra_data_bag_item)

expect(File).to be_directory(File.join(export_dir, "cookbooks"))
expect(File).to be_directory(File.join(export_dir, "data_bags"))
end
end

context "When the export dir has conflicting content" do

let(:non_conflicting_file_in_export_dir) { File.join(export_dir, "some_random_cruft") }

let(:cookbooks_dir) { File.join(export_dir, "cookbooks") }

let(:file_in_cookbooks_dir) { File.join(cookbooks_dir, "some_random_cruft") }

let(:policyfiles_data_bag_dir) { File.join(export_dir, "data_bags", "policyfiles") }

let(:extra_policyfile_data_item) { File.join(policyfiles_data_bag_dir, "leftover-policy.json") }

before do
FileUtils.mkdir_p(export_dir)
FileUtils.mkdir_p(cookbooks_dir)
FileUtils.mkdir_p(policyfiles_data_bag_dir)
File.open(non_conflicting_file_in_export_dir, "wb+") { |f| f.print "some random cruft" }
File.open(file_in_cookbooks_dir, "wb+") { |f| f.print "some random cruft" }
File.open(extra_policyfile_data_item, "wb+") { |f| f.print "some random cruft" }
end

it "raises a PolicyfileExportRepoError" do
message = "Export dir (#{export_dir}) not empty. Refusing to export."
message = "Export dir (#{export_dir}) not clean. Refusing to export. (Conflicting files: #{file_in_cookbooks_dir}, #{extra_policyfile_data_item})"
expect { export_service.run }.to raise_error(ChefDK::ExportDirNotEmpty, message)
expect(File).to be_file(file_in_export_dir)
expect(File).to_not exist(File.join(export_dir, "cookbooks"))
expect(File).to_not exist(File.join(export_dir, "data_bags"))
expect(File).to exist(non_conflicting_file_in_export_dir)
expect(File).to exist(file_in_cookbooks_dir)
expect(File).to exist(extra_policyfile_data_item)
end

context "and the force option is set" do
Expand All @@ -259,7 +297,11 @@
it "clears the export dir and exports" do
export_service.run

expect(File).to_not exist(file_in_export_dir)
expect(File).to_not exist(file_in_cookbooks_dir)
expect(File).to_not exist(extra_policyfile_data_item)

expect(File).to exist(non_conflicting_file_in_export_dir)

expect(File).to be_directory(File.join(export_dir, "cookbooks"))
expect(File).to be_directory(File.join(export_dir, "data_bags"))
end
Expand Down

0 comments on commit b98117e

Please sign in to comment.