Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit command subject type #111

Merged
merged 2 commits into from May 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Guardfile
@@ -0,0 +1,4 @@
guard :rspec do
watch(%r{^spec/.+/.+_spec\.rb$})
watch('spec/spec_helper.rb') { "spec" }
end
21 changes: 13 additions & 8 deletions lib/serverspec/helper/type.rb
@@ -1,14 +1,19 @@
require 'serverspec/type/base'
require 'serverspec/type/service'
require 'serverspec/type/package'
require 'serverspec/type/port'
require 'serverspec/type/file'
require 'serverspec/type/cron'

module Serverspec
module Helper
module Type
%w( service package port file cron ).each do |type|
types = %w(
base
service
package
port
file
cron
command
)

types.each {|type| require "serverspec/type/#{type}" }

types.each do |type|
define_method type do |*args|
name = args.first
self.class.const_get('Serverspec').const_get('Type').const_get(type.capitalize).new(name)
Expand Down
8 changes: 6 additions & 2 deletions lib/serverspec/matchers/return_exit_status.rb
@@ -1,6 +1,10 @@
RSpec::Matchers.define :return_exit_status do |status|
match do |command|
ret = backend.run_command(command)
ret[:exit_status].to_i == status
if command.respond_to?(:return_exit_status?)
command.return_exit_status?(status)
else
ret = backend.run_command(command)
ret[:exit_status].to_i == status
end
end
end
12 changes: 8 additions & 4 deletions lib/serverspec/matchers/return_stderr.rb
@@ -1,10 +1,14 @@
RSpec::Matchers.define :return_stderr do |content|
match do |command|
ret = backend.run_command(command)
if content.instance_of?(Regexp)
ret[:stderr] =~ content
if command.respond_to?(:return_stderr?)
command.return_stderr?(content)
else
ret[:stderr].strip == content
ret = backend.run_command(command)
if content.instance_of?(Regexp)
ret[:stderr] =~ content
else
ret[:stderr].strip == content
end
end
end
end
12 changes: 8 additions & 4 deletions lib/serverspec/matchers/return_stdout.rb
@@ -1,10 +1,14 @@
RSpec::Matchers.define :return_stdout do |content|
match do |command|
ret = backend.run_command(command)
if content.instance_of?(Regexp)
ret[:stdout] =~ content
if command.respond_to?(:return_stdout?)
command.return_stdout?(content)
else
ret[:stdout].strip == content
ret = backend.run_command(command)
if content.instance_of?(Regexp)
ret[:stdout] =~ content
else
ret[:stdout].strip == content
end
end
end
end
31 changes: 31 additions & 0 deletions lib/serverspec/type/command.rb
@@ -0,0 +1,31 @@
module Serverspec
module Type
class Command < Base
def return_stdout?(content)
ret = backend.run_command(@name)
if content.instance_of?(Regexp)
ret[:stdout] =~ content
else
ret[:stdout].strip == content
end
end

def return_stderr?(content)
ret = backend.run_command(@name)
# In ssh access with pty, stderr is merged to stdout
# See http://stackoverflow.com/questions/7937651/receiving-extended-data-with-ssh-using-twisted-conch-as-client
# So I use stdout instead of stderr
if content.instance_of?(Regexp)
ret[:stdout] =~ content
else
ret[:stdout].strip == content
end
end

def return_exit_status?(status)
ret = backend.run_command(@name)
ret[:exit_status].to_i == status
end
end
end
end
2 changes: 2 additions & 0 deletions serverspec.gemspec
Expand Up @@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "highline"
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "guard-rspec"
spec.add_development_dependency "growl"
end
13 changes: 13 additions & 0 deletions spec/darwin/command_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

include Serverspec::Helper::Darwin

describe 'Serverspec command matchers of Darwin family' do
it_behaves_like 'support command return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
it_behaves_like 'support command return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/

it_behaves_like 'support command return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
it_behaves_like 'support command return_stderr matcher with regexp', 'cat /foo', /No such file or directory/

it_behaves_like 'support command return_exit_status matcher', 'ls /tmp', 0
end
13 changes: 13 additions & 0 deletions spec/debian/command_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

include Serverspec::Helper::Debian

describe 'Serverspec command matchers of Debian family' do
it_behaves_like 'support command return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
it_behaves_like 'support command return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/

it_behaves_like 'support command return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
it_behaves_like 'support command return_stderr matcher with regexp', 'cat /foo', /No such file or directory/

it_behaves_like 'support command return_exit_status matcher', 'ls /tmp', 0
end
13 changes: 13 additions & 0 deletions spec/gentoo/command_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

include Serverspec::Helper::Gentoo

describe 'Serverspec command matchers of Gentoo family' do
it_behaves_like 'support command return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
it_behaves_like 'support command return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/

it_behaves_like 'support command return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
it_behaves_like 'support command return_stderr matcher with regexp', 'cat /foo', /No such file or directory/

it_behaves_like 'support command return_exit_status matcher', 'ls /tmp', 0
end
13 changes: 13 additions & 0 deletions spec/redhat/command_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

include Serverspec::Helper::RedHat

describe 'Serverspec command matchers of Red Hat family' do
it_behaves_like 'support command return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
it_behaves_like 'support command return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/

it_behaves_like 'support command return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
it_behaves_like 'support command return_stderr matcher with regexp', 'cat /foo', /No such file or directory/

it_behaves_like 'support command return_exit_status matcher', 'ls /tmp', 0
end
13 changes: 13 additions & 0 deletions spec/solaris/command_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

include Serverspec::Helper::Solaris

describe 'Serverspec command matchers of Solaris family' do
it_behaves_like 'support command return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
it_behaves_like 'support command return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/

it_behaves_like 'support command return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
it_behaves_like 'support command return_stderr matcher with regexp', 'cat /foo', /No such file or directory/

it_behaves_like 'support command return_exit_status matcher', 'ls /tmp', 0
end
108 changes: 108 additions & 0 deletions spec/support/shared_command_examples.rb
@@ -0,0 +1,108 @@
shared_examples_for 'support command return_stdout matcher' do |name, content|
describe 'return_stdout' do
describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "#{content}\r\n"
end
end
it { should return_stdout(content) }
end

describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "foo#{content}bar\r\n"
end
end
it { should_not return_stdout(content) }
end


describe command('invalid-command') do
it { should_not return_stdout(content) }
end
end
end

shared_examples_for 'support command return_stdout matcher with regexp' do |name, content|
describe 'return_stdout' do
describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "foo#{content}bar\r\n"
end
end
it { should return_stdout(content) }
end

describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "foobar\r\n"
end
end
it { should_not return_stdout(content) }
end

describe command('invalid-command') do
it { should_not return_stdout(content) }
end
end
end

shared_examples_for 'support command return_stderr matcher' do |name, content|
describe 'return_stderr' do
describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "#{content}\r\n"
end
end
it { should return_stderr(content) }
end

describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "No such file or directory\r\n"
end
end
it { should_not return_stderr(content) }
end
end
end

shared_examples_for 'support command return_stderr matcher with regexp' do |name, content|
describe 'return_stderr' do
describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "cat: /foo: No such file or directory\r\n"
end
end
it { should return_stdout(content) }
end

describe command(name) do
before :all do
RSpec.configure do |c|
c.stdout = "foobar\r\n"
end
end
it { should_not return_stdout(content) }
end
end
end

shared_examples_for 'support command return_exit_status matcher' do |name, status|
describe 'return_exit_status' do
describe command(name) do
it { should return_exit_status(status) }
end

describe command('invalid-command') do
it { should_not return_exit_status(status) }
end
end
end