diff options
-rw-r--r-- | lib/chef/node.rb | 8 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 30 |
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 65ed21a442..34a1edf835 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -510,6 +510,14 @@ class Chef #Render correctly for run_list items so malformed json does not result "run_list" => @primary_runlist.run_list.map { |item| item.to_s } } + # Chef Server rejects node JSON with extra keys; prior to 12.3, + # "policy_name" and "policy_group" are unknown; after 12.3 they are + # optional, therefore only including them in the JSON if present + # maximizes compatibility for most people. + unless policy_group.nil? && policy_name.nil? + result["policy_name"] = policy_name + result["policy_group"] = policy_group + end result end diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 39a4e829c9..fcf5f56655 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -1185,6 +1185,35 @@ describe Chef::Node do expect(serialized_node.run_list).to eq(node.run_list) end + context "when policyfile attributes are not present" do + + it "does not have a policy_name key in the json" do + expect(node.for_json.keys).to_not include("policy_name") + end + + it "does not have a policy_group key in the json" do + expect(node.for_json.keys).to_not include("policy_name") + end + end + + context "when policyfile attributes are present" do + + before do + node.policy_name = "my-application" + node.policy_group = "staging" + end + + it "includes policy_name key in the json" do + expect(node.for_json).to have_key("policy_name") + expect(node.for_json["policy_name"]).to eq("my-application") + end + + it "includes a policy_group key in the json" do + expect(node.for_json).to have_key("policy_group") + expect(node.for_json["policy_group"]).to eq("staging") + end + end + include_examples "to_json equivalent to Chef::JSONCompat.to_json" do let(:jsonable) { node.from_file(File.expand_path("nodes/test.example.com.rb", CHEF_SPEC_DATA)) @@ -1380,6 +1409,7 @@ describe Chef::Node do node.save end end + end end |