Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pop <apop@chef.io>
  • Loading branch information
alexpop committed May 12, 2017
1 parent edc706c commit ea81471
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 87 deletions.
1 change: 0 additions & 1 deletion libraries/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def get_reporters(audit)
end
handle_reporters(audit['reporter'])
end

end

::Chef::Recipe.send(:include, ReportHelpers)
1 change: 1 addition & 0 deletions libraries/reporters/cs_automate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(opts)
@run_id = opts[:run_id]
@node_name = opts[:node_info][:node]
@insecure = opts[:insecure]
@environment = opts[:node_info][:environment]
@url = opts[:url]
end

Expand Down
3 changes: 2 additions & 1 deletion spec/data/mock.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class MockData
def self.node_info
{ node: "chef-client.solo" }
{ node: "chef-client.solo",
environment: 'My Prod Env' }
end

def self.inspec_results
Expand Down
28 changes: 12 additions & 16 deletions spec/unit/libraries/automate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
entity_uuid = 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz'
run_id = '3f0536f7-3361-4bca-ae53-b45118dceb5d'
insecure = false
@enriched_report_expected = { "profiles":
@enriched_report_expected = {"version":"1.2.1",
"profiles":
[{"name":"tmp_compliance_profile",
"title":"/tmp Compliance Profile",
"summary":"An Example Compliance Profile",
Expand Down Expand Up @@ -58,20 +59,15 @@
"results":[{"status":"passed", "code_desc":"File /tmp should be owned by \"root\"", "run_time":0.028845, "start_time":"2016-10-19 11:09:43 -0400"}]}],
"groups":[{"title":"/tmp Compliance Profile", "controls":["tmp-1.0", "tmp-1.1"], "id":"controls/tmp.rb"}],
"attributes":[{"name":"syslog_pkg", "options":{"default":"rsyslog", "description":"syslog package...", "type":"string"}}]}],
"event_type":"inspec",
"event_action":"exec",
"compliance_summary":{
"total":2,
"passed":{"total":2},
"skipped":{"total":0},
"failed":{"total":0, "minor":0, "major":0, "critical":0},
"status":"passed",
"node_name":"chef-client.solo",
"end_time":"2016-07-19T19:19:19+01:00",
"duration":0.032332,
"inspec_version":"1.2.1"},
"entity_uuid":"aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz",
"run_id":"3f0536f7-3361-4bca-ae53-b45118dceb5d"}
"other_checks": [],
"statistics":{"duration":0.032332},
"type":"inspec_report",
"node_name":"chef-client.solo",
"end_time":"2016-07-19T19:19:19+01:00",
"node_uuid":"aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz",
"environment":"My Prod Env",
"report_uuid":"3f0536f7-3361-4bca-ae53-b45118dceb5d"
}

opts = {
entity_uuid: entity_uuid,
Expand All @@ -89,7 +85,7 @@
@automate = Reporter::ChefAutomate.new(opts)
end

it 'sends report successfully' do
it 'sends report successfully to ChefAutomate' do
allow(DateTime).to receive(:now).and_return(DateTime.parse('2016-07-19T19:19:19+01:00'))
expect(@automate.send_report(MockData.inspec_results)).to eq(true)
end
Expand Down
30 changes: 13 additions & 17 deletions spec/unit/libraries/cs_automate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
entity_uuid = 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz'
run_id = '3f0536f7-3361-4bca-ae53-b45118dceb5d'
insecure = false
@enriched_report_expected = { "profiles":
@enriched_report_expected = { "version": "1.2.1",
"profiles":
[{"name": "tmp_compliance_profile",
"title": "/tmp Compliance Profile",
"summary": "An Example Compliance Profile",
Expand Down Expand Up @@ -44,20 +45,15 @@
"results": [{"status": "passed", "code_desc": "File /tmp should be owned by \"root\"", "run_time": 0.028845, "start_time": "2016-10-19 11:09:43 -0400"}]}],
"groups": [{"title": "/tmp Compliance Profile", "controls": ["tmp-1.0", "tmp-1.1"], "id": "controls/tmp.rb"}],
"attributes": [{"name": "syslog_pkg", "options": {"default": "rsyslog", "description": "syslog package...", "type": "string"}}]}],
"event_type": "inspec",
"event_action": "exec",
"compliance_summary": {
"total": 2,
"passed": {"total": 2},
"skipped": {"total": 0},
"failed": {"total": 0, "minor": 0, "major": 0, "critical": 0},
"status": "passed",
"node_name": "chef-client.solo",
"end_time": "2016-07-19T19:19:19+01:00",
"duration": 0.032332,
"inspec_version": "1.2.1"},
"entity_uuid": "aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz",
"run_id": "3f0536f7-3361-4bca-ae53-b45118dceb5d"}
"other_checks": [],
"statistics":{"duration":0.032332},
"type": "inspec_report",
"node_name": "chef-client.solo",
"end_time": "2016-07-19T19:19:19+01:00",
"node_uuid": "aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz",
"environment": "My Prod Env",
"report_uuid": "3f0536f7-3361-4bca-ae53-b45118dceb5d"
}

