Skip to content

Commit 2862786

Browse files
authored
Output generalized 'compliance threshold was not met' string after running compliance command. Refactor Summary#results_meet_threshold? (#209)
1 parent 1e96ad2 commit 2862786

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

lib/inspec_tools/summary.rb

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,49 +50,46 @@ def output_summary
5050
puts @summary[:status].to_json if @json_counts
5151
end
5252

53-
# rubocop:disable Metrics/AbcSize
54-
# rubocop:disable Metrics/PerceivedComplexity
55-
# rubocop:disable Metrics/CyclomaticComplexity
5653
def results_meet_threshold?
5754
raise 'Please provide threshold as a yaml file or inline yaml' unless @threshold_provided
5855

5956
compliance = true
6057
failure = []
61-
max = @threshold['compliance.max']
62-
min = @threshold['compliance.min']
63-
if max != -1 and @summary[:compliance] > max
64-
compliance = false
65-
failure << expected_to_string('', 'compliance', 'max', max, @summary[:compliance])
66-
end
67-
if min != -1 and @summary[:compliance] < min
68-
compliance = false
69-
failure << expected_to_string('', 'compliance', 'min', min, @summary[:compliance])
70-
end
71-
status = @summary[:status]
58+
failure << check_max_compliance(@threshold['compliance.max'], @summary[:compliance], '', 'compliance')
59+
failure << check_min_compliance(@threshold['compliance.min'], @summary[:compliance], '', 'compliance')
60+
7261
BUCKETS.each do |bucket|
7362
TALLYS.each do |tally|
74-
max = @threshold["#{bucket}.#{tally}.max"]
75-
min = @threshold["#{bucket}.#{tally}.min"]
76-
if max != -1 and status[bucket][tally] > max
77-
compliance = false
78-
failure << expected_to_string(bucket, tally, 'max', max, status[bucket][tally])
79-
end
80-
if min != -1 and status[bucket][tally] < min
81-
compliance = false
82-
failure << expected_to_string(bucket, tally, 'min', min, status[bucket][tally])
83-
end
63+
failure << check_min_compliance(@threshold["#{bucket}.#{tally}.min"], @summary[:status][bucket][tally], bucket, tally)
64+
failure << check_max_compliance(@threshold["#{bucket}.#{tally}.max"], @summary[:status][bucket][tally], bucket, tally)
8465
end
8566
end
86-
puts failure.join("\n") unless compliance
87-
puts "Overall compliance threshold of #{@threshold['compliance.min']}\% met. Current compliance at #{@summary[:compliance]}\%" if compliance
67+
68+
failure.reject!(&:nil?)
69+
compliance = false if failure.length.positive?
70+
output(compliance, failure)
8871
compliance
8972
end
90-
# rubocop:enable Metrics/AbcSize
91-
# rubocop:enable Metrics/PerceivedComplexity
92-
# rubocop:enable Metrics/CyclomaticComplexity
9373

9474
private
9575

76+
def check_min_compliance(min, data, bucket, tally)
77+
expected_to_string(bucket, tally, 'min', min, data) if min != -1 and data < min
78+
end
79+
80+
def check_max_compliance(max, data, bucket, tally)
81+
expected_to_string(bucket, tally, 'max', max, data) if max != -1 and data > max
82+
end
83+
84+
def output(passed_threshold, what_failed)
85+
if passed_threshold
86+
puts "Overall compliance threshold of #{@threshold['compliance.min']}\% met. Current compliance at #{@summary[:compliance]}\%"
87+
else
88+
puts 'Compliance threshold was not met: '
89+
puts what_failed.join("\n")
90+
end
91+
end
92+
9693
def expected_to_string(bucket, tally, maxmin, value, got)
9794
return "Expected #{bucket}.#{tally}.#{maxmin}:#{value} got:#{got}" unless bucket.empty? || bucket.nil?
9895

0 commit comments

Comments
 (0)