<commit_before># Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'helper'
require 'json'
require 'webmock/test_unit'

class GoogleCloudOutputTest < Test::Unit::TestCase
  def setup
    Fluent::Test.setup

    # Setup stubs used for authentication.
    ENV.delete('GOOGLE_APPLICATION_CREDENTIALS')
    setup_auth_stubs

    # Create stubs for all the GCE metadata lookups the agent needs to make.
    stub_metadata_request('project/project-id', PROJECT_ID)
    stub_metadata_request('instance/zone', FULLY_QUALIFIED_ZONE)
    stub_metadata_request('instance/id', VM_ID)
    stub_metadata_request('instance/attributes/',
                          "attribute1\nattribute2\nattribute3")
    @logs_sent = []
  end

  def setup_logging_stubs
    [COMPUTE_PARAMS, VMENGINE_PARAMS].each do |params|
      stub_request(:post, uri_for_log(params)).to_return do |request|
        @logs_sent << JSON.parse(request.body)
        {:body => ''}
      end
    end
  end

  PROJECT_ID = 'test-project-id'
  ZONE = 'us-central1-b'
  FULLY_QUALIFIED_ZONE = 'projects/' + PROJECT_ID + '/zones/' + ZONE
  VM_ID = '9876543210'

  CUSTOM_PROJECT_ID = 'test-custom-project-id'
  CUSTOM_ZONE = 'us-custom-central1-b'
  CUSTOM_FULLY_QUALIFIED_ZONE = 'projects/' + PROJECT_ID + '/zones/' + ZONE
  CUSTOM_VM_ID = 'C9876543210'

  MANAGED_VM_BACKEND_NAME = 'default'
  MANAGED_VM_BACKEND_VERSION = 'guestbook2.0'

  AUTH_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
  FAKE_AUTH_TOKEN = 'abc123'

  APPLICATION_DEFAULT_CONFIG = %[
  ]

  PRIVATE_KEY_CONFIG = %[
    auth_method private_key
    private_key_email 271661262351-ft99kc9kjro9rrihq3k2n3s2inbplu0q@developer.gserviceaccount.com
    private_key_path test/plugin/data/c31e573fd7f62ed495c9ca3821a5a85cb036dee1-privatekey.p12
  ]

  CUSTOM_METADATA_CONFIG = %[
    fetch_gce_metadata false
    project_id #{CUSTOM_PROJECT_ID}
    zone #{CUSTOM_ZONE}
    vm_id #{CUSTOM_VM_ID}
  ]

  INVALID_CONFIG_MISSING_PRIVATE_KEY_PATH = %[
    auth_method private_key
    private_key_email <EMAIL>
  ]
  INVALID_CONFIG_MISSING_PRIVATE_KEY_EMAIL = %[
    auth_method private_key
    private_key_path /fake/path/to/key
  ]
  INVALID_CONFIG_MISSING_METADATA_VM_ID = %[
    fetch_gce_metadata false
    project_id #{CUSTOM_PROJECT_ID}
    zone #{CUSTOM_ZONE}
  ]

  COMPUTE_SERVICE_NAME = 'compute.googleapis.com'
  APPENGINE_SERVICE_NAME = 'appengine.googleapis.com'

  COMPUTE_PARAMS = {
    'service_name' => COMPUTE_SERVICE_NAME,
    'log_name' => 'test',
    'labels' => {
      "#{COMPUTE_SERVICE_NAME}/resource_type" => 'instance',
      "#{COMPUTE_SERVICE_NAME}/resource_id" => VM_ID
    }
  }

  VMENGINE_PARAMS = {
    'service_name' => APPENGINE_SERVICE_NAME,
    'log_name' => "#{APPENGINE_SERVICE_NAME}%2Ftest",
    'labels' => {
      "#{APPENGINE_SERVICE_NAME}/module_id" => MANAGED_VM_BACKEND_NAME,
      "#{APPENGINE_SERVICE_NAME}/version_id" => MANAGED_VM_BACKEND_VERSION,
      "#{COMPUTE_SERVICE_NAME}/resource_type" => 'instance',
      "#{COMPUTE_SERVICE_NAME}/resource_id" => VM_ID
    }
  }

  def create_driver(conf=APPLICATION_DEFAULT_CONFIG)
    Fluent::Test::BufferedOutputTestDriver.new(
        Fluent::GoogleCloudOutput).configure(conf)
  end

  def test_configure_service_account_application_default
    d = create_driver(APPLICATION_DEFAULT_CONFIG)
    assert d.instance.auth_method.nil?
  end

  def test_configure_service_account_private_key
    d = create_driver(PRIVATE_KEY_CONFIG)
    assert_equal 'private_key', d.instance.auth_method
  end

  def test_configure_custom_metadata
    d = create_driver(CUSTOM_METADATA_CONFIG)
    assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
    assert_equal CUSTOM_ZONE, d.instance.zone
    assert_equal CUSTOM_VM_ID, d.instance.vm_id
  end

  def test_configure_invalid_configs
    begin
      d = create_driver(INVALID_CONFIG_MISSING_PRIVATE_KEY_PATH)
      assert false
    rescue Fluent::ConfigError => error
      assert error.message.include? 'private_key_path'
    end
    begin
      d = create_driver(INVALID_CONFIG_MISSING_PRIVATE_KEY_EMAIL)
      assert false
    rescue Fluent::ConfigError => error
      assert error.message.include? 'private_key_email'
    end
    begin
      d = create_driver(INVALID_CONFIG_MISSING_METADATA_VM_ID)
      assert false
    rescue Fluent::ConfigError => error
      assert error.message.include? 'fetch_gce_metadata'
    end
  end

  def test_metadata_loading
    d = create_driver()
    d.run
    assert_equal PROJECT_ID, d.instance.project_id
    assert_equal ZONE, d.instance.zone
    assert_equal VM_ID, d.instance.vm_id
    assert_equal false, d.instance.running_on_managed_vm
  end

  def test_managed_vm_metadata_loading
    setup_managed_vm_metadata_stubs
    d = create_driver()
    d.run
    assert_equal PROJECT_ID, d.instance.project_id
    assert_equal ZONE, d.instance.zone
    assert_equal VM_ID, d.instance.vm_id
    assert_equal true, d.instance.running_on_managed_vm
    assert_equal MANAGED_VM_BACKEND_NAME, d.instance.gae_backend_name
    assert_equal MANAGED_VM_BACKEND_VERSION, d.instance.gae_backend_version
  end

  def test_gce_metadata_does_not_load_when_fetch_gce_metadata_is_false
    d = create_driver(CUSTOM_METADATA_CONFIG)
    d.run
    assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
    assert_equal CUSTOM_ZONE, d.instance.zone
    assert_equal CUSTOM_VM_ID, d.instance.vm_id
    assert_equal false, d.instance.running_on_managed_vm<commit_msg>Make exception checking in the test consistent.<commit_after># Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'helper'
