Skip to content

Commit

Permalink
Fixing unit test adding Serverspec DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-ball committed Dec 18, 2014
1 parent 44fc2fa commit 0587f48
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Expand Up @@ -2,8 +2,6 @@ source "https://rubygems.org"
gemspec :name => "chef"

gem "activesupport", "< 4.0.0", :group => :compat_testing, :platform => "ruby"
# TODO after serverspec stops including their top level DSL we can remove this
gem "serverspec", :git => "https://github.com/tyler-ball/serverspec/"

group(:docgen) do
gem "yard"
Expand Down
3 changes: 1 addition & 2 deletions lib/chef/audit/audit_event_proxy.rb
@@ -1,6 +1,5 @@
#
# Auther:: Tyler Ball (<tball@chef.io>)
#
# Author:: Tyler Ball (<tball@chef.io>)
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
Expand Down
3 changes: 1 addition & 2 deletions lib/chef/audit/rspec_formatter.rb
@@ -1,6 +1,5 @@
#
# Auther:: Tyler Ball (<tball@chef.io>)
#
# Author:: Serdar Sutay (<serdar@chef.io>)
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
Expand Down
100 changes: 49 additions & 51 deletions spec/functional/audit/runner_spec.rb
Expand Up @@ -36,72 +36,70 @@
# Running in a sub_process means the serverspec libraries will only be included in a forked process, not the main one.
include RSpec::Support::InSubProcess

let(:events) { double("events").as_null_object }
let(:runner) { Chef::Audit::Runner.new(run_context) }
let(:stdout) { StringIO.new }

around(:each) do |ex|
Sandboxing.sandboxed { ex.run }
end

before do
Chef::Config[:log_location] = stdout
end

# When running these, because we are not mocking out any of the formatters we expect to get dual output on the
# command line
describe "#run" do

let(:audits) { {} }
let(:run_context) { instance_double(Chef::RunContext, :events => events, :audits => audits) }
let(:controls_name) { "controls_name" }

it "Correctly runs an empty controls block" do
in_sub_process do
runner.run
end
let(:events) { double("events").as_null_object }
let(:runner) { Chef::Audit::Runner.new(run_context) }
let(:stdout) { StringIO.new }

around(:each) do |ex|
Sandboxing.sandboxed { ex.run }
end

before do
Chef::Config[:log_location] = stdout
end

describe "#run" do

let(:audits) { {} }
let(:run_context) { instance_double(Chef::RunContext, :events => events, :audits => audits) }
let(:controls_name) { "controls_name" }

it "Correctly runs an empty controls block" do
in_sub_process do
runner.run
end
end

context "there is a single successful control" do
let(:audits) do
should_pass = lambda do
it "should pass" do
expect(2 - 2).to eq(0)
end
context "there is a single successful control" do
let(:audits) do
should_pass = lambda do
it "should pass" do
expect(2 - 2).to eq(0)
end
{ controls_name => Struct.new(:args, :block).new([controls_name], should_pass)}
end
{ controls_name => Struct.new(:args, :block).new([controls_name], should_pass)}
end

it "correctly runs" do
in_sub_process do
runner.run
it "correctly runs" do
in_sub_process do
runner.run

expect(stdout.string).to match(/1 example, 0 failures/)
end
expect(stdout.string).to match(/1 example, 0 failures/)
end
end
end

context "there is a single failing control" do
let(:audits) do
should_fail = lambda do
it "should fail" do
expect(2 - 1).to eq(0)
end
context "there is a single failing control" do
let(:audits) do
should_fail = lambda do
it "should fail" do
expect(2 - 1).to eq(0)
end
{ controls_name => Struct.new(:args, :block).new([controls_name], should_fail)}
end
{ controls_name => Struct.new(:args, :block).new([controls_name], should_fail)}
end

it "correctly runs" do
in_sub_process do
runner.run
it "correctly runs" do
in_sub_process do
runner.run

expect(stdout.string).to match(/Failure\/Error: expect\(2 - 1\)\.to eq\(0\)/)
expect(stdout.string).to match(/1 example, 1 failure/)
expect(stdout.string).to match(/# controls_name should fail/)
end
expect(stdout.string).to match(/Failure\/Error: expect\(2 - 1\)\.to eq\(0\)/)
expect(stdout.string).to match(/1 example, 1 failure/)
expect(stdout.string).to match(/# controls_name should fail/)
end
end

end

end

end
33 changes: 21 additions & 12 deletions spec/unit/audit/runner_spec.rb
Expand Up @@ -19,8 +19,12 @@
require 'spec_helper'
require 'spec/support/audit_helper'
require 'chef/audit/runner'
require 'chef/audit/audit_event_proxy'
require 'chef/audit/rspec_formatter'
require 'rspec/support/spec/in_sub_process'

describe Chef::Audit::Runner do
include RSpec::Support::InSubProcess

let(:events) { double("events") }
let(:run_context) { instance_double(Chef::RunContext, :events => events) }
Expand Down Expand Up @@ -48,23 +52,27 @@
end

it "sets all the config values" do
runner.send(:setup)
# This runs the Serverspec includes - we don't want these hanging around in all subsequent tests so
# we run this in a forked process. Keeps Serverspec files from getting loaded into main process.
in_sub_process do
runner.send(:setup)

expect(RSpec.configuration.output_stream).to eq(log_location)
expect(RSpec.configuration.error_stream).to eq(log_location)
expect(RSpec.configuration.output_stream).to eq(log_location)
expect(RSpec.configuration.error_stream).to eq(log_location)

expect(RSpec.configuration.formatters.size).to eq(2)
expect(RSpec.configuration.formatters).to include(instance_of(Chef::Audit::AuditEventProxy))
expect(RSpec.configuration.formatters).to include(instance_of(Chef::Audit::RspecFormatter))
expect(Chef::Audit::AuditEventProxy.class_variable_get(:@@events)).to eq(run_context.events)
expect(RSpec.configuration.formatters.size).to eq(2)
expect(RSpec.configuration.formatters).to include(instance_of(Chef::Audit::AuditEventProxy))
expect(RSpec.configuration.formatters).to include(instance_of(Chef::Audit::RspecFormatter))
expect(Chef::Audit::AuditEventProxy.class_variable_get(:@@events)).to eq(run_context.events)

expect(RSpec.configuration.expectation_frameworks).to eq([RSpec::Matchers])
expect(RSpec::Matchers.configuration.syntax).to eq([:expect])
expect(RSpec.configuration.expectation_frameworks).to eq([RSpec::Matchers])
expect(RSpec::Matchers.configuration.syntax).to eq([:expect])

expect(RSpec.configuration.color).to eq(color)
expect(RSpec.configuration.expose_dsl_globally?).to eq(false)
expect(RSpec.configuration.color).to eq(color)
expect(RSpec.configuration.expose_dsl_globally?).to eq(false)

expect(Specinfra.configuration.backend).to eq(:exec)
expect(Specinfra.configuration.backend).to eq(:exec)
end
end
end

Expand All @@ -88,6 +96,7 @@

expect(RSpec.world.example_groups.size).to eq(1)
# For whatever reason, `kind_of` is not working
# expect(RSpec.world.example_groups).to include(kind_of(RSpec::Core::ExampleGroup)) => FAIL
g = RSpec.world.example_groups[0]
expect(g.ancestors).to include(RSpec::Core::ExampleGroup)
expect(g.description).to eq("group_name")
Expand Down

0 comments on commit 0587f48

Please sign in to comment.