summaryrefslogtreecommitdiff
path: root/qa/qa/service/docker_run/smocker.rb
blob: b48f8aa84e369263411938c5c9ec2463a624123a (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module QA
  module Service
    module DockerRun
      class Smocker < Base
        def initialize
          @image = 'thiht/smocker:0.17.1'
          @name = 'smocker-server'
          @public_port = 8080
          @admin_port = 8081
          super
          @network_cache = network
        end

        # @param wait [Integer] seconds to wait for server
        # @yieldparam [SmockerApi] the api object ready for interaction
        def self.init(wait: 10)
          if @container.nil?
            @container = new
            @container.register!
            @container.wait_for_running

            @api = Vendor::Smocker::SmockerApi.new(
              host: @container.host_name,
              public_port: @container.public_port,
              admin_port: @container.admin_port
            )
            @api.wait_for_ready(wait: wait)
          end

          yield @api
        end

        def self.teardown!
          @container&.remove!
          @container = nil
          @api = nil
        end

        attr_reader :public_port, :admin_port

        def host_name
          return '127.0.0.1' unless QA::Runtime::Env.running_in_ci? || QA::Runtime::Env.qa_hostname

          "#{@name}.#{@network_cache}"
        end

        def wait_for_running
          Support::Waiter.wait_until(raise_on_failure: false, reload_page: false) do
            running?
          end
        end

        def register!
          command = <<~CMD.tr("\n", ' ')
            docker run -d --rm
            --network #{@network_cache}
            --hostname #{host_name}
            --name #{@name}
            --publish #{@public_port}:8080
            --publish #{@admin_port}:8081
            #{@image}
          CMD

          unless QA::Runtime::Env.running_in_ci? || QA::Runtime::Env.qa_hostname
            command.gsub!("--network #{@network_cache} ", '')
          end

          shell command
        end
      end
    end
  end
end