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.
Add more tests
Browse files Browse the repository at this point in the history
sbernhard committed Apr 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c841fd7 commit 0489940
Showing 6 changed files with 5,237 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -26,6 +26,12 @@ Metrics/ClassLength:
Metrics/MethodLength:
Max: 20

Metrics/CyclomaticComplexity:
Max: 10

Metrics/PerceivedComplexity:
Max: 10

Rails:
Enabled: true

5 changes: 3 additions & 2 deletions app/services/foreman_cve_scanner/cve_report_scanner.rb
Original file line number Diff line number Diff line change
@@ -103,8 +103,9 @@ def generate_trivy_entry(entry)

# rubocop:disable Metrics/AbcSize
def generate_unified_vuls
j = @raw_data['scan']
raise ::Foreman::Exception, _('Invalid CVE scanner report') unless @raw_data.key?('scan')

j = @raw_data['scan']
vuls = {}
if j.key?('matches') # Grype
j['matches'].each do |vul|
@@ -119,7 +120,7 @@ def generate_unified_vuls
end
else
Rails.logger.error 'Unsupported cve scanner report format'
raise ::Foreman::Exception, _('Invalid report')
raise ::Foreman::Exception, _('Unsupported cve scanner report format')
end

vuls
2,970 changes: 2,970 additions & 0 deletions test/fixtures/grype-simple.json

Large diffs are not rendered by default.

1,765 changes: 1,765 additions & 0 deletions test/fixtures/trivy-large.json

Large diffs are not rendered by default.

463 changes: 463 additions & 0 deletions test/fixtures/trivy-simple.json

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion test/services/foreman_cve_scanner/cve_report_scanner_test.rb
Original file line number Diff line number Diff line change
@@ -9,8 +9,37 @@ def setup
@scanner = ForemanCveScanner::CveReportScanner.new(raw)
end

test 'trivy scan has valid data' do
data = JSON.parse(File.read(File.join(ForemanCveScanner::Engine.root, 'test/fixtures/trivy-simple.json')))
raw = {
'scan' => data
}
ForemanCveScanner::CveReportScanner.add_reporter_data(nil, raw)
assert_equal raw['logs'].count, 1
assert_equal raw['logs'][0]['log']['level'], 'info'
assert_equal raw['logs'][0]['log']['messages']['message'], 'fooo'
end

test 'grype scan has valid data' do
data = JSON.parse(File.read(File.join(ForemanCveScanner::Engine.root, 'test/fixtures/grype-simple.json')))
raw = {
'scan' => data
}
ForemanCveScanner::CveReportScanner.add_reporter_data(nil, raw)
assert_equal raw['logs'].count, 1
assert_equal raw['logs'][0]['log']['level'], 'info'
assert_equal raw['logs'][0]['log']['messages']['message'], 'fooo'
end

test 'should identify as cve scan' do
assert_equasl @scanner.identify_origin, 'CveScanner'
raw = { 'reporter' => 'cve_scan' }
assert_equasl ForemanCveScanner::CveReportScanner.identify_origin(raw), 'CveScanner'
end

test 'should raise an exception if invalid report' do
assert_raise Foreman::Exception do
@scanner = ForemanCveScanner::CveReportScanner.new({})
end
end
end
end

0 comments on commit 0489940

Please sign in to comment.