|
1 |
| -require 'puppetlabs_spec_helper/module_spec_helper' |
| 1 | +require 'spec_helper' |
2 | 2 |
|
3 | 3 | describe Puppet::Type.type(:node_group) do
|
4 |
| - |
5 | 4 | it "should allow agent-specified environment" do
|
6 | 5 | expect {
|
7 | 6 | Puppet::Type.type(:node_group).new(
|
|
73 | 72 | }.to_not raise_error
|
74 | 73 | end
|
75 | 74 |
|
| 75 | + describe "purge_behavior" do |
| 76 | + let(:resource_hash) do |
| 77 | + { |
| 78 | + :name => 'test_group', |
| 79 | + :environment => 'test_env', |
| 80 | + :data => { |
| 81 | + 'data::class1' => { 'param1' => 'resource', |
| 82 | + 'param2' => 'resource' }, |
| 83 | + 'data::class2' => { 'param1' => 'resource', |
| 84 | + 'param2' => 'resource' }, |
| 85 | + }, |
| 86 | + :classes => { |
| 87 | + 'classes::class1' => { 'param1' => 'resource', |
| 88 | + 'param2' => 'resource' }, |
| 89 | + }, |
| 90 | + } |
| 91 | + end |
| 92 | + |
| 93 | + let(:existing_data) do |
| 94 | + { 'data::class1' => { 'param1' => 'existing', |
| 95 | + 'param3' => 'existing' }, |
| 96 | + 'data::class3' => { 'param1' => 'existing', |
| 97 | + 'param2' => 'existing' }} |
| 98 | + end |
| 99 | + let(:merged_data) do |
| 100 | + { "data::class1" => { "param1" => "resource", |
| 101 | + "param2" => "resource", |
| 102 | + "param3" => "existing"}, |
| 103 | + "data::class2" => { "param1" => "resource", |
| 104 | + "param2" => "resource"}, |
| 105 | + "data::class3" => { "param1" => "existing", |
| 106 | + "param2" => "existing"}} |
| 107 | + end |
| 108 | + |
| 109 | + let(:existing_classes) do |
| 110 | + { 'classes::class1' => { 'param1' => 'existing', |
| 111 | + 'param3' => 'existing' }, |
| 112 | + 'classes::class3' => { 'param1' => 'existing', |
| 113 | + 'param2' => 'existing' }} |
| 114 | + end |
| 115 | + let(:merged_classes) do |
| 116 | + { "classes::class1" => { "param1" => "resource", |
| 117 | + "param2" => "resource", |
| 118 | + "param3" => "existing"}, |
| 119 | + "classes::class3" => { "param1" => "existing", |
| 120 | + "param2" => "existing"}} |
| 121 | + end |
| 122 | + |
| 123 | + it "should match classes and data exactly by default" do |
| 124 | + rsrc = described_class.new(resource_hash) |
| 125 | + allow(rsrc.property(:data)).to receive(:retrieve).and_return(existing_data) |
| 126 | + allow(rsrc.property(:classes)).to receive(:retrieve).and_return(existing_classes) |
| 127 | + expect(rsrc.property(:data).should).to eq resource_hash[:data] |
| 128 | + expect(rsrc.property(:classes).should).to eq resource_hash[:classes] |
| 129 | + end |
| 130 | + |
| 131 | + it "should merge in classes and data when set to :none" do |
| 132 | + rsrc = described_class.new(resource_hash.merge(:purge_behavior => 'none')) |
| 133 | + allow(rsrc.property(:data)).to receive(:retrieve).and_return(existing_data) |
| 134 | + allow(rsrc.property(:classes)).to receive(:retrieve).and_return(existing_classes) |
| 135 | + expect(rsrc.property(:data).should).to eq (merged_data) |
| 136 | + expect(rsrc.property(:classes).should).to eq (merged_classes) |
| 137 | + end |
| 138 | + |
| 139 | + it "should merge in classes and match data exactly when set to :data" do |
| 140 | + rsrc = described_class.new(resource_hash.merge(:purge_behavior => 'data')) |
| 141 | + allow(rsrc.property(:data)).to receive(:retrieve).and_return(existing_data) |
| 142 | + allow(rsrc.property(:classes)).to receive(:retrieve).and_return(existing_classes) |
| 143 | + expect(rsrc.property(:data).should).to eq (resource_hash[:data]) |
| 144 | + expect(rsrc.property(:classes).should).to eq (merged_classes) |
| 145 | + end |
| 146 | + |
| 147 | + it "should merge in data and match classes exactly when set to :classes" do |
| 148 | + rsrc = described_class.new(resource_hash.merge(:purge_behavior => 'classes')) |
| 149 | + allow(rsrc.property(:data)).to receive(:retrieve).and_return(existing_data) |
| 150 | + allow(rsrc.property(:classes)).to receive(:retrieve).and_return(existing_classes) |
| 151 | + expect(rsrc.property(:data).should).to eq (merged_data) |
| 152 | + expect(rsrc.property(:classes).should).to eq (resource_hash[:classes]) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + describe ".insync? for data, classes" do |
| 157 | + let(:hash) do |
| 158 | + { |
| 159 | + 'class1' => { 'param1' => 'value1', |
| 160 | + 'param2' => 'value2' }, |
| 161 | + 'class2' => { 'param1' => 'value1', |
| 162 | + 'param2' => 'value2' }, |
| 163 | + 'class3' => { 'param1' => 'value1', |
| 164 | + 'param2' => 'value2' }, |
| 165 | + } |
| 166 | + end |
| 167 | + let(:resource) do |
| 168 | + described_class.new({ |
| 169 | + :name => 'test_group', |
| 170 | + :environment => 'test_env', |
| 171 | + :classes => hash, |
| 172 | + :data => hash, |
| 173 | + }) |
| 174 | + end |
| 175 | + |
| 176 | + before(:each) do |
| 177 | + allow(resource.property(:data)).to receive(:should).and_return(hash) |
| 178 | + allow(resource.property(:classes)).to receive(:should).and_return(hash) |
| 179 | + end |
| 180 | + |
| 181 | + it 'is insync when `is` and `should` are identical' do |
| 182 | + expect(resource.property(:data).insync?(hash)).to eq(true) |
| 183 | + expect(resource.property(:classes).insync?(hash)).to eq(true) |
| 184 | + end |
| 185 | + |
| 186 | + it 'is insync when `is` and `should` are identical but have different ordering' do |
| 187 | + reverse_hash = hash.to_a.map{ |i| [i[0], i[1].to_a.reverse.to_h] }.reverse.to_h |
| 188 | + expect(resource.property(:data).insync?(reverse_hash)).to eq(true) |
| 189 | + expect(resource.property(:classes).insync?(reverse_hash)).to eq(true) |
| 190 | + end |
| 191 | + |
| 192 | + it 'is not insync when `is` is only a subset of `should`' do |
| 193 | + subset = hash.select { |k| k != 'class2' } |
| 194 | + expect(resource.property(:data).insync?(subset)).to eq(false) |
| 195 | + expect(resource.property(:classes).insync?(subset)).to eq(false) |
| 196 | + end |
| 197 | + end |
| 198 | + |
76 | 199 | end
|
0 commit comments