Module: PDQTest::Docker

Defined in:
lib/pdqtest/docker.rb

Constant Summary collapse

OUT =
0
ERR =
1
STATUS =
2
IMAGES =
{
 :DEFAULT => 'declarativesystems/pdqtest-centos:2019-04-01-0',
 :UBUNTU  => 'declarativesystems/pdqtest-ubuntu:2018-10-06-0',
 :WINDOWS => 'declarativesystems/pdqtest-windows:2018-09-30-0',
}
CONTAINER_PATHS =
{
    :windows => {
        :testcase   => 'C:\\testcase',
    },
    :linux => {
        :yum_cache => "/var/cache/yum",
        :testcase  => '/testcase',
    }
}
HOST_PATHS =

path for common things on the host computer running pdqtest (vm, laptop, etc)

{
    :windows => {
    },
    :linux => {
        :yum_cache  => "#{Util::app_dir_expanded}/cache/yum",
    }
}

Class Method Summary collapse

Class Method Details

._exec_real(container, real_c) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pdqtest/docker.rb', line 72

def self._exec_real(container, real_c)
  $logger.debug("exec_real: running docker command: #{real_c}")
  _res = container.exec(real_c, tty: true)

  # docker returns an array of stuff - convert to hash with labels
  res = {
      :OUT => _res[OUT],
      :ERR => _res[ERR],
      :STATUS => _res[STATUS],
  }

  res
end

.acceptance_test_imagesObject

detect the image to use based on metadata.json



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

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"
          supported_images << IMAGES[:WINDOWS]
        else
          supported_images << IMAGES[:DEFAULT]
      end
    }
  end

  supported_images.uniq
end

.cleanup_container(container) ⇒ Object



196
197
198
199
# File 'lib/pdqtest/docker.rb', line 196

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

.new_container(image_name, privileged) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pdqtest/docker.rb', line 122

def self.new_container(image_name, privileged)

  if Util.is_windows
    ::Docker.url = "tcp://127.0.0.1:2375"
    # nasty hack for https://github.com/swipely/docker-api/issues/441
    ::Docker.send(:remove_const, 'API_VERSION')
    ::Docker.const_set('API_VERSION', '1.24')
  end

  # 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 ! Util.is_windows
    if ! Dir.exists?(HOST_PATHS[Util.host_platform][:yum_cache])
      FileUtils.mkdir_p(HOST_PATHS[Util.host_platform][:yum_cache])
    end
  end

  #
  #  volumes (container -> host)
  #
  volumes = supporting_volumes

  #
  # binds (host -> container)
  #
  binds = Util.volumes2binds(volumes)

  #
  # hostconfig->tmpfs (linux)
  #
  if Util.is_windows
    start_body = {}
    if privileged
      $logger.error "--privileged has no effect on windows"
    end
  else
    start_body = {
        'HostConfig' => {
            'Tmpfs': {
                '/run'      => '',
                '/run/lock' => '',
            },
            CapAdd: [ 'SYS_ADMIN'],
            Privileged: privileged,
        }
    }
  end

  #
  # container
  #

  container = ::Docker::Container.create(
    'Image'   => image_name,
    'Volumes' => volumes,
    'HostConfig' => {
      "SecurityOpt" => security_opt,
      "Binds": binds,
    },
  )
  container.start(start_body)

  container
end

.supporting_volumesObject

Map the testcase and any OS specific volumes we would always want, eg yum cache, random crap for systemd, etc



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pdqtest/docker.rb', line 54

def self.supporting_volumes
  pwd = Dir.pwd
  platform = Util.host_platform
  if Util.is_windows
    # normalise output for windows
    pwd = pwd.gsub(/\//, '\\')
  end
  test_dir = CONTAINER_PATHS[platform][:testcase]
  volumes = {test_dir => {pwd => 'rw'}}

  if ! Util.is_windows
    volumes['/sys/fs/cgroup']                      = {'/sys/fs/cgroup'                 => 'ro'}
    volumes[CONTAINER_PATHS[platform][:yum_cache]] = {HOST_PATHS[platform][:yum_cache] => 'rw'}
  end

  volumes
end

.test_dirObject

convenience lookup for container testcase dir since its used all over the place fixme! - belongs somewhere else now…



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

def self.test_dir
  CONTAINER_PATHS[Util.host_platform][:testcase]
end