Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add chefspec tests.

  • Loading branch information...
commit 4a1e7fc5127a525b13f196ee62390e8d2dae9093 1 parent dc2fe99
Yuji Yamano yyamano authored
3  Gemfile
View
@@ -1,3 +1,6 @@
source "http://rubygems.org"
gem "chef", '>0.10'
+
+gem "chefspec"
+gem "knife-spec"
75 Gemfile.lock
View
@@ -1,47 +1,88 @@
GEM
remote: http://rubygems.org/
specs:
- chef (11.4.4)
- erubis
- highline (>= 1.6.9)
+ chef (11.8.2)
+ chef-zero (~> 1.6, >= 1.6.2)
+ diff-lcs (~> 1.2, >= 1.2.4)
+ erubis (~> 2.7)
+ highline (~> 1.6, >= 1.6.9)
json (>= 1.4.4, <= 1.7.7)
- mixlib-authentication (>= 1.3.0)
- mixlib-cli (~> 1.3.0)
- mixlib-config (>= 1.1.2)
- mixlib-log (>= 1.3.0)
- mixlib-shellout
+ mime-types (~> 1.16)
+ mixlib-authentication (~> 1.3)
+ mixlib-cli (~> 1.3)
+ mixlib-config (~> 2.0)
+ mixlib-log (~> 1.3)
+ mixlib-shellout (~> 1.2)
net-ssh (~> 2.6)
net-ssh-multi (~> 1.1.0)
- ohai (>= 0.6.0)
+ ohai (~> 6.0)
+ pry (~> 0.9)
+ puma (~> 1.6)
rest-client (>= 1.0.4, < 1.7.0)
yajl-ruby (~> 1.1)
+ chef-zero (1.7.2)
+ hashie (~> 2.0)
+ json
+ mixlib-log (~> 1.3)
+ moneta (< 0.7.0)
+ rack
+ chefspec (3.0.2)
+ chef (~> 11.0)
+ fauxhai (~> 2.0)
+ rspec (~> 2.14)
+ coderay (1.1.0)
+ diff-lcs (1.2.5)
erubis (2.7.0)
- highline (1.6.18)
+ fauxhai (2.0.0)
+ net-ssh
+ ohai
+ hashie (2.0.5)
+ highline (1.6.20)
ipaddress (0.8.0)
json (1.7.7)
- mime-types (1.23)
+ knife-spec (1.0.0)
+ chef
+ method_source (0.8.2)
+ mime-types (1.25.1)
mixlib-authentication (1.3.0)
mixlib-log
mixlib-cli (1.3.0)
- mixlib-config (1.1.2)
+ mixlib-config (2.0.0)
mixlib-log (1.6.0)
- mixlib-shellout (1.1.0)
- net-ssh (2.6.7)
+ mixlib-shellout (1.3.0)
+ moneta (0.6.0)
+ net-ssh (2.7.0)
net-ssh-gateway (1.2.0)
net-ssh (>= 2.6.5)
net-ssh-multi (1.1)
net-ssh (>= 2.1.4)
net-ssh-gateway (>= 0.99.0)
- ohai (6.16.0)
+ ohai (6.20.0)
ipaddress
mixlib-cli
mixlib-config
mixlib-log
mixlib-shellout
- systemu
+ systemu (~> 2.5.2)
yajl-ruby
+ pry (0.9.12.4)
+ coderay (~> 1.0)
+ method_source (~> 0.8)
+ slop (~> 3.4)
+ puma (1.6.3)
+ rack (~> 1.2)
+ rack (1.5.2)
rest-client (1.6.7)
mime-types (>= 1.16)
+ rspec (2.14.1)
+ rspec-core (~> 2.14.0)
+ rspec-expectations (~> 2.14.0)
+ rspec-mocks (~> 2.14.0)
+ rspec-core (2.14.7)
+ rspec-expectations (2.14.4)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.14.4)
+ slop (3.4.7)
systemu (2.5.2)
yajl-ruby (1.1.0)
@@ -50,3 +91,5 @@ PLATFORMS
DEPENDENCIES
chef (> 0.10)
+ chefspec
+ knife-spec
1  spec/spec_helper.rb
View
@@ -0,0 +1 @@
+require 'chefspec'
80 spec/unit/recipes/default_spec.rb
View
@@ -0,0 +1,80 @@
+require 'spec_helper'
+
+describe 'monit::default' do
+ context "with default attributes" do
+ let(:chef_run) do
+ runner = ChefSpec::Runner.new.converge(described_recipe)
+ end
+
+ it 'creates /etc/monit/conf.d/' do
+ expect(chef_run).to create_directory('/etc/monit/conf.d/').with(user: 'root', group: 'root', mode: 0755)
+ end
+
+ it 'creates /etc/monit/monitrc' do
+ expect(chef_run).to create_template('/etc/monit/monitrc').with(user: 'root', group: 'root', mode: 0700)
+ file = chef_run.template('/etc/monit/monitrc')
+ expect(file).to notify('service[monit]').to(:restart)
+
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set daemon 60\n\s*with start delay 120$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set mailserver localhost\n\s*with timeout 60 seconds$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set httpd port 3737\n\s*use address localhost\n\s*allow localhost$/)
+ end
+
+ it 'enables the service' do
+ expect(chef_run).to enable_service('monit')
+ end
+
+ it 'start the service' do
+ expect(chef_run).to start_service('monit')
+ end
+ end
+
+ context "with configuration" do
+ let(:chef_run) do
+ runner = ChefSpec::Runner.new do |node|
+ node.set[:monit][:notify_email] = 'johndoe@example.com'
+ node.set[:monit][:logfile] = '/var/log/monit.log'
+ node.set[:monit][:poll_period] = 30
+ node.set[:monit][:poll_start_delay] = 90
+ node.set[:monit][:mail_format][:subject] = 'Hello from monit'
+ node.set[:monit][:mail_format][:from] = 'monit@example.com'
+ node.set[:monit][:mail_format][:message] = 'Hello!'
+ node.set[:monit][:mailserver][:host] = 'smtp.example.com'
+ node.set[:monit][:mailserver][:port] = 587
+ node.set[:monit][:mailserver][:username] = 'johndoe'
+ node.set[:monit][:mailserver][:password] = 'secret'
+ node.set[:monit][:mailserver][:password_suffix] = 'smtp'
+ node.set[:monit][:mailserver][:encryption] = 'SSLV2'
+ node.set[:monit][:mailserver][:timeout] = 10
+ node.set[:monit][:port] = 8000
+ node.set[:monit][:address] = '192.168.0.1'
+ node.set[:monit][:ssl] = true
+ node.set[:monit][:allow] = ["localhost", '192.168.0.2']
+ node.set[:monit][:username] = 'johndoe'
+ node.set[:monit][:password] = 'secret'
+ end.converge(described_recipe)
+ end
+
+ it 'creates /etc/monit/monitrc' do
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set daemon 30\n\s*with start delay 90$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(%r|set logfile /var/log/monit.log$|)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set mailserver smtp.example.com port 587\n\s*username "johndoe"\n\s*password "secret" smtp\n\s*using SSLV2\n\s*with timeout 10 seconds$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set mail-format {\n\s*from: monit@example.com\n\s*subject: Hello from monit\n\s*message: Hello!$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set alert johndoe@example.com NOT ON { action, instance, pid, ppid }$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/set httpd port 8000\n\s*use address 192.168.0.1\n\s*allow localhost\n\s*allow 192.168.0.2$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(/allow johndoe:secret$/)
+ expect(chef_run).to render_file('/etc/monit/monitrc').with_content(%r|ssl enable\n\s*pemfile /etc/monit/monit.pem$|)
+ end
+ end
+
+ context "on ubuntu" do
+ let(:chef_run) do
+ runner = ChefSpec::Runner.new(:platform => 'ubuntu', :version => '12.04').converge(described_recipe)
+ end
+
+ it 'creates /etc/default/monit' do
+ expect(chef_run).to create_cookbook_file('/etc/default/monit').with(user: 'root', group: 'root', mode: 0644)
+ end
+ end
+
+end
16 spec/unit/recipes/postfix_spec.rb
View
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe 'monit::postfix' do
+ let(:chef_run) do
+ runner = ChefSpec::Runner.new.converge(described_recipe)
+ end
+
+ # Temporary soultion for testing definitions/monitrc.erb without monky patch.
+ # See http://amespinosa.wordpress.com/2013/05/06/testing-recipe-definitions-with-chefspec/
+ it 'creates /etc/monit/conf.d/postfix.conf' do
+ expect(chef_run).to create_template('/etc/monit/conf.d/postfix.conf').with(user: 'root', group: 'root', mode: 0644)
+ file = chef_run.template('/etc/monit/conf.d/postfix.conf')
+ expect(file).to notify('service[monit]').to(:restart)
+ end
+
+end
12 spec/unit/recipes/ubuntu12fix_spec.rb
View
@@ -0,0 +1,12 @@
+require 'spec_helper'
+
+describe 'monit::ubuntu12fix' do
+ let(:chef_run) do
+ runner = ChefSpec::Runner.new.converge(described_recipe)
+ end
+
+ it 'overrides /etc/init.d/monit' do
+ # I have no idea why mode doesn't work without single quotes.
+ expect(chef_run).to create_cookbook_file('/etc/init.d/monit').with(user: 'root', group: 'root', mode: '0755')
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.