Module: DockerSpec

Defined in:
lib/docker/spec.rb,
lib/docker/spec/docker.rb

Overview

Documentation

Constant Summary collapse

CONTAINER_RUN_WAIT_TIMEOUT =
60
CONFIG_FILE =
'docker_spec.yml'
ROOT_DIR =
'root'
DOCKER_AUTH_FILE =
'~/.docker/config.json'

Class Method Summary collapse

Class Method Details

.build_docker_imageObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/docker/spec.rb', line 107

def self.build_docker_image
  # Rebuild the cache filesystem
  build_args = ''
  build_args += ' --no-cache' if @config[:clear_cache]

  # Build the docker image
  build_cmd = "docker build -t #{@config[:image_name]} #{build_args} ."
  status = POpen4.popen4(build_cmd) do |stdout, stderr, _stdin|
    stdout.each { |line| puts line }
    stderr.each { |line| puts line.red }
  end
  fail("#{build_cmd} failed") if status.exitstatus != 0
end

.build_rootObject



100
101
102
103
104
105
# File 'lib/docker/spec.rb', line 100

def self.build_root
  system 'bash -ec \'sudo chown root:root -R root &&' \
         '(cd root && sudo tar zcf ../root.tar.gz .) && ' \
         'sudo chown -R `id -u`:`id -g` root.tar.gz root\'' \
         if Dir.exist?(ROOT_DIR)
end

.clean_upObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/docker/spec.rb', line 171

def self.clean_up
  # Keep container running
  @config[:keep_running] = DockerSpec.get_config(:keep_running, 'DOCKER_SPEC_KEEP_RUNNING',
                                                 'Keep container running? ')
  if @config[:keep_running]
    puts "\nINFO: To connect to a running container: \n\n" \
      "docker exec -ti #{@config[:container_name]} bash"
  else
    delete_container
  end
end

.delete_containerObject



163
164
165
166
167
168
169
# File 'lib/docker/spec.rb', line 163

def self.delete_container
  filters = { name: [@config[:container_name]] }.to_json
  Docker::Container.all(all: true, filters: filters).each do |c|
    c.kill
    c.delete
  end
end

.docker_testsObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/docker/spec/docker.rb', line 2

def self.docker_tests
  describe 'Running a docker container' do
    before(:all) do
      @config = DockerSpec.load_config
      @image = Docker::Image.all.detect do |i|
        i.info['RepoTags'].include?(@config[:image_name] + ':latest')
      end
      @container = Docker::Container.all.select do |c|
        c.info["Names"] == [ "/" + @config[:container_name] ]
      end.first
    end

    it 'should be available' do
      expect(@image).to_not be_nil
    end

    it 'should have state running' do
      expect(@container.json['State']['Running']).to be true
    end

    it 'Should stay running' do
      expect(@container.json['State']['Running']).to be true
    end

    it 'Should not have exit processes' do
      expect(@container.logs(stdout: true)).to_not match(/exit/)
    end

    it 'Services supervisor should be running' do
      expect(process('supervisord')).to be_running
    end
  end
end

.get_config(key, envvar, question) ⇒ Object



183
184
185
186
187
188
# File 'lib/docker/spec.rb', line 183

def self.get_config(key, envvar, question)
  value = to_boolean(ENV[envvar])
  value = @config[key] if value.nil?
  value = agree(question, 'n') if value.nil?
  value
end

.load_configObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/docker/spec.rb', line 84

def self.load_config
  File.exist?(CONFIG_FILE) || fail('Could not load docker_spec.yml')
  @config = YAML.load(File.read(CONFIG_FILE)) ||
            fail('docker_spec.yml is not a valid yml file')

  @config[:name] || fail('name is not defined in docker_spec.yml')
  @config[:account] || fail('account is not defined in docker_spec.yml')
  @config[:image_name] = format '%s/%s', @config[:account], @config[:name]
  @config[:container_name] = format 'docker_spec-%s', @config[:name]
  @config[:build_root] = DockerSpec.get_config(:build_root, 'DOCKER_SPEC_BUILD_ROOT',
                                               'Rebuild root filesystem? ')
  @config[:clear_cache] = DockerSpec.get_config(:clear_cache, 'DOCKER_SPEC_CLEAR_CACHE',
                                                'Clear docker cache? ')
  @config
