serverspec 0.4.14 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ task :spec => 'spec:all'
6
6
  namespace :spec do
7
7
  oses = %w( darwin debian gentoo redhat solaris )
8
8
 
9
- task :all => [ oses.map {|os| "spec:#{os}" }, :others].flatten
9
+ task :all => [ oses.map {|os| "spec:#{os}" }, :helpers, :exec, :ssh ].flatten
10
10
 
11
11
  oses.each do |os|
12
12
  RSpec::Core::RakeTask.new(os.to_sym) do |t|
@@ -14,7 +14,15 @@ namespace :spec do
14
14
  end
15
15
  end
16
16
 
17
- RSpec::Core::RakeTask.new(:others) do |t|
18
- t.pattern = "spec/{helpers,backend}/*_spec.rb"
17
+ RSpec::Core::RakeTask.new(:helpers) do |t|
18
+ t.pattern = "spec/helpers/*_spec.rb"
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new(:exec) do |t|
22
+ t.pattern = "spec/backend/exec/*_spec.rb"
23
+ end
24
+
25
+ RSpec::Core::RakeTask.new(:ssh) do |t|
26
+ t.pattern = "spec/backend/ssh/*_spec.rb"
19
27
  end
20
28
  end
data/lib/serverspec.rb CHANGED
@@ -14,8 +14,20 @@ require 'serverspec/commands/debian'
14
14
  require 'serverspec/commands/gentoo'
15
15
  require 'serverspec/commands/solaris'
16
16
  require 'serverspec/commands/darwin'
17
+ require 'serverspec/configuration'
18
+
19
+ include Serverspec
20
+
21
+ module Serverspec
22
+ class << self
23
+ def configuration
24
+ Serverspec::Configuration
25
+ end
26
+ end
27
+ end
17
28
 
18
29
  RSpec.configure do |c|
30
+ c.include(Serverspec::Helper::Configuration)
19
31
  c.include(Serverspec::Helper::RedHat, :os => :redhat)
20
32
  c.include(Serverspec::Helper::Debian, :os => :debian)
21
33
  c.include(Serverspec::Helper::Gentoo, :os => :gentoo)
@@ -25,7 +37,7 @@ RSpec.configure do |c|
25
37
  c.add_setting :host, :default => nil
26
38
  c.add_setting :ssh, :default => nil
27
39
  c.add_setting :sudo_password, :default => nil
28
- c.add_setting :path, :default => nil
40
+ Serverspec.configuration.defaults.each { |k, v| c.add_setting k, :default => v }
29
41
  c.before :each do
30
42
  if described_class.nil?
31
43
  puts
@@ -10,6 +10,8 @@ module Serverspec
10
10
  end
11
11
 
12
12
  def run_command(cmd, opts={})
13
+ cmd = build_command(cmd)
14
+ cmd = add_pre_command(cmd)
13
15
  stdout = `#{build_command(cmd)} 2>&1`
14
16
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
15
17
  #stdout, stderr, status = Open3.capture3(cmd)
@@ -24,8 +26,18 @@ module Serverspec
24
26
  end
25
27
 
26
28
  def build_command(cmd)
27
- if ! RSpec.configuration.path.nil?
28
- cmd = "PATH=#{RSpec.configuration.path}:$PATH #{cmd}"
29
+ path = Serverspec.configuration.path || RSpec.configuration.path
30
+ if path
31
+ cmd = "PATH=#{path}:$PATH #{cmd}"
32
+ end
33
+ cmd
34
+ end
35
+
36
+ def add_pre_command(cmd)
37
+ path = Serverspec.configuration.path || RSpec.configuration.path
38
+ if Serverspec.configuration.pre_command
39
+ cmd = "#{Serverspec.configuration.pre_command} && #{cmd}"
40
+ cmd = "PATH=#{path}:$PATH #{cmd}" if path
29
41
  end
30
42
  cmd
31
43
  end
@@ -5,7 +5,7 @@ module Serverspec
5
5
  class Ssh < Exec
6
6
  def run_command(cmd, opt={})
7
7
  cmd = build_command(cmd)
8
- cmd = "sudo #{cmd}" if not RSpec.configuration.ssh.options[:user] == 'root'
8
+ cmd = add_pre_command(cmd)
9
9
  ret = ssh_exec!(cmd)
10
10
  if ! @example.nil?
11
11
  @example.metadata[:command] = cmd
@@ -14,6 +14,18 @@ module Serverspec
14
14
  ret
15
15
  end
16
16
 
17
+ def build_command(cmd)
18
+ cmd = super(cmd)
19
+ cmd = "sudo #{cmd}" if RSpec.configuration.ssh.options[:user] != 'root'
20
+ cmd
21
+ end
22
+
23
+ def add_pre_command(cmd)
24
+ cmd = super(cmd)
25
+ cmd = "sudo #{cmd}" if RSpec.configuration.ssh.options[:user] != 'root'
26
+ cmd
27
+ end
28
+
17
29
  private
18
30
  def ssh_exec!(command)
19
31
  stdout_data = ''
