summaryrefslogtreecommitdiff
path: root/lib/e2e/docker.rb
blob: 4ae530af43813602f40258aeb35785f94360a317 (plain)
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

class Docker
  DEFAULT_ENV = {
    'CI_DEBUG_TRACE' => 'true',
    'CI_PROJECT_DIR' => '/tmp/app',
    'SECURE_LOG_LEVEL' => 'debug'
  }.freeze
  attr_reader :pwd

  def initialize(pwd: Pathname.pwd)
    @pwd = pwd
  end

  def build(tag:)
    Dir.chdir pwd do
      env = { 'DOCKER_BUILDKIT' => '1' }
      system(env, 'docker', 'build', "--network=host", "-t", tag, ".", exception: true)
    end
  end

  def run(
    image:,
    project_path: Pathname.pwd,
    env: {},
    debug: ENV.fetch('DEBUG', 'false') == 'true',
    command: '/analyzer run'
  )
    env_options = DEFAULT_ENV.merge(env).map { |(key, value)| "--env #{key}='#{value}'" }
    Dir.chdir pwd do
      arguments = [
        :docker, :run, '--rm',
        "--add-host=maven.test:127.0.0.1",
        "--entrypoint=/bin/sh",
        "--network=host",
        "--volume=#{project_path}:/tmp/app",
        "--workdir=/tmp/app",
        env_options
      ]
      arguments.push("--add-host=clair-vulnerabilities-db:127.0.0.1") unless ENV['CI']
      arguments.push(debug ? ["-it", image] : [image])
      arguments.push("-c '#{command}'")
      system(expand(arguments), exception: true)
    end
  end

  private

  def expand(command)
    command.flatten.map(&:to_s).join(' ')
  end
end