end

.pushObject



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/docker/spec.rb', line 31

def self.push
  @config[:push_container] = DockerSpec.get_config(:push_container, 'DOCKER_SPEC_PUSH_CONTAINER',
                                                   "\nPush new tag? ")
  if @config[:push_container]

    @config[:tag_db] ||
      fail('tag_db is not defined in docker_spec.yml')

    # Load credentials from config file, or default docker config
    if @config[:dockerhub]
      @config[:dockerhub][:username] ||
        fail('dockerhub->username is not defined in docker_spec.yml')
      @config[:dockerhub][:password] ||
        fail('dockerhub->password is not defined in docker_spec.yml')
      @config[:dockerhub][:email] ||
        fail('dockerhub->email is not defined in docker_spec.yml')
    else
      @config[:dockerhub] = Hash.new
      docker_auth = JSON.parse(File.read(File.expand_path(DOCKER_AUTH_FILE)))
      auth_base64 = docker_auth['auths']['https://index.docker.io/v1/']['auth']
      @config[:dockerhub][:username] = Base64.decode64(auth_base64).split(':').first
      @config[:dockerhub][:password] = Base64.decode64(auth_base64).split(':').last
      @config[:dockerhub][:email] = docker_auth['auths']['https://index.docker.io/v1/']['email']
    end

    # Open key value store and get the current tag for this repo
    store = Moneta.new(:YAML, file: @config[:tag_db])
    current_tag = store.key?(@config[:image_name]) ? store[@config[:image_name]].to_i : 0
    new_tag = current_tag + 1

    image = Docker::Image.all.detect do |i|
      i.info['RepoTags'].include?(@config[:image_name] + ':latest')
    end

    # Login to docker hub and push latest and new_tag
    Docker.authenticate! username: @config[:dockerhub][:username],
                         password: @config[:dockerhub][:password],
                         email: @config[:dockerhub][:email]

    image.tag repo: @config[:image_name], tag: new_tag, force: true
    puts "\nINFO: pushing #{@config[:image_name]}:#{new_tag} to DockerHub"
    image.push nil, tag: new_tag

    image.tag repo: @config[:image_name], tag: 'latest', force: true
    puts "INFO: pushing #{@config[:image_name]}:latest to DockerHub"
    image.push nil, tag: 'latest'

    # Store the new tag in the tag_db
    store[@config[:image_name]] = new_tag
    store.close
  end
end

.rspec_runObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/docker/spec.rb', line 121

def self.rspec_run
  set :backend, :docker

  RSpec.configure do |rc|
    rc.fail_fast = true

    rc.before(:suite) do
      DockerSpec.delete_container
      DockerSpec.start_container
    end

    rc.after(:suite) do
      DockerSpec.clean_up
      DockerSpec.push
    end
  end
  docker_tests
end

.runObject



24
25
26
27
28
29
# File 'lib/docker/spec.rb', line 24

def self.run
  load_config
  build_root if @config[:build_root]
  build_docker_image
  rspec_run
end

.start_containerObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/docker/spec.rb', line 140

def self.start_container
  # Run  the container with options
  opts = {}
  opts['HostConfig'] = { 'NetworkMode' => @config[:network_mode] } \
    unless @config[:network_mode].nil?

  opts['env'] = @config[:env] unless @config[:env].nil?
  opts['Image'] = @config[:image_name]
  opts['name'] = @config[:container_name]
  container = Docker::Container.create(opts).start

  # Check the logs, when it stops logging we can assume the container
  # is fully up and running
  log_size_ary = []
  CONTAINER_RUN_WAIT_TIMEOUT.times do
    log_size_ary << container.logs(stdout: true).size
    break if log_size_ary.last(3).sort.uniq.size == 1 &&
             log_size_ary.last(3).sort.uniq.last > 0
    sleep 1
  end
  set :docker_container, container.id
end

.to_boolean(str) ⇒ Object



190
191
192
# File 'lib/docker/spec.rb', line 190

def self.to_boolean(str)
  str == 'true' unless str.nil?
end