specinfra 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1723c36eee1bc0172723b6f7b25e523dea4ea64e
4
- data.tar.gz: 038004ce1ec9c778c815c2432103943688c93e06
3
+ metadata.gz: c43b0fc54df451fdfd3f4d2277ccd43b086903b4
4
+ data.tar.gz: 0ea1cdca32da29426db6c404424fe88eb0a5ce04
5
5
  SHA512:
6
- metadata.gz: 8f1bc4cce205831e97c2ea9c2c775def26160abbb9e0de7bcb4b3d2af80a02bdf469fd7496a8c20138328ae1b6df2da210775d645c3b548c6ef27155c8e96486
7
- data.tar.gz: 2cf652b80148120f9603ad1757677e4ea3b80b2c648cc6cac066b97046e2463bef8ebe7ff2cc9c10ac750958e58d3876354bfb58bb2b787b5dbb5b49d69173dd
6
+ metadata.gz: fe76cc14f1638e62d32e5e617d7efd61d333245228c5d4d4a6c3cb9ee52f39eb662b7bea29993e0bbf116afe34754f85a3080a1ef3d779817686d6d878a3adbf
7
+ data.tar.gz: 31590df84b9539f7a2f1a3d2e4f8d34021a182a3f87d0f38ebd86db16de9c85a44fc9b505de1aac2464192e71ed95ecd66766822b657d3fc9cc07cc07be2f13a
data/Rakefile CHANGED
@@ -10,8 +10,17 @@ namespace :spec do
10
10
  t.pattern = "spec/helper/*_spec.rb"
11
11
  end
12
12
 
13
- RSpec::Core::RakeTask.new(:backend) do |t|
14
- t.pattern = "spec/backend/*/*_spec.rb"
13
+ task :backend => 'backend:all'
14
+ namespace :backend do
15
+ backends = %w[exec ssh]
16
+
17
+ task :all => backends
18
+
19
+ backends.each do |backend|
20
+ RSpec::Core::RakeTask.new(backend) do |t|
21
+ t.pattern = "spec/backend/#{backend}/*_spec.rb"
22
+ end
23
+ end
15
24
  end
16
25
 
17
26
  RSpec::Core::RakeTask.new(:configuration) do |t|
@@ -1,4 +1,5 @@
1
1
  require 'singleton'
2
+ require 'specinfra/command_result'
2
3
 
3
4
  module SpecInfra
4
5
  module Backend
@@ -18,8 +19,7 @@ module SpecInfra
18
19
  end
19
20
 
20
21
  def check_zero(cmd, *args)
21
- ret = run_command(commands.send(cmd, *args))
22
- ret[:exit_status] == 0
22
+ run_command(commands.send(cmd, *args)).success?
23
23
  end
24
24
 
25
25
  # Default action is to call check_zero with args
@@ -13,8 +13,8 @@ module SpecInfra
13
13
  @example.metadata[:command] = script
14
14
  @example.metadata[:stdout] = result[:stdout] + result[:stderr]
15
15
  end
16
- { :stdout => result[:stdout], :stderr => result[:stderr],
17
- :exit_status => result[:status], :exit_signal => nil }
16
+ CommandResult.new :stdout => result[:stdout], :stderr => result[:stderr],
17
+ :exit_status => result[:status]
18
18
  end
19
19
 
20
20
  def execute_script script
@@ -54,8 +54,8 @@ module SpecInfra
54
54
  begin
55
55
  stdout, stderr = container.attach
56
56
  result = container.wait
57
- return {:stdout => stdout.join, :stderr => stderr.join,
58
- :exit_status => result['StatusCode'], :exit_signal => nil}
57
+ return CommandResult.new :stdout => stdout.join, :stderr => stderr.join,
58
+ :exit_status => result['StatusCode']
59
59
  rescue
60
60
  container.kill
61
61
  end
@@ -10,8 +10,7 @@ module SpecInfra
10
10
 
11
11
  def run_command(cmd, opts={})
12
12
  @lines << "RUN #{cmd}"
13
- { :stdout => nil, :stderr => nil,
14
- :exit_status => 0, :exit_signal => nil }
13
+ CommandResult.new
15
14
  end
16
15
 
17
16
  def from(base)
@@ -19,8 +19,7 @@ module SpecInfra
19
19
  @example.metadata[:stdout] = stdout
20
20
  end
21
21
 
22
- { :stdout => stdout, :stderr => nil,
23
- :exit_status => $?.exitstatus, :exit_signal => nil }
22
+ CommandResult.new :stdout => stdout, :exit_status => $?.exitstatus
24
23
  end
