Skip to content

Commit

Permalink
Refactor integration tests and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hxiong388 committed Nov 21, 2016
1 parent 48e205b commit 7ef51ef
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 259 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $ rake test:acceptance
### Integration Tests
The google-cloud-ruby integration tests are end-to-end tests that validate library functionality on real Google Cloud Platform hosting environments. The integration process deploys a test Rack-based application to Google Cloud Platform, then validates google-cloud-ruby code by making requests to this test application.
The google-cloud-ruby integration tests are end-to-end tests that validate library functionality on real Google Cloud Platform hosting environments. The integration process deploys several Rack-based applications to Google Cloud Platform one by one, then validates google-cloud-ruby code by making requests to these test applications.
All integration tests require [Cloud SDK](https://cloud.google.com/sdk/) for deployment. Following the instructions in [Authentication guide](AUTHENTICATION.md) for installation and authentication. Make sure a project ID is set:
```sh
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ gem "coveralls", "~> 0.7"
gem "yard", "~> 0.9"
gem "yard-doctest", "~> 0.1.8"
gem "gems", "~> 0.8"
gem "actionpack", ">= 4.0"
gem "railties", ">= 4.0"
gem "actionpack", "~> 4.0"
gem "railties", "~> 4.0"
gem "rack", ">= 0.1"
gem "sinatra", "~> 1.4"

Expand Down
15 changes: 12 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,19 @@ task :release, :tag do |t, args|
end
end

desc "Run all integration tests"
task :integration, :project_uri, :bundleupdate do |t, args|
bundleupdate = args[:bundleupdate]
Rake::Task["bundleupdate"].invoke if bundleupdate
sh "bundle exec rake integration:gae[#{args[:project_uri]}]"
sh "bundle exec rake integration:gke"
end

namespace :integration do
desc "Run integration:gae for all gems"
task :gae, :project_uri do |t, args|
require_relative "integration/helper"
require_relative "integration/deploy"

if executable_exists? "gcloud"
project_id = gcloud_project_id
fail "Unabled to determine project_id from gcloud SDK. Please make " \
Expand Down Expand Up @@ -720,7 +729,8 @@ namespace :integration do

desc "Run integration:gke for all gems"
task :gke do
require_relative "integration/helper"
require_relative "integration/deploy"

if executable_exists?("gcloud")&& executable_exists?("kubectl")
project_id = gcloud_project_id
fail "Unabled to determine project_id from gcloud SDK. Please make " \
Expand Down Expand Up @@ -781,7 +791,6 @@ end
# subdirectories.
def run_task_if_exists task_name, params = ""
if `bundle exec rake --tasks #{task_name}` =~ /#{task_name}[^:]/
puts "bundle exec rake #{task_name}[#{params}]"
sh "bundle exec rake #{task_name}[#{params}]"
end
end
Expand Down
2 changes: 2 additions & 0 deletions google-cloud-error_reporting/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace :integration do
ENV["TEST_GOOGLE_CLOUD_PROJECT_URI"] = args[:project_uri]

$LOAD_PATH.unshift "lib", "integration"
Dir.glob("integration/*_test.rb").each { |file| require_relative file }
Dir.glob("integration/gae/**/*_test.rb").each { |file| require_relative file }
end

Expand All @@ -69,6 +70,7 @@ namespace :integration do
ENV["TEST_GKE_POD_NAME"] = args[:pod_name]

$LOAD_PATH.unshift "lib", "integration"
Dir.glob("integration/*_test.rb").each { |file| require_relative file }
Dir.glob("integration/gke/**/*_test.rb").each { |file| require_relative file }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "minitest-autotest", "~> 1.0"
gem.add_development_dependency "minitest-focus", "~> 1.1"
gem.add_development_dependency "minitest-rg", "~> 5.2"
gem.add_development_dependency "actionpack", ">= 4.0"
gem.add_development_dependency "railties", ">= 4.0"
gem.add_development_dependency "actionpack", "~> 4.0"
gem.add_development_dependency "railties", "~> 4.0"
gem.add_development_dependency "rack", ">= 0.1"
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@
# limitations under the License.


require "minitest/autorun"
require "minitest/rg"
require "minitest/focus"
require "net/http"
require "error_reporting_helper"
require "google/cloud/error_reporting/v1beta1"


describe Google::Cloud::ErrorReporting do
let(:project_uri) { ENV['TEST_GOOGLE_CLOUD_PROJECT_URI'] }

it "submits error event to Stackdriver Error Reporting service" do
error_token = Time.now.to_i
error_reporting_uri = URI(project_uri + "/test_error_reporting")
error_reporting_uri.query="token=#{error_token}"
response = Net::HTTP.get_response error_reporting_uri
token = Time.now.to_i
response = send_request "test_error_reporting", "token=#{token}"

# TODO: Find a better way to verify response. Or even better, validate the
# error event was indeed reported to ErrorReporting
response.code.must_equal "500"
response.body.must_match error_token.to_s
response.must_match token.to_s
end
end
53 changes: 53 additions & 0 deletions google-cloud-error_reporting/integration/error_reporting_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2016 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 "minitest/autorun"
require "minitest/rg"
require "minitest/focus"
require "net/http"
require "open3"
require "json"
require_relative "../../integration/helper"


##
# Helper method to send a HTTP request to test app hosted from GCP environments.
#
# If TEST_GKE_POD_NAME environment variable is set, then we assume we're testing
# against an app hosted in GKE. Then the HTTP requestion will be made from
# inside the GKE VM instance by using "kubectl exec". If TEST_GKE_POD_NAME is
# not set, then we assume it's GAE environment, and we can make an HTTP request
# the normal way.
#
# The base URL can be provided through TEST_GOOGLE_CLOUD_PROJECT_URI environment
# variable. Otherwise default to "http://localhost:8080"
#
# return the response body
#
def send_request path, query_params = nil
base_url = ENV["TEST_GOOGLE_CLOUD_PROJECT_URI"] || "http://localhost:8080"
uri = URI.join(base_url, path)
uri.query= query_params if query_params

gke_pod_name = ENV["TEST_GKE_POD_NAME"]

if gke_pod_name
Open3.capture3("kubectl exec #{gke_pod_name} -- curl #{uri.to_s}").first
else
response = Net::HTTP.get_response uri
response.body
end
end

This file was deleted.

2 changes: 2 additions & 0 deletions google-cloud-logging/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace :integration do
ENV["TEST_GOOGLE_CLOUD_PROJECT_URI"] = args[:project_uri]

$LOAD_PATH.unshift "lib", "integration"
Dir.glob("integration/*_test.rb").each { |file| require_relative file }
Dir.glob("integration/gae/**/*_test.rb").each { |file| require_relative file }
end

Expand All @@ -117,6 +118,7 @@ namespace :integration do
ENV["TEST_GKE_POD_NAME"] = args[:pod_name]

$LOAD_PATH.unshift "lib", "integration"
Dir.glob("integration/*_test.rb").each { |file| require_relative file }
Dir.glob("integration/gke/**/*_test.rb").each { |file| require_relative file }
end
end
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-logging/google-cloud-logging.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "simplecov", "~> 0.9"
gem.add_development_dependency "yard", "~> 0.9"
gem.add_development_dependency "yard-doctest", "~> 0.1.8"
gem.add_development_dependency "railties", ">= 4.0"
gem.add_development_dependency "railties", "~> 4.0"
gem.add_development_dependency "rack", ">= 0.1"
end
45 changes: 45 additions & 0 deletions google-cloud-logging/integration/common_logging_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2016 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 "logging_helper"
require "google/cloud/logging"

describe Google::Cloud::Logging do
it "correctly setups logger" do
response = JSON.parse send_request("test_logger")

response["logger_class"].must_equal "Google::Cloud::Logging::Logger"
response["writer_class"].must_equal "Google::Cloud::Logging::AsyncWriter"
end

it "submits logs on GAE" do
token = Time.now.to_i
send_request "test_logging", "token=#{token}"

logs = []
keep_trying_till_true 60 do
stdout = Open3.capture3(
"gcloud beta logging read \"logName=projects/#{gcloud_project_id}/logs/google-cloud-ruby_integration_test " \
"AND textPayload:#{token}\" --limit 1 --format json"
).first
logs = JSON.parse stdout
logs.length == 1
end

logs.length.must_equal 1
end
end


33 changes: 14 additions & 19 deletions google-cloud-logging/integration/gae/logging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,32 @@
# limitations under the License.


require "minitest/autorun"
require "minitest/rg"
require "minitest/focus"
require "net/http"
require "open3"
require "json"
require "logging_helper"
require "google/cloud/logging"
require_relative "../../../integration/helper"

describe Google::Cloud::Logging do
let(:project_uri) { ENV["TEST_GOOGLE_CLOUD_PROJECT_URI"] }
it "Uses monitored resource with 'gae_app' type" do
response = JSON.parse send_request("test_logger")

it "submits logs on GAE with sinatra" do
token = Time.now.to_i
logging_uri = URI(project_uri + "/test_logging")
logging_uri.query="token=#{token}"
Net::HTTP.get logging_uri
response["monitored_resource"]["type"].must_equal "gae_app"
response["monitored_resource"]["labels"]["module_id"].wont_be_nil
response["monitored_resource"]["labels"]["version_id"].wont_be_nil
end

stdout = Open3.capture3("gcloud config list project").first
project_id = stdout.scan(/project = (.*)/).first.first
it "injects trace_id into each log entry" do
token = Time.now.to_i
send_request "test_logging", "token=#{token}"

logs = []
keep_trying_till_true do
keep_trying_till_true 60 do
stdout = Open3.capture3(
"gcloud beta logging read \"logName=projects/#{project_id}/logs/google-cloud-ruby_integration_test " \
"gcloud beta logging read \"logName=projects/#{gcloud_project_id}/logs/google-cloud-ruby_integration_test " \
"AND textPayload:#{token}\" --limit 1 --format json"
).first
logs = JSON.parse(stdout)
logs = JSON.parse stdout
logs.length == 1
end

logs.length.must_equal 1
logs[0]["labels"]["traceId"].wont_be_nil
end
end
35 changes: 6 additions & 29 deletions google-cloud-logging/integration/gke/logging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,15 @@
# limitations under the License.


require "minitest/autorun"
require "minitest/rg"
require "minitest/focus"
require "net/http"
require "open3"
require "json"
require "logging_helper"
require "google/cloud/logging"
require_relative "../../../integration/helper"

describe Google::Cloud::Logging do
let(:gke_pod_name) { ENV["TEST_GKE_POD_NAME"] }
it "Uses monitored resource with 'container' type" do
response = JSON.parse send_request("test_logger")

it "submits log event to Stackdriver Logging service" do
token = Time.now.to_i
logging_uri = URI("http://localhost:8080/test_logging")
logging_uri.query="token=#{token}"

stdout = Open3.capture3("gcloud config list project").first
project_id = stdout.scan(/project = (.*)/).first.first

`kubectl exec #{gke_pod_name} -- curl #{logging_uri.to_s}`

logs = []
keep_trying_till_true do
stdout = Open3.capture3(
"gcloud beta logging read \"logName=projects/#{project_id}/logs/google-cloud-ruby_integration_test " \
"AND textPayload:#{token}\" --limit 1 --format json"
).first
logs = JSON.parse(stdout)
logs.length == 1
end

logs.length.must_equal 1
response["monitored_resource"]["type"].must_equal "container"
response["monitored_resource"]["labels"]["cluster_name"].wont_be_nil
response["monitored_resource"]["labels"]["namespace_id"].wont_be_nil
end
end
Loading

0 comments on commit 7ef51ef

Please sign in to comment.