From 2c7ddd871a6633bb73289c95003c2278ef74a2ac Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Tue, 2 Aug 2016 13:04:38 -0600 Subject: [PATCH] Add coverage to service factory methods Rename Backoff to GrpcBackoff, since this is now private. Fix calls to Core::GCE. --- .../test/google/cloud/bigquery_test.rb | 133 +++++++++++------ .../lib/google/cloud/core/backoff.rb | 77 ++-------- .../lib/google/cloud/core/grpc_backoff.rb | 89 ++++++++++++ .../lib/google/cloud/datastore/dataset.rb | 2 +- .../lib/google/cloud/datastore/service.rb | 4 +- .../test/google/cloud/datastore_test.rb | 133 +++++++++++------ .../lib/google/cloud/dns/project.rb | 2 +- .../test/google/cloud/dns_test.rb | 133 +++++++++++------ .../lib/google/cloud/logging/project.rb | 2 +- .../lib/google/cloud/logging/service.rb | 4 +- .../test/google/cloud/logging/backoff_test.rb | 4 +- .../test/google/cloud/logging_test.rb | 133 +++++++++++------ .../lib/google/cloud/pubsub/project.rb | 2 +- .../lib/google/cloud/pubsub/service.rb | 4 +- .../test/google/cloud/pubsub/backoff_test.rb | 4 +- .../test/google/cloud/pubsub_test.rb | 133 +++++++++++------ .../google/cloud/resource_manager_test.rb | 124 +++++++++++----- .../lib/google/cloud/storage/project.rb | 2 +- .../lib/google/cloud/storage/service.rb | 2 +- .../test/google/cloud/storage_test.rb | 135 ++++++++++++------ .../test/google/cloud/translate_test.rb | 114 ++++++++++----- .../lib/google/cloud/vision/project.rb | 2 +- .../test/google/cloud/vision_test.rb | 134 +++++++++++------ 23 files changed, 934 insertions(+), 438 deletions(-) create mode 100644 google-cloud-core/lib/google/cloud/core/grpc_backoff.rb diff --git a/google-cloud-bigquery/test/google/cloud/bigquery_test.rb b/google-cloud-bigquery/test/google/cloud/bigquery_test.rb index 79ca6a8ca96a..08df281d5a59 100644 --- a/google-cloud-bigquery/test/google/cloud/bigquery_test.rb +++ b/google-cloud-bigquery/test/google/cloud/bigquery_test.rb @@ -16,51 +16,104 @@ require "google/cloud/bigquery" describe Google::Cloud do - it "calls out to Google::Cloud.bigquery" do - gcloud = Google::Cloud.new - stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "bigquery-project-object-empty" - } - Google::Cloud.stub :bigquery, stubbed_bigquery do - project = gcloud.bigquery - project.must_equal "bigquery-project-object-empty" + describe "#bigquery" do + it "calls out to Google::Cloud.bigquery" do + gcloud = Google::Cloud.new + stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "bigquery-project-object-empty" + } + Google::Cloud.stub :bigquery, stubbed_bigquery do + project = gcloud.bigquery + project.must_equal "bigquery-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.bigquery" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "bigquery-project-object" + } + Google::Cloud.stub :bigquery, stubbed_bigquery do + project = gcloud.bigquery + project.must_equal "bigquery-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.bigquery" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "bigquery-project-object" - } - Google::Cloud.stub :bigquery, stubbed_bigquery do - project = gcloud.bigquery - project.must_equal "bigquery-project-object" + it "passes project and keyfile and options to Google::Cloud.bigquery" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "bigquery-project-object-scoped" + } + Google::Cloud.stub :bigquery, stubbed_bigquery do + project = gcloud.bigquery scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "bigquery-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.bigquery" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_bigquery = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "bigquery-project-object-scoped" - } - Google::Cloud.stub :bigquery, stubbed_bigquery do - project = gcloud.bigquery scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "bigquery-project-object-scoped" + describe ".bigquery" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Bigquery::Credentials.stub :default, default_credentials do + bigquery = Google::Cloud.bigquery + bigquery.must_be_kind_of Google::Cloud::Bigquery::Project + bigquery.project.must_equal "project-id" + bigquery.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "bigquery-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "bigquery-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Bigquery::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Bigquery::Service.stub :new, stubbed_service do + bigquery = Google::Cloud.bigquery "project-id", "path/to/keyfile.json" + bigquery.must_be_kind_of Google::Cloud::Bigquery::Project + bigquery.project.must_equal "project-id" + bigquery.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-core/lib/google/cloud/core/backoff.rb b/google-cloud-core/lib/google/cloud/core/backoff.rb index 6441cf9ecb0d..bf10c94090c3 100644 --- a/google-cloud-core/lib/google/cloud/core/backoff.rb +++ b/google-cloud-core/lib/google/cloud/core/backoff.rb @@ -25,7 +25,7 @@ module Core # increasing delay will be added between each retried call. The first # retry will be delayed one second, the second retry will be delayed # two seconds, and so on. - class Backoff + class GrpcBackoff class << self ## # The number of times a retriable API call should be retried. @@ -42,19 +42,6 @@ def retries= new_retries # The default values are `14`. attr_accessor :grpc_codes - ## - # The HTTP Status Codes that should be retried. - # - # The default values are `500` and `503`. - attr_accessor :http_codes - - ## - # The Google API error reasons that should be retried. - # - # The default values are `rateLimitExceeded` and - # `userRateLimitExceeded`. - attr_accessor :reasons - ## # The code to run when a backoff is handled. # This must be a Proc and must take the number of @@ -66,42 +53,24 @@ def retries= new_retries # Set the default values self.retries = 3 self.grpc_codes = [14] - self.http_codes = [500, 503] - self.reasons = %w(rateLimitExceeded userRateLimitExceeded) self.backoff = ->(retries) { sleep retries.to_i } ## # @private - # Creates a new Backoff object to catch common errors when calling + # Creates a new GrpcBackoff object to catch common errors when calling # the Google API and handle the error by retrying the call. # - # Google::Cloud::Backoff.new(options).execute_gapi do - # client.execute api_method: service.things.insert, - # parameters: { thing: @thing }, - # body_object: { name: thing_name } + # Google::Cloud::Core::GrpcBackoff.new(options).execute do + # datastore.lookup lookup_req # end def initialize options = {} - @retries = (options[:retries] || Backoff.retries).to_i - @grpc_codes = (options[:grpc_codes] || Backoff.grpc_codes).to_a - @http_codes = (options[:http_codes] || Backoff.http_codes).to_a - @reasons = (options[:reasons] || Backoff.reasons).to_a - @backoff = options[:backoff] || Backoff.backoff + @retries = (options[:retries] || GrpcBackoff.retries).to_i + @grpc_codes = (options[:grpc_codes] || GrpcBackoff.grpc_codes).to_a + @backoff = options[:backoff] || GrpcBackoff.backoff end # @private - def execute_gapi - current_retries = 0 - loop do - result = yield - return result unless result.is_a? Google::APIClient::Result - break result if result.success? || !retry?(result, current_retries) - current_retries += 1 - @backoff.call current_retries - end - end - - # @private - def execute_grpc + def execute current_retries = 0 loop do begin @@ -114,36 +83,6 @@ def execute_grpc end end end - - protected - - # @private - def retry? result, current_retries #:nodoc: - if current_retries < @retries - return true if retry_http_code? result - return true if retry_error_reason? result - end - false - end - - # @private - def retry_http_code? result #:nodoc: - @http_codes.include? result.response.status - end - - # @private - def retry_error_reason? result - if result.data && - result.data["error"] && - result.data["error"]["errors"] - Array(result.data["error"]["errors"]).each do |error| - if error["reason"] && @reasons.include?(error["reason"]) - return true - end - end - end - false - end end end end diff --git a/google-cloud-core/lib/google/cloud/core/grpc_backoff.rb b/google-cloud-core/lib/google/cloud/core/grpc_backoff.rb new file mode 100644 index 000000000000..9e849552397c --- /dev/null +++ b/google-cloud-core/lib/google/cloud/core/grpc_backoff.rb @@ -0,0 +1,89 @@ +# 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. + + +module Google + module Cloud + module Core + ## + # @private + # Backoff allows users to control how Google API calls are retried. + # If an API call fails the response will be checked to see if the + # call can be retried. If the response matches the criteria, then it + # will be retried with an incremental backoff. This means that an + # increasing delay will be added between each retried call. The first + # retry will be delayed one second, the second retry will be delayed + # two seconds, and so on. + class GrpcBackoff + class << self + ## + # The number of times a retriable API call should be retried. + # + # The default value is `3`. + attr_reader :retries + def retries= new_retries + @retries = new_retries + end + + ## + # The GRPC Status Codes that should be retried. + # + # The default values are `14`. + attr_accessor :grpc_codes + + ## + # The code to run when a backoff is handled. + # This must be a Proc and must take the number of + # retries as an argument. + # + # Note: This method is undocumented and may change. + attr_accessor :backoff # :nodoc: + end + # Set the default values + self.retries = 3 + self.grpc_codes = [14] + self.backoff = ->(retries) { sleep retries.to_i } + + ## + # @private + # Creates a new Backoff object to catch common errors when calling + # the Google API and handle the error by retrying the call. + # + # Google::Cloud::Core::GrpcBackoff.new(options).execute do + # datastore.lookup lookup_ref + # end + def initialize options = {} + @retries = (options[:retries] || GrpcBackoff.retries).to_i + @grpc_codes = (options[:grpc_codes] || GrpcBackoff.grpc_codes).to_a + @backoff = options[:backoff] || GrpcBackoff.backoff + end + + # @private + def execute + current_retries = 0 + loop do + begin + return yield + rescue GRPC::BadStatus => e + raise e unless @grpc_codes.include?(e.code) && + (current_retries < @retries) + current_retries += 1 + @backoff.call current_retries + end + end + end + end + end + end +end diff --git a/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb b/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb index e3790e40ccff..6c5c79ec2a24 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb @@ -89,7 +89,7 @@ def self.default_project ENV["DATASTORE_PROJECT"] || ENV["GCLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-datastore/lib/google/cloud/datastore/service.rb b/google-cloud-datastore/lib/google/cloud/datastore/service.rb index f0d46502d55b..7b291caa1a48 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/service.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/service.rb @@ -15,7 +15,7 @@ require "google/cloud/datastore/credentials" require "google/datastore/v1beta3/datastore_services" -require "google/cloud/core/backoff" +require "google/cloud/core/grpc_backoff" module Google module Cloud @@ -143,7 +143,7 @@ def inspect ## # Performs backoff and error handling def execute - Google::Cloud::Core::Backoff.new(retries: retries).execute_grpc do + Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do yield end rescue GRPC::BadStatus => e diff --git a/google-cloud-datastore/test/google/cloud/datastore_test.rb b/google-cloud-datastore/test/google/cloud/datastore_test.rb index 4cffcccdf2af..20deffe2771a 100644 --- a/google-cloud-datastore/test/google/cloud/datastore_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore_test.rb @@ -16,51 +16,104 @@ require "google/cloud/datastore" describe Google::Cloud do - it "calls out to Google::Cloud.datastore" do - gcloud = Google::Cloud.new - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "datastore-dataset-object-empty" - } - Google::Cloud.stub :datastore, stubbed_datastore do - dataset = gcloud.datastore - dataset.must_equal "datastore-dataset-object-empty" + describe "#datastore" do + it "calls out to Google::Cloud.datastore" do + gcloud = Google::Cloud.new + stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "datastore-dataset-object-empty" + } + Google::Cloud.stub :datastore, stubbed_datastore do + dataset = gcloud.datastore + dataset.must_equal "datastore-dataset-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.datastore" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "datastore-dataset-object" + } + Google::Cloud.stub :datastore, stubbed_datastore do + dataset = gcloud.datastore + dataset.must_equal "datastore-dataset-object" + end end - end - it "passes project and keyfile to Google::Cloud.datastore" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "datastore-dataset-object" - } - Google::Cloud.stub :datastore, stubbed_datastore do - dataset = gcloud.datastore - dataset.must_equal "datastore-dataset-object" + it "passes project and keyfile and options to Google::Cloud.datastore" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "datastore-dataset-object-scoped" + } + Google::Cloud.stub :datastore, stubbed_datastore do + dataset = gcloud.datastore scope: "http://example.com/scope", retries: 5, timeout: 60 + dataset.must_equal "datastore-dataset-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.datastore" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "datastore-dataset-object-scoped" - } - Google::Cloud.stub :datastore, stubbed_datastore do - dataset = gcloud.datastore scope: "http://example.com/scope", retries: 5, timeout: 60 - dataset.must_equal "datastore-dataset-object-scoped" + describe ".datastore" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Datastore::Credentials.stub :default, default_credentials do + datastore = Google::Cloud.datastore + datastore.must_be_kind_of Google::Cloud::Datastore::Dataset + datastore.project.must_equal "project-id" + datastore.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "datastore-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "datastore-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Datastore::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Datastore::Service.stub :new, stubbed_service do + datastore = Google::Cloud.datastore "project-id", "path/to/keyfile.json" + datastore.must_be_kind_of Google::Cloud::Datastore::Dataset + datastore.project.must_equal "project-id" + datastore.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-dns/lib/google/cloud/dns/project.rb b/google-cloud-dns/lib/google/cloud/dns/project.rb index cbce8ebdabcd..5f57eccdecfc 100644 --- a/google-cloud-dns/lib/google/cloud/dns/project.rb +++ b/google-cloud-dns/lib/google/cloud/dns/project.rb @@ -130,7 +130,7 @@ def self.default_project ENV["DNS_PROJECT"] || ENV["GCLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-dns/test/google/cloud/dns_test.rb b/google-cloud-dns/test/google/cloud/dns_test.rb index 1155dc29def2..6573a7c4f14e 100644 --- a/google-cloud-dns/test/google/cloud/dns_test.rb +++ b/google-cloud-dns/test/google/cloud/dns_test.rb @@ -16,51 +16,104 @@ require "google/cloud/dns" describe Google::Cloud do - it "calls out to Google::Cloud.dns" do - gcloud = Google::Cloud.new - stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "dns-project-object-empty" - } - Google::Cloud.stub :dns, stubbed_dns do - project = gcloud.dns - project.must_equal "dns-project-object-empty" + describe "#dns" do + it "calls out to Google::Cloud.dns" do + gcloud = Google::Cloud.new + stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "dns-project-object-empty" + } + Google::Cloud.stub :dns, stubbed_dns do + project = gcloud.dns + project.must_equal "dns-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.dns" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "dns-project-object" + } + Google::Cloud.stub :dns, stubbed_dns do + project = gcloud.dns + project.must_equal "dns-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.dns" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "dns-project-object" - } - Google::Cloud.stub :dns, stubbed_dns do - project = gcloud.dns - project.must_equal "dns-project-object" + it "passes project and keyfile and options to Google::Cloud.dns" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "dns-project-object-scoped" + } + Google::Cloud.stub :dns, stubbed_dns do + project = gcloud.dns scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "dns-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.dns" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_dns = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "dns-project-object-scoped" - } - Google::Cloud.stub :dns, stubbed_dns do - project = gcloud.dns scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "dns-project-object-scoped" + describe ".dns" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Dns::Credentials.stub :default, default_credentials do + dns = Google::Cloud.dns + dns.must_be_kind_of Google::Cloud::Dns::Project + dns.project.must_equal "project-id" + dns.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "dns-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "dns-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Dns::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Dns::Service.stub :new, stubbed_service do + dns = Google::Cloud.dns "project-id", "path/to/keyfile.json" + dns.must_be_kind_of Google::Cloud::Dns::Project + dns.project.must_equal "project-id" + dns.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-logging/lib/google/cloud/logging/project.rb b/google-cloud-logging/lib/google/cloud/logging/project.rb index 5d5e7dfa0d4c..78fc66af3f38 100644 --- a/google-cloud-logging/lib/google/cloud/logging/project.rb +++ b/google-cloud-logging/lib/google/cloud/logging/project.rb @@ -78,7 +78,7 @@ def self.default_project ENV["LOGGING_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-logging/lib/google/cloud/logging/service.rb b/google-cloud-logging/lib/google/cloud/logging/service.rb index b3ba04be4b84..2fe1b2503352 100644 --- a/google-cloud-logging/lib/google/cloud/logging/service.rb +++ b/google-cloud-logging/lib/google/cloud/logging/service.rb @@ -14,7 +14,7 @@ require "google/cloud/errors" -require "google/cloud/core/backoff" +require "google/cloud/core/grpc_backoff" require "google/logging/v2/logging_services" require "google/logging/v2/logging_config_services" require "google/logging/v2/logging_metrics_services" @@ -258,7 +258,7 @@ def metric_path metric_name end def execute - Google::Cloud::Core::Backoff.new(retries: retries).execute_grpc do + Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do yield end rescue GRPC::BadStatus => e diff --git a/google-cloud-logging/test/google/cloud/logging/backoff_test.rb b/google-cloud-logging/test/google/cloud/logging/backoff_test.rb index 08da15cdb932..3b1016fd6ff7 100644 --- a/google-cloud-logging/test/google/cloud/logging/backoff_test.rb +++ b/google-cloud-logging/test/google/cloud/logging/backoff_test.rb @@ -56,9 +56,9 @@ def assert_backoff_sleep *args mock = Minitest::Mock.new args.each { |intv| mock.expect :sleep, nil, [intv] } callback = ->(retries) { mock.sleep retries } - backoff = Google::Cloud::Core::Backoff.new retries: 5, backoff: callback + backoff = Google::Cloud::Core::GrpcBackoff.new retries: 5, backoff: callback - Google::Cloud::Core::Backoff.stub :new, backoff do + Google::Cloud::Core::GrpcBackoff.stub :new, backoff do yield end diff --git a/google-cloud-logging/test/google/cloud/logging_test.rb b/google-cloud-logging/test/google/cloud/logging_test.rb index 440fa48aa6c5..d16cc92e62d2 100644 --- a/google-cloud-logging/test/google/cloud/logging_test.rb +++ b/google-cloud-logging/test/google/cloud/logging_test.rb @@ -16,51 +16,104 @@ require "google/cloud/logging" describe Google::Cloud do - it "calls out to Google::Cloud.logging" do - gcloud = Google::Cloud.new - stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "logging-project-object-empty" - } - Google::Cloud.stub :logging, stubbed_logging do - project = gcloud.logging - project.must_equal "logging-project-object-empty" + describe "#logging" do + it "calls out to Google::Cloud.logging" do + gcloud = Google::Cloud.new + stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "logging-project-object-empty" + } + Google::Cloud.stub :logging, stubbed_logging do + project = gcloud.logging + project.must_equal "logging-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.logging" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "logging-project-object" + } + Google::Cloud.stub :logging, stubbed_logging do + project = gcloud.logging + project.must_equal "logging-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.logging" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "logging-project-object" - } - Google::Cloud.stub :logging, stubbed_logging do - project = gcloud.logging - project.must_equal "logging-project-object" + it "passes project and keyfile and options to Google::Cloud.logging" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "logging-project-object-scoped" + } + Google::Cloud.stub :logging, stubbed_logging do + project = gcloud.logging scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "logging-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.logging" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_logging = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "logging-project-object-scoped" - } - Google::Cloud.stub :logging, stubbed_logging do - project = gcloud.logging scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "logging-project-object-scoped" + describe ".logging" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Logging::Credentials.stub :default, default_credentials do + logging = Google::Cloud.logging + logging.must_be_kind_of Google::Cloud::Logging::Project + logging.project.must_equal "project-id" + logging.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "logging-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "logging-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Logging::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Logging::Service.stub :new, stubbed_service do + logging = Google::Cloud.logging "project-id", "path/to/keyfile.json" + logging.must_be_kind_of Google::Cloud::Logging::Project + logging.project.must_equal "project-id" + logging.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb index 9f93b24be0ab..06fbe9e51c61 100644 --- a/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb +++ b/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb @@ -76,7 +76,7 @@ def self.default_project ENV["PUBSUB_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb index 331603911164..002dc313e970 100644 --- a/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb +++ b/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb @@ -14,7 +14,7 @@ require "google/cloud/errors" -require "google/cloud/core/backoff" +require "google/cloud/core/grpc_backoff" require "google/pubsub/v1/pubsub_services" require "google/iam/v1/iam_policy_services" require "google/cloud/core/grpc_utils" @@ -328,7 +328,7 @@ def inspect protected def execute - Google::Cloud::Core::Backoff.new(retries: retries).execute_grpc do + Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do yield end rescue GRPC::BadStatus => e diff --git a/google-cloud-pubsub/test/google/cloud/pubsub/backoff_test.rb b/google-cloud-pubsub/test/google/cloud/pubsub/backoff_test.rb index 213abfe1b557..f5bc80a4b598 100644 --- a/google-cloud-pubsub/test/google/cloud/pubsub/backoff_test.rb +++ b/google-cloud-pubsub/test/google/cloud/pubsub/backoff_test.rb @@ -57,9 +57,9 @@ def assert_backoff_sleep *args mock = Minitest::Mock.new args.each { |intv| mock.expect :sleep, nil, [intv] } callback = ->(retries) { mock.sleep retries } - backoff = Google::Cloud::Core::Backoff.new retries: 5, backoff: callback + backoff = Google::Cloud::Core::GrpcBackoff.new retries: 5, backoff: callback - Google::Cloud::Core::Backoff.stub :new, backoff do + Google::Cloud::Core::GrpcBackoff.stub :new, backoff do yield end diff --git a/google-cloud-pubsub/test/google/cloud/pubsub_test.rb b/google-cloud-pubsub/test/google/cloud/pubsub_test.rb index 744a3fb2ff17..8f10aec64a9b 100644 --- a/google-cloud-pubsub/test/google/cloud/pubsub_test.rb +++ b/google-cloud-pubsub/test/google/cloud/pubsub_test.rb @@ -16,51 +16,104 @@ require "google/cloud/pubsub" describe Google::Cloud do - it "calls out to Google::Cloud.pubsub" do - gcloud = Google::Cloud.new - stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "pubsub-project-object-empty" - } - Google::Cloud.stub :pubsub, stubbed_pubsub do - project = gcloud.pubsub - project.must_equal "pubsub-project-object-empty" + describe "#pubsub" do + it "calls out to Google::Cloud.pubsub" do + gcloud = Google::Cloud.new + stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "pubsub-project-object-empty" + } + Google::Cloud.stub :pubsub, stubbed_pubsub do + project = gcloud.pubsub + project.must_equal "pubsub-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.pubsub" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "pubsub-project-object" + } + Google::Cloud.stub :pubsub, stubbed_pubsub do + project = gcloud.pubsub + project.must_equal "pubsub-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.pubsub" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "pubsub-project-object" - } - Google::Cloud.stub :pubsub, stubbed_pubsub do - project = gcloud.pubsub - project.must_equal "pubsub-project-object" + it "passes project and keyfile and options to Google::Cloud.pubsub" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "pubsub-project-object-scoped" + } + Google::Cloud.stub :pubsub, stubbed_pubsub do + project = gcloud.pubsub scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "pubsub-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.pubsub" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_pubsub = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "pubsub-project-object-scoped" - } - Google::Cloud.stub :pubsub, stubbed_pubsub do - project = gcloud.pubsub scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "pubsub-project-object-scoped" + describe ".pubsub" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Pubsub::Credentials.stub :default, default_credentials do + pubsub = Google::Cloud.pubsub + pubsub.must_be_kind_of Google::Cloud::Pubsub::Project + pubsub.project.must_equal "project-id" + pubsub.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "pubsub-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "pubsub-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Pubsub::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Pubsub::Service.stub :new, stubbed_service do + pubsub = Google::Cloud.pubsub "project-id", "path/to/keyfile.json" + pubsub.must_be_kind_of Google::Cloud::Pubsub::Project + pubsub.project.must_equal "project-id" + pubsub.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-resource_manager/test/google/cloud/resource_manager_test.rb b/google-cloud-resource_manager/test/google/cloud/resource_manager_test.rb index a9b34027afe8..76908006ac27 100644 --- a/google-cloud-resource_manager/test/google/cloud/resource_manager_test.rb +++ b/google-cloud-resource_manager/test/google/cloud/resource_manager_test.rb @@ -16,48 +16,98 @@ require "google/cloud/resource_manager" describe Google::Cloud do - it "calls out to Google::Cloud.resource_manager" do - gcloud = Google::Cloud.new - stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "resource_manager-manager-object-empty" - } - Google::Cloud.stub :resource_manager, stubbed_resource_manager do - manager = gcloud.resource_manager - manager.must_equal "resource_manager-manager-object-empty" + describe "#resource_manager" do + it "calls out to Google::Cloud.resource_manager" do + gcloud = Google::Cloud.new + stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "resource_manager-manager-object-empty" + } + Google::Cloud.stub :resource_manager, stubbed_resource_manager do + manager = gcloud.resource_manager + manager.must_equal "resource_manager-manager-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.resource_manager" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "resource_manager-manager-object" + } + Google::Cloud.stub :resource_manager, stubbed_resource_manager do + manager = gcloud.resource_manager + manager.must_equal "resource_manager-manager-object" + end end - end - it "passes project and keyfile to Google::Cloud.resource_manager" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "resource_manager-manager-object" - } - Google::Cloud.stub :resource_manager, stubbed_resource_manager do - manager = gcloud.resource_manager - manager.must_equal "resource_manager-manager-object" + it "passes project and keyfile and options to Google::Cloud.resource_manager" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "resource_manager-manager-object-scoped" + } + Google::Cloud.stub :resource_manager, stubbed_resource_manager do + manager = gcloud.resource_manager scope: "http://example.com/scope", retries: 5, timeout: 60 + manager.must_equal "resource_manager-manager-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.resource_manager" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_resource_manager = ->(keyfile, scope: nil, retries: nil, timeout: nil) { - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "resource_manager-manager-object-scoped" - } - Google::Cloud.stub :resource_manager, stubbed_resource_manager do - manager = gcloud.resource_manager scope: "http://example.com/scope", retries: 5, timeout: 60 - manager.must_equal "resource_manager-manager-object-scoped" + describe ".resource_manager" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::ResourceManager::Credentials.stub :default, default_credentials do + resource_manager = Google::Cloud.resource_manager + resource_manager.must_be_kind_of Google::Cloud::ResourceManager::Manager + resource_manager.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "resource_manager-credentials" + } + stubbed_service = ->(credentials, retries: nil, timeout: nil) { + credentials.must_equal "resource_manager-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::ResourceManager::Credentials.stub :new, stubbed_credentials do + Google::Cloud::ResourceManager::Service.stub :new, stubbed_service do + resource_manager = Google::Cloud.resource_manager "path/to/keyfile.json" + resource_manager.must_be_kind_of Google::Cloud::ResourceManager::Manager + resource_manager.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 833b6a609cc3..131fa665a586 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -82,7 +82,7 @@ def self.default_project ENV["STORAGE_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index 98f9169f29a9..62afaccef3d9 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -14,7 +14,7 @@ require "google/cloud/storage/version" -require "google/cloud/core/backoff" +require "google/cloud/core/grpc_backoff" require "google/apis/storage_v1" require "digest" require "mime/types" diff --git a/google-cloud-storage/test/google/cloud/storage_test.rb b/google-cloud-storage/test/google/cloud/storage_test.rb index 222fc89fdb6c..66b78f5cc3b7 100644 --- a/google-cloud-storage/test/google/cloud/storage_test.rb +++ b/google-cloud-storage/test/google/cloud/storage_test.rb @@ -16,51 +16,104 @@ require "google/cloud/storage" describe Google::Cloud do - it "calls out to Google::Cloud.storage" do - gcloud = Google::Cloud.new - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "storage-project-object-empty" - } - Google::Cloud.stub :storage, stubbed_storage do - project = gcloud.storage - project.must_equal "storage-project-object-empty" + describe "#storage" do + it "calls out to Google::Cloud.storage" do + gcloud = Google::Cloud.new + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "storage-project-object-empty" + } + Google::Cloud.stub :storage, stubbed_storage do + project = gcloud.storage + project.must_equal "storage-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.storage" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "storage-project-object" + } + Google::Cloud.stub :storage, stubbed_storage do + project = gcloud.storage + project.must_equal "storage-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.storage" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "storage-project-object" - } - Google::Cloud.stub :storage, stubbed_storage do - project = gcloud.storage - project.must_equal "storage-project-object" + it "passes project and keyfile and options to Google::Cloud.storage" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "storage-project-object-scoped" + } + Google::Cloud.stub :storage, stubbed_storage do + project = gcloud.storage scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "storage-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.storage" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "storage-project-object-scoped" - } - Google::Cloud.stub :storage, stubbed_storage do - project = gcloud.storage scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "storage-project-object-scoped" + describe ".storage" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Storage::Credentials.stub :default, default_credentials do + storage = Google::Cloud.storage + storage.must_be_kind_of Google::Cloud::Storage::Project + storage.project.must_equal "project-id" + storage.service.credentials.must_equal default_credentials + end + end + end end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "storage-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "storage-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Storage::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Storage::Service.stub :new, stubbed_service do + storage = Google::Cloud.storage "project-id", "path/to/keyfile.json" + storage.must_be_kind_of Google::Cloud::Storage::Project + storage.project.must_equal "project-id" + storage.service.must_be_kind_of OpenStruct + end + end + end + end + end + end + end end -end diff --git a/google-cloud-translate/test/google/cloud/translate_test.rb b/google-cloud-translate/test/google/cloud/translate_test.rb index 95244b2922a6..9f590dbf88c6 100644 --- a/google-cloud-translate/test/google/cloud/translate_test.rb +++ b/google-cloud-translate/test/google/cloud/translate_test.rb @@ -16,45 +16,91 @@ require "google/cloud/translate" describe Google::Cloud do - it "calls out to Google::Cloud.translate" do - gcloud = Google::Cloud.new - stubbed_translate = ->(key, retries: nil, timeout: nil) { - key.must_equal "this-is-the-api-key" - retries.must_be :nil? - timeout.must_be :nil? - "translate-api-object-empty" - } - Google::Cloud.stub :translate, stubbed_translate do - api = gcloud.translate "this-is-the-api-key" - api.must_equal "translate-api-object-empty" + describe "#translate" do + it "calls out to Google::Cloud.translate" do + gcloud = Google::Cloud.new + stubbed_translate = ->(key, retries: nil, timeout: nil) { + key.must_equal "this-is-the-api-key" + retries.must_be :nil? + timeout.must_be :nil? + "translate-api-object-empty" + } + Google::Cloud.stub :translate, stubbed_translate do + api = gcloud.translate "this-is-the-api-key" + api.must_equal "translate-api-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.translate" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_translate = ->(key, retries: nil, timeout: nil) { + key.must_equal "this-is-the-api-key" + retries.must_be :nil? + timeout.must_be :nil? + "translate-api-object" + } + Google::Cloud.stub :translate, stubbed_translate do + api = gcloud.translate "this-is-the-api-key" + api.must_equal "translate-api-object" + end end - end - it "passes project and keyfile to Google::Cloud.translate" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_translate = ->(key, retries: nil, timeout: nil) { - key.must_equal "this-is-the-api-key" - retries.must_be :nil? - timeout.must_be :nil? - "translate-api-object" - } - Google::Cloud.stub :translate, stubbed_translate do - api = gcloud.translate "this-is-the-api-key" - api.must_equal "translate-api-object" + it "passes project and keyfile and options to Google::Cloud.translate" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_translate = ->(key, retries: nil, timeout: nil) { + key.must_equal "this-is-the-api-key" + retries.must_equal 5 + timeout.must_equal 60 + "translate-api-object-scoped" + } + Google::Cloud.stub :translate, stubbed_translate do + api = gcloud.translate "this-is-the-api-key", retries: 5, timeout: 60 + api.must_equal "translate-api-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.translate" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_translate = ->(key, retries: nil, timeout: nil) { - key.must_equal "this-is-the-api-key" - retries.must_equal 5 - timeout.must_equal 60 - "translate-api-object-scoped" - } - Google::Cloud.stub :translate, stubbed_translate do - api = gcloud.translate "this-is-the-api-key", retries: 5, timeout: 60 - api.must_equal "translate-api-object-scoped" + describe ".translate" do + it "gets defaults for api_key" do + stubbed_env = ->(name) { + "found-api-key" if name == "GOOGLE_CLOUD_KEY" + } + stubbed_service = ->(key, retries: nil, timeout: nil) { + key.must_equal "found-api-key" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new key: key + } + + # Clear all environment variables + # ENV.stub :[], nil do + ENV.stub :[], stubbed_env do + Google::Cloud::Translate::Service.stub :new, stubbed_service do + translate = Google::Cloud.translate + translate.must_be_kind_of Google::Cloud::Translate::Api + translate.service.must_be_kind_of OpenStruct + translate.service.key.must_equal "found-api-key" + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_service = ->(key, retries: nil, timeout: nil) { + key.must_equal "my-api-key" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new key: key + } + + # Clear all environment variables + ENV.stub :[], nil do + Google::Cloud::Translate::Service.stub :new, stubbed_service do + translate = Google::Cloud.translate "my-api-key" + translate.must_be_kind_of Google::Cloud::Translate::Api + translate.service.must_be_kind_of OpenStruct + translate.service.key.must_equal "my-api-key" + end + end end end end diff --git a/google-cloud-vision/lib/google/cloud/vision/project.rb b/google-cloud-vision/lib/google/cloud/vision/project.rb index 0a2ce4434eac..e1a15d0880ec 100644 --- a/google-cloud-vision/lib/google/cloud/vision/project.rb +++ b/google-cloud-vision/lib/google/cloud/vision/project.rb @@ -79,7 +79,7 @@ def self.default_project ENV["VISION_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"] || - Google::Cloud::GCE.project_id + Google::Cloud::Core::GCE.project_id end ## diff --git a/google-cloud-vision/test/google/cloud/vision_test.rb b/google-cloud-vision/test/google/cloud/vision_test.rb index 098d451a66ee..c88396873d9a 100644 --- a/google-cloud-vision/test/google/cloud/vision_test.rb +++ b/google-cloud-vision/test/google/cloud/vision_test.rb @@ -14,53 +14,107 @@ require "helper" require "google/cloud/vision" +require "google/cloud/core/gce" describe Google::Cloud do - it "calls out to Google::Cloud.vision" do - gcloud = Google::Cloud.new - stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal nil - keyfile.must_equal nil - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "vision-project-object-empty" - } - Google::Cloud.stub :vision, stubbed_vision do - project = gcloud.vision - project.must_equal "vision-project-object-empty" + describe "#vision" do + it "calls out to Google::Cloud.vision" do + gcloud = Google::Cloud.new + stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal nil + keyfile.must_equal nil + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "vision-project-object-empty" + } + Google::Cloud.stub :vision, stubbed_vision do + project = gcloud.vision + project.must_equal "vision-project-object-empty" + end + end + + it "passes project and keyfile to Google::Cloud.vision" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_be :nil? + retries.must_be :nil? + timeout.must_be :nil? + "vision-project-object" + } + Google::Cloud.stub :vision, stubbed_vision do + project = gcloud.vision + project.must_equal "vision-project-object" + end end - end - it "passes project and keyfile to Google::Cloud.vision" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_be :nil? - retries.must_be :nil? - timeout.must_be :nil? - "vision-project-object" - } - Google::Cloud.stub :vision, stubbed_vision do - project = gcloud.vision - project.must_equal "vision-project-object" + it "passes project and keyfile and options to Google::Cloud.vision" do + gcloud = Google::Cloud.new "project-id", "keyfile-path" + stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + project.must_equal "project-id" + keyfile.must_equal "keyfile-path" + scope.must_equal "http://example.com/scope" + retries.must_equal 5 + timeout.must_equal 60 + "vision-project-object-scoped" + } + Google::Cloud.stub :vision, stubbed_vision do + project = gcloud.vision scope: "http://example.com/scope", retries: 5, timeout: 60 + project.must_equal "vision-project-object-scoped" + end end end - it "passes project and keyfile and options to Google::Cloud.vision" do - gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_vision = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { - project.must_equal "project-id" - keyfile.must_equal "keyfile-path" - scope.must_equal "http://example.com/scope" - retries.must_equal 5 - timeout.must_equal 60 - "vision-project-object-scoped" - } - Google::Cloud.stub :vision, stubbed_vision do - project = gcloud.vision scope: "http://example.com/scope", retries: 5, timeout: 60 - project.must_equal "vision-project-object-scoped" + describe ".vision" do + let(:default_credentials) { OpenStruct.new empty: true } + let(:found_credentials) { "{}" } + + it "gets defaults for project_id and keyfile" do + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud::Core::GCE.stub :project_id, "project-id" do + Google::Cloud::Vision::Credentials.stub :default, default_credentials do + vision = Google::Cloud.vision + vision.must_be_kind_of Google::Cloud::Vision::Project + vision.project.must_equal "project-id" + vision.service.credentials.must_equal default_credentials + end + end + end + end + + it "uses provided project_id and keyfile" do + stubbed_credentials = ->(keyfile, scope: nil) { + keyfile.must_equal "path/to/keyfile.json" + scope.must_equal nil + "vision-credentials" + } + stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + project.must_equal "project-id" + credentials.must_equal "vision-credentials" + retries.must_equal nil + timeout.must_equal nil + OpenStruct.new project: project + } + + # Clear all environment variables + ENV.stub :[], nil do + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Vision::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Vision::Service.stub :new, stubbed_service do + vision = Google::Cloud.vision "project-id", "path/to/keyfile.json" + vision.must_be_kind_of Google::Cloud::Vision::Project + vision.project.must_equal "project-id" + vision.service.must_be_kind_of OpenStruct + end + end + end + end + end end end end