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

Commit 01610f5

Browse files
committed
Ensure attributes are maintained in deserialization
1 parent dd38147 commit 01610f5

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

lib/chef-dk/policyfile_lock.rb

+26
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def build_from_lock_data(lock_data)
256256
set_name_from_lock_data(lock_data)
257257
set_run_list_from_lock_data(lock_data)
258258
set_cookbook_locks_from_lock_data(lock_data)
259+
set_attributes_from_lock_data(lock_data)
259260
set_solution_dependencies_from_lock_data(lock_data)
260261
self
261262
end
@@ -422,6 +423,31 @@ def set_cookbook_locks_from_lock_data(lock_data)
422423
end
423424
end
424425

426+
def set_attributes_from_lock_data(lock_data)
427+
default_attr_data = lock_data["default_attributes"]
428+
429+
if default_attr_data.nil?
430+
raise InvalidLockfile, "lockfile does not have a `default_attributes` attribute"
431+
end
432+
433+
unless default_attr_data.kind_of?(Hash)
434+
raise InvalidLockfile, "lockfile's `default_attributes` attribute must be a Hash (JSON object). (got: #{default_attr_data.inspect})"
435+
end
436+
437+
override_attr_data = lock_data["override_attributes"]
438+
439+
if override_attr_data.nil?
440+
raise InvalidLockfile, "lockfile does not have a `override_attributes` attribute"
441+
end
442+
443+
unless override_attr_data.kind_of?(Hash)
444+
raise InvalidLockfile, "lockfile's `override_attributes` attribute must be a Hash (JSON object). (got: #{override_attr_data.inspect})"
445+
end
446+
447+
@default_attributes = default_attr_data
448+
@override_attributes = override_attr_data
449+
end
450+
425451
def set_solution_dependencies_from_lock_data(lock_data)
426452
soln_deps = lock_data["solution_dependencies"]
427453

spec/unit/policyfile_lock_serialization_spec.rb

+47
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"cookbook_locks" => {
2828
# TODO: add some valid locks
2929
},
30+
"default_attributes" => { "foo" => "bar" },
31+
"override_attributes" => { "override_foo" => "override_bar" },
3032
"solution_dependencies" => {
3133
"Policyfile" => [],
3234
"dependencies" => {}
@@ -38,6 +40,27 @@
3840

3941
let(:lockfile) { ChefDK::PolicyfileLock.new(storage_config) }
4042

43+
describe "populating the deserialized lock" do
44+
45+
before do
46+
lockfile.build_from_lock_data(valid_lock_data)
47+
end
48+
49+
it "includes the run list" do
50+
expect(lockfile.run_list).to eq(["recipe[cookbook::recipe_name]"])
51+
end
52+
53+
it "includes the cookbook locks" do
54+
expect(lockfile.cookbook_locks).to eq({})
55+
end
56+
57+
it "includes the attributes" do
58+
expect(lockfile.default_attributes).to eq({"foo" => "bar"})
59+
expect(lockfile.override_attributes).to eq({"override_foo" => "override_bar"})
60+
end
61+
62+
end
63+
4164
describe "validating required fields" do
4265

4366
it "does not raise an error when all fields are valid" do
@@ -88,6 +111,30 @@
88111
expect { lockfile.build_from_lock_data(invalid_locks) }.to raise_error(ChefDK::InvalidLockfile)
89112
end
90113

114+
it "requires the `default_attributes` section be present and its value is a Hash" do
115+
missing_attrs = valid_lock_data.dup
116+
missing_attrs.delete("default_attributes")
117+
118+
expect { lockfile.build_from_lock_data(missing_attrs) }.to raise_error(ChefDK::InvalidLockfile)
119+
120+
invalid_attrs = valid_lock_data.dup
121+
invalid_attrs["default_attributes"] = []
122+
123+
expect { lockfile.build_from_lock_data(invalid_attrs) }.to raise_error(ChefDK::InvalidLockfile)
124+
end
125+
126+
it "requires the `override_attributes` section be present and its value is a Hash" do
127+
missing_attrs = valid_lock_data.dup
128+
missing_attrs.delete("override_attributes")
129+
130+
expect { lockfile.build_from_lock_data(missing_attrs) }.to raise_error(ChefDK::InvalidLockfile)
131+
132+
invalid_attrs = valid_lock_data.dup
133+
invalid_attrs["override_attributes"] = []
134+
135+
expect { lockfile.build_from_lock_data(invalid_attrs) }.to raise_error(ChefDK::InvalidLockfile)
136+
end
137+
91138
describe "validating solution_dependencies" do
92139

93140
it "requires the `solution_dependencies' section be present" do

spec/unit/policyfile_services/export_repo_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
}
124124
}
125125
},
126+
"default_attributes": {},
127+
"override_attributes": {},
126128
"solution_dependencies": {
127129
"Policyfile": [
128130
[

spec/unit/policyfile_services/push_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@
145145
}
146146
}
147147
},
148+
"default_attributes": {},
149+
"override_attributes": {},
148150
"solution_dependencies": {
149151
"Policyfile": [
150152
[

0 commit comments

Comments
 (0)