Class: Serverspec::Backend::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/serverspec/backend/exec.rb

Direct Known Subclasses

Puppet, Ssh

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Exec

Returns a new instance of Exec.



6
7
8
# File 'lib/serverspec/backend/exec.rb', line 6

def initialize(commands)
  @commands ||= commands
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Default action is to call check_zero with args



34
35
36
37
38
# File 'lib/serverspec/backend/exec.rb', line 34

def method_missing(meth, *args, &block)
  # Remove example object from *args
  args.shift
  check_zero(meth, *args)
end

Instance Method Details

#check_executable(example, file, by_whom) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/serverspec/backend/exec.rb', line 94

def check_executable(example, file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0111 != 0
  when 'owner'
    mode_octal & 0100 != 0
  when 'group'
    mode_octal & 0010 != 0
  when 'others'
    mode_octal & 0001 != 0
  end
end

#check_installed_by_gem(example, package, version) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/serverspec/backend/exec.rb', line 40

def check_installed_by_gem(example, package, version)
  ret = run_command(commands.check_installed_by_gem(package))
  res = ret[:exit_status] == 0
  if res && version
    res = false if not ret[:stdout].match(/\(#{version}\)/)
  end
  res
end

#check_mounted(example, path, expected_attr, only_with) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/serverspec/backend/exec.rb', line 110

def check_mounted(example, path, expected_attr, only_with)
  ret = run_command(commands.check_mounted(path))
  if expected_attr.nil? || ret[:exit_status] != 0
    return ret[:exit_status] == 0
  end

  mount = ret[:stdout].scan(/\S+/)
  actual_attr    = { :device => mount[0], :type => mount[4] }
  mount[5].gsub(/\(|\)/, '').split(',').each do |option|
    name, val = option.split('=')
    if val.nil?
      actual_attr[name.to_sym] = true
    else
      val = val.to_i if val.match(/^\d+$/)
      actual_attr[name.to_sym] = val
    end
  end

  if ! expected_attr[:options].nil?
    expected_attr.merge!(expected_attr[:options])
    expected_attr.delete(:options)
  end

  if only_with
    actual_attr == expected_attr
  else
    match = true
    expected_attr.each do |key, val|
      match = actual_attr[key] == val
    end
    match
  end
end

#check_osObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/serverspec/backend/exec.rb', line 144

def check_os
  if run_command('ls /etc/redhat-release')[:exit_status] == 0
    'RedHat'
  elsif run_command('ls /etc/debian_version')[:exit_status] == 0
    'Debian'
  elsif run_command('ls /etc/gentoo-release')[:exit_status] == 0
    'Gentoo'
  elsif run_command('uname -s')[:stdout] =~ /SunOS/i
    'Solaris'
  end
end

#check_readable(example, file, by_whom) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/serverspec/backend/exec.rb', line 62

def check_readable(example, file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0444 != 0
  when 'owner'
    mode_octal & 0400 != 0
  when 'group'
    mode_octal & 0040 != 0
  when 'others'
    mode_octal & 0004 != 0
  end
end

#check_running(example, process) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/serverspec/backend/exec.rb', line 49

def check_running(example, process)
  ret = run_command(commands.check_running(process))
  if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/
    ret = run_command(commands.check_process(process))
  end
  ret[:exit_status] == 0
end

#check_running_under_supervisor(example, process) ⇒ Object



57
58
59
60
# File 'lib/serverspec/backend/exec.rb', line 57

def check_running_under_supervisor(example, process)
  ret = run_command(commands.check_running_under_supervisor(process))
  ret[:exit_status] == 0 && ret[:stdout] =~ /RUNNING/
end

#check_writable(example, file, by_whom) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/serverspec/backend/exec.rb', line 78

def check_writable(example, file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0222 != 0
  when 'owner'
    mode_octal & 0200 != 0
  when 'group'
    mode_octal & 0020 != 0
  when 'others'
    mode_octal & 0002 != 0
  end
end

#check_zero(cmd, *args) ⇒ Object



28
29
30
31
# File 'lib/serverspec/backend/exec.rb', line 28

def check_zero(cmd, *args)
  ret = run_command(commands.send(cmd, *args))
  ret[:exit_status] == 0
end

#commandsObject



10
11
12
# File 'lib/serverspec/backend/exec.rb', line 10

def commands
  @commands
end

#run_command(cmd, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/serverspec/backend/exec.rb', line 14

def run_command(cmd, opts={})
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
  #stdout, stderr, status = Open3.capture3(cmd)
  # So get exit status with `command`
  `#{cmd} 2>&1`
  ret = { :exit_status => $?, :exit_signal => nil }

  # Get stdout and stderr
  stdin, stdout, stderr = Open3.popen3(cmd)
  ret[:stdout] = stdout.read
  ret[:stderr] = stderr.read
  ret
end