require 'json'
require 'webmock/test_unit'

class GoogleCloudOutputTest < Test::Unit::TestCase
  def setup
    Fluent::Test.setup

    # Setup stubs used for authentication.
    ENV.delete('GOOGLE_APPLICATION_CREDENTIALS')
    setup_auth_stubs

    # Create stubs for all the GCE metadata lookups the agent needs to make.
    stub_metadata_request('project/project-id', PROJECT_ID)
    stub_metadata_request('instance/zone', FULLY_QUALIFIED_ZONE)
    stub_metadata_request('instance/id', VM_ID)
    stub_metadata_request('instance/attributes/',
                          "attribute1\nattribute2\nattribute3")
    @logs_sent = []
  end

  def setup_logging_stubs
    [COMPUTE_PARAMS, VMENGINE_PARAMS].each do |params|
      stub_request(:post, uri_for_log(params)).to_return do |request|
        @logs_sent << JSON.parse(request.body)
        {:body => ''}
      end
    end
  end

  PROJECT_ID = 'test-project-id'
  ZONE = 'us-central1-b'
  FULLY_QUALIFIED_ZONE = 'projects/' + PROJECT_ID + '/zones/' + ZONE
  VM_ID = '9876543210'

  CUSTOM_PROJECT_ID = 'test-custom-project-id'
  CUSTOM_ZONE = 'us-custom-central1-b'
  CUSTOM_FULLY_QUALIFIED_ZONE = 'projects/' + PROJECT_ID + '/zones/' + ZONE
  CUSTOM_VM_ID = 'C9876543210'

  MANAGED_VM_BACKEND_NAME = 'default'
  MANAGED_VM_BACKEND_VERSION = 'guestbook2.0'

  AUTH_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
  FAKE_AUTH_TOKEN = 'abc123'

  APPLICATION_DEFAULT_CONFIG = %[
  ]

  PRIVATE_KEY_CONFIG = %[
    auth_method private_key
    private_key_email 271661262351-ft99kc9kjro9rrihq3k2n3s2inbplu0q@developer.gserviceaccount.com
    private_key_path test/plugin/data/c31e573fd7f62ed495c9ca3821a5a85cb036dee1-privatekey.p12
  ]

  CUSTOM_METADATA_CONFIG = %[
    fetch_gce_metadata false
    project_id #{CUSTOM_PROJECT_ID}
    zone #{CUSTOM_ZONE}
    vm_id #{CUSTOM_VM_ID}
  ]

  INVALID_CONFIG_MISSING_PRIVATE_KEY_PATH = %[
    auth_method private_key
    private_key_email <EMAIL>
  ]
  INVALID_CONFIG_MISSING_PRIVATE_KEY_EMAIL = %[
    auth_method private_key
    private_key_path /fake/path/to/key
  ]
  INVALID_CONFIG_MISSING_METADATA_VM_ID = %[
    fetch_gce_metadata false
    project_id #{CUSTOM_PROJECT_ID}
    zone #{CUSTOM_ZONE}
  ]

  COMPUTE_SERVICE_NAME = 'compute.googleapis.com'
  APPENGINE_SERVICE_NAME = 'appengine.googleapis.com'

  COMPUTE_PARAMS = {
    'service_name' => COMPUTE_SERVICE_NAME,
    'log_name' => 'test',
    'labels' => {
      "#{COMPUTE_SERVICE_NAME}/resource_type" => 'instance',
      "#{COMPUTE_SERVICE_NAME}/resource_id" => VM_ID
    }
  }

  VMENGINE_PARAMS = {
    'service_name' => APPENGINE_SERVICE_NAME,
    'log_name' => "#{APPENGINE_SERVICE_NAME}%2Ftest",
    'labels' => {
      "#{APPENGINE_SERVICE_NAME}/module_id" => MANAGED_VM_BACKEND_NAME,
      "#{APPENGINE_SERVICE_NAME}/version_id" => MANAGED_VM_BACKEND_VERSION,
      "#{COMPUTE_SERVICE_NAME}/resource_type" => 'instance',
      "#{COMPUTE_SERVICE_NAME}/resource_id" => VM_ID
    }
  }

  def create_driver(conf=APPLICATION_DEFAULT_CONFIG)
    Fluent::Test::BufferedOutputTestDriver.new(
        Fluent::GoogleCloudOutput).configure(conf)
  end

  def test_configure_service_account_application_default
    d = create_driver(APPLICATION_DEFAULT_CONFIG)
    assert d.instance.auth_method.nil?
  end

  def test_configure_service_account_private_key
    d = create_driver(PRIVATE_KEY_CONFIG)
    assert_equal 'private_key', d.instance.auth_method
  end

  def test_configure_custom_metadata
    d = create_driver(CUSTOM_METADATA_CONFIG)
    assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
    assert_equal CUSTOM_ZONE, d.instance.zone
    assert_equal CUSTOM_VM_ID, d.instance.vm_id
  end

  def test_configure_invalid_configs
    exception_count = 0
    begin
      d = create_driver(INVALID_CONFIG_MISSING_PRIVATE_KEY_PATH)
    rescue Fluent::ConfigError => error
      assert error.message.include? 'private_key_path'
      exception_count += 1
    end
    assert_equal 1, exception_count

    exception_count = 0
    begin
      d = create_driver(INVALID_CONFIG_MISSING_PRIVATE_KEY_EMAIL)
    rescue Fluent::ConfigError => error
      assert error.message.include? 'private_key_email'
      exception_count += 1
    end
    assert_equal 1, exception_count

    exception_count = 0
    begin
      d = create_driver(INVALID_CONFIG_MISSING_METADATA_VM_ID)
    rescue Fluent::ConfigError => error
      assert error.message.include? 'fetch_gce_metadata'
      exception_count += 1
    end
    assert_equal 1, exception_count
  end

  def test_metadata_loading
    d = create_driver()
    d.run
    assert_equal PROJECT_ID, d.instance.project_id
    assert_equal ZONE, d.instance.zone
    assert_equal VM_ID, d.instance.vm_id
    assert_equal false, d.instance.running_on_managed_vm
  end

  def test_managed_vm_metadata_loading
    setup_managed_vm_metadata_stubs
    d = create_driver()
    d.run
    assert_equal PROJECT_ID, d.instance.project_id
    assert_equal ZONE, d.instance.zone
    assert_equal VM_ID, d.instance.vm_id
    assert_equal true, d.instance.running_on_managed_vm
    assert_equal MANAGED_VM_BACKEND_NAME, d.instance.gae_backend_name
    assert_equal MANAGED_VM_BACKEND_VERSION, d.instance.gae_backend_version
  end

  def test_gce_metadata_does_not_load_when_fetch_gce_metadata_is_false
    d = create_driver(CUSTOM_METADATA_CONFIG)
    d.run
    assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
    assert_equal CUSTOM_ZONE, d.instance.zone
    assert_equal CUSTOM_VM_ID, d.instance.vm_id
    assert_equal false, d.instance.running_on_managed_vm
{'fraction_non_alphanumeric': 0.08237892670157068, 'fraction_numerical': 0.014643324607329844, 'mean_word_length': 4.182280627384485, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4652686', 'n_tokens_mistral': 4432, 'n_tokens_neox': 4274, 'n_words': 758}