# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative '../../test_helper'

module NewRelic::Agent
  class CollectorHostname < Minitest::Test
    test_cases = load_cross_agent_test('collector_hostname')

    test_cases.each do |test_case|
      define_method("test_#{test_case['name']}".tr(' ', '_')) do
        # The configuration manager checks the following places to
        # determine the collector host:
        #
        # 1. Explicit configuration option (e.g., newrelic.yml)
        # 2. Implicit in a region-specific license key
        # 3. The default host
        #
        # We'll initialize those same three environments, layered on
        # top of one another.
        #
        explicit_config = {}
        explicit_config[:host] = test_case['hostname'] if test_case['hostname']
        explicit_config = ::NewRelic::Agent::Configuration::DottedHash.new(explicit_config, true)

        implicit_config = {license_key: test_case['key']}
        implicit_config = ::NewRelic::Agent::Configuration::DottedHash.new(implicit_config, true)

        default_config = ::NewRelic::Agent::Configuration::DefaultSource.new

        with_config default_config do
          with_config explicit_config do
            with_config implicit_config do
              # Bypass the cache so that we don't end up with the host from
              # the previous run of this test case.
              #
              actual_host = ::NewRelic::Agent.config.fetch(:host)

              assert_equal test_case['hostname'], actual_host
            end
          end
        end
      end
    end
  end
end
