Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #30723: Use Puppet 6.6 evaltrace for progress
Browse files Browse the repository at this point in the history
9fc5fd3 disabled the progress bar
monkey patch on Puppet 6. Puppet 6.6.0 started to mention the progress
when evaluating resources which can be used to update the progress bar.
ekohl authored and ehelms committed Aug 27, 2020
1 parent 6c7f49f commit 4a25cc6
Showing 2 changed files with 59 additions and 30 deletions.
30 changes: 20 additions & 10 deletions lib/kafo/progress_bar.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ module Kafo
# change more methods like #done_message or #print_error
class ProgressBar
MONITOR_RESOURCE = %r{\w*MONITOR_RESOURCE ([^\]]+\])}
EVALTRACE_START = %r{/(.+\]): Starting to evaluate the resource}
EVALTRACE_START = %r{/(.+\]): Starting to evaluate the resource( \((\d+) of (\d+)\))?}
EVALTRACE_END = %r{/(.+\]): Evaluated in [\d\.]+ seconds}
PREFETCH = %r{Prefetching .* resources for}

@@ -42,15 +42,28 @@ def update(line)
end

if (line_start = EVALTRACE_START.match(line))
if (known_resource = find_known_resource(line_start[1]))
if line_start[4]
# Puppet 6.6 introduced progress in evaltrace
# Puppet counts 1-based where we count 0-based here
new_lines = line_start[3].to_i - 1
new_total = line_start[4].to_i
if new_lines != @lines || @total != new_total
@lines = new_lines
@total = new_total
update_bar = true
force_update = true
end
end

if (known_resource = find_resource(line_start[1]))
line = known_resource
update_bar = true
force_update = true
end
end

if (line_end = EVALTRACE_END.match(line)) && @total != :unknown && @lines < @total
if (known_resource = find_known_resource(line_end[1]))
if (known_resource = find_resource(line_end[1]))
@resources.delete(known_resource) # ensure it's only counted once
@lines += 1
end
@@ -102,13 +115,10 @@ def infinite_template
'Installing...'
end

def find_known_resource(resource)
loop do
return resource if @resources.include?(resource)
# continue to remove prefixes from /Stage[main]/Example/File[/etc/foo] until a resource name is found
break unless resource.include?('/')
resource = resource.sub %r{.*?/}, ''
end
def find_resource(resource)
found = resource.match(%r{Stage.*\/(.*\[.*\])$})

return found[1] unless found.nil?
nil
end

59 changes: 39 additions & 20 deletions test/kafo/progress_bar_test.rb
Original file line number Diff line number Diff line change
@@ -5,30 +5,49 @@ module Kafo
let(:bar) { ProgressBar.new.tap { |pb| pb.instance_variable_set(:@bar, powerbar) } }
let(:powerbar) { MiniTest::Mock.new }

it "calls powerbar.show" do
powerbar.expect(:show, nil, [{:msg => 'Notify[test] ', :done => 0, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'Prefetching example resources for example_type ', :done => 1, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'File[/foo/bar] ', :done => 1, :total => 2}, true])
describe 'Puppet 5' do
it "calls powerbar.show" do
powerbar.expect(:show, nil, [{:msg => 'Notify[test] ', :done => 0, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'Prefetching example resources for example_type ', :done => 1, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'File[/foo/bar] ', :done => 1, :total => 2}, true])

bar.update('MONITOR_RESOURCE File[/foo/bar]')
bar.update('MONITOR_RESOURCE Notify[test]')
bar.update('/Stage[main]/Example/Notify[test]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/Notify[test]: Evaluated in 0.5 seconds')
bar.update('Prefetching example resources for example_type')
bar.update('/Stage[main]/Example/File[/foo/bar]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/File[/foo/bar]: Evaluated in 0.5 seconds')
powerbar.verify
bar.update('MONITOR_RESOURCE File[/foo/bar]')
bar.update('MONITOR_RESOURCE Notify[test]')
bar.update('/Stage[main]/Example/Notify[test]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/Notify[test]: Evaluated in 0.5 seconds')
bar.update('Prefetching example resources for example_type')
bar.update('/Stage[main]/Example/File[/foo/bar]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/File[/foo/bar]: Evaluated in 0.5 seconds')
powerbar.verify
end

it 'handles an unknown total' do
powerbar.expect(:show, nil, [{:msg => 'Notify[test] ', :done => 0, :total => :unknown}, true])
powerbar.expect(:show, nil, [{:msg => 'Prefetching example resources for example_type ', :done => 0, :total => :unknown}, true])
powerbar.expect(:show, nil, [{:msg => 'File[/foo/bar] ', :done => 0, :total => :unknown}, true])

bar.update('/Stage[main]/Example/Notify[test]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/Notify[test]: Evaluated in 0.5 seconds')
bar.update('Prefetching example resources for example_type')
bar.update('/Stage[main]/Example/File[/foo/bar]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/File[/foo/bar]: Evaluated in 0.5 seconds')
powerbar.verify
end
end

it 'handles an unknown total' do
powerbar.expect(:show, nil, [{:msg => 'Prefetching example resources for example_type ', :done => 0, :total => :unknown}, true])
describe 'Puppet 6' do
it "calls powerbar.show" do
powerbar.expect(:show, nil, [{:msg => 'Notify[test] ', :done => 0, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'Prefetching example resources for example_type ', :done => 1, :total => 2}, true])
powerbar.expect(:show, nil, [{:msg => 'File[/foo/bar] ', :done => 1, :total => 2}, true])

bar.update('/Stage[main]/Example/Notify[test]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/Notify[test]: Evaluated in 0.5 seconds')
bar.update('Prefetching example resources for example_type')
bar.update('/Stage[main]/Example/File[/foo/bar]: Starting to evaluate the resource')
bar.update('/Stage[main]/Example/File[/foo/bar]: Evaluated in 0.5 seconds')
powerbar.verify
bar.update('/Stage[main]/Example/Notify[test]: Starting to evaluate the resource (1 of 2)')
bar.update('/Stage[main]/Example/Notify[test]: Evaluated in 0.5 seconds')
bar.update('Prefetching example resources for example_type')
bar.update('/Stage[main]/Example/File[/foo/bar]: Starting to evaluate the resource (2 of 2)')
bar.update('/Stage[main]/Example/File[/foo/bar]: Evaluated in 0.5 seconds')
powerbar.verify
end
end
end
end

0 comments on commit 4a25cc6

Please sign in to comment.