<commit_before>require 'test_helper'

class AgentTest < Minitest::Test
  def test_agent_host_detection
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url)
    assert_equal true, ::Instana.agent.host_agent_ready?
  end

  def test_successful_discovery
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url)
    stub_request(:get, docker_url)
    discovered = ::Instana.agent.run_discovery

    assert discovered.is_a?(Hash)
    assert_equal "127.0.0.1", discovered[:agent_host]
    assert_equal 42699, discovered[:agent_port]
  end

  def test_failed_discovery
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
    stub_request(:get, docker_url).to_raise(Errno::ECONNREFUSED)
    discovered = ::Instana.agent.run_discovery

    assert_equal nil, discovered
  end

  def test_no_host_agent
    localhost_url = "http://#{::Instana::Agent::LOCALHOST}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, localhost_url).to_raise(Errno::ECONNREFUSED)
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, docker_url).to_timeout
    assert_equal false, ::Instana.agent.host_agent_ready?
  end

  def test_announce_sensor
    # Fake discovery values
    discovery = {}
    discovery[:agent_host] = ::Instana.config[:agent_host]
    discovery[:agent_port] = ::Instana.config[:agent_port]
    ::Instana.agent.instance_variable_set(:@discovered, discovery)

    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"<commit_msg>Add test to validate custom configured agent/port pair<commit_after>require 'test_helper'

class AgentTest < Minitest::Test
  def test_agent_host_detection
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url)
    assert_equal true, ::Instana.agent.host_agent_ready?
  end

  def test_successful_discovery
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url)
    stub_request(:get, docker_url)
    discovered = ::Instana.agent.run_discovery

    assert discovered.is_a?(Hash)
    assert_equal "127.0.0.1", discovered[:agent_host]
    assert_equal 42699, discovered[:agent_port]
  end

  def test_failed_discovery
    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
    stub_request(:get, docker_url).to_raise(Errno::ECONNREFUSED)
    discovered = ::Instana.agent.run_discovery

    assert_equal nil, discovered
  end

  def test_custom_configure_agent
    original_host = ::Instana.config[:agent_host]
    original_port = ::Instana.config[:agent_port]

    # Set custom agent/port
    ::Instana.config[:agent_host] = '172.16.58.3'
    ::Instana.config[:agent_port] = 12829

    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, url)
    discovered = ::Instana.agent.run_discovery

    assert discovered.is_a?(Hash)
    assert_equal "172.16.58.3", discovered[:agent_host]
    assert_equal 12829, discovered[:agent_port]

    ::Instana.config[:agent_host] = original_host
    ::Instana.config[:agent_port] = original_port
  end

  def test_no_host_agent
    localhost_url = "http://#{::Instana::Agent::LOCALHOST}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, localhost_url).to_raise(Errno::ECONNREFUSED)
    docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
    stub_request(:get, docker_url).to_timeout
    assert_equal false, ::Instana.agent.host_agent_ready?
  end

  def test_announce_sensor
    # Fake discovery values
    discovery = {}
    discovery[:agent_host] = ::Instana.config[:agent_host]
    discovery[:agent_port] = ::Instana.config[:agent_port]
    ::Instana.agent.instance_variable_set(:@discovered, discovery)

    url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"
{'fraction_non_alphanumeric': 0.1656506201387429, 'fraction_numerical': 0.010090393104898045, 'mean_word_length': 5.517808219178082, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067755', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1648, 'n_words': 198}