Sha256: e16184d9540d554f30aeabdbf2cdb9fb6b19efef323a0e0a59a24504c9130b54

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

module Gitlab
  module QA
    module Component
      class PostgreSQL
        include Scenario::Actable

        POSTGRES_IMAGE = 'postgres'.freeze
        POSTGRES_IMAGE_TAG = '11'.freeze

        attr_reader :docker
        attr_accessor :environment, :network
        attr_writer :name

        def initialize
          @docker = Docker::Engine.new
          @environment = {}
        end

        def name
          @name ||= "postgres"
        end

        def instance
          prepare
          start
          wait_until_ready
          yield self
        ensure
          teardown
        end

        def prepare
          @docker.pull(POSTGRES_IMAGE, POSTGRES_IMAGE_TAG)
          return if @docker.network_exists?(network)

          @docker.network_create(network)
        end

        def start
          @docker.run(POSTGRES_IMAGE, POSTGRES_IMAGE_TAG) do |command|
            command << "-d"
            command << "--name #{name}"
            command << "--net #{network}"

            command.env("POSTGRES_PASSWORD", "SQL_PASSWORD")
          end
        end

        def teardown
          @docker.stop(name)
          @docker.remove(name)
        end

        def run_psql(command)
          @docker.exec(name, %(psql -U postgres #{command}))
        end

        private

        def wait_until_ready
          start = Time.now
          begin
            run_psql 'template1'
          rescue StandardError
            retry if Time.now - start < 30
            raise
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gitlab-qa-5.13.6 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.13.3 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.13.2 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.13.1 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.13.0 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.12.0 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.10.1 lib/gitlab/qa/component/postgresql.rb
gitlab-qa-5.10.0 lib/gitlab/qa/component/postgresql.rb