Skip to content

Commit d2323a5

Browse files
author
cthorn42
authored
Merge pull request #8891 from joshcooper/lookup_spec
(maint) Don't skip application lifecycle methods
2 parents 4202cda + 3faa45d commit d2323a5

File tree

1 file changed

+45
-59
lines changed

1 file changed

+45
-59
lines changed

spec/integration/application/lookup_spec.rb

+45-59
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
include PuppetSpec::Files
88

99
context 'with an environment' do
10-
let(:fqdn) { Puppet.runtime[:facter].value(:fqdn) }
10+
let(:fqdn) { Puppet[:certname] }
1111
let(:env_name) { 'spec' }
1212
let(:env_dir) { tmpdir('environments') }
1313
let(:environment_files) do
@@ -43,12 +43,10 @@
4343
end
4444

4545
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, []) }
4846
let(:facts) { Puppet::Node::Facts.new("facts", {'my_fact' => 'my_fact_value'}) }
4947
let(:cert) { pem_content('oid.pem') }
5048

51-
let(:node) { Puppet::Node.new('testnode', :facts => facts, :environment => env) }
49+
let(:node) { Puppet::Node.new('testnode', :facts => facts) }
5250
let(:populated_env_dir) do
5351
dir_contained_in(env_dir, environment_files)
5452
env_dir
@@ -57,105 +55,86 @@
5755
before do
5856
stub_request(:get, "https://puppet:8140/puppet-ca/v1/certificate/#{fqdn}").to_return(body: cert)
5957
allow(Puppet::Node::Facts.indirection).to receive(:find).and_return(facts)
60-
end
6158

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
8761

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
9064
end
9165

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
9668
end
9769

9870
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/)
10073
end
10174

10275
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-
10876
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/)
11185
end
11286

11387
it 'loads external facts when running without --node' do
11488
expect(Puppet::Util).not_to receive(:skip_external_facts)
11589
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/)
11793
end
11894

11995
describe 'when using --node' do
12096
let(:fqdn) { 'random_node' }
12197

12298
it 'skips loading of external facts' do
123-
app.options[:node] = fqdn
99+
app.command_line.args << 'a' << '--node' << fqdn
124100

125101
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/)
129105
end
130106
end
131107

132108
context 'uses node_terminus' do
133109
require 'puppet/indirector/node/exec'
134110
require 'puppet/indirector/node/plain'
135111

136-
let(:node) { Puppet::Node.new('testnode', :facts => facts, :environment => env) }
112+
let(:node) { Puppet::Node.new('testnode', :facts => facts) }
137113

138114
it ':plain without --compile' do
139115
Puppet.settings[:node_terminus] = 'exec'
140116
expect_any_instance_of(Puppet::Node::Plain).to receive(:find).and_return(node)
141117
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/)
143121
end
144122

145123
it 'configured in Puppet settings with --compile' do
146124
Puppet.settings[:node_terminus] = 'exec'
147125
expect_any_instance_of(Puppet::Node::Plain).not_to receive(:find)
148126
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/)
150130
end
151131
end
152132

153133
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')]) }
155134
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'/)
159138
end
160139
end
161140

@@ -200,15 +179,22 @@ def explain(key, options = {})
200179
end
201180

202181
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\)/)
204184
end
205185

206186
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\)/)
208189
end
209190

210191
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
212198
end
213199
end
214200
end

0 commit comments

Comments
 (0)