25
24
 
26
25
  def run_with_no_ruby_environment
@@ -58,21 +57,21 @@ module SpecInfra
58
57
  # In Ubuntu, some services are under upstart and "service foo status" returns
59
58
  # exit status 0 even though they are stopped.
60
59
  # So return false if stdout contains "stopped/waiting".
61
- return false if ret[:stdout] =~ /stopped\/waiting/
60
+ return false if ret.stdout =~ /stopped\/waiting/
62
61
 
63
62
  # If the service is not registered, check by ps command
64
- if ret[:exit_status] == 1
63
+ if ret.exit_status == 1
65
64
  ret = run_command(commands.check_process(process))
66
65
  end
67
66
 
68
- ret[:exit_status] == 0
67
+ ret.success?
69
68
  end
70
69
 
71
70
  def check_monitored_by_monit(process)
72
71
  ret = run_command(commands.check_monitored_by_monit(process))
73
- return false unless ret[:stdout] != nil && ret[:exit_status] == 0
72
+ return false unless ret.stdout != nil && ret.success?
74
73
 
75
- retlines = ret[:stdout].split(/[\r\n]+/).map(&:strip)
74
+ retlines = ret.stdout.split(/[\r\n]+/).map(&:strip)
76
75
  proc_index = retlines.index("Process '#{process}'")
77
76
  return false unless proc_index
78
77
 
@@ -80,7 +79,7 @@ module SpecInfra
80
79
  end
81
80
 
82
81
  def check_readable(file, by_whom)
83
- mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
82
+ mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip)
84
83
  mode = mode.split('')
85
84
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
86
85
  case by_whom
@@ -96,7 +95,7 @@ module SpecInfra
96
95
  end
97
96
 
98
97
  def check_writable(file, by_whom)
99
- mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
98
+ mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip)
100
99
  mode = mode.split('')
101
100
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
102
101
  case by_whom
@@ -112,7 +111,7 @@ module SpecInfra
112
111
  end
113
112
 
114
113
  def check_executable(file, by_whom)
115
- mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
114
+ mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip)
116
115
  mode = mode.split('')
117
116
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
118
117
  case by_whom
@@ -129,11 +128,11 @@ module SpecInfra
129
128
 
130
129
  def check_mounted(path, expected_attr, only_with)
131
130
  ret = run_command(commands.check_mounted(path))
132
- if expected_attr.nil? || ret[:exit_status] != 0
133
- return ret[:exit_status] == 0
131
+ if expected_attr.nil? || ret.failure?
132
+ return ret.success?
134
133
  end
135
134
 
136
- mount = ret[:stdout].scan(/\S+/)
135
+ mount = ret.stdout.scan(/\S+/)
137
136
  actual_attr = { :device => mount[0], :type => mount[4] }
138
137
  mount[5].gsub(/\(|\)/, '').split(',').each do |option|
139
138
  name, val = option.split('=')
@@ -163,11 +162,11 @@ module SpecInfra
163
162
  def check_routing_table(expected_attr)
164
163
  return false if ! expected_attr[:destination]
165
164
  ret = run_command(commands.check_routing_table(expected_attr[:destination]))
166
- return false if ret[:exit_status] != 0
165
+ return false if ret.failure?
167
166
 
168
- ret[:stdout].gsub!(/\r\n/, "\n")
167
+ ret.stdout.gsub!(/\r\n/, "\n")
169
168
 
