Skip to content

Commit

Permalink
fixe check_all_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarne committed Aug 6, 2024
1 parent 85a1db3 commit dacf12f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 65 deletions.
8 changes: 4 additions & 4 deletions lib/puppet/provider/archive/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,16 @@ def environment
envlist = [envlist] unless envlist.is_a? Array
envlist.each do |setting|
unless (match = %r{^(\w+)=((.|\n)*)$}.match(setting))
warning format(_('Cannot understand environment setting %{setting}'), setting: setting.inspect)
warning "Cannot understand environment setting #{setting.inspect}"
next
end
var = match[1]
value = match[2]

warning format(_("Overriding environment setting '%{var}' with '%{value}'"), var: var, value: value) if env.include?(var) || env.include?(var.to_sym)
warning "Overriding environment setting '#{var}' with '#{value}'" if env.include?(var) || env.include?(var.to_sym)

if value.nil? || value.empty?
msg = format(_("Empty environment setting '%{var}'"), var: var)
msg = "Empty environment setting '#{var}'"
Puppet.warn_once('undefined_variables', "empty_env_var_#{var}", msg, resource.file, resource.line)
end

Expand Down Expand Up @@ -364,6 +364,6 @@ def extractexe(command)
def validatecmd(command)
exe = extractexe(command)
# if we're not fully qualified, require a path
self.fail format(_("'%{exe}' is not qualified and no path was specified. Please qualify the command or specify a path."), exe: exe) if !absolute_path?(exe) && resource[:path].nil?
self.fail "'#{exe}' is not qualified and no path was specified. Please qualify the command or specify a path." if !absolute_path?(exe) && resource[:path].nil?
end
end
53 changes: 5 additions & 48 deletions lib/puppet/type/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def value=(*values)
def check(value)
# TRANSLATORS 'creates' is a parameter name and should not be translated
debug("Checking that 'creates' path '#{value}' exists")
Puppet::FileSystem.exist?(value)
! Puppet::FileSystem.exist?(value)

Check failure on line 297 in lib/puppet/type/archive.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/SpaceAfterNot: Do not leave space between `!` and its argument. (https://rubystyle.guide#no-space-bang)
end
end

Expand Down Expand Up @@ -457,7 +457,7 @@ def check(value)
# Verify that we pass all of the checks. The argument determines whether
# we skip the :refreshonly check, which is necessary because we now check
# within refresh
def check_all_attributes(_refreshing = false)
def check_all_attributes
self.class.checks.each do |check|
next unless @parameters.include?(check)

Expand All @@ -470,54 +470,11 @@ def check_all_attributes(_refreshing = false)
# but don't print sensitive commands or parameters in the clear
sourcestring = @parameters[:source].sensitive ? '[command redacted]' : @parameters[:source].value

debug(format("'%{source}' won't be executed because of failed check '%{check}'", source: sourcestring, check: check))
debug("'#{sourcestring}' won't be executed because of failed check '#{check}'")

return false
end
end
true
end

def output
if property(:returns).nil?
nil
else
property(:returns).output
end
end

# Run the command, or optionally run a separately-specified command.
def refresh
return unless check_all_attributes(true)

cmd = self[:refresh]
if cmd
provider.run(cmd)
else
property(:returns).sync
end
end

private

def set_sensitive_parameters(sensitive_parameters)
# If any are sensitive, mark all as sensitive
sensitive = false
parameters_to_check = %i[command unless onlyif]

parameters_to_check.each do |p|
if sensitive_parameters.include?(p)
sensitive_parameters.delete(p)
sensitive = true
return true
end
end

if sensitive
parameters_to_check.each do |p|
parameter(p).sensitive = true if parameters.include?(p)
end
end

super(sensitive_parameters)
false
end
end
2 changes: 1 addition & 1 deletion spec/acceptance/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
context 'authenticated download' do
let(:source) do
parser = URI::RFC2396_Parser.new
parser.escape("http://httpbin.org/basic-auth/user/#{password}")
parser.escape("https://httpbin.org/basic-auth/user/#{password}")
end
let(:pp) do
<<-EOS
Expand Down
24 changes: 12 additions & 12 deletions spec/unit/puppet/type/archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,41 +245,41 @@
context 'with a single item' do
it 'runs if the command exits non-zero' do
resource[param] = cmd_fail
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
end

it 'does not run if the command exits zero' do
resource[param] = cmd_pass
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end
end

context 'with an array with a single item' do
it 'runs if the command exits non-zero' do
resource[param] = [cmd_fail]
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
end

it 'does not run if the command exits zero' do
resource[param] = [cmd_pass]
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end
end

context 'with an array with multiple items' do
it 'runs if all the commands exits non-zero' do
resource[param] = [cmd_fail] * 3
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
end

it 'does not run if one command exits zero' do
resource[param] = [cmd_pass, cmd_fail, cmd_pass]
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end

it 'does not run if all command exits zero' do
resource[param] = [cmd_pass] * 3
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end
end

Expand All @@ -299,32 +299,32 @@

it 'runs if all the commands exits non-zero' do
resource[param] = [[cmd_fail, '--flag'], [cmd_fail], [cmd_fail, '--flag']]
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
end

it 'does not run if one command exits zero' do
resource[param] = [[cmd_pass, '--flag'], [cmd_pass], [cmd_fail, '--flag']]
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end

it 'does not run if all command exits zero' do
resource[param] = [[cmd_pass, '--flag'], [cmd_pass], [cmd_pass, '--flag']]
expect(resource.check_all_attributes).to be(false)
expect(resource.check_all_attributes).to be(true)
end
end

it 'emits output to debug' do
Puppet::Util::Log.level = :debug
resource[param] = cmd_fail
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
expect(@logs.shift.message).to eq('test output')
end

it 'does not emit output to debug if sensitive is true' do
Puppet::Util::Log.level = :debug
resource[param] = cmd_fail
allow(resource.parameters[param]).to receive(:sensitive).and_return(true)
expect(resource.check_all_attributes).to be(true)
expect(resource.check_all_attributes).to be(false)
expect(@logs).not_to include(an_object_having_attributes(level: :debug, message: 'test output'))
expect(@logs).to include(an_object_having_attributes(level: :debug, message: '[output redacted]'))
end
Expand Down

0 comments on commit dacf12f

Please sign in to comment.