Skip to content

Commit

Permalink
Added basic skeleton of the rewritten cookbook
Browse files Browse the repository at this point in the history
First pass at framework with testing infrastructure, Rake tasks, etc

- Due to Foodcritic and how Supermarket renders README.md, rename README.markdown to README.md
- Add some default files for .gitignore, .rubocop.yml, .kitchen.yml, etc
- Added a default recipe to install curl and create a dummy chef resource, just as an example to start with
- Add chefspec and test-kitchen/serverspec default test suites that just test the current default recipe
- Add test fixtures like a wrapper cookbook to test how downstream cookbooks might use this one
- Add lengthy documentation on testing, how it works in this cookbook, etc

Closes #249
  • Loading branch information
martinb3 authored and karmi committed Oct 22, 2014
1 parent d2bf4ad commit 6c37af8
Show file tree
Hide file tree
Showing 22 changed files with 519 additions and 22 deletions.
24 changes: 23 additions & 1 deletion .gitignore
@@ -1 +1,23 @@
.DS_Store
*~
*#
.#*
\#*#
.*.sw[a-z]
*.un~
pkg/

# Berkshelf
.vagrant
/cookbooks
Berksfile.lock

# Bundler
Gemfile.lock
bin/*
.bundle/*

.kitchen/
.kitchen.local.yml

# chefspec
.coverage/
41 changes: 41 additions & 0 deletions .kitchen.yml
@@ -0,0 +1,41 @@
---
################################################################################
# Any local customizations should be placed inside the .kitchen.local.yml, which
# will not be checked in or overwritten. You may also use ~/.kitchen/config.yml
# or environment variables like VAGRANT_DEFAULT_PROVIDER. Anything in those will
# take precedence over anything set in `.kitchen.yml`.
################################################################################
driver:
name: vagrant # provide a default test-kitchen driver, vagrant

driver_config:
require_chef_omnibus: latest

provisioner:
name: chef_zero
nodes_path: 'test/fixtures/nodes'
data_bags_path: 'test/fixtures/data_bags'
environments_path: 'test/fixtures/environments'
# commented as it doesn't currently exist
# encrypted_data_bag_secret_key_path: "test/fixtures/encrypted_data_bag_secret_for_test"
client_rb:
environment: _default
attributes:
foo: 'bar'

platforms:
- name: ubuntu-14.04
run_list:
- recipe[apt]
- name: ubuntu-12.04
run_list:
- recipe[apt]
- name: centos-6.5
run_list:
- recipe[yum]

suites:
- name: default
run_list: recipe[elasticsearch]
attributes:
foo: 'bar'
45 changes: 45 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,45 @@
# Rubocop, we're buddies and all, but we're going to have to disagree on the following -

# Disable requirement of "encoding" headers on files
Encoding:
Enabled: false

# Increase line length, we're not on VT220s anymore
LineLength:
Max: 180

# Increase allowed lines in a method. Short methods are good, but 10 lines
# is a bit too low.
MethodLength:
Max: 40

# Favor explicit over implicit code: don't complain of "redundant returns"
RedundantReturn:
Enabled: false

# Don't complain about if/unless modifiers. The merit of this is debatable
# and it will likely require building of over-length lines.
IfUnlessModifier:
Enabled: false

# Raise allowed CyclomaticComplexity to 10.
CyclomaticComplexity:
Max: 10

# Disable Single Space before first arg
SingleSpaceBeforeFirstArg:
Enabled: false

# Don't force a word array unless 5 elements
WordArray:
MinSize: 5

# Don't complain about unused block args
UnusedBlockArgument:
Enabled: false

# There are too many non-ruby files that run up against rubocop rules in a cookbook
AllCops:
Include:
- '**/metadata.rb'
- '**/*.rb'
7 changes: 7 additions & 0 deletions Berksfile
@@ -0,0 +1,7 @@
source "https://supermarket.getchef.com"

metadata

