Skip to content

Commit 4202cda

Browse files
authored
Merge pull request #8886 from cthorn42/bug_fix/6.x/PUP-11440_exit_run_if_no_env_and_strict_env
(PUP-11440) If no env found, and strict env mode cancel puppet run
2 parents b6efe65 + bb902a3 commit 4202cda

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/puppet/configurer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def run_internal(options)
330330
temp_value = options[:pluginsync]
331331

332332
# only validate server environment if pluginsync is requested
333-
options[:pluginsync] = valid_server_environment? if options[:pluginsync] == true
333+
options[:pluginsync] = valid_server_environment? if options[:pluginsync]
334334

335335
query_options, facts = get_facts(options) unless query_options
336336
options[:pluginsync] = temp_value
@@ -443,7 +443,11 @@ def valid_server_environment?
443443
true
444444
rescue Puppet::HTTP::ResponseError => detail
445445
if detail.response.code == 404
446-
Puppet.notice(_("Environment '%{environment}' not found on server, skipping initial pluginsync.") % { environment: @environment })
446+
if Puppet[:strict_environment_mode]
447+
raise Puppet::Error.new(_("Environment '%{environment}' not found on server, aborting run.") % { environment: @environment })
448+
else
449+
Puppet.notice(_("Environment '%{environment}' not found on server, skipping initial pluginsync.") % { environment: @environment })
450+
end
447451
else
448452
Puppet.log_exception(detail, detail.message)
449453
end

spec/unit/configurer_spec.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
Puppet[:report] = true
1010

1111
catalog.add_resource(resource)
12-
allow_any_instance_of(described_class).to(
13-
receive(:valid_server_environment?).and_return(true)
14-
)
1512

1613
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1714
---
@@ -78,10 +75,44 @@
7875
end
7976
end
8077

78+
describe "when executing a catalog run without stubbing valid_server_environment?" do
79+
before do
80+
Puppet::Resource::Catalog.indirection.terminus_class = :rest
81+
allow(Puppet::Resource::Catalog.indirection).to receive(:find).and_return(catalog)
82+
end
83+
84+
it 'skips initial plugin sync if environment is not found and no strict_environment_mode' do
85+
body = "{\"message\":\"Not Found: Could not find environment 'fasdfad'\",\"issue_kind\":\"RUNTIME_ERROR\"}"
86+
stub_request(:get, %r{/puppet/v3/file_metadatas/plugins?}).to_return(
87+
status: 404, body: body, headers: {'Content-Type' => 'application/json'}
88+
)
89+
90+
configurer.run(:pluginsync => true)
91+
92+
expect(@logs).to include(an_object_having_attributes(level: :notice, message: %r{Environment 'production' not found on server, skipping initial pluginsync.}))
93+
expect(@logs).to include(an_object_having_attributes(level: :notice, message: /Applied catalog in .* seconds/))
94+
end
95+
96+
it 'if strict_environment_mode is set and environment is not found, aborts the puppet run' do
97+
Puppet[:strict_environment_mode] = true
98+
body = "{\"message\":\"Not Found: Could not find environment 'fasdfad'\",\"issue_kind\":\"RUNTIME_ERROR\"}"
99+
stub_request(:get, %r{/puppet/v3/file_metadatas/plugins?}).to_return(
100+
status: 404, body: body, headers: {'Content-Type' => 'application/json'}
101+
)
102+
103+
configurer.run(:pluginsync => true)
104+
105+
expect(@logs).to include(an_object_having_attributes(level: :err, message: %r{Failed to apply catalog: Environment 'production' not found on server, aborting run.}))
106+
end
107+
end
108+
81109
describe "when executing a catalog run" do
82110
before do
83111
Puppet::Resource::Catalog.indirection.terminus_class = :rest
84112
allow(Puppet::Resource::Catalog.indirection).to receive(:find).and_return(catalog)
113+
allow_any_instance_of(described_class).to(
114+
receive(:valid_server_environment?).and_return(true)
115+
)
85116
end
86117

87118
it "downloads plugins when told" do

0 commit comments

Comments
 (0)