summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/ip_address.rb
blob: bec5c412a6a0afab5bbac0c83196b84bd2a6243f (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
# frozen_string_literal: true
require 'socket'

module QA
  module Runtime
    module IPAddress
      include Support::API
      HostUnreachableError = Class.new(StandardError)

      LOOPBACK_ADDRESS = '127.0.0.1'
      PUBLIC_IP_ADDRESS_API = "https://api.ipify.org"

      def fetch_current_ip_address
        # When running on CI against a live environment such as staging.gitlab.com,
        # we use the public facing IP address
        ip_address = if Env.running_in_ci? && !URI.parse(Scenario.gitlab_address).host.include?('test')
                       response = get(PUBLIC_IP_ADDRESS_API)
                       raise HostUnreachableError, "#{PUBLIC_IP_ADDRESS_API} is unreachable" unless response.code == Support::API::HTTP_STATUS_OK

                       response.body
                     elsif page.current_host.include?('localhost')
                       LOOPBACK_ADDRESS
                     else
                       Socket.ip_address_list.detect { |intf| intf.ipv4_private? }.ip_address
                     end

        QA::Runtime::Logger.info "Current IP address: #{ip_address}"

        ip_address
      end
    end
  end
end