opts = {
entity_uuid: entity_uuid,
Expand All @@ -73,12 +69,12 @@
# set data_collector
stub_request(:post, 'https://chef.server/data_collector').
with(:body => @enriched_report_expected.to_json,
:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'2818', 'Content-Type'=>'application/json', 'Host'=>/.+/, 'User-Agent'=>/.+/, 'X-Chef-Version'=>/.+/, 'X-Ops-Authorization-1'=>/.+/, 'X-Ops-Authorization-2'=>/.+/, 'X-Ops-Authorization-3'=>/.+/, 'X-Ops-Authorization-4' => /.+/, 'X-Ops-Authorization-5'=>/.+/, 'X-Ops-Authorization-6'=>/.+/, 'X-Ops-Content-Hash'=>/.+/, 'X-Ops-Server-Api-Version'=>'1', 'X-Ops-Sign'=>'algorithm=sha1;version=1.1;', 'X-Ops-Timestamp'=>/.+/, 'X-Ops-Userid'=>'spec-node', 'X-Remote-Request-Id'=>/.+/}).
:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'2706', 'Content-Type'=>'application/json', 'Host'=>/.+/, 'User-Agent'=>/.+/, 'X-Chef-Version'=>/.+/, 'X-Ops-Authorization-1'=>/.+/, 'X-Ops-Authorization-2'=>/.+/, 'X-Ops-Authorization-3'=>/.+/, 'X-Ops-Authorization-4' => /.+/, 'X-Ops-Authorization-5'=>/.+/, 'X-Ops-Authorization-6'=>/.+/, 'X-Ops-Content-Hash'=>/.+/, 'X-Ops-Server-Api-Version'=>'1', 'X-Ops-Sign'=>'algorithm=sha1;version=1.1;', 'X-Ops-Timestamp'=>/.+/, 'X-Ops-Userid'=>'spec-node', 'X-Remote-Request-Id'=>/.+/}).
to_return(:status => 200, :body => "", :headers => {})
@automate = Reporter::ChefServerAutomate.new(opts)
end

it 'sends report successfully' do
it 'sends report successfully to ChefServerAutomate' do
allow(DateTime).to receive(:now).and_return(DateTime.parse('2016-07-19T19:19:19+01:00'))
expect(@automate.send_report(MockData.inspec_results)).to eq(true)
end
Expand Down
52 changes: 0 additions & 52 deletions spec/unit/libraries/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,4 @@
expect(File).to exist("#{expected_file_path}")
File.delete("#{expected_file_path}")
end

it 'returns the correct control status' do
expect(@helpers.control_status(nil)).to eq(nil)
expect(@helpers.control_status([{"status":"failed"}])).to eq('failed')
expect(@helpers.control_status([{"status":"passed"}])).to eq('passed')
expect(@helpers.control_status([{"status":"passed"},{"status":"failed"}])).to eq('failed')
expect(@helpers.control_status([{"status":"failed"},{"status":"passed"}])).to eq('failed')
expect(@helpers.control_status([{"status":"passed"},{"status":"skipped"}])).to eq('skipped')
expect(@helpers.control_status([{"status":"skipped"},{"status":"failed"}])).to eq('failed')
end

it 'reports impact criticality correctly' do
expect(@helpers.impact_to_s(0)).to eq('minor')
expect(@helpers.impact_to_s(0.1234)).to eq('minor')
expect(@helpers.impact_to_s(0.4)).to eq('major')
expect(@helpers.impact_to_s(0.69)).to eq('major')
expect(@helpers.impact_to_s(0.7)).to eq('critical')
expect(@helpers.impact_to_s(1.0)).to eq('critical')
end

it 'reports compliance status like a compliance officer' do
passed = {"total":5, "passed":{"total":3}, "skipped":{"total":2}, "failed":{"total":0, "minor":0, "major":0, "critical":0}}
failed = {"total":5, "passed":{"total":1}, "skipped":{"total":1}, "failed":{"total":3, "minor":1, "major":1, "critical":1}}
skipped = {"total":5, "passed":{"total":0}, "skipped":{"total":5}, "failed":{"total":0, "minor":0, "major":0, "critical":0}}
expect(@helpers.compliance_status(nil)).to eq('unknown')
expect(@helpers.compliance_status(failed)).to eq('failed')
expect(@helpers.compliance_status(passed)).to eq('passed')
expect(@helpers.compliance_status(skipped)).to eq('skipped')
end

it 'counts controls like an accountant' do
profil = [{"name":"test-profile",
"controls":
[{"id":"Checking /etc/missing1.rb existance",
"impact":0,
"results":[{"status":"failed"}]},
{"id":"Checking /etc/missing2.rb existance",
"impact":0.5,
"results":[{"status":"failed"}]},
{"id":"Checking /etc/missing3.rb existance",
"impact":0.8,
"results":[{"status":"failed"}]},
{"id":"Checking /etc/passwd existance",
"impact":0.88,
"results":[{"status":"passed"}]},
{"id":"Checking /etc/something existance",
"impact":1.0,
"results":[{"status":"skipped"}]}]
}]
expected_count = {"total":5, "passed":{"total":1}, "skipped":{"total":1}, "failed":{"total":3, "minor":1, "major":1, "critical":1}}
expect(@helpers.count_controls(profil)).to eq(expected_count)
end
end

0 comments on commit ea81471

Please sign in to comment.