Skip to content

Instantly share code, notes, and snippets.

@pburkholder
Forked from tyler-ball/gist:f7c16e814265f34260e9
Created February 20, 2015 02:00
Show Gist options
  • Save pburkholder/a9cdde0b33be8d5e3c08 to your computer and use it in GitHub Desktop.
Save pburkholder/a9cdde0b33be8d5e3c08 to your computer and use it in GitHub Desktop.
Shared examples in rspec/serverspec.....

Create the following folder structure in your cookbook:

test
└── integrationq
    ├── helpers
    │   ├── serverspec
    │   │   ├── shared_serverspec_tests
    │   │   │   └── shared_tests2.rb
    │   │   └── spec_helper.rb
    │   └── shared_tests
    │       └── shared_tests1.rb
    ├── prod
    │   └── serverspec
    │       └── prod_spec.rb
    └── qa
        └── serverspec
            └── qa_spec.rb

And the following test suite setup in your .kitchen.yml

suites:
  - name: qa
    environment: qa
    run_list:
      - recipe[test-tk::default]
  - name: prod
    environment: prod
    run_list:
      - recipe[test-tk::default]

This will create the following suite structure on the test nodes when you run kitchen verify:

QA Node

ubuntu@ip-172-31-18-237:/tmp/busser/suites$ tree .
.
├── serverspec
│   ├── qa_spec.rb
│   ├── shared_serverspec_tests
│   │   └── shared_tests2.rb
│   └── spec_helper.rb
└── shared_tests
    └── shared_tests1.rb

Prod Node

ubuntu@ip-172-31-21-211:/tmp/busser/suites$ tree .
.
├── serverspec
│   ├── prod_spec.rb
│   ├── shared_serverspec_tests
│   │   └── shared_tests2.rb
│   └── spec_helper.rb
└── shared_tests
    └── shared_tests1.rb

This will allow you to write the following from within either prod_spec.rb or qa_spec.rb:

require 'spec_helper.rb'
require_relative '../shared_tests/shared_tests1.rb'
require_relative 'shared_serverspec_tests/shared_tests2.rb'

Examples

Inside shared_tests/shared_tests1.rb

require 'rspec' # You could require a spec_helper here - you just need to RSpec functionality to defined shared examples
require 'serverspec' # If you want to use serverspec matchers, you will need this too

RSpec.shared_examples "user examples" do
  describe user('apache') do
    it { should belong_to_group apache_group }
  end
end

Inside your QA test:

require 'spec_helper.rb'
require_relative '../shared_tests/shared_tests1.rb'

describe "qa environment" do
  let(:apache_group) { 'qa' }
  include_examples "user examples"
end

And your prod test:

require 'spec_helper.rb'
require_relative '../shared_tests/shared_tests1.rb'

describe "qa environment" do
  let(:apache_group) { 'prod' }
  include_examples "user examples"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment