<commit_before>require_relative '../helper'

class Elb_LogInputTest < Test::Unit::TestCase

  def setup
    Fluent::Test.setup
  end

  DEFAULT_CONFIG = {
    access_key_id: 'dummy_access_key_id',
    secret_access_key: 'dummy_secret_access_key',
    s3_endpoint: 's3.ap-northeast-1.amazonaws.com',
    s3_bucketname: 'dummy_bucket',
    s3_prefix: 'test',
    region: 'ap-northeast-1',
    timestamp_file: 'elb_last_at.dat',
    refresh_interval: 300
  }

  def parse_config(conf = {})
    ''.tap{|s| conf.each { |k, v| s << "#{k} #{v}\n" } }
  end

  def create_driver(conf = DEFAULT_CONFIG)
    Fluent::Test::Driver::Input.new(Fluent::Plugin::Elb_LogInput).configure(parse_config conf)
  end

  def iam_info_url
    'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
  end

  def use_iam_role
    stub_request(:get, iam_info_url)
      .to_return(status: [200, 'OK'], body: "hostname")
    stub_request(:get, "#{iam_info_url}hostname")
      .to_return(status: [200, 'OK'],
                 body: {
                   "AccessKeyId" => "dummy",
                   "SecretAccessKey" => "secret",
                   "Token" => "token"
                 }.to_json)
  end

  def iam_info_timeout
    stub_request(:get, iam_info_url).to_timeout
  end

  def not_use_iam_role
    stub_request(:get, iam_info_url)
      .to_return(status: [404, 'Not Found'])
  end

  def s3bucket_ok
    stub_request(:get, 'https://s3.ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 200, body: "", headers: {})
  end

  def s3bucket_not_found
    stub_request(:get, 'https://s3.ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 404, body: "", headers: {})
  end

  def test_configure_default
    s3bucket_ok<commit_msg>add endpoint case testcases<commit_after>require_relative '../helper'

class Elb_LogInputTest < Test::Unit::TestCase

  def setup
    Fluent::Test.setup
  end

  DEFAULT_CONFIG = {
    access_key_id: 'dummy_access_key_id',
    secret_access_key: 'dummy_secret_access_key',
    s3_endpoint: 's3.ap-northeast-1.amazonaws.com',
    s3_bucketname: 'dummy_bucket',
    s3_prefix: 'test',
    region: 'ap-northeast-1',
    timestamp_file: 'elb_last_at.dat',
    refresh_interval: 300
  }

  def parse_config(conf = {})
    ''.tap{|s| conf.each { |k, v| s << "#{k} #{v}\n" } }
  end

  def create_driver(conf = DEFAULT_CONFIG)
    Fluent::Test::Driver::Input.new(Fluent::Plugin::Elb_LogInput).configure(parse_config conf)
  end

  def iam_info_url
    'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
  end

  def use_iam_role
    stub_request(:get, iam_info_url)
      .to_return(status: [200, 'OK'], body: "hostname")
    stub_request(:get, "#{iam_info_url}hostname")
      .to_return(status: [200, 'OK'],
                 body: {
                   "AccessKeyId" => "dummy",
                   "SecretAccessKey" => "secret",
                   "Token" => "token"
                 }.to_json)
  end

  def iam_info_timeout
    stub_request(:get, iam_info_url).to_timeout
  end

  def not_use_iam_role
    stub_request(:get, iam_info_url)
      .to_return(status: [404, 'Not Found'])
  end

  def s3bucket_ok
    stub_request(:get, 'https://s3.ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 200, body: "", headers: {})
    stub_request(:get, 'https://s3-ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 200, body: "", headers: {})
  end

  def s3bucket_not_found
    stub_request(:get, 'https://s3.ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 404, body: "", headers: {})
    stub_request(:get, 'https://s3-ap-northeast-1.amazonaws.com/dummy_bucket?encoding-type=url&max-keys=1&prefix=test')
      .to_return(status: 404, body: "", headers: {})
  end

  def test_configure_default
    s3bucket_ok
{'fraction_non_alphanumeric': 0.1498015873015873, 'fraction_numerical': 0.025297619047619048, 'mean_word_length': 3.383695652173913, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1277533', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1482, 'n_words': 222}