Class: Bidi2pdf::TestHelpers::Testcontainers::ChromedriverContainer

Inherits:
Testcontainers::DockerContainer
  • Object
show all
Defined in:
lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb

Constant Summary collapse

DEFAULT_CHROMEDRIVER_PORT =
3000
DEFAULT_IMAGE =
"dieters877565/chromedriver"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image = DEFAULT_IMAGE, **options) ⇒ ChromedriverContainer

Returns a new instance of ChromedriverContainer.



12
13
14
15
16
17
18
19
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 12

def initialize(image = DEFAULT_IMAGE, **options)
  @docker_file = options.delete(:docker_file) || "Dockerfile"
  @build_dir = options.delete(:build_dir) || options[:working_dir]

  super

  @wait_for ||= add_wait_for(:logs, /ChromeDriver was started successfully on port/)
end

Instance Attribute Details

#build_dirObject (readonly)

Returns the value of attribute build_dir.



10
11
12
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 10

def build_dir
  @build_dir
end

#docker_fileObject (readonly)

Returns the value of attribute docker_file.



10
11
12
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 10

def docker_file
  @docker_file
end

Instance Method Details

#build_local_imageObject

rubocop: disable Metrics/AbcSize



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 31

def build_local_image
  old_timeout = Docker.options[:read_timeout]
  Docker.options[:read_timeout] = 60 * 10

  Docker::Image.build_from_dir(build_dir, { "t" => image, "dockerfile" => docker_file }) do |lines|
    lines.split("\n").each do |line|
      next unless (log = JSON.parse(line)) && log.key?("stream")
      next unless log["stream"] && !(trimmed_stream = log["stream"].strip).empty?

      timestamp = Time.now.strftime("[%Y-%m-%dT%H:%M:%S.%6N]")
      $stdout.write "#{timestamp} #{trimmed_stream}\n"
    end
  end

  Docker.options[:read_timeout] = old_timeout
end

#portObject



26
27
28
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 26

def port
  DEFAULT_CHROMEDRIVER_PORT
end

#session_url(protocol: "http") ⇒ Object

rubocop: enable Metrics/AbcSize



75
76
77
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 75

def session_url(protocol: "http")
  "#{protocol}://#{host}:#{mapped_port(port)}/session"
end

#startObject



21
22
23
24
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 21

def start
  with_exposed_ports(port)
  super
end

#start_local_imageObject

rubocop: disable Metrics/AbcSize



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb', line 51

def start_local_image
  build_local_image

  with_exposed_ports(port)

  @_container ||= Docker::Container.create(_container_create_options)
  @_container.start

  @_id = @_container.id
  json = @_container.json
  @name = json["Name"]
  @_created_at = json["Created"]

  @wait_for&.call(self)

  self
rescue Docker::Error::NotFoundError => e
  raise Testcontainers::NotFoundError, e.message
rescue Excon::Error::Socket => e
  raise Testcontainers::ConnectionError, e.message
end