lib/gitlab/qa/scenario/test/integration/ai_gateway.rb



# frozen_string_literal: true

module Gitlab
  module QA
    module Scenario
      module Test
        module Integration
          class AiGateway < Scenario::Template
            SETUP_SRC_PATH = File.expand_path('../../../../../../support/setup', __dir__)
            SETUP_DEST_PATH = '/tmp/setup-scripts'

            def initialize
              @network = 'test'
              @ai_gateway_name = 'ai-gateway'
              @ai_gateway_hostname = "#{@ai_gateway_name}.#{@network}"
              @ai_gateway_port = 5000
            end

            def perform(release, *rspec_args)
              Component::Gitlab.perform do |gitlab|
                setup_gitlab(gitlab, release)

                Component::AiGateway.perform do |ai_gateway|
                  setup_ai_gateway(ai_gateway, gitlab_hostname: gitlab.hostname)

                  ai_gateway.instance do
                    gitlab.instance do
                      setup_code_suggestions(gitlab)
                      run_specs(gitlab, *rspec_args)
                    end
                  end
                end
              end
            end

            private

            def setup_gitlab(gitlab, release)
              gitlab.release = QA::Release.new(release)
              gitlab.name = 'gitlab'
              gitlab.network = @network

              gitlab.omnibus_gitlab_rails_env['CODE_SUGGESTIONS_BASE_URL'] = "http://#{@ai_gateway_hostname}:#{@ai_gateway_port}"

              # Determines whether to use the SyncServiceTokenWorker or SyncSeatLinkWorker
              # for generating a code suggestions access token during license activation
              gitlab.omnibus_gitlab_rails_env['QA_USE_SYNC_SERVICE_TOKEN_WORKER'] = Runtime::Env.use_sync_service_token_worker?

              gitlab.set_ee_activation_code
            end

            def setup_ai_gateway(ai_gateway, gitlab_hostname:)
              ai_gateway.name = @ai_gateway_name
              ai_gateway.network = @network
              ai_gateway.ports = [@ai_gateway_port]

              ai_gateway.configure_environment(gitlab_hostname: gitlab_hostname)
            end

            def setup_code_suggestions(gitlab)
              Runtime::Logger.info('Setting up Code Suggestions on GitLab instance')

              gitlab.docker.copy(gitlab.name, SETUP_SRC_PATH, SETUP_DEST_PATH)

              gitlab.docker.exec(
                gitlab.name,
                "gitlab-rails runner #{SETUP_DEST_PATH}/code_suggestions_setup.rb",
                mask_secrets: gitlab.secrets
              )
            end

            def run_specs(gitlab, *rspec_args)
              Runtime::Logger.info('Running AI Gateway specs!')

              rspec_args << "--" unless rspec_args.include?('--')
              rspec_args << %w[--tag ai_gateway]

              Component::Specs.perform do |specs|
                specs.suite = 'Test::Instance::All'
                specs.release = gitlab.release
                specs.network = gitlab.network
                specs.args = [gitlab.address, *rspec_args]
              end
            end
          end
        end
      end
    end
  end
end