From 15979e73e51067e9f73b19a5261ba58b04d0b21d Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 18 Jan 2014 23:14:08 -0500 Subject: [PATCH] Update cookbook to latest standards - Refactor providers to support whyrun mode - Package custom ChefSpec matchers - Add Test Kitchen integration test - Update ChefSpec tests - Expand environment variables (like `$PATH`) (Fixes #6) - Add Rakefile for running tests - Use the latest and greatest testing strategies --- .gitignore | 38 +++++---- .kitchen.yml | 27 ++++++ .rspec | 1 - .rubocop.yml | 4 + .travis.yml | 9 +- Berksfile | 8 ++ CHANGELOG.md | 12 ++- Gemfile | 12 ++- README.md | 32 ++++--- Rakefile | 38 +++++++++ Strainerfile | 3 - attributes/default.rb | 1 - chefignore | 7 -- libraries/matchers.rb | 17 ++++ metadata.rb | 2 +- providers/alias.rb | 83 +++++++++++++----- providers/environment.rb | 84 ++++++++++++++----- resources/alias.rb | 39 +++++---- resources/environment.rb | 39 +++++---- spec/alias_spec.rb | 22 ----- spec/cookbooks/alias/metadata.rb | 9 -- spec/cookbooks/alias/recipes/add_explicit.rb | 4 - spec/cookbooks/environment/metadata.rb | 9 -- .../environment/recipes/add_explicit.rb | 4 - spec/environment_spec.rb | 22 ----- spec/providers/alias_spec.rb | 15 ++++ spec/providers/environment_spec.rb | 16 ++++ spec/spec_helper.rb | 13 +-- spec/support/alias.rb | 39 --------- spec/support/environment.rb | 39 --------- .../cookbooks/magic_shell_alias/metadata.rb | 2 + .../magic_shell_alias/recipes/add.rb | 0 .../magic_shell_alias/recipes/remove.rb | 5 ++ .../magic_shell_environment/metadata.rb | 2 + .../magic_shell_environment/recipes/add.rb | 0 .../magic_shell_environment/recipes/remove.rb | 5 ++ .../serverspec/assert_added_spec.rb | 6 ++ .../serverspec/assert_removed_spec.rb | 5 ++ .../serverspec/assert_added_spec.rb | 6 ++ .../serverspec/assert_removed_spec.rb | 5 ++ test/shared/spec_helper.rb | 9 ++ 41 files changed, 411 insertions(+), 282 deletions(-) create mode 100644 .kitchen.yml delete mode 100644 .rspec create mode 100644 .rubocop.yml create mode 100644 Berksfile create mode 100644 Rakefile delete mode 100644 Strainerfile delete mode 100644 attributes/default.rb delete mode 100644 chefignore create mode 100644 libraries/matchers.rb delete mode 100644 spec/alias_spec.rb delete mode 100644 spec/cookbooks/alias/metadata.rb delete mode 100644 spec/cookbooks/alias/recipes/add_explicit.rb delete mode 100644 spec/cookbooks/environment/metadata.rb delete mode 100644 spec/cookbooks/environment/recipes/add_explicit.rb delete mode 100644 spec/environment_spec.rb create mode 100644 spec/providers/alias_spec.rb create mode 100644 spec/providers/environment_spec.rb delete mode 100644 spec/support/alias.rb delete mode 100644 spec/support/environment.rb create mode 100644 test/fixtures/cookbooks/magic_shell_alias/metadata.rb rename spec/cookbooks/alias/recipes/add_implicit.rb => test/fixtures/cookbooks/magic_shell_alias/recipes/add.rb (100%) create mode 100644 test/fixtures/cookbooks/magic_shell_alias/recipes/remove.rb create mode 100644 test/fixtures/cookbooks/magic_shell_environment/metadata.rb rename spec/cookbooks/environment/recipes/add_implicit.rb => test/fixtures/cookbooks/magic_shell_environment/recipes/add.rb (100%) create mode 100644 test/fixtures/cookbooks/magic_shell_environment/recipes/remove.rb create mode 100644 test/integration/magic_shell_alias_add/serverspec/assert_added_spec.rb create mode 100644 test/integration/magic_shell_alias_remove/serverspec/assert_removed_spec.rb create mode 100644 test/integration/magic_shell_environment_add/serverspec/assert_added_spec.rb create mode 100644 test/integration/magic_shell_environment_remove/serverspec/assert_removed_spec.rb create mode 100644 test/shared/spec_helper.rb diff --git a/.gitignore b/.gitignore index 8f0e94f..447c930 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,23 @@ -*.swp -.rvmrc - -.idea -.yardoc -*.gem -*.html -*.rbc -*.swp -coverage -doc -tmp +*~ +*# +.#* +\#*# +.*.sw[a-z] +*.un~ +*.tmp +*.bk +*.bkup +.kitchen.local.yml +Berksfile.lock Gemfile.lock -gemfiles/*.lock -.bundle -bin -vendor + +.bundle/ +.cache/ +.kitchen/ +.vagrant/ +.vagrant.d/ +.yardoc/ +bin/ +doc/ +tmp/ +vendor/ diff --git a/.kitchen.yml b/.kitchen.yml new file mode 100644 index 0000000..6c0e6a7 --- /dev/null +++ b/.kitchen.yml @@ -0,0 +1,27 @@ +driver: + name: vagrant + +provisioner: + name: chef_solo + data_path: test/shared + +platforms: + - name: ubuntu-12.04 + - name: centos-6.4 + +suites: + # + # Alias suites + # + - name: magic_shell_alias_add + run_list: magic_shell_alias::add + - name: magic_shell_alias_remove + run_list: magic_shell_alias::remove + + # + # Environment suites + # + - name: magic_shell_environment_add + run_list: magic_shell_environment::add + - name: magic_shell_environment_remove + run_list: magic_shell_environment::remove diff --git a/.rspec b/.rspec deleted file mode 100644 index 4e1e0d2..0000000 --- a/.rspec +++ /dev/null @@ -1 +0,0 @@ ---color diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..c953580 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,4 @@ +Encoding: + Enabled: false +LineLength: + Enabled: false diff --git a/.travis.yml b/.travis.yml index ef19161..080e92a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ -language: ruby rvm: - - 1.9.2 - 1.9.3 -env: - - CHEF_VERSION='~> 10.0' - - CHEF_VERSION='~> 11.0' + - 2.0.0 + - 2.1.0 script: - - bundle exec strainer test + - bundle exec rake travis:ci diff --git a/Berksfile b/Berksfile new file mode 100644 index 0000000..44b3224 --- /dev/null +++ b/Berksfile @@ -0,0 +1,8 @@ +source 'https://api.berkshelf.com' + +group :integration do + cookbook 'magic_shell_alias', path: 'test/fixtures/cookbooks/magic_shell_alias' + cookbook 'magic_shell_environment', path: 'test/fixtures/cookbooks/magic_shell_environment' +end + +metadata diff --git a/CHANGELOG.md b/CHANGELOG.md index 079dc1e..2d292da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,18 @@ magic_shell CHANGELOG ===================== +1.0.0 +----- +* Refactor providers to support whyrun mode +* Package custom ChefSpec matchers +* Add Test Kitchen integration test +* Update ChefSpec tests +* Expand environment variables (like `$PATH`) +* Add Rakefile for running tests +* Use the latest and greatest testing strategies + 0.3.1, 0.3.2 ------------ +------------ * **No changes** - chefigonre with `knife cookbook site share` was being ignored, so unnecessary artifacts were uploaded diff --git a/Gemfile b/Gemfile index 1667511..565f4d0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,9 +1,13 @@ source 'https://rubygems.org' -gem 'chef', ENV['CHEF_VERSION'] || '>= 10.0' +gem 'rake' group :test do - gem 'chefspec', '~> 1.0.0' - gem 'foodcritic', '~> 2.1.0' - gem 'strainer', '~> 2.0.0' + gem 'berkshelf', '~> 3.0.0.beta' + gem 'chefspec', '~> 3.0' + gem 'foodcritic', '~> 3.0' + gem 'rubocop', '~> 0.16' + + gem 'test-kitchen', '~> 1.1' + gem 'kitchen-vagrant', '~> 0.14' end diff --git a/README.md b/README.md index 875af3d..f10339e 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,6 @@ magic_shell Cookbook Provides utility for adding some syntactic sugar to your shell. -Requirements ------------- -None - -Attributes ----------- -None - Usage ----- Update the `metadata.rb` for your cookbook to depend on magic_shell @@ -63,8 +55,24 @@ Contributing 3. Code, document, write specs, test 4. Submit a PR -License and Authors -------------------- -Author: [Nathen Harvey](https://github.com/nathenharvey) ([@nathenharvey](https://twitter.com/nathenharvey)) -Copyright 2012, CustomInk, LLC +License & Authors +----------------- +- Author: Nathen Harvey +- Author: Seth Vargo + +```text +Copyright 2012-2014 CustomInk, LLC. + +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. +``` diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..bedbb2d --- /dev/null +++ b/Rakefile @@ -0,0 +1,38 @@ +require 'bundler/setup' + +namespace :style do + require 'rubocop/rake_task' + desc 'Run Ruby style checks' + Rubocop::RakeTask.new(:ruby) + + require 'foodcritic' + desc 'Run Chef style checks' + FoodCritic::Rake::LintTask.new(:chef) +end + +desc 'Run all style checks' +task style: ['style:chef', 'style:ruby'] + +require 'rspec/core/rake_task' +desc 'Run ChefSpec unit tests' +RSpec::Core::RakeTask.new(:unit) do |t| + t.rspec_opts = '--color --format progress' +end + +require 'kitchen' +desc 'Run Test Kitchen integration tests' +task :integration do + Kitchen.logger = Kitchen.default_file_logger + Kitchen::Config.new.instances.each do |instance| + instance.test(:always) + end +end + +# We cannot run Test Kitchen on Travis CI yet... +namespace :travis do + desc 'Run tests on Travis' + task ci: ['style', 'unit'] +end + +# The default rake task should just run it all +task default: ['style', 'unit', 'integration'] diff --git a/Strainerfile b/Strainerfile deleted file mode 100644 index 0d87a0c..0000000 --- a/Strainerfile +++ /dev/null @@ -1,3 +0,0 @@ -knife: bundle exec knife cookbook test $COOKBOOK -foodcritic: bundle exec foodcritic $SANDBOX/$COOKBOOK -rspec: (cd $COOKBOOK && bundle exec rspec --color --format documentation) diff --git a/attributes/default.rb b/attributes/default.rb deleted file mode 100644 index 854a442..0000000 --- a/attributes/default.rb +++ /dev/null @@ -1 +0,0 @@ -default['magic_shell']['environment'] = {} diff --git a/chefignore b/chefignore deleted file mode 100644 index 15a9388..0000000 --- a/chefignore +++ /dev/null @@ -1,7 +0,0 @@ -spec/ -.gitignore -.rspec -.travis.yml -Gemfile -Gemfile.lock -Strainerfile diff --git a/libraries/matchers.rb b/libraries/matchers.rb new file mode 100644 index 0000000..f316efe --- /dev/null +++ b/libraries/matchers.rb @@ -0,0 +1,17 @@ +if defined?(ChefSpec) + def add_magic_shell_alias(name) + ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_alias, :add, name) + end + + def remove_magic_shell_alias(name) + ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_alias, :remove, name) + end + + def add_magic_shell_environment(name) + ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_environment, :add, name) + end + + def remove_magic_shell_environment(name) + ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_environment, :remove, name) + end +end diff --git a/metadata.rb b/metadata.rb index ab4ceb1..dd9a8aa 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,4 +4,4 @@ license 'Apache 2.0' description 'Installs/Configures command_alias' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.3.2' +version '1.0.0' diff --git a/providers/alias.rb b/providers/alias.rb index 73e3356..d842ca0 100644 --- a/providers/alias.rb +++ b/providers/alias.rb @@ -1,27 +1,68 @@ +# +# Cookbook Name:: magic_shell +# Provider:: alias +# +# Author:: Nathen Harvey +# Author:: Seth Vargo +# +# Copyright 2012-2014, CustomInk, LLC. +# +# 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. +# + +# +# Delegate update actions to inline resources +# +use_inline_resources + +# +# This provider supports why-run mode. +# +def whyrun_supported? + true +end + +# +# Create a file entry for the given alias. +# action :add do - command_name = new_resource.alias_name.gsub(/ /,"_") - if !new_resource.command.nil? - Chef::Log.info("Adding #{command_name}.sh to /etc/profile.d/") - file_contents = "# This alias was generated by Chef for #{node["fqdn"]}\n" - file_contents += "alias #{command_name}='#{new_resource.command}'" - resource = file "/etc/profile.d/#{command_name}.sh" do - owner "root" - group "root" - mode "0755" - content file_contents - action :nothing - end - resource.run_action(:create) - new_resource.updated_by_last_action(true) if resource.updated_by_last_action? - end + file = Chef::Resource::File.new(destination, run_context) + file.owner('root') + file.group('root') + file.mode('0755') + file.content(<<-EOH.gsub(/^ {8}/, '')) + # + # This file was generated by Chef for #{node['fqdn']} + # Do NOT modify this file by hand! + # + + alias #{new_resource.alias_name}="#{new_resource.command}" + EOH + file.run_action(:create) end +# +# Delete the file entry for the given alias. +# action :remove do - command_name = new_resource.alias_name.gsub(/ /,"_") - resource = file "/etc/profile.d/#{command_name}.sh" do - action :nothing - end + file = Chef::Resource::File.new(destination, run_context) + file.run_action(:delete) +end + +def destination + "/etc/profile.d/#{filename}.sh" +end - resource.run_action(:delete) - new_resource.updated_by_last_action(true) if resource.updated_by_last_action? +def filename + new_resource.alias_name.to_s.gsub(/\s/, '_') end diff --git a/providers/environment.rb b/providers/environment.rb index 5df9549..5eea670 100644 --- a/providers/environment.rb +++ b/providers/environment.rb @@ -1,30 +1,68 @@ +# +# Cookbook Name:: magic_shell +# Provider:: environment +# +# Author:: Nathen Harvey +# Author:: Seth Vargo +# +# Copyright 2012-2014, CustomInk, LLC. +# +# 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. +# + +# +# Delegate update actions to inline resources +# +use_inline_resources + +# +# This provider supports why-run mode. +# +def whyrun_supported? + true +end + +# +# Create a file entry for the given environment. +# action :add do - environment_name = new_resource.environment_variable.gsub(/ /,"_") - if !new_resource.value.nil? - Chef::Log.info("Adding #{environment_name}.sh to /etc/profile.d/") - file_contents = "# This file was generated by Chef for #{node["fqdn"]}\n" - file_contents += "export #{environment_name}='#{new_resource.value}'" - resource = file "/etc/profile.d/#{environment_name}.sh" do - owner "root" - group "root" - mode "0755" - content file_contents - action :nothing - end - node.set['magic_shell']['environment'][environment_name] = new_resource.value + file = Chef::Resource::File.new(destination, run_context) + file.owner('root') + file.group('root') + file.mode('0755') + file.content(<<-EOH.gsub(/^ {8}/, '')) + # + # This file was generated by Chef for #{node['fqdn']} + # Do NOT modify this file by hand! + # - resource.run_action(:create) - new_resource.updated_by_last_action(true) if resource.updated_by_last_action? - end + export #{new_resource.environment_variable}="#{new_resource.value}" + EOH + file.run_action(:create) end +# +# +# action :remove do - environment_name = new_resource.environment_variable.gsub(/ /,"_") - Chef::Log.info("Removing #{environment_name}.sh from /etc/profile.d/") - resource = file "/etc/profile.d/#{environment_name}.sh" do - action :nothing - end + file = Chef::Resource::File.new(destination, run_context) + file.run_action(:delete) +end + +def destination + "/etc/profile.d/#{filename}.sh" +end - resource.run_action(:delete) - new_resource.updated_by_last_action(true) if resource.updated_by_last_action? +def filename + new_resource.environment_variable.to_s.gsub(/\s/, '_') end diff --git a/resources/alias.rb b/resources/alias.rb index 6d6c97d..c71c72c 100644 --- a/resources/alias.rb +++ b/resources/alias.rb @@ -1,16 +1,27 @@ -actions :add, :remove - -default_action :add if defined?(:default_action) # Chef > 10.8 +# +# Cookbook Name:: magic_shell +# Resource:: alias +# +# Author:: Nathen Harvey +# Author:: Seth Vargo +# +# Copyright 2012-2014, CustomInk, LLC. +# +# 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. +# -attribute :alias_name, - :kind_of => String, - :name_attribute => true -attribute :command, - :kind_of => String, - :default => :add +actions :add, :remove +default_action :add -# Default action for Chef <= 10.8 -def initialize(*args) - super - @action = :add -end +attribute :alias_name, kind_of: String, name_attribute: true +attribute :command, kind_of: String diff --git a/resources/environment.rb b/resources/environment.rb index 9bb8287..849aebb 100644 --- a/resources/environment.rb +++ b/resources/environment.rb @@ -1,16 +1,27 @@ -actions :add, :remove - -default_action :add if defined?(:default_action) # Chef > 10.8 +# +# Cookbook Name:: magic_shell +# Resource:: environment +# +# Author:: Nathen Harvey +# Author:: Seth Vargo +# +# Copyright 2012-2014, CustomInk, LLC. +# +# 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. +# -attribute :environment_variable, - :kind_of => String, - :name_attribute => true -attribute :value, - :kind_of => String, - :default => :add +actions :add, :remove +default_action :add -# Default action for Chef <= 10.8 -def initialize(*args) - super - @action = :add -end +attribute :environment_variable, kind_of: String, name_attribute: true +attribute :value, kind_of: String diff --git a/spec/alias_spec.rb b/spec/alias_spec.rb deleted file mode 100644 index e2285e9..0000000 --- a/spec/alias_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'spec_helper' - -describe 'magic_shell_alias' do - let(:chef_run) do - ChefSpec::ChefRunner.new( - cookbook_path: $cookbook_paths, - step_into: ['magic_shell_alias'] - ) - end - - context ':add' do - it 'is the default action' do - chef_run.converge('alias::add_implicit') - expect(chef_run).to add_a_magic_shell_alias('h').with_command('cd ~') - end - - it 'creates the alias explicitly' do - chef_run.converge('alias::add_explicit') - expect(chef_run).to add_a_magic_shell_alias('h').with_command('cd ~') - end - end -end diff --git a/spec/cookbooks/alias/metadata.rb b/spec/cookbooks/alias/metadata.rb deleted file mode 100644 index 69099df..0000000 --- a/spec/cookbooks/alias/metadata.rb +++ /dev/null @@ -1,9 +0,0 @@ -name 'alias' -maintainer 'No One' -maintainer_email 'fake@fake.com' -license 'Apache 2.0' -description 'A sample cookbook for testing the magic_shell LWRP' -long_description 'A sample cookbook for testing the magic_shell LWRP' -version '0.1.0' - -depends 'magic_shell' diff --git a/spec/cookbooks/alias/recipes/add_explicit.rb b/spec/cookbooks/alias/recipes/add_explicit.rb deleted file mode 100644 index becfd9f..0000000 --- a/spec/cookbooks/alias/recipes/add_explicit.rb +++ /dev/null @@ -1,4 +0,0 @@ -magic_shell_alias 'h' do - command 'cd ~' - action :add -end diff --git a/spec/cookbooks/environment/metadata.rb b/spec/cookbooks/environment/metadata.rb deleted file mode 100644 index 5f6438e..0000000 --- a/spec/cookbooks/environment/metadata.rb +++ /dev/null @@ -1,9 +0,0 @@ -name 'environment' -maintainer 'No One' -maintainer_email 'fake@fake.com' -license 'Apache 2.0' -description 'A sample cookbook for testing the magic_shell LWRP' -long_description 'A sample cookbook for testing the magic_shell LWRP' -version '0.1.0' - -depends 'magic_shell' diff --git a/spec/cookbooks/environment/recipes/add_explicit.rb b/spec/cookbooks/environment/recipes/add_explicit.rb deleted file mode 100644 index 8164eaa..0000000 --- a/spec/cookbooks/environment/recipes/add_explicit.rb +++ /dev/null @@ -1,4 +0,0 @@ -magic_shell_environment 'RAILS_ENV' do - value 'production' - action :add -end diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb deleted file mode 100644 index fe0f54b..0000000 --- a/spec/environment_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'spec_helper' - -describe 'magic_shell_environment' do - let(:chef_run) do - ChefSpec::ChefRunner.new( - cookbook_path: $cookbook_paths, - step_into: ['magic_shell_environment'] - ) - end - - context ':add' do - it 'is the default action' do - chef_run.converge('environment::add_implicit') - expect(chef_run).to set_a_magic_shell_environment_variable('RAILS_ENV').with_value('production') - end - - it 'creates the alias explicitly' do - chef_run.converge('environment::add_explicit') - expect(chef_run).to set_a_magic_shell_environment_variable('RAILS_ENV').with_value('production') - end - end -end diff --git a/spec/providers/alias_spec.rb b/spec/providers/alias_spec.rb new file mode 100644 index 0000000..c0894cf --- /dev/null +++ b/spec/providers/alias_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'magic_shell_alias' do + let(:chef_run) { ChefSpec::Runner.new } + + it 'adds a new magic_shell_alias' do + chef_run.converge('magic_shell_alias::add') + expect(chef_run).to add_magic_shell_alias('h').with(command: 'cd ~') + end + + it 'removes the magic_shell_alias' do + chef_run.converge('magic_shell_alias::remove') + expect(chef_run).to remove_magic_shell_alias('h') + end +end diff --git a/spec/providers/environment_spec.rb b/spec/providers/environment_spec.rb new file mode 100644 index 0000000..a54b003 --- /dev/null +++ b/spec/providers/environment_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'magic_shell_environment::add' do + let(:chef_run) { ChefSpec::Runner.new } + + it 'adds a new magic_shell_environment' do + chef_run.converge('magic_shell_environment::add') + expect(chef_run).to add_magic_shell_environment('RAILS_ENV') + .with(value: 'production') + end + + it 'removes the magic_shell_environment' do + chef_run.converge('magic_shell_environment::remove') + expect(chef_run).to remove_magic_shell_environment('RAILS_ENV') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b1e8c6f..8fc37fd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,13 +1,6 @@ require 'chefspec' +require 'chefspec/berkshelf' -Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f } - -$cookbook_paths = [ - File.expand_path('../../..', __FILE__), - File.expand_path('../cookbooks', __FILE__) -] - -RSpec.configure do |c| - c.filter_run :focus => true - c.run_all_when_everything_filtered = true +RSpec.configure do |config| + config.log_level = :fatal end diff --git a/spec/support/alias.rb b/spec/support/alias.rb deleted file mode 100644 index 4cb9f75..0000000 --- a/spec/support/alias.rb +++ /dev/null @@ -1,39 +0,0 @@ -# -# Add a magic shell alias -# -RSpec::Matchers.define :add_a_magic_shell_alias do |the_alias| - match do |chef_run| - @the_alias = the_alias - chef_run.file(filepath) && chef_run.file(filepath).content.include?(content) - end - - chain :with_command do |cmd| - @cmd = cmd - end - - failure_message_for_should do |chef_run| - if chef_run.file(filepath) - "expected `#{filepath}` to have content:\n\n #{content}\n\nbut got:\n\n #{chef_run.file(filepath).content.split("\n").join("\n ")}" - else - "expected a file at `#{filepath}` to exist, but one was not" - end - end - - failure_message_for_should_not do |chef_run| - if chef_run.file(filepath) && chef_run.file(filepath).content.include?(content) - "did not expect `#{filepath}` to have content:\n\n #{content}\n\nbut it did:\n\n #{chef_run.file(filepath).content.split("\n").join("\n ")}" - else - "did not expect `#{filepath}` to exist, but it did" - end - end - - # Helper Methods - # ------------------------- - def filepath - "/etc/profile.d/#{@the_alias}.sh" - end - - def content - @cmd ? "alias #{@the_alias}='#{@cmd}'" : "alias #{the_alias}" - end -end diff --git a/spec/support/environment.rb b/spec/support/environment.rb deleted file mode 100644 index 997abb2..0000000 --- a/spec/support/environment.rb +++ /dev/null @@ -1,39 +0,0 @@ -# -# Set a magic shell environment_variable -# -RSpec::Matchers.define :set_a_magic_shell_environment_variable do |variable| - match do |chef_run| - @variable = variable - chef_run.file(filepath) && chef_run.file(filepath).content.include?(content) - end - - chain :with_value do |value| - @value = value - end - - failure_message_for_should do |chef_run| - if chef_run.file(filepath) - "expected `#{filepath}` to have content:\n\n #{content}\n\nbut got:\n\n #{chef_run.file(filepath).content.split("\n").join("\n ")}" - else - "expected a file at `#{filepath}` to exist, but one was not" - end - end - - failure_message_for_should_not do |chef_run| - if chef_run.file(filepath) && chef_run.file(filepath).content.include?(content) - "did not expect `#{filepath}` to have content:\n\n #{content}\n\nbut it did:\n\n #{chef_run.file(filepath).content.split("\n").join("\n ")}" - else - "did not expect `#{filepath}` to exist, but it did" - end - end - - # Helper Methods - # ------------------------- - def filepath - "/etc/profile.d/#{@variable}.sh" - end - - def content - @value ? "export #{@variable}='#{@value}'" : "export #{@variable}" - end -end diff --git a/test/fixtures/cookbooks/magic_shell_alias/metadata.rb b/test/fixtures/cookbooks/magic_shell_alias/metadata.rb new file mode 100644 index 0000000..95e1f22 --- /dev/null +++ b/test/fixtures/cookbooks/magic_shell_alias/metadata.rb @@ -0,0 +1,2 @@ +name 'magic_shell_alias' +depends 'magic_shell' diff --git a/spec/cookbooks/alias/recipes/add_implicit.rb b/test/fixtures/cookbooks/magic_shell_alias/recipes/add.rb similarity index 100% rename from spec/cookbooks/alias/recipes/add_implicit.rb rename to test/fixtures/cookbooks/magic_shell_alias/recipes/add.rb diff --git a/test/fixtures/cookbooks/magic_shell_alias/recipes/remove.rb b/test/fixtures/cookbooks/magic_shell_alias/recipes/remove.rb new file mode 100644 index 0000000..68b128d --- /dev/null +++ b/test/fixtures/cookbooks/magic_shell_alias/recipes/remove.rb @@ -0,0 +1,5 @@ +include_recipe 'magic_shell_alias::add' + +magic_shell_alias 'h' do + action :remove +end diff --git a/test/fixtures/cookbooks/magic_shell_environment/metadata.rb b/test/fixtures/cookbooks/magic_shell_environment/metadata.rb new file mode 100644 index 0000000..64e45c7 --- /dev/null +++ b/test/fixtures/cookbooks/magic_shell_environment/metadata.rb @@ -0,0 +1,2 @@ +name 'magic_shell_environment' +depends 'magic_shell' diff --git a/spec/cookbooks/environment/recipes/add_implicit.rb b/test/fixtures/cookbooks/magic_shell_environment/recipes/add.rb similarity index 100% rename from spec/cookbooks/environment/recipes/add_implicit.rb rename to test/fixtures/cookbooks/magic_shell_environment/recipes/add.rb diff --git a/test/fixtures/cookbooks/magic_shell_environment/recipes/remove.rb b/test/fixtures/cookbooks/magic_shell_environment/recipes/remove.rb new file mode 100644 index 0000000..9c86037 --- /dev/null +++ b/test/fixtures/cookbooks/magic_shell_environment/recipes/remove.rb @@ -0,0 +1,5 @@ +include_recipe 'magic_shell_environment::add' + +magic_shell_environment 'RAILS_ENV' do + action :remove +end diff --git a/test/integration/magic_shell_alias_add/serverspec/assert_added_spec.rb b/test/integration/magic_shell_alias_add/serverspec/assert_added_spec.rb new file mode 100644 index 0000000..2eaaecc --- /dev/null +++ b/test/integration/magic_shell_alias_add/serverspec/assert_added_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../kitchen/data/spec_helper' + +describe file('/etc/profile.d/h.sh') do + it { should be_file } + its(:content) { should include('alias h="cd ~"') } +end diff --git a/test/integration/magic_shell_alias_remove/serverspec/assert_removed_spec.rb b/test/integration/magic_shell_alias_remove/serverspec/assert_removed_spec.rb new file mode 100644 index 0000000..8f35dc5 --- /dev/null +++ b/test/integration/magic_shell_alias_remove/serverspec/assert_removed_spec.rb @@ -0,0 +1,5 @@ +require_relative '../../../kitchen/data/spec_helper' + +describe file('/etc/profile.d/h.sh') do + it { should_not be_file } +end diff --git a/test/integration/magic_shell_environment_add/serverspec/assert_added_spec.rb b/test/integration/magic_shell_environment_add/serverspec/assert_added_spec.rb new file mode 100644 index 0000000..8f420a3 --- /dev/null +++ b/test/integration/magic_shell_environment_add/serverspec/assert_added_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../kitchen/data/spec_helper' + +describe file('/etc/profile.d/RAILS_ENV.sh') do + it { should be_file } + its(:content) { should include('export RAILS_ENV="production"') } +end diff --git a/test/integration/magic_shell_environment_remove/serverspec/assert_removed_spec.rb b/test/integration/magic_shell_environment_remove/serverspec/assert_removed_spec.rb new file mode 100644 index 0000000..5ed9854 --- /dev/null +++ b/test/integration/magic_shell_environment_remove/serverspec/assert_removed_spec.rb @@ -0,0 +1,5 @@ +require_relative '../../../kitchen/data/spec_helper' + +describe file('/etc/profile.d/RAILS_ENV.sh') do + it { should_not be_file } +end diff --git a/test/shared/spec_helper.rb b/test/shared/spec_helper.rb new file mode 100644 index 0000000..d97be63 --- /dev/null +++ b/test/shared/spec_helper.rb @@ -0,0 +1,9 @@ +require 'serverspec' +include Serverspec::Helper::Exec +include Serverspec::Helper::DetectOS + +RSpec.configure do |config| + config.before(:all) do + config.os = backend(Serverspec::Commands::Base).check_os + end +end