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-01-08-0'

Class Method Summary collapse

Class Method Details

.cleanup_container(container) ⇒ Object



29
30
31
32
# File 'lib/pdqtest/docker.rb', line 29

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



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

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

.exec_out(res) ⇒ Object



45
46
47
# File 'lib/pdqtest/docker.rb', line 45

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

.exec_status(res, puppet = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/pdqtest/docker.rb', line 34

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_err(res) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/pdqtest/docker.rb', line 61

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



53
54
55
56
57
58
59
# File 'lib/pdqtest/docker.rb', line 53

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
# 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 => 'ro'}},
  )
  container.start({'Binds' => [ pwd +':'+ test_dir]})

  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