|
19 | 19 | describe "#storage" do |
20 | 20 | it "calls out to Google::Cloud.storage" do |
21 | 21 | gcloud = Google::Cloud.new |
22 | | - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil) { |
| 22 | + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil, emulator_host: nil) { |
23 | 23 | _(project).must_be :nil? |
24 | 24 | _(keyfile).must_be :nil? |
25 | 25 | _(scope).must_be :nil? |
|
44 | 44 |
|
45 | 45 | it "passes project and keyfile to Google::Cloud.storage" do |
46 | 46 | gcloud = Google::Cloud.new "project-id", "keyfile-path" |
47 | | - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil) { |
| 47 | + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil, emulator_host: nil) { |
48 | 48 | _(project).must_equal "project-id" |
49 | 49 | _(keyfile).must_equal "keyfile-path" |
50 | 50 | _(scope).must_be :nil? |
|
69 | 69 |
|
70 | 70 | it "passes project and keyfile and options to Google::Cloud.storage" do |
71 | 71 | gcloud = Google::Cloud.new "project-id", "keyfile-path" |
72 | | - stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil) { |
| 72 | + stubbed_storage = ->(project, keyfile, scope: nil, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, max_elapsed_time: nil, base_interval: nil, max_interval: nil, multiplier: nil, upload_chunk_size: nil, emulator_host: nil) { |
73 | 73 | _(project).must_equal "project-id" |
74 | 74 | _(keyfile).must_equal "keyfile-path" |
75 | 75 | _(scope).must_equal "http://example.com/scope" |
@@ -387,6 +387,103 @@ def creds.is_a? target |
387 | 387 | end |
388 | 388 | end |
389 | 389 |
|
| 390 | + describe "Storage.emulator" do |
| 391 | + let(:emulator_host) { "http://localhost:9000" } |
| 392 | + let(:default_credentials) do |
| 393 | + creds = OpenStruct.new empty: true |
| 394 | + def creds.is_a? target |
| 395 | + target == Google::Auth::Credentials |
| 396 | + end |
| 397 | + creds |
| 398 | + end |
| 399 | + |
| 400 | + it "uses STORAGE_EMULATOR_HOST environment variable" do |
| 401 | + emulator_check = ->(name) { (name == "STORAGE_EMULATOR_HOST") ? emulator_host : nil } |
| 402 | + stubbed_service = ->(project, credentials, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, quota_project: nil, max_elapsed_time: nil, base_interval: nil, |
| 403 | + max_interval: nil, multiplier: nil, upload_chunk_size: nil, universe_domain: nil) { |
| 404 | + _(project).must_equal "project-id" |
| 405 | + _(credentials).must_be :nil? |
| 406 | + _(host).must_equal emulator_host |
| 407 | + OpenStruct.new project: project |
| 408 | + } |
| 409 | + |
| 410 | + # Clear all environment variables, except STORAGE_EMULATOR_HOST |
| 411 | + ENV.stub :[], emulator_check do |
| 412 | + # Get project_id from Google Compute Engine |
| 413 | + Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do |
| 414 | + Google::Cloud::Storage::Service.stub :new, stubbed_service do |
| 415 | + storage = Google::Cloud::Storage.new |
| 416 | + _(storage).must_be_kind_of Google::Cloud::Storage::Project |
| 417 | + _(storage.project).must_equal "project-id" |
| 418 | + end |
| 419 | + end |
| 420 | + end |
| 421 | + end |
| 422 | + |
| 423 | + it "allows emulator_host to be set" do |
| 424 | + stubbed_service = ->(project, credentials, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, quota_project: nil, max_elapsed_time: nil, base_interval: nil, |
| 425 | + max_interval: nil, multiplier: nil, upload_chunk_size: nil, universe_domain: nil) { |
| 426 | + _(project).must_equal "project-id" |
| 427 | + _(credentials).must_be :nil? |
| 428 | + _(host).must_equal emulator_host |
| 429 | + OpenStruct.new project: project |
| 430 | + } |
| 431 | + |
| 432 | + # Clear all environment variables |
| 433 | + ENV.stub :[], nil do |
| 434 | + Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do |
| 435 | + Google::Cloud::Storage::Service.stub :new, stubbed_service do |
| 436 | + storage = Google::Cloud::Storage.new emulator_host: emulator_host |
| 437 | + _(storage).must_be_kind_of Google::Cloud::Storage::Project |
| 438 | + _(storage.project).must_equal "project-id" |
| 439 | + end |
| 440 | + end |
| 441 | + end |
| 442 | + end |
| 443 | + |
| 444 | + it "does not require a project_id when using an emulator" do |
| 445 | + stubbed_service = ->(project, credentials, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, quota_project: nil, max_elapsed_time: nil, base_interval: nil, |
| 446 | + max_interval: nil, multiplier: nil, upload_chunk_size: nil, universe_domain: nil) { |
| 447 | + _(project).must_equal "" |
| 448 | + _(credentials).must_be :nil? |
| 449 | + _(host).must_equal emulator_host |
| 450 | + OpenStruct.new project: project |
| 451 | + } |
| 452 | + |
| 453 | + # Clear all environment variables, and provide no project_id from any source |
| 454 | + ENV.stub :[], nil do |
| 455 | + Google::Cloud.stub :env, OpenStruct.new(project_id: nil) do |
| 456 | + Google::Cloud::Storage::Service.stub :new, stubbed_service do |
| 457 | + storage = Google::Cloud::Storage.new emulator_host: emulator_host |
| 458 | + _(storage).must_be_kind_of Google::Cloud::Storage::Project |
| 459 | + _(storage.project).must_equal "" |
| 460 | + end |
| 461 | + end |
| 462 | + end |
| 463 | + end |
| 464 | + |
| 465 | + it "honors provided credentials when using an emulator" do |
| 466 | + stubbed_service = ->(project, credentials, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, quota_project: nil, max_elapsed_time: nil, base_interval: nil, |
| 467 | + max_interval: nil, multiplier: nil, upload_chunk_size: nil, universe_domain: nil) { |
| 468 | + _(project).must_equal "project-id" |
| 469 | + _(credentials).must_equal default_credentials |
| 470 | + _(host).must_equal emulator_host |
| 471 | + OpenStruct.new project: project |
| 472 | + } |
| 473 | + |
| 474 | + # Clear all environment variables |
| 475 | + ENV.stub :[], nil do |
| 476 | + Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do |
| 477 | + Google::Cloud::Storage::Service.stub :new, stubbed_service do |
| 478 | + storage = Google::Cloud::Storage.new credentials: default_credentials, emulator_host: emulator_host |
| 479 | + _(storage).must_be_kind_of Google::Cloud::Storage::Project |
| 480 | + _(storage.project).must_equal "project-id" |
| 481 | + end |
| 482 | + end |
| 483 | + end |
| 484 | + end |
| 485 | + end |
| 486 | + |
390 | 487 | describe "Storage.configure" do |
391 | 488 | let(:found_credentials) { "{}" } |
392 | 489 |
|
@@ -660,6 +757,31 @@ def creds.is_a? target |
660 | 757 | end |
661 | 758 | end |
662 | 759 |
|
| 760 | + it "uses storage config for emulator_host" do |
| 761 | + stubbed_service = ->(project, credentials, retries: nil, timeout: nil, open_timeout: nil, read_timeout: nil, send_timeout: nil, host: nil, quota_project: nil, max_elapsed_time: nil, base_interval: nil, |
| 762 | + max_interval: nil, multiplier: nil, upload_chunk_size: nil, universe_domain: nil) { |
| 763 | + _(project).must_equal "project-id" |
| 764 | + _(credentials).must_be :nil? |
| 765 | + _(host).must_equal "http://localhost:9000" |
| 766 | + OpenStruct.new project: project |
| 767 | + } |
| 768 | + |
| 769 | + # Clear all environment variables |
| 770 | + ENV.stub :[], nil do |
| 771 | + # Set new configuration |
| 772 | + Google::Cloud::Storage.configure do |config| |
| 773 | + config.project_id = "project-id" |
| 774 | + config.emulator_host = "http://localhost:9000" |
| 775 | + end |
| 776 | + |
| 777 | + Google::Cloud::Storage::Service.stub :new, stubbed_service do |
| 778 | + storage = Google::Cloud::Storage.new |
| 779 | + _(storage).must_be_kind_of Google::Cloud::Storage::Project |
| 780 | + _(storage.project).must_equal "project-id" |
| 781 | + end |
| 782 | + end |
| 783 | + end |
| 784 | + |
663 | 785 | it "uses storage config for quota project" do |
664 | 786 | stubbed_credentials = ->(keyfile, scope: nil) { |
665 | 787 | _(keyfile).must_equal "path/to/keyfile.json" |
|
0 commit comments