Skip to content

Commit

Permalink
Merge pull request #52 from grosser/helpers
Browse files Browse the repository at this point in the history
new test helpers
  • Loading branch information
David Calavera committed Mar 24, 2013
2 parents d8f6a7b + f391fb1 commit 8611d29
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source "https://rubygems.org"
gemspec
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -87,7 +87,12 @@ can also make assertions like these:
file("/etc/fstab").must_have(:mode, "644")
package("less").must_be_installed
service("chef-client").must_be_running
assert_directory "/etc", "root", "root", "755"
assert_file "/etc/fstab", "root", "root", "644"
assert_sh "ls /etc"
assert_logrotate "/etc/logrotate.d/mysql"
```
and [many more](lib/minitest-chef-handler/assertions.rb)

The resources supported are: `cron`, `directory`, `file`, `group`, `ifconfig`,
`link`, `mount`, `package`, `service` and `user`.
Expand Down
2 changes: 1 addition & 1 deletion gemfile_chef_10
@@ -1,3 +1,3 @@
source 'http://rubygems.org'
source 'https://rubygems.org'
gemspec
gem 'chef', '~> 10.8'
4 changes: 0 additions & 4 deletions gemfile_chef_11

This file was deleted.

51 changes: 51 additions & 0 deletions lib/minitest-chef-handler/assertions.rb
Expand Up @@ -124,6 +124,57 @@ def refute_running(service)
service
end

def assert_directory(dir, *args)
assert File.directory?(dir), "Expected #{dir} is be a directory"
assert_acl(dir, *args)
end

def assert_file(file, *args)
assert File.file?(file), "Expected #{file} is be a file"
assert_acl(file, *args)
end

def assert_acl(file, owner, group, mode)
mode = mode.to_s(8) if mode.is_a?(Integer) # better diff since 0755 would be converted to 493
mode = mode.sub(/^0+/, "")

file_resource = file(file)
file_mode = file_resource.mode
file_mode = if file_mode.is_a?(Integer) # chef 10/11
file_mode.to_s(8)
else
file_mode.sub(/^0+/, "")
end

assert_equal owner, file_resource.owner, "Expected #{file} to have owner #{owner}, not #{file_resource.owner}"
assert_equal group, file_resource.group, "Expected #{file} to have group #{group}, not #{file_resource.group}"
assert_equal mode, file_mode, "Expected #{file} to have mode #{mode}, not #{file_mode}"
end

def assert_symlinked_file(file, *args)
assert File.symlink?(file), "Expected #{file} to be a symlink"
assert_acl file, *args
assert File.read(file, 1), "Expected #{file} to be linked to an existing file"
end

def assert_symlinked_directory(directory, *args)
assert File.symlink?(directory), "Expected #{directory} to be a symlink"
assert_acl directory, *args
assert_sh "ls #{directory}/", "Expected #{directory} to link to an existing directory"
end

def assert_logrotate(file)
assert_file file, "root", "root", 0644
assert_sh "logrotate -d #{file}", "Expected #{file} to pass logrotate validation"
end

def assert_sh(command, text=nil)
text ||= "Expected #{command} to succeed"
out = `#{command} 2>&1`
assert $?.success?, "#{text}, but failed with: #{out}"
out
end

end
end
end
1 change: 1 addition & 0 deletions lib/minitest-chef-handler/ci_runner.rb
@@ -1,4 +1,5 @@
require 'ci/reporter/minitest'

module MiniTest
module Chef
class CIRunner < CI::Reporter::Runner
Expand Down
2 changes: 1 addition & 1 deletion script/bootstrap
Expand Up @@ -6,6 +6,6 @@ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../gemfile_chef_10', __FILE__)
system "bundle", "install"

puts "-> Configuring Bundler with Chef 11 dependency."
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../gemfile_chef_11', __FILE__)
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)

system "bundle", "install"
2 changes: 1 addition & 1 deletion script/test
Expand Up @@ -23,6 +23,6 @@ test_all

puts '-> Testing with Chef 11'

ENV['BUNDLE_GEMFILE'] = File.expand_path('gemfile_chef_11', ROOT)
ENV['BUNDLE_GEMFILE'] = File.expand_path('Gemfile', ROOT)

test_all
165 changes: 162 additions & 3 deletions spec/minitest-chef-handler/assertions_spec.rb
Expand Up @@ -14,14 +14,28 @@ def resource_for(hash)

module MiniTest::Chef::Assertions
class File
def self.read(path)
'# Generated by Chef'
def self.read(path, to=-1)
'# Generated by Chef'[0..to]
end

def self.mtime(path)
Time.now.utc
end

def self.exists?(path)
path == '/etc/foo'
["/etc", "/etc/foo", "/etc/baz", "/etc/gar"].include?(path)
end

def self.directory?(path)
['/etc', '/etc/gar'].include?(path)
end

def self.file?(path)
['/etc/foo'].include?(path)
end

def self.symlink?(path)
['/etc/baz', '/etc/gar'].include?(path)
end
end
end
Expand Down Expand Up @@ -290,4 +304,149 @@ def self.exists?(path)
end
end

describe "#assert_acl" do
def file(path)
resource_for(:mode => 0755, :owner => "me", :group => "family", :path => path)
end

it "does not blow up if everything is correct" do
assert_acl("/etc", "me", "family", 0755)
assert_acl("/etc", "me", "family", "755")
assert_acl("/etc", "me", "family", "0755")
end

it "verifies that it has the correct owner" do
assert_triggered "Expected /etc to have owner foo, not me" do
assert_acl("/etc", "foo", "bar", 0755)
end
end

it "verifies that it has the correct group" do
assert_triggered "Expected /etc to have group bar, not family" do
assert_acl("/etc", "me", "bar", 0755)
end
end

it "verifies that it has the correct mode" do
assert_triggered "Expected /etc to have mode 750, not 755" do
assert_acl("/etc", "me", "family", 0750)
end
end
end

describe "#assert_directory" do
def file(path)
resource_for(:mode => 0755, :owner => "me", :group => "family", :path => path)
end

it "does not blow up if everything is correct" do
assert_directory("/etc", "me", "family", 0755)
end

it "verifies that it is a directory" do
assert_triggered "Expected /etc/foo is be a directory" do
assert_directory("/etc/foo", "foo", "bar", 0755)
end
end

it "verifies that it has the correct acl" do
assert_triggered "Expected /etc to have owner foo, not me" do
assert_directory("/etc", "foo", "bar", 0755)
end
end
end

describe "#assert_file" do
def file(path)
resource_for(:mode => 0755, :owner => "me", :group => "family", :path => path)
end

it "does not blow up if everything is correct" do
assert_file("/etc/foo", "me", "family", 0755)
end

it "verifies that it is a file" do
assert_triggered "Expected /etc is be a file" do
assert_file("/etc", "foo", "bar", 0755)
end
end

it "verifies that it has the correct acl" do
assert_triggered "Expected /etc/foo to have owner foo, not me" do
assert_file("/etc/foo", "foo", "bar", 0755)
end
end
end

describe "#assert_symlinked_file" do
def file(path)
resource_for(:mode => 0755, :owner => "me", :group => "family", :path => path)
end

it "does not blow up if everything is correct" do
assert_symlinked_file("/etc/baz", "me", "family", 0755)
end

it "verifies that it is a symlink" do
assert_triggered "Expected /etc/foo to be a symlink" do
assert_symlinked_file("/etc/foo", "foo", "bar", 0755)
end
end

it "verifies that it has the correct acl" do
assert_triggered "Expected /etc/baz to have owner foo, not me" do
assert_symlinked_file("/etc/baz", "foo", "bar", 0755)
end
end
end

describe "#assert_symlinked_directory" do
def file(path)
resource_for(:mode => 0755, :owner => "me", :group => "family", :path => path)
end

def assert_sh(*args)
end

it "does not blow up if everything is correct" do
assert_symlinked_directory("/etc/gar", "me", "family", 0755)
end

it "verifies that it is a symlink" do
assert_triggered "Expected /etc/foo to be a symlink" do
assert_symlinked_directory("/etc/foo", "foo", "bar", 0755)
end
end

it "verifies that it has the correct acl" do
assert_triggered "Expected /etc/gar to have owner foo, not me" do
assert_symlinked_directory("/etc/gar", "foo", "bar", 0755)
end
end
end

describe "#assert_logrotate" do
def file(path)
resource_for(:mode => 0644, :owner => "root", :group => "root", :path => path)
end

def assert_sh(*args)
end

it "does not blow up if everything is correct" do
assert_logrotate("/etc/foo")
end
end

describe "#assert_sh" do
it "does not blow up if everything is correct" do
assert_sh("true")
end

it "fails when command fails" do
assert_triggered "Expected echo ABC && false to succeed, but failed with: ABC" do
assert_sh("echo ABC && false")
end
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
@@ -1,9 +1,9 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'minitest/spec'
require 'mocha/setup'

require File.expand_path('../../lib/minitest-chef-handler', __FILE__)
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require "minitest-chef-handler"

# Borrowed from MiniTest
def assert_triggered(expected)
Expand Down

0 comments on commit 8611d29

Please sign in to comment.