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

Commit

Permalink
Include named_run_lists when deserializing a lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Sep 18, 2015
1 parent 4e9aee7 commit 899b510
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/chef-dk/policyfile_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def build_from_compiler(compiler)
def build_from_lock_data(lock_data)
set_name_from_lock_data(lock_data)
set_run_list_from_lock_data(lock_data)
set_named_run_lists_from_lock_data(lock_data)
set_cookbook_locks_from_lock_data(lock_data)
set_attributes_from_lock_data(lock_data)
set_solution_dependencies_from_lock_data(lock_data)
Expand All @@ -264,6 +265,7 @@ def build_from_lock_data(lock_data)
def build_from_archive(lock_data)
set_name_from_lock_data(lock_data)
set_run_list_from_lock_data(lock_data)
set_named_run_lists_from_lock_data(lock_data)
set_cookbook_locks_as_archives_from_lock_data(lock_data)
set_attributes_from_lock_data(lock_data)
set_solution_dependencies_from_lock_data(lock_data)
Expand Down Expand Up @@ -416,6 +418,34 @@ def set_run_list_from_lock_data(lock_data)
@run_list = run_list_attribute
end

def set_named_run_lists_from_lock_data(lock_data)
return unless lock_data.key?("named_run_lists")

lock_data_named_run_lists = lock_data["named_run_lists"]

unless lock_data_named_run_lists.kind_of?(Hash)
msg = "lockfile's named_run_lists must be a Hash (JSON object). (got: #{lock_data_named_run_lists.inspect})"
raise InvalidLockfile, msg
end

lock_data_named_run_lists.each do |name, run_list|
unless name.kind_of?(String)
msg = "Keys in lockfile's named_run_lists must be Strings. (got: #{name.inspect})"
raise InvalidLockfile, msg
end
unless run_list.kind_of?(Array)
msg = "Values in lockfile's named_run_lists must be Arrays. (got: #{run_list.inspect})"
raise InvalidLockfile, msg
end
bad_run_list_items = run_list.select { |e| e !~ RUN_LIST_ITEM_FORMAT }
unless bad_run_list_items.empty?
msg = "lockfile's run_list items must be formatted like `recipe[$COOKBOOK_NAME::$RECIPE_NAME]'. Invalid items: `#{bad_run_list_items.join("' `")}'"
raise InvalidLockfile, msg
end
end
@named_run_lists = lock_data_named_run_lists
end

def set_cookbook_locks_from_lock_data(lock_data)
cookbook_lock_data = lock_data["cookbook_locks"]

Expand Down
41 changes: 41 additions & 0 deletions spec/unit/policyfile_lock_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"name" => "example",
"run_list" => [ "recipe[cookbook::recipe_name]" ],
"named_run_lists" => {
"fast-deploy" => [ "recipe[cookbook::deployit]" ]
},
"cookbook_locks" => {
# TODO: add some valid locks
},
Expand All @@ -50,6 +53,10 @@
expect(lockfile.run_list).to eq(["recipe[cookbook::recipe_name]"])
end

it "includes the named run lists" do
expect(lockfile.named_run_lists).to eq({ "fast-deploy" => [ "recipe[cookbook::deployit]" ] })
end

it "includes the cookbook locks" do
expect(lockfile.cookbook_locks).to eq({})
end
Expand Down Expand Up @@ -100,6 +107,40 @@
expect { lockfile.build_from_lock_data(bad_run_list) }.to raise_error(ChefDK::InvalidLockfile)
end

it "allows the named_run_lists field to be absent" do
missing_named_run_lists = valid_lock_data.dup
missing_named_run_lists.delete("named_run_lists")

expect { lockfile.build_from_lock_data(missing_named_run_lists) }.to_not raise_error
end

it "requires the named_run_lists field to be a Hash if present" do
bad_named_run_lists = valid_lock_data.dup
bad_named_run_lists["named_run_lists"] = false

expect { lockfile.build_from_lock_data(bad_named_run_lists) }.to raise_error(ChefDK::InvalidLockfile)
end

it "requires the keys in named_run_lists to be strings" do
bad_named_run_lists = valid_lock_data.dup
bad_named_run_lists["named_run_lists"] = { 42 => [] }

expect { lockfile.build_from_lock_data(bad_named_run_lists) }.to raise_error(ChefDK::InvalidLockfile)
end

it "requires the values in named_run_lists to be arrays" do
bad_named_run_lists = valid_lock_data.dup
bad_named_run_lists["named_run_lists"] = { "bad" => 42 }

expect { lockfile.build_from_lock_data(bad_named_run_lists) }.to raise_error(ChefDK::InvalidLockfile)
end

it "requires the values in named_run_lists to be valid run lists" do
bad_named_run_lists = valid_lock_data.dup
bad_named_run_lists["named_run_lists"] = { "bad" => [ 42 ] }

expect { lockfile.build_from_lock_data(bad_named_run_lists) }.to raise_error(ChefDK::InvalidLockfile)
end
it "requires the `cookbook_locks` section be present and its value is a Hash" do
missing_locks = valid_lock_data.dup
missing_locks.delete("cookbook_locks")
Expand Down

0 comments on commit 899b510

Please sign in to comment.