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;'
IMAGES =
{
 :DEFAULT => 'declarativesystems/pdqtest-centos:2018-04-13-0',
 :UBUNTU  => 'declarativesystems/pdqtest-ubuntu:2018-04-13-0',
}
HIERA_YAML_CONTAINER =
'/etc/puppetlabs/puppet/hiera.yaml'
HIERA_YAML_HOST =
'/spec/fixtures/hiera.yaml'
HIERA_DIR =
'/spec/fixtures/hieradata'
YUM_CACHE_CONTAINER =
"/var/cache/yum"
YUM_CACHE_HOST =
"#{Util::app_dir}/cache/yum"

Class Method Summary collapse

Class Method Details

.acceptance_test_imagesObject

detect the image to use based on metadata.json



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

def self.acceptance_test_images()
  supported_images = []
  os_hash = Puppet::['operatingsystem_support'] || {}
  # returns a hash that looks like this (if non-empty):
  # [
  #   {
  #     "operatingsystem": "RedHat",
  #     "operatingsystemrelease": [
  #         "6",
  #         "7"
  #     ]
  #   },
  # ]
  # We will map this list of OSs to the simple list of docker images we
  # supply
  if os_hash.size == 0
    # Always support the default test system if no metadata present
    supported_images << IMAGES[:DEFAULT]
  else
    os_hash.each { |os|
      case os["operatingsystem"].downcase
        when "ubuntu"
          supported_images << IMAGES[:UBUNTU]
        when "windows"
          Escort::Logger.output.puts "Windows acceptance testing not supported yet... any ideas?"
        else
          supported_images << IMAGES[:DEFAULT]
      end
    }
  end

  supported_images.uniq
end

.cleanup_container(container) ⇒ Object



124
125
126
127
# File 'lib/pdqtest/docker.rb', line 124

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

.exec(container, cmd) ⇒ Object



24
25
26
# File 'lib/pdqtest/docker.rb', line 24

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

.exec_err(res) ⇒ Object



144
145
146
# File 'lib/pdqtest/docker.rb', line 144

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

.exec_out(res) ⇒ Object



140
141
142
# File 'lib/pdqtest/docker.rb', line 140

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

.exec_status(res, puppet = false) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/pdqtest/docker.rb', line 129

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



156
157
158
159
# File 'lib/pdqtest/docker.rb', line 156

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

.log_err(res) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/pdqtest/docker.rb', line 161

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



148
149
150
151
152
153
154
# File 'lib/pdqtest/docker.rb', line 148

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, image_name, privileged) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pdqtest/docker.rb', line 63

def self.new_container(test_dir, image_name, privileged)
  pwd = Dir.pwd
  hiera_yaml_host = File.join(pwd, HIERA_YAML_HOST)
  hiera_dir = File.join(pwd, HIERA_DIR)

  # security options seem required on OSX to allow SYS_ADMIN capability to
  # work - without this container starts fine with no errors but the CAP is
  # missing from the inspect output and all systemd commands fail with errors
  # about dbus
  security_opt =
    if (/darwin/ =~ RUBY_PLATFORM) != nil
      ["seccomp:unconfined"]
    else
      []
    end

  if ! Dir.exists?(YUM_CACHE_HOST)
    FileUtils.mkdir_p(YUM_CACHE_HOST)
  end

  # hiera.yaml *must* exist on the host or we will get errors from Docker
  if ! File.exists?(hiera_yaml_host)
    File.write(hiera_yaml_host, '# hiera configuration for testing')
  end
  container = ::Docker::Container.create(
    'Image'   => image_name,
    'Volumes' => {
      test_dir              => {pwd               => 'rw'},
      HIERA_YAML_CONTAINER  => {hiera_yaml_host   => 'rw'},
      HIERA_DIR             => {hiera_dir         => 'rw'},
      '/sys/fs/cgroup'      => {'/sys/fs/cgroup'  => 'ro'},
      YUM_CACHE_CONTAINER   => {YUM_CACHE_HOST    => 'rw'},
    },
    'HostConfig' => {
      "SecurityOpt" => security_opt,
      "Binds": [
        "/sys/fs/cgroup:/sys/fs/cgroup:ro",
        "#{pwd}:#{test_dir}:rw",
        "#{hiera_yaml_host}:#{HIERA_YAML_CONTAINER}:rw",
        "#{hiera_dir}:#{HIERA_DIR}:rw",
        "#{YUM_CACHE_HOST}:#{YUM_CACHE_CONTAINER}:rw",
      ],
    },
  )
  container.start(
    {
      #'Binds' => [ pwd +':'+ test_dir,],
      'HostConfig' => {
        'Tmpfs': {
          '/run'      => '',
          '/run/lock' => '',
        },
        CapAdd: [ 'SYS_ADMIN'],
        Privileged: privileged,
      },
    }
  )

  container
end

.wrap_cmd(cmd) ⇒ Object



20
21
22
# File 'lib/pdqtest/docker.rb', line 20

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