@@ -0,0 +1,12 @@
1
+ module Serverspec
2
+ module Configuration
3
+ class << self
4
+ VALID_OPTIONS_KEYS = [:path, :pre_command].freeze
5
+ attr_accessor(*VALID_OPTIONS_KEYS)
6
+
7
+ def defaults
8
+ VALID_OPTIONS_KEYS.inject({}) { |o, k| o.merge!(k => send(k)) }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -22,3 +22,5 @@ require 'serverspec/helper/attributes'
22
22
  # Subject type helper
23
23
  require 'serverspec/helper/type'
24
24
  include Serverspec::Helper::Type
25
+
26
+ require 'serverspec/helper/configuration'
@@ -0,0 +1,36 @@
1
+ module Serverspec
2
+ module Helper
3
+ module Configuration
4
+ def subject
5
+ build_configurations
6
+ super
7
+ end
8
+
9
+ # You can create a set of configurations provided to all specs in your spec_helper:
10
+ #
11
+ # RSpec.configure { |c| c.pre_command = "source ~/.zshrc" }
12
+ #
13
+ # Any configurations you provide with `let(:option_name)` in a spec will
14
+ # automatically be merged on top of the configurations.
15
+ #
16
+ # @example
17
+ #
18
+ # describe 'Gem' do
19
+ # let(:pre_command) { "source ~/.zshrc" }
20
+ #
21
+ # %w(pry awesome_print bundler).each do |p|
22
+ # describe package(p) do
23
+ # it { should be_installed.by('gem') }
24
+ # end
25
+ # end
26
+ # end
27
+ def build_configurations
28
+ Serverspec::Configuration.defaults.keys.each do |c|
29
+ value = self.respond_to?(c.to_sym) ?
30
+ self.send(c) : RSpec.configuration.send(c)
31
+ Serverspec::Configuration.send(:"#{c}=", value)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.4.14"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ require 'serverspec/helper/base'
4
+ include Serverspec::Helper::Base
5
+ include Serverspec::Helper::Exec
6
+
7
+ describe 'configurations are not set' do
8
+ context package('httpd') do
9
+ its(:command) { should eq 'command' }
10
+ end
11
+ end
12
+
13
+ describe 'path is set' do
14
+ let(:path) { '/sbin:/usr/sbin' }
15
+ context package('httpd') do
16
+ its(:command) { should eq 'PATH=/sbin:/usr/sbin:$PATH command' }
17
+ end
18
+ end
19
+
20
+ describe 'pre_command is set' do
21
+ let(:pre_command) { 'source ~/.zshrc' }
22
+ context package('httpd') do
23
+ its(:command) { should eq 'source ~/.zshrc && command' }
24
+ end
25
+ end
26
+
27
+ describe 'path and pre_command are set' do
28
+ let(:path) { '/sbin:/usr/sbin' }
29
+ let(:pre_command) { 'source ~/.zshrc' }
30
+ context package('httpd') do
31
+ its(:command) { should eq 'PATH=/sbin:/usr/sbin:$PATH source ~/.zshrc && PATH=/sbin:/usr/sbin:$PATH command' }
32
+ end
33
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ require 'serverspec/helper/base'
4
+ include Serverspec::Helper::Base
5
+ include Serverspec::Helper::Ssh
6
+
7
+ ssh = double
8
+
9
+ describe 'configurations are not set' do
10
+ before :all do
11
+ RSpec.configure do |c|
12
+ ssh.stub(:options) { { :user => 'root' } }
13
+ c.ssh = ssh
14
+ end
15
+ end
16
+
17
+ context package('httpd') do
18
+ its(:command) { should eq 'command' }
19
+ end
20
+ end
21
+
22
+ describe 'path is set' do
23
+ before :all do
24
+ RSpec.configure do |c|
25
+ ssh.stub(:options) { { :user => 'root' } }
26
+ c.ssh = ssh
27
+ end
28
+ end
29
+
30
+ let(:path) { '/sbin:/usr/sbin' }
31
+ context package('httpd') do
32
+ its(:command) { should eq 'PATH=/sbin:/usr/sbin:$PATH command' }
33
+ end
34
+ end
35
+
36
+ describe 'pre_command is set and user is root' do
37
+ before :all do
38
+ RSpec.configure do |c|
39
+ ssh.stub(:options) { { :user => 'root' } }
40
+ c.ssh = ssh
41
+ end
42
+ end
43
+
44
+ let(:pre_command) { 'source ~/.zshrc' }
45
+ context package('httpd') do
46
+ its(:command) { should eq 'source ~/.zshrc && command' }
47
+ end
48
+ end
49
+
50
+ describe 'pre_command is set and user is non-root' do
51
+ before :all do
52
+ RSpec.configure do |c|
53
+ ssh.stub(:options) { { :user => 'foo' } }
54
+ c.ssh = ssh
55
+ end
56
+ end
57
+
58
+ let(:pre_command) { 'source ~/.zshrc' }
59
+ context package('httpd') do
60
+ its(:command) { should eq 'sudo source ~/.zshrc && sudo command' }
61
+ end
62
+ end
63
+
64
+ describe 'path pre_command and set and user is non-root' do
65
+ before :all do
66
+ RSpec.configure do |c|
67
+ ssh.stub(:options) { { :user => 'foo' } }
68
+ c.ssh = ssh
69
+ end
70
+ end
71
+
72
+ let(:path) { '/sbin:/usr/sbin' }
73
+ let(:pre_command) { 'source ~/.zshrc' }
74
+ context package('httpd') do
75
+ its(:command) { should eq 'sudo PATH=/sbin:/usr/sbin:$PATH source ~/.zshrc && sudo PATH=/sbin:/usr/sbin:$PATH command' }
76
+ end
77
+ end
78
+
79
+ describe 'path pre_command and set and user is non-root' do
80
+ before :all do
81
+ RSpec.configure do |c|
82
+ ssh.stub(:options) { { :user => 'root' } }
83
+ c.ssh = ssh
84
+ end
85
+ end
86
+
87
+ let(:path) { '/sbin:/usr/sbin' }
88
+ let(:pre_command) { 'source ~/.zshrc' }
89
+ context package('httpd') do
90
+ its(:command) { should eq 'PATH=/sbin:/usr/sbin:$PATH source ~/.zshrc && PATH=/sbin:/usr/sbin:$PATH command' }
91
+ end
92
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'serverspec'
2
2
  require 'pathname'
