Class: CemAcpt::Scan::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/scan/result.rb

Overview

Normalized scan result. Wraps the JSON payload returned by the on-node scan daemon and exposes the bits the runner needs: the score the daemon reported (scanner-native pass-rate, 0-100), the pass/fail decision against the configured threshold, and a status code compatible with TestRunner::TestResults so the existing result-aggregation plumbing doesn’t have to grow a new branch.

Status semantics: 200 if ‘score >= threshold`, 1 otherwise. This makes Result look-alike enough to Goss::Api::ActionResponse for the runner’s ‘process_test_results` loop to treat it uniformly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_case:, scanner:, profile:, threshold:, body:) ⇒ Result

Returns a new instance of Result.

Parameters:

  • test_case (String)

    The acceptance-test directory name.

  • scanner (Symbol, String)

    :openscap or :ciscat.

  • profile (String)

    The scanner-native profile id used.

  • threshold (Float)

    The pass threshold this result was evaluated against.

  • body (Hash)

    The parsed JSON payload from the scan daemon. Must include numeric ‘score` and counts (`passed_count`, `failed_count`, `not_applicable_count`, `error_count`); may include a `rules` array.



27
28
29
30
31
32
33
34
# File 'lib/cem_acpt/scan/result.rb', line 27

def initialize(test_case:, scanner:, profile:, threshold:, body:)
  @test_case = test_case
  @scanner = scanner.to_sym
  @profile = profile
  @threshold = threshold.to_f
  @body = body || {}
  @score = (@body['score'] || @body[:score] || 0.0).to_f
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def body
  @body
end

#profileObject (readonly)

Returns the value of attribute profile.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def profile
  @profile
end

#scannerObject (readonly)

Returns the value of attribute scanner.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def scanner
  @scanner
end

#scoreObject (readonly)

Returns the value of attribute score.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def score
  @score
end

#test_caseObject (readonly)

Returns the value of attribute test_case.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def test_case
  @test_case
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



18
19
20
# File 'lib/cem_acpt/scan/result.rb', line 18

def threshold
  @threshold
end

Instance Method Details

#fail?Boolean Also known as: error?

Returns true if the score did not meet the threshold.

Returns:

  • (Boolean)

    true if the score did not meet the threshold.



43
44
45
# File 'lib/cem_acpt/scan/result.rb', line 43

def fail?
  !pass?
end

#pass?Boolean Also known as: success?

Returns true if the score met the threshold.

Returns:

  • (Boolean)

    true if the score met the threshold.



37
38
39
# File 'lib/cem_acpt/scan/result.rb', line 37

def pass?
  @score >= @threshold
end

#statusInteger Also known as: http_status

Conforms to the contract TestRunner::Runner#process_test_results uses to decide the run’s exit code.

Returns:

  • (Integer)

    HTTP-style status: 200 on pass, 1 on fail.



51
52
53
# File 'lib/cem_acpt/scan/result.rb', line 51

def status
  pass? ? 200 : 1
end

#to_hHash

Returns The full normalized result, suitable for ‘to_json`.

Returns:

  • (Hash)

    The full normalized result, suitable for ‘to_json`.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cem_acpt/scan/result.rb', line 57

def to_h
  {
    test_case: @test_case,
    scanner: @scanner,
    profile: @profile,
    score: @score,
    threshold: @threshold,
    passed_count: counts(:passed_count),
    failed_count: counts(:failed_count),
    not_applicable_count: counts(:not_applicable_count),
    error_count: counts(:error_count),
    rules: @body['rules'] || @body[:rules] || [],
    pass: pass?,
  }
end

#to_json(*args) ⇒ Object



73
74
75
# File 'lib/cem_acpt/scan/result.rb', line 73

def to_json(*args)
  to_h.to_json(*args)
end

#to_sObject Also known as: inspect



77
78
79
# File 'lib/cem_acpt/scan/result.rb', line 77

def to_s
  "<#{self.class.name} test_case=#{@test_case} scanner=#{@scanner} score=#{@score} threshold=#{@threshold} pass=#{pass?}>"
end