170
- ret[:stdout] =~ /^(\S+)(?: via (\S+))? dev (\S+).+\n(?:default via (\S+))?/
169
+ ret.stdout =~ /^(\S+)(?: via (\S+))? dev (\S+).+\n(?:default via (\S+))?/
171
170
  actual_attr = {
172
171
  :destination => $1,
173
172
  :gateway => $2 ? $2 : $4,
@@ -182,47 +181,47 @@ module SpecInfra
182
181
 
183
182
  def check_os
184
183
  return SpecInfra.configuration.os if SpecInfra.configuration.os
185
- if run_command('ls /etc/redhat-release')[:exit_status] == 0
186
- line = run_command('cat /etc/redhat-release')[:stdout]
184
+ if run_command('ls /etc/redhat-release').success?
185
+ line = run_command('cat /etc/redhat-release').stdout
187
186
  if line =~ /release (\d[\d.]*)/
188
187
  release = $1
189
188
  end
190
189
  { :family => 'RedHat', :release => release }
191
- elsif run_command('ls /etc/system-release')[:exit_status] == 0
190
+ elsif run_command('ls /etc/system-release').success?
192
191
  { :family => 'RedHat', :release => nil } # Amazon Linux
193
- elsif run_command('ls /etc/debian_version')[:exit_status] == 0
192
+ elsif run_command('ls /etc/debian_version').success?
194
193
  lsb_release = run_command("lsb_release -i")
195
- if lsb_release[:exit_status] == 0
196
- distro = $' if lsb_release[:stdout] =~ /:/
194
+ if lsb_release.success?
195
+ distro = $' if lsb_release.stdout =~ /:/
197
196
  else
198
197
  lsb_release = run_command("cat /etc/lsb-release")
199
- if lsb_release[:exit_status] == 0
200
- lsb_release[:stdout].each_line do |line|
198
+ if lsb_release.success?
199
+ lsb_release.stdout.each_line do |line|
201
200
  distro = $' if line =~ /^DISTRIB_ID=/
202
201
  end
203
202
  end
204
203
  end
205
204
  distro ||= 'Debian'
206
205
  { :family => distro.strip, :release => nil }
207
- elsif run_command('ls /etc/gentoo-release')[:exit_status] == 0
206
+ elsif run_command('ls /etc/gentoo-release').success?
208
207
  { :family => 'Gentoo', :release => nil }
209
- elsif run_command('ls /usr/lib/setup/Plamo-*')[:exit_status] == 0
208
+ elsif run_command('ls /usr/lib/setup/Plamo-*').success?
210
209
  { :family => 'Plamo', :release => nil }
211
- elsif run_command('uname -s')[:stdout] =~ /AIX/i
210
+ elsif run_command('uname -s').stdout =~ /AIX/i
212
211
  { :family => 'AIX', :release => nil }
213
- elsif (os = run_command('uname -sr')[:stdout]) && os =~ /SunOS/i
212
+ elsif (os = run_command('uname -sr').stdout) && os =~ /SunOS/i
214
213
  if os =~ /5.10/
215
214
  { :family => 'Solaris10', :release => nil }
216
- elsif run_command('grep -q "Oracle Solaris 11" /etc/release')[:exit_status] == 0
215
+ elsif run_command('grep -q "Oracle Solaris 11" /etc/release').success?
217
216
  { :family => 'Solaris11', :release => nil }
218
- elsif run_command('grep -q SmartOS /etc/release')[:exit_status] == 0
217
+ elsif run_command('grep -q SmartOS /etc/release').success?
219
218
  { :family => 'SmartOS', :release => nil }
220
219
  else
221
220
  { :family => 'Solaris', :release => nil }
222
221
  end
223
- elsif run_command('uname -s')[:stdout] =~ /Darwin/i
222
+ elsif run_command('uname -s').stdout =~ /Darwin/i
224
223
  { :family => 'Darwin', :release => nil }
225
- elsif run_command('uname -s')[:stdout] =~ /FreeBSD/i
224
+ elsif run_command('uname -s').stdout =~ /FreeBSD/i
226
225
  { :family => 'FreeBSD', :release => nil }
227
226
  else
228
227
  { :family => 'Base', :release => nil }
@@ -73,11 +73,11 @@ exit $exitCode
73
73
  ret = run_command(commands.check_running(process))
74
74
 
75
75
  # If the service is not registered, check the process
76
- if ret[:exit_status] == 1
76
+ if ret.exit_status == 1
77
77
  ret = run_command(commands.check_process(process))
78
78
  end
79
79
 
80
- ret[:exit_status] == 0
80
+ ret.success?
81
81
  end
82
82
  end
83
83
  end
@@ -10,8 +10,7 @@ module SpecInfra
10
10
 
11
11
  def run_command(cmd, opts={})
12
12
  @lines << cmd
13
- { :stdout => nil, :stderr => nil,
14
- :exit_status => 0, :exit_signal => nil }
13
+ CommandResult.new
15
14
  end
16
15
 
17
16
  class Writer
@@ -15,7 +15,7 @@ module SpecInfra
15
15
  @example.metadata[:stdout] = ret[:stdout]
16
16
  end
17
17
 
18
- ret
18
+ CommandResult.new ret
19
19
  end
20
20
 
21
21
  def build_command(cmd)
@@ -18,8 +18,8 @@ module SpecInfra
18
18
  @example.metadata[:stdout] = stdout + stderr
19
19
  end
20
20
 
21
- { :stdout => stdout, :stderr => stderr,
22
- :exit_status => result[:exitcode], :exit_signal => nil }
21
+ CommandResult.new :stdout => stdout, :stderr => stderr,
22
+ :exit_status => result[:exitcode]
23
23
  end
24
24
  end
25
25
  end
@@ -0,0 +1,28 @@
1
+ module SpecInfra
2
+ class CommandResult
3
+ attr_reader :stdout, :stderr, :exit_status, :exit_signal
4
+
5
+ def initialize(args = {})
6
+ @stdout = args[:stdout] || ''
7
+ @stderr = args[:stderr] || ''
8
+ @exit_status = args[:exit_status] || 0
9
+ @exit_signal = args[:exit_signal]
10
+ end
11
+
12
+ def success?
13
+ @exit_status == 0
14
+ end
15
+
16
+ def failure?
17
+ @exit_status != 0
18
+ end
19
+
20
+ def [](x)
21
+ warn "CommandResult#[] is obsolete. Use accessors instead. in #{caller[0]}"
22
+ case x
23
+ when :stdout, :stderr, :exit_status, :exit_signal
24
+ self.send(x)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Specinfra
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/specinfra.rb CHANGED
@@ -2,6 +2,7 @@ require "specinfra/version"
2
2
  require "specinfra/helper"
3
3
  require "specinfra/backend"
4
4
  require "specinfra/command"
5
+ require "specinfra/command_result"
5
6
  require "specinfra/configuration"
6
7
 
7
8
  include SpecInfra
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ include SpecInfra::Helper::Exec
4
+
5
+ describe 'build command with path' do
6
+ before :each do
7
+ RSpec.configure do |c|
8
+ c.path = '/sbin:/usr/sbin'
9
+ end
10
+ end
11
+
12
+ context 'command pattern 1' do
13
+ subject { backend.build_command('test -f /etc/passwd') }
14
+ it { should eq 'env PATH=/sbin:/usr/sbin:$PATH test -f /etc/passwd' }
15
+ end
16
+
17
+ context 'command pattern 2' do
18
+ subject { backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)') }
19
+ it { should eq 'env PATH=/sbin:/usr/sbin:$PATH test ! -f /etc/selinux/config || (env PATH=/sbin:/usr/sbin:$PATH getenforce | grep -i -- disabled && env PATH=/sbin:/usr/sbin:$PATH grep -i -- ^SELINUX=disabled$ /etc/selinux/config)' }
20
+ end
21
+
22
+ context 'command pattern 3' do
23
+ subject { backend.build_command("dpkg -s apache2 && ! dpkg -s apache2 | grep -E '^Status: .+ not-installed$'") }
24
+ it { should eq "env PATH=/sbin:/usr/sbin:$PATH dpkg -s apache2 && ! env PATH=/sbin:/usr/sbin:$PATH dpkg -s apache2 | grep -E '^Status: .+ not-installed$'" }
25
+ end
26
+
27
+ after :each do
28
+ RSpec.configure do |c|
29
+ c.path = nil
30
+ end
31
+ end
32
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,8 +7,7 @@ module SpecInfra
7
7
  module Backend
8
8
  class Ssh
9
9
  def run_command(cmd, opts={})
10
- { :stdout => nil, :stderr => nil,
11
- :exit_status => 0, :exit_signal => nil }
10
+ CommandResult.new :stdout => nil, :exit_status => 0
12
11
  end
13
12
  end
14
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: specinfra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gosuke Miyashita
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-16 00:00:00.000000000 Z
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,7 @@ files:
101
101
  - lib/specinfra/command/solaris11.rb
102
102
  - lib/specinfra/command/ubuntu.rb
103
103
  - lib/specinfra/command/windows.rb
104
+ - lib/specinfra/command_result.rb
104
105
  - lib/specinfra/configuration.rb
105
106
  - lib/specinfra/helper.rb
106
107
  - lib/specinfra/helper/backend.rb
@@ -111,6 +112,7 @@ files:
111
112
  - lib/specinfra/helper/properties.rb
112
113
  - lib/specinfra/properties.rb
113
114
  - lib/specinfra/version.rb
115
+ - spec/backend/exec/build_command_spec.rb
114
116
  - spec/backend/ssh/build_command_spec.rb
115
117
  - spec/configuration_spec.rb
116
118
  - spec/helper/backend_spec.rb
@@ -142,6 +144,7 @@ signing_key:
142
144
  specification_version: 4
143
145
  summary: Common layer for serverspec and configspec
144
146
  test_files:
147
+ - spec/backend/exec/build_command_spec.rb
145
148
  - spec/backend/ssh/build_command_spec.rb
146
149
  - spec/configuration_spec.rb
147
150
  - spec/helper/backend_spec.rb