Skip to content

Commit

Permalink
Check for missing local files in a cookbook on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
gregkare committed Apr 5, 2018
1 parent 07d3633 commit 9dd99bb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib/health_inspector/checklists/cookbooks.rb
Expand Up @@ -72,6 +72,55 @@ def validate_changes_on_the_server_not_in_the_repo
end
end

def validate_files_in_the_repo_not_on_the_server
return unless versions_exist? && versions_match?

begin
# Cast local (Chef::Version) into a string
cookbook_loader = Chef::Cookbook::CookbookVersionLoader.new(cookbook_path)
cookbook_loader.load!
local_cookbook = cookbook_loader.cookbook_version

remote_cookbook = Chef::CookbookVersion.load(name, local.to_s)

messages = []

Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
local_manifest_records = if Gem::Version.new(Chef::VERSION) < Gem::Version.new('13.0.0')
# `files_for` was introduced in Chef 13
local_cookbook.manifest[segment]
else
local_cookbook.files_for(segment)
end
remote_manifest_records = if Gem::Version.new(Chef::VERSION) < Gem::Version.new('13.0.0')
# `files_for` was introduced in Chef 13
remote_cookbook.manifest[segment]
else
remote_cookbook.files_for(segment)
end

local_manifest_records.each do |local_manifest_record|
local_path = local_manifest_record['path']
remote_manifest_record = remote_manifest_records.find { |r| r['path'] == local_path }
unless remote_manifest_record.nil?
messages << "#{local_path}" if local_manifest_record['checksum'] != remote_manifest_record['checksum']
else
messages << "#{local_path} does not exist on the server"
end
end
end

unless messages.empty?
message = "has a checksum mismatch between server and repo in\n"
message << messages.map { |f| " #{f}" }.join("\n")
errors.add message
end

rescue Net::HTTPServerException
errors.add "Could not find cookbook #{name} on the server"
end
end

def versions_exist?
local && server
end
Expand Down
Empty file.
42 changes: 41 additions & 1 deletion spec/health_inspector/checklists/cookbook_spec.rb
Expand Up @@ -2,7 +2,7 @@

RSpec.describe HealthInspector::Checklists::Cookbook do
let(:pairing) do
described_class.new(health_inspector_context, :name => 'dummy')
described_class.new(health_inspector_context, :name => 'cookbook_one')
end

it 'detects if an item does not exist locally' do
Expand Down Expand Up @@ -33,11 +33,51 @@
end

it 'detects if an item is the same' do
cookbook_version = double('Chef::CookbookVersion')
allow(cookbook_version).to receive(:files_for).with(:attributes).and_return([])
allow(cookbook_version).to receive(:files_for).with(:definitions).and_return([])
allow(cookbook_version).to receive(:files_for).with(:files).and_return([])
allow(cookbook_version).to receive(:files_for).with(:libraries).and_return([])
allow(cookbook_version).to receive(:files_for).with(:providers).and_return([])
allow(cookbook_version).to receive(:files_for).with(:recipes).and_return([{ "path" => "recipes/default.rb", "checksum" => "d41d8cd98f00b204e9800998ecf8427e" }])
allow(cookbook_version).to receive(:files_for).with(:root_files).and_return([{"path" => "metadata.rb", "checksum" => "feaa135db12fdc37c3cd155d16f80b5f"}])
allow(cookbook_version).to receive(:files_for).with(:resources).and_return([])
allow(cookbook_version).to receive(:files_for).with(:templates).and_return([])
expect(Chef::CookbookVersion).to receive(:load)
.with('cookbook_one', '0.0.1')
.and_return(
cookbook_version
)
expect(pairing).to receive(:validate_changes_on_the_server_not_in_the_repo)
pairing.server = '0.0.1'
pairing.local = '0.0.1'
pairing.validate

expect(pairing.errors).to be_empty
end

it 'detects if an item is not on the server' do
cookbook_version = double('Chef::CookbookVersion')
allow(cookbook_version).to receive(:files_for).with(:attributes).and_return([])
allow(cookbook_version).to receive(:files_for).with(:definitions).and_return([])
allow(cookbook_version).to receive(:files_for).with(:files).and_return([])
allow(cookbook_version).to receive(:files_for).with(:libraries).and_return([])
allow(cookbook_version).to receive(:files_for).with(:providers).and_return([])
allow(cookbook_version).to receive(:files_for).with(:recipes).and_return([])
allow(cookbook_version).to receive(:files_for).with(:root_files).and_return([{"path" => "metadata.rb", "checksum" => "feaa135db12fdc37c3cd155d16f80b5f"}])
allow(cookbook_version).to receive(:files_for).with(:resources).and_return([])
allow(cookbook_version).to receive(:files_for).with(:templates).and_return([])
expect(Chef::CookbookVersion).to receive(:load)
.with('cookbook_one', '0.0.1')
.and_return(
cookbook_version
)
expect(pairing).to receive(:validate_changes_on_the_server_not_in_the_repo)
pairing.server = '0.0.1'
pairing.local = '0.0.1'
pairing.validate

expect(pairing.errors).not_to be_empty
expect(pairing.errors.first).to eq("has a checksum mismatch between server and repo in\n recipes/default.rb does not exist on the server")
end
end

0 comments on commit 9dd99bb

Please sign in to comment.