3
-
4
- include Serverspec::Helper::Exec
3
+ require 'rspec/mocks/standalone'
5
4
 
6
5
  PROJECT_ROOT = (Pathname.new(File.dirname(__FILE__)) + '..').expand_path
7
6
 
@@ -29,6 +28,15 @@ module Serverspec
29
28
  end
30
29
  end
31
30
  end
31
+
32
+ module Type
33
+ class Base
34
+ def command
35
+ cmd = backend.build_command('command')
36
+ backend.add_pre_command(cmd)
37
+ end
38
+ end
39
+ end
32
40
  end
33
41
 
34
42
  RSpec.configure do |c|
@@ -1,3 +1,5 @@
1
+ include Serverspec::Helper::Exec
2
+
1
3
  shared_examples_for 'support user exist matcher' do |name|
2
4
  describe 'user exist' do
3
5
  describe user(name) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.14
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-29 00:00:00.000000000 Z
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
@@ -153,10 +153,12 @@ files:
153
153
  - lib/serverspec/commands/linux.rb
154
154
  - lib/serverspec/commands/redhat.rb
155
155
  - lib/serverspec/commands/solaris.rb
156
+ - lib/serverspec/configuration.rb
156
157
  - lib/serverspec/filter.rb
157
158
  - lib/serverspec/helper.rb
158
159
  - lib/serverspec/helper/attributes.rb
159
160
  - lib/serverspec/helper/base.rb
161
+ - lib/serverspec/helper/configuration.rb
160
162
  - lib/serverspec/helper/darwin.rb
161
163
  - lib/serverspec/helper/debian.rb
162
164
  - lib/serverspec/helper/detect_os.rb
@@ -234,7 +236,8 @@ files:
234
236
  - lib/serverspec/type/zfs.rb
235
237
  - lib/serverspec/version.rb
236
238
  - serverspec.gemspec
237
- - spec/backend/exec_spec.rb
239
+ - spec/backend/exec/configuration_spec.rb
240
+ - spec/backend/ssh/configuration_spec.rb
238
241
  - spec/darwin/command_spec.rb
239
242
  - spec/darwin/commands_spec.rb
240
243
  - spec/darwin/cron_spec.rb
@@ -351,12 +354,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
351
354
  version: '0'
352
355
  requirements: []
353
356
  rubyforge_project:
354
- rubygems_version: 1.8.23
357
+ rubygems_version: 1.8.25
355
358
  signing_key:
356
359
  specification_version: 3
357
360
  summary: RSpec tests for your servers provisioned by Puppet, Chef or anything else
358
361
  test_files:
359
- - spec/backend/exec_spec.rb
362
+ - spec/backend/exec/configuration_spec.rb
363
+ - spec/backend/ssh/configuration_spec.rb
360
364
  - spec/darwin/command_spec.rb
361
365
  - spec/darwin/commands_spec.rb
362
366
  - spec/darwin/cron_spec.rb
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'serverspec/helper/exec'
4
- require 'serverspec/helper/base'
5
- include Serverspec::Helper::Exec
6
- include Serverspec::Helper::Base
7
-
8
- describe 'Default path setting' do
9
- subject { backend.build_command('service httpd status') }
10
- it { should eq 'service httpd status' }
11
- end
12
-
13
- describe 'Custom path setting' do
14
- before :all do
15
- RSpec.configure do |c|
16
- c.path = '/usr/local/rbenv/shims:/sbin:/usr/sbin'
17
- end
18
- end
19
- subject { backend.build_command('service httpd status') }
20
- it { should eq 'PATH=/usr/local/rbenv/shims:/sbin:/usr/sbin:$PATH service httpd status' }
21
- end