group :integration do
cookbook "elasticsearch_test", :path => "./test/fixtures/cookbooks/elasticsearch_test"
end
40 changes: 40 additions & 0 deletions Gemfile
@@ -0,0 +1,40 @@
source 'https://rubygems.org'

group :lint do
gem 'foodcritic', '~> 3.0'
gem 'rubocop', '~> 0.26'
end

group :unit do
gem 'berkshelf', '~> 3'
gem 'chefspec'
gem 'chef-sugar'
end

group :kitchen_common do
gem 'test-kitchen'
end

group :kitchen_vagrant do
gem 'kitchen-vagrant'
gem 'vagrant-wrapper'
end

group :kitchen_rackspace do
gem 'kitchen-rackspace'
end

group :kitchen_ec2 do
gem 'kitchen-ec2'
end

group :development do
gem 'growl'
gem 'rb-fsevent'
gem 'guard'
gem 'guard-kitchen'
gem 'guard-foodcritic'
gem 'guard-rubocop'
gem 'fauxhai'
gem 'pry-nav'
end
21 changes: 0 additions & 21 deletions README.markdown

This file was deleted.

94 changes: 94 additions & 0 deletions README.md
@@ -0,0 +1,94 @@
# Elasticsearch Chef Cookbook

This branch contains the next version of the cookbook.

## Testing

This cookbook is equipped with both unit tests (chefspec) and integration tests
(test-kitchen and serverspec). It also comes with rubocop and foodcritic tasks
in the supplied Rakefile. Contributions to this cookbook should include tests
for new features or bugfixes, with a preference for unit tests over integration
tests to ensure speedy testing runs. ***All tests and most other commands here
should be run using bundler*** and our standard Gemfile. This ensures that
contributions and changes are made in a standardized way against the same
versions of gems. We recommend installing rubygems-bundler so that bundler is
automatically inserting `bundle exec` in front of commands run in a directory
that contains a Gemfile.

A full test run of all tests and style checks would look like:
```bash
$ bundle exec rake style
$ bundle exec rake spec
$ bundle exec rake integration
$ bundle exec rake destroy
```
The final destroy is intended to clean up any systems that failed a test, and is
mostly useful when running with kitchen drivers for cloud providers, so that no
machines are left orphaned and costing you money.

### Fixtures

This cookbook supplies a few different test fixtures (under `test/fixtures/`)
that can be shared amongst any number of unit or integration tests: cookbooks,
environments, and nodes. Environments and nodes are automatically loaded into
chef-zero for both chefspec tests that run locally and serverspec tests that run
from test-kitchen.

It also contains 'platform data' that can be used to drive unit testing, for
example, you might read `httpd` for some platforms and `apache2` for others,
allowing you to write a single test for the Apache webserver. Unfortunately,
without further modifications to `busser` and `busser-serverspec`, the platform
data will not be available to serverspec tests.

### Style and Best Practices

Rubocop and Foodcritic evaluations may be made by running `rake style`. There
are no overrides for foodcritic rules, however the adjustments to
rubocop are made using the supplied `.rubocop.yml` file and have been documented
by comments within. Most notably, rubocop has been restricted to only apply to
`.rb` files.

Rubocop and foodcritic tests can be executed using `rake style`.

### Unit testing

Unit testing is done using the latest versions of Chefspec. The current default
test layout includes running against all supported platforms, as well as
stubbing data into chef-zero. This allows us to also test against chef search.
As is currently a best practice in the community, we will avoid the use of
chef-solo, but not create barriers to explicitly fail for chef-solo.

Unit tests can be executed using `rake spec`.

### Integration testing

Integration testing is accomplished using the latest versions of test-kitchen
and serverspec. Currently, this cookbook uses the busser-serverspec plugin for
copying serverspec files to the system being tested. There is some debate in the
community about whether this should be done using busser-rspec instead, and each
busser plugin has a slightly different feature set.

While the default test-kitchen configuration uses the vagrant driver, you may
override this using `~/.kitchen/config.yml` or by placing a `.kitchen.local.yml`
in the current directory. This allows you to run these integration tests using
any supported test-kitchen driver (ec2, rackspace, docker, etc).

Integration tests can be executed using `rake integration` or `kitchen test`.

## License

This software is licensed under the Apache 2 license, quoted below.

Copyright (c) 2014 Elasticsearch <http://www.elasticsearch.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
63 changes: 63 additions & 0 deletions Rakefile
@@ -0,0 +1,63 @@
# Encoding: utf-8
require 'bundler/setup'

namespace :style do
require 'rubocop/rake_task'
desc 'Run Ruby style checks'
RuboCop::RakeTask.new(:ruby) do |task|
# see templatestack's .rubocop.yml for comparison
task.patterns = ['**/*.rb']

# only show the files with failures
task.formatters = ['files']

# abort rake on failure
task.fail_on_error = true
end

require 'foodcritic'
desc 'Run Chef style checks'
FoodCritic::Rake::LintTask.new(:chef) do |t|
# 'search_gems' doesn't work, but :search_gems does
# rubocop:disable Style/HashSyntax
t.options = { :search_gems => true, # allows us to add addl gems with more rules
:fail_tags => ['correctness'],
:chef_version => '11.6.0'
}
# rubocop:enable Style/HashSyntax
end
end

desc 'Run all style checks'
task style: ['style:chef', 'style:ruby']

require 'kitchen'
desc 'Run Test Kitchen integration tests'
task :integration do
Kitchen.logger = Kitchen.default_file_logger
sh 'kitchen test -c'
end

desc 'Destroy test kitchen instances'
task :destroy do
Kitchen.logger = Kitchen.default_file_logger
Kitchen::Config.new.instances.each do |instance|
instance.destroy
end
end

require 'rspec/core/rake_task'
desc 'Run ChefSpec unit tests'
RSpec::Core::RakeTask.new(:spec) do |t, args|
t.rspec_opts = 'test/unit'
end

# The default rake task should just run it all
task default: ['style', 'spec', 'integration']

begin
require 'kitchen/rake_tasks'
Kitchen::RakeTasks.new
rescue LoadError
puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI']
end
13 changes: 13 additions & 0 deletions metadata.rb
@@ -0,0 +1,13 @@
# Encoding: utf-8
name 'elasticsearch'
maintainer 'karmi'
maintainer_email 'karmi@karmi.cz'
license 'Apache 2.0'
description 'Installs/Configures elasticsearch'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '1.0.0'

depends 'apt'
depends 'yum'
depends 'chef-sugar'
depends 'curl'
15 changes: 15 additions & 0 deletions recipes/default.rb
@@ -0,0 +1,15 @@
# Encoding: utf-8
#
# Cookbook Name:: elasticsearch
# Recipe:: default
#

include_recipe 'chef-sugar'
include_recipe 'curl'

ruby_block 'dummy_block for test coverage' do
block do
# some Ruby code
end
action :run
end
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/cookbooks/elasticsearch_test/metadata.rb
@@ -0,0 +1,12 @@
# Encoding: utf-8
name 'elasticsearch_test'
maintainer 'karmi'
maintainer_email 'karmi@karmi.cz'
license 'Apache 2.0'
description 'A wrapper cookbook for use in testing that elasticsearch cookbook works well with wrappers calling it'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'

depends 'apt'
depends 'yum'
depends 'chef-sugar'
4 changes: 4 additions & 0 deletions test/fixtures/cookbooks/elasticsearch_test/recipes/default.rb
@@ -0,0 +1,4 @@
# this is a test fixture used to test that the elasticsearch cookbook's
# provided LWRPs and recipes can be used correctly from a wrapper

include_recipe 'chef-sugar' # placeholder while we're writing recipes still

0 comments on commit 6c37af8

Please sign in to comment.