summaryrefslogtreecommitdiff
path: root/tests/server-tests/helpers/os_helper.rb
blob: 6a71388c636c7a9bcda0276d04c814629ce9aacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class LeapTest

  #
  # works like pgrep command line
  # return an array of hashes like so [{:pid => "1234", :process => "ls"}]
  #
  def pgrep(match)
    output = `pgrep --full --list-name '#{match}'`
    output.each_line.map{|line|
      pid = line.split(' ')[0]
      process = line.gsub(/(#{pid} |\n)/, '')
      # filter out pgrep cmd itself
      # on wheezy hosts, the "process" var contains the whole cmd including all parameters
      # on jessie hosts, it only contains the first cmd (which is the default sheel invoked by 'sh')
      if process =~ /^sh/
        nil
      else
        {:pid => pid, :process => process}
      end
    }.compact
  end

  #
  # passes if the specified process is runnin.
  #
  # arguments:
  #
  #  match   => VALUE      -- scan process table for VALUE
  #  service => VALUE      -- call systemctl is-active VALUE
  #
  #  single  => true|false -- if true, there must be one result
  #
  def assert_running(match:nil, service:nil, single:false)
    if match
      processes = pgrep(match)
      assert processes.any?, "No running process for #{match}"
      if single
        assert processes.length == 1, "More than one process for #{match}"
      end
    elsif service
      `systemctl is-active #{service} 2>&1`
      if $?.exitstatus != 0
        output = `systemctl status #{service} 2>&1`
        fail "Service '#{service}' is not running:\n#{output}"
      end
    end
  end

  #
  # runs the specified command, failing on a non-zero exit status.
  #
  def assert_run(command)
    output = `#{command} 2>&1`
    if $?.exitstatus != 0
      fail "Error running `#{command}`:\n#{output}"
    end
  end

end