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 => 'geoffwilliams/pdqtest-centos:2017-09-19-0', :UBUNTU => 'geoffwilliams/pdqtest-ubuntu:2017-09-20-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
-
.acceptance_test_images ⇒ Object
detect the image to use based on metadata.json.
- .cleanup_container(container) ⇒ Object
- .exec(container, cmd) ⇒ Object
- .exec_err(res) ⇒ Object
- .exec_out(res) ⇒ Object
- .exec_status(res, puppet = false) ⇒ Object
- .log_all(res) ⇒ Object
- .log_err(res) ⇒ Object
- .log_out(res) ⇒ Object
- .new_container(test_dir, image_name) ⇒ Object
- .wrap_cmd(cmd) ⇒ Object
Class Method Details
.acceptance_test_images ⇒ Object
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
125 126 127 128 |
# File 'lib/pdqtest/docker.rb', line 125 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
145 146 147 |
# File 'lib/pdqtest/docker.rb', line 145 def self.exec_err(res) res[ERR] end |
.exec_out(res) ⇒ Object
141 142 143 |
# File 'lib/pdqtest/docker.rb', line 141 def self.exec_out(res) res[OUT] end |
.exec_status(res, puppet = false) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/pdqtest/docker.rb', line 130 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
157 158 159 160 |
# File 'lib/pdqtest/docker.rb', line 157 def self.log_all(res) log_err(res) log_out(res) end |
.log_err(res) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/pdqtest/docker.rb', line 162 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
149 150 151 152 153 154 155 |
# File 'lib/pdqtest/docker.rb', line 149 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) ⇒ 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 123 |
# File 'lib/pdqtest/docker.rb', line 63 def self.new_container(test_dir, image_name) 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'}, '/cut' => {pwd => 'rw'}, # DEPRECATED -FOR REMOVAL '/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}:/cut:rw", # DEPRECATED -FOR REMOVAL "#{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'], }, } ) 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 |