Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Puppet 6.6 evaltrace for progress #249

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions lib/kafo/progress_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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{/(?<resource>.+\]): Starting to evaluate the resource( \((?<number>\d+) of (?<total>\d+)\))?}
EVALTRACE_END = %r{/(.+\]): Evaluated in [\d\.]+ seconds}
PREFETCH = %r{Prefetching .* resources for}

Expand All @@ -37,26 +37,40 @@ def update(line)
force_update = false

if (line_monitor = MONITOR_RESOURCE.match(line))
# In Puppet < 6 Kafo's custom progress bar hooks into internals and
# prints a MONITOR_RESOURCE line. This gives a total.
@resources << line_monitor[1]
@total = (@total == :unknown ? 1 : @total + 1)
end
elsif (line_start = EVALTRACE_START.match(line))
if line_start[:total]
# Puppet 6.6 introduced progress in evaltrace. There is no complete
# set prior to evaluation, so the set it built as evaluation is
# progressing.
line = line_start[:resource]
@resources << line
update_bar = true
force_update = true

if (line_start = EVALTRACE_START.match(line))
if (known_resource = find_known_resource(line_start[1]))
# Puppet counts 1-based where Kafo counts 0-based here
new_lines = line_start[:number].to_i - 1
new_total = line_start[:total].to_i
if new_lines != @lines || @total != new_total
@lines = new_lines
@total = new_total
end
elsif (known_resource = find_known_resource(line_start[1]))
# In Puppet < 6 we have a complete set of all resources prior to
# evaluation
line = known_resource
update_bar = true
force_update = true
end
end

if (line_end = EVALTRACE_END.match(line)) && @total != :unknown && @lines < @total
elsif (line_end = EVALTRACE_END.match(line)) && @total != :unknown && @lines < @total
if (known_resource = find_known_resource(line_end[1]))
@resources.delete(known_resource) # ensure it's only counted once
@lines += 1
end
end

if PREFETCH =~ line
elsif PREFETCH =~ line
update_bar = true
force_update = true
end
Expand Down
57 changes: 37 additions & 20 deletions test/kafo/progress_bar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@ 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 => 'Prefetching example resources for example_type ', :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