|
7 | 7 | include PuppetSpec::Files
|
8 | 8 |
|
9 | 9 | context 'with an environment' do
|
10 |
| - let(:fqdn) { Puppet.runtime[:facter].value(:fqdn) } |
| 10 | + let(:fqdn) { Puppet[:certname] } |
11 | 11 | let(:env_name) { 'spec' }
|
12 | 12 | let(:env_dir) { tmpdir('environments') }
|
13 | 13 | let(:environment_files) do
|
|
43 | 43 | end
|
44 | 44 |
|
45 | 45 | let(:app) { Puppet::Application[:lookup] }
|
46 |
| - let(:env) { Puppet::Node::Environment.create(env_name.to_sym, [File.join(populated_env_dir, env_name, 'modules')]) } |
47 |
| - let(:environments) { Puppet::Environments::Directories.new(populated_env_dir, []) } |
48 | 46 | let(:facts) { Puppet::Node::Facts.new("facts", {'my_fact' => 'my_fact_value'}) }
|
49 | 47 | let(:cert) { pem_content('oid.pem') }
|
50 | 48 |
|
51 |
| - let(:node) { Puppet::Node.new('testnode', :facts => facts, :environment => env) } |
| 49 | + let(:node) { Puppet::Node.new('testnode', :facts => facts) } |
52 | 50 | let(:populated_env_dir) do
|
53 | 51 | dir_contained_in(env_dir, environment_files)
|
54 | 52 | env_dir
|
|
57 | 55 | before do
|
58 | 56 | stub_request(:get, "https://puppet:8140/puppet-ca/v1/certificate/#{fqdn}").to_return(body: cert)
|
59 | 57 | allow(Puppet::Node::Facts.indirection).to receive(:find).and_return(facts)
|
60 |
| - end |
61 | 58 |
|
62 |
| - def lookup(key, options = {}, explain = false) |
63 |
| - key = [key] unless key.is_a?(Array) |
64 |
| - allow(app.command_line).to receive(:args).and_return(key) |
65 |
| - if explain |
66 |
| - app.options[:explain] = true |
67 |
| - app.options[:render_as] = :s |
68 |
| - else |
69 |
| - app.options[:render_as] = :json |
70 |
| - end |
71 |
| - options.each_pair { |k, v| app.options[k] = v } |
72 |
| - capture = StringIO.new |
73 |
| - saved_stdout = $stdout |
74 |
| - begin |
75 |
| - $stdout = capture |
76 |
| - expect { app.run_command }.to exit_with(0) |
77 |
| - ensure |
78 |
| - $stdout = saved_stdout |
79 |
| - end |
80 |
| - out = capture.string.strip |
81 |
| - if explain |
82 |
| - out |
83 |
| - else |
84 |
| - out.empty? ? nil : JSON.parse("[#{out}]")[0] |
85 |
| - end |
86 |
| - end |
| 59 | + Puppet[:environment] = env_name |
| 60 | + Puppet[:environmentpath] = populated_env_dir |
87 | 61 |
|
88 |
| - def explain(key, options = {}) |
89 |
| - lookup(key, options, true) |
| 62 | + http = Puppet::HTTP::Client.new(ssl_context: Puppet::SSL::SSLProvider.new.create_insecure_context) |
| 63 | + Puppet.runtime[:http] = http |
90 | 64 | end
|
91 | 65 |
|
92 |
| - around(:each) do |example| |
93 |
| - Puppet.override(:environments => environments, :current_environment => env) do |
94 |
| - example.run |
95 |
| - end |
| 66 | + def expect_lookup_with_output(exitcode, out) |
| 67 | + expect { app.run }.to exit_with(exitcode).and output(out).to_stdout |
96 | 68 | end
|
97 | 69 |
|
98 | 70 | it 'finds data in the environment' do
|
99 |
| - expect(lookup('a')).to eql('value a') |
| 71 | + app.command_line.args << 'a' |
| 72 | + expect_lookup_with_output(0, /value a/) |
100 | 73 | end
|
101 | 74 |
|
102 | 75 | it 'loads trusted information from the node certificate' do
|
103 |
| - allow(Puppet).to receive(:override).and_call_original |
104 |
| - expect(Puppet).to receive(:override).with(trusted_information: an_object_having_attributes( |
105 |
| - certname: fqdn, |
106 |
| - extensions: { "1.3.6.1.4.1.34380.1.2.1.1" => "somevalue" })) |
107 |
| - |
108 | 76 | Puppet.settings[:node_terminus] = 'exec'
|
109 |
| - expect_any_instance_of(Puppet::Node::Exec).to receive(:find).and_return(node) |
110 |
| - lookup('a', :compile => true) |
| 77 | + expect_any_instance_of(Puppet::Node::Exec).to receive(:find) do |args| |
| 78 | + info = Puppet.lookup(:trusted_information) |
| 79 | + expect(info.certname).to eq(fqdn) |
| 80 | + expect(info.extensions).to eq({ "1.3.6.1.4.1.34380.1.2.1.1" => "somevalue" }) |
| 81 | + end.and_return(node) |
| 82 | + |
| 83 | + app.command_line.args << 'a' << '--compile' |
| 84 | + expect_lookup_with_output(0, /--- value a/) |
111 | 85 | end
|
112 | 86 |
|
113 | 87 | it 'loads external facts when running without --node' do
|
114 | 88 | expect(Puppet::Util).not_to receive(:skip_external_facts)
|
115 | 89 | expect(Facter).not_to receive(:load_external)
|
116 |
| - lookup('a') |
| 90 | + |
| 91 | + app.command_line.args << 'a' |
| 92 | + expect_lookup_with_output(0, /--- value a/) |
117 | 93 | end
|
118 | 94 |
|
119 | 95 | describe 'when using --node' do
|
120 | 96 | let(:fqdn) { 'random_node' }
|
121 | 97 |
|
122 | 98 | it 'skips loading of external facts' do
|
123 |
| - app.options[:node] = fqdn |
| 99 | + app.command_line.args << 'a' << '--node' << fqdn |
124 | 100 |
|
125 | 101 | expect(Puppet::Node::Facts.indirection).to receive(:find).and_return(facts)
|
126 |
| - expect(Facter).to receive(:load_external).once.with(false) |
127 |
| - expect(Facter).to receive(:load_external).once.with(true) |
128 |
| - lookup('a') |
| 102 | + expect(Facter).to receive(:load_external).twice.with(false) |
| 103 | + expect(Facter).to receive(:load_external).twice.with(true) |
| 104 | + expect_lookup_with_output(0, /--- value a/) |
129 | 105 | end
|
130 | 106 | end
|
131 | 107 |
|
132 | 108 | context 'uses node_terminus' do
|
133 | 109 | require 'puppet/indirector/node/exec'
|
134 | 110 | require 'puppet/indirector/node/plain'
|
135 | 111 |
|
136 |
| - let(:node) { Puppet::Node.new('testnode', :facts => facts, :environment => env) } |
| 112 | + let(:node) { Puppet::Node.new('testnode', :facts => facts) } |
137 | 113 |
|
138 | 114 | it ':plain without --compile' do
|
139 | 115 | Puppet.settings[:node_terminus] = 'exec'
|
140 | 116 | expect_any_instance_of(Puppet::Node::Plain).to receive(:find).and_return(node)
|
141 | 117 | expect_any_instance_of(Puppet::Node::Exec).not_to receive(:find)
|
142 |
| - expect(lookup('a')).to eql('value a') |
| 118 | + |
| 119 | + app.command_line.args << 'a' |
| 120 | + expect_lookup_with_output(0, /--- value a/) |
143 | 121 | end
|
144 | 122 |
|
145 | 123 | it 'configured in Puppet settings with --compile' do
|
146 | 124 | Puppet.settings[:node_terminus] = 'exec'
|
147 | 125 | expect_any_instance_of(Puppet::Node::Plain).not_to receive(:find)
|
148 | 126 | expect_any_instance_of(Puppet::Node::Exec).to receive(:find).and_return(node)
|
149 |
| - expect(lookup('a', :compile => true)).to eql('value a') |
| 127 | + |
| 128 | + app.command_line.args << 'a' << '--compile' |
| 129 | + expect_lookup_with_output(0, /--- value a/) |
150 | 130 | end
|
151 | 131 | end
|
152 | 132 |
|
153 | 133 | context 'configured with the wrong environment' do
|
154 |
| - let(:env) { Puppet::Node::Environment.create(env_name.to_sym, [File.join(populated_env_dir, env_name, 'modules')]) } |
155 | 134 | it 'does not find data in non-existing environment' do
|
156 |
| - Puppet.override(:environments => environments, :current_environment => 'someother') do |
157 |
| - expect(lookup('a', {}, true)).to match(/did not find a value for the name 'a'/) |
158 |
| - end |
| 135 | + Puppet[:environment] = 'doesntexist' |
| 136 | + app.command_line.args << 'a' |
| 137 | + expect { app.run }.to raise_error(Puppet::Environments::EnvironmentNotFound, /Could not find a directory environment named 'doesntexist'/) |
159 | 138 | end
|
160 | 139 | end
|
161 | 140 |
|
@@ -200,15 +179,22 @@ def explain(key, options = {})
|
200 | 179 | end
|
201 | 180 |
|
202 | 181 | it 'finds data in the module' do
|
203 |
| - expect(lookup('mod_a::b')).to eql('value mod_a::b (from mod_a)') |
| 182 | + app.command_line.args << 'mod_a::b' |
| 183 | + expect_lookup_with_output(0, /value mod_a::b \(from mod_a\)/) |
204 | 184 | end
|
205 | 185 |
|
206 | 186 | it 'finds quoted keys in the module' do
|
207 |
| - expect(lookup('"mod_a::a.quoted.key"')).to eql('value mod_a::a.quoted.key (from mod_a)') |
| 187 | + app.command_line.args << "'mod_a::a.quoted.key'" |
| 188 | + expect_lookup_with_output(0, /value mod_a::a.quoted.key \(from mod_a\)/) |
208 | 189 | end
|
209 | 190 |
|
210 | 191 | it 'merges hashes from environment and module when merge strategy hash is used' do
|
211 |
| - expect(lookup('mod_a::hash_a', :merge => 'hash')).to eql({'a' => 'value mod_a::hash_a.a (from environment)', 'b' => 'value mod_a::hash_a.b (from mod_a)'}) |
| 192 | + app.command_line.args << 'mod_a::hash_a' << '--merge' << 'hash' |
| 193 | + expect_lookup_with_output(0, <<~END) |
| 194 | + --- |
| 195 | + a: value mod_a::hash_a.a (from environment) |
| 196 | + b: value mod_a::hash_a.b (from mod_a) |
| 197 | + END |
212 | 198 | end
|
213 | 199 | end
|
214 | 200 | end
|
|
0 commit comments