Module: PDQTest::Docker

Defined in:
lib/pdqtest/docker.rb

Constant Summary collapse

OUT =
0
ERR =
1
STATUS =
2
ENV =
'export TERM=xterm LC_ALL=C PATH=/usr/local/bats/bin:/opt/puppetlabs/puppet/bin:$PATH;'
IMAGE_NAME =
'geoffwilliams/pdqtest-centos:2017-04-04-0'

Class Method Summary collapse

Class Method Details

.cleanup_container(container) ⇒ Object



49
50
51
52
# File 'lib/pdqtest/docker.rb', line 49

def self.cleanup_container(container)
  container.stop
  container.delete(:force => true)
end

.exec(container, cmd) ⇒ Object



14
15
16
# File 'lib/pdqtest/docker.rb', line 14

def self.exec(container, cmd)
  container.exec(wrap_cmd(cmd), tty: true)
end

.exec_err(res) ⇒ Object



69
70
71
# File 'lib/pdqtest/docker.rb', line 69

def self.exec_err(res)
  res[ERR]
end

.exec_out(res) ⇒ Object



65
66
67
# File 'lib/pdqtest/docker.rb', line 65

def self.exec_out(res)
  res[OUT]
end

.exec_status(res, puppet = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/pdqtest/docker.rb', line 54

def self.exec_status(res, puppet=false)
  if puppet
    # 0 == ok, no changes
    # 2 == ok, changes made
    allowable_values = [0,2]
  else
    allowable_values = [0]
  end
  status = allowable_values.include?(res[STATUS])
end

.log_all(res) ⇒ Object



81
82
83
84
# File 'lib/pdqtest/docker.rb', line 81

def self.log_all(res)
  log_err(res)
  log_out(res)
end

.log_err(res) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/pdqtest/docker.rb', line 86

def self.log_err(res)
  exec_err(res).each { |l|
    # Output comes back as an array and needs to be iterated or we lose our
    # ansi formatting
    Escort::Logger.error.error l
  }
end

.log_out(res) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/pdqtest/docker.rb', line 73

def self.log_out(res)
  exec_out(res).each { |l|
    # Output comes back as an array and needs to be iterated or we lose our
    # ansi formatting
    Escort::Logger.output.puts l
  }
end

.new_container(test_dir) ⇒ Object



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
# File 'lib/pdqtest/docker.rb', line 18

def self.new_container(test_dir)
  pwd = Dir.pwd
  container = ::Docker::Container.create(
    'Image'   => IMAGE_NAME,
    'Volumes' => {
      test_dir         => {pwd               => 'rw'},
      '/sys/fs/cgroup' => {'/sys/fs/cgroup'  => 'ro'},
    },
    'HostConfig' => {
      "Binds": [
        "/sys/fs/cgroup:/sys/fs/cgroup:ro",
        "#{pwd}:#{test_dir}:rw",
      ],
    },
  )
  container.start(
    {
      #'Binds' => [ pwd +':'+ test_dir,],
      'HostConfig' => {
        'Tmpfs': {
          '/run'      => '',
          '/run/lock' => '',
        },
        CapAdd: [ 'SYS_ADMIN'],
      },
    }
  )

  container
end

.wrap_cmd(cmd) ⇒ Object



10
11
12
# File 'lib/pdqtest/docker.rb', line 10

def self.wrap_cmd(cmd)
  ['bash',  '-c', "#{ENV} #{cmd}"]
end