lib/gitlab/qa/component/ldap.rb in gitlab-qa-5.13.5 vs lib/gitlab/qa/component/ldap.rb in gitlab-qa-5.13.6

- old
+ new

@@ -13,20 +13,29 @@ # that looks for custom LDIF files in the BOOTSTRAP_LDIF directory. Note that the LDIF # files must have a "changetype" option specified for the script to work. module Gitlab module QA module Component - class LDAP < Base - DOCKER_IMAGE = 'osixia/openldap'.freeze - DOCKER_IMAGE_TAG = 'latest'.freeze + class LDAP + include Scenario::Actable + + LDAP_IMAGE = 'osixia/openldap'.freeze + LDAP_IMAGE_TAG = 'latest'.freeze LDAP_USER = 'tanuki'.freeze LDAP_PASSWORD = 'password'.freeze BOOTSTRAP_LDIF = '/container/service/slapd/assets/config/bootstrap/ldif/custom'.freeze FIXTURE_PATH = File.expand_path('../../../../fixtures/ldap'.freeze, __dir__) + attr_reader :docker + attr_accessor :volumes, :network, :environment + attr_writer :name + def initialize - super + @docker = Docker::Engine.new + @environment = {} + @volumes = {} + @network_aliases = [] @volumes[FIXTURE_PATH] = BOOTSTRAP_LDIF end # LDAP_TLS is true by default @@ -47,25 +56,45 @@ def password LDAP_PASSWORD end + def add_network_alias(name) + @network_aliases.push(name) + end + def name @name ||= "openldap-#{SecureRandom.hex(4)}" end + def hostname + "#{name}.#{network}" + end + def instance raise 'Please provide a block!' unless block_given? - super + prepare + start + + yield self + ensure + teardown end - # rubocop:disable Metrics/AbcSize + def prepare + @docker.pull(LDAP_IMAGE, LDAP_IMAGE_TAG) + + return if @docker.network_exists?(network) + + @docker.network_create(network) + end + def start # copy-service needed for bootstraping LDAP user: # https://github.com/osixia/docker-openldap#seed-ldap-database-with-ldif - docker.run(image, tag, '--copy-service') do |command| + docker.run(LDAP_IMAGE, LDAP_IMAGE_TAG, '--copy-service') do |command| command << '-d ' command << "--name #{name}" command << "--net #{network}" command << "--hostname #{hostname}" @@ -80,10 +109,24 @@ @network_aliases.to_a.each do |network_alias| command << "--network-alias #{network_alias}" end end end - # rubocop:enable Metrics/AbcSize + + def restart + @docker.restart(name) + end + + def teardown + raise 'Invalid instance name!' unless name + + @docker.stop(name) + @docker.remove(name) + end + + def pull + @docker.pull(LDAP_IMAGE, LDAP_IMAGE_TAG) + end def set_gitlab_credentials ::Gitlab::QA::Runtime::Env.ldap_username = username ::Gitlab::QA::Runtime::Env.ldap_password = password end