Skip to content

Instantly share code, notes, and snippets.

@tyler-ball
Last active March 14, 2016 23:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tyler-ball/f7c16e814265f34260e9 to your computer and use it in GitHub Desktop.
Save tyler-ball/f7c16e814265f34260e9 to your computer and use it in GitHub Desktop.
Test Kitchen Shared Examples

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
@joelhandwell
Copy link

Why is the directory test/integration and not test/integration ? Is this naming convention?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment