Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new assertions for acl / file / directory / symlink / sh / logrotate

  • Loading branch information...
commit 350d308ec095b175fe2f9d46816ebfecc6528867 1 parent d8f6a7b
@grosser grosser authored
View
6 Gemfile
@@ -0,0 +1,6 @@
+source "https://rubygems.org"
+
+gem "minitest"
+gem "mocha"
+gem "chef"
+gem "ci_reporter"
View
5 README.md
@@ -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`.
View
51 lib/minitest-chef-handler/assertions.rb
@@ -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
View
1  lib/minitest-chef-handler/ci_runner.rb
@@ -1,4 +1,5 @@
require 'ci/reporter/minitest'
+
module MiniTest
module Chef
class CIRunner < CI::Reporter::Runner
View
165 spec/minitest-chef-handler/assertions_spec.rb
@@ -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
@@ -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
View
4 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)

1 comment on commit 350d308

@grosser

0.6.10 plz, I want to use it :D

Please sign in to comment.
Something went wrong with that request. Please try again.