Skip to content

Commit efabead

Browse files
updating test cases with delay
1 parent 4ffa540 commit efabead

9 files changed

Lines changed: 91 additions & 82 deletions

google-cloud-storage-control/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,3 @@ might need to use one if there is no modern client available for the service.
141141
However, if a modern client is available, we generally recommend it over the
142142
older `google-apis-` clients.
143143

144-
## Note
145-
146-
Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations.
147-

google-cloud-storage-control/samples/acceptance/storage_control_anywhere_cache_test.rb

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
require_relative "helper"
16+
require "google/cloud/storage/control"
1617
require_relative "../storage_control_create_anywhere_cache"
1718
require_relative "../storage_control_list_anywhere_caches"
1819
require_relative "../storage_control_get_anywhere_cache"
@@ -21,69 +22,66 @@
2122
require_relative "../storage_control_resume_anywhere_cache"
2223
require_relative "../storage_control_disable_anywhere_cache"
2324

24-
# require 'pry'
25-
2625
describe "Storage Control Anywhere Cache" do
2726
let(:bucket_name) { random_bucket_name }
28-
let(:storage_client) { Google::Cloud::Storage.new }
29-
let(:zone) {'us-east1-b'}
30-
# let(:zone) {@bucket.location}
27+
let(:zone) { "us-east1-b" }
28+
# Set project to "_" to signify global bucket
29+
let(:anywhere_cache_name) { "projects/_/buckets/#{bucket_name}/anywhereCaches/#{zone}" }
3130

3231
before :all do
33-
@bucket= storage_client.bucket bucket_name
34-
@bucket = create_bucket_helper bucket_name
32+
@bucket = create_bucket_helper bucket_name
3533
end
3634

37-
# after do
38-
# delete_bucket_helper bucket_name
39-
# end
40-
41-
it "create Anywhere cache" do
42-
create_anywhere_cache bucket_name: bucket_name, zone: zone
35+
after do
36+
delete_bucket_helper bucket_name until count_anywhere_caches bucket_name == 0
4337
end
4438

45-
# it "list Anywhere cache" do
46-
# # create_anywhere_cache bucket_name: bucket_name, zone: zone
39+
it "handles Anywhere cache lifecycle in sequence" do
40+
out_create, _err = capture_io do
41+
create_anywhere_cache bucket_name: bucket_name, zone: zone
42+
end
43+
assert_includes out_create, "AnywhereCache created - #{anywhere_cache_name}"
4744

48-
# out, _err = capture_io do
49-
# list_anywhere_caches bucket_name: bucket_name
50-
# end
45+
out_list, _err = capture_io do
46+
list_anywhere_caches bucket_name: bucket_name
47+
end
48+
assert_includes out_list, "AnywhereCache #{anywhere_cache_name} found in list"
5149

52-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
53-
# end
50+
out_get, _err = capture_io do
51+
get_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
52+
end
53+
assert_includes out_get, "AnywhereCache #{anywhere_cache_name} fetched"
5454

55-
# it "Get Anywhere cache" do
56-
# out, _err = capture_io do
57-
# get_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
58-
# end
59-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
60-
# end
55+
out_update, _err = capture_io do
56+
update_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
57+
end
58+
assert_includes out_update, "AnywhereCache #{anywhere_cache_name} updated"
6159

62-
# it "Pause Anywhere cache" do
63-
# out, _err = capture_io do
64-
# pause_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
65-
# end
66-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} paused"
67-
# end
60+
out_pause, _err = capture_io do
61+
pause_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
62+
end
63+
assert_includes out_pause, "AnywhereCache #{anywhere_cache_name} paused"
6864

69-
# it "Resume Anywhere cache" do
70-
# out, _err = capture_io do
71-
# resume_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
72-
# end
73-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} running"
74-
# end
65+
out_resume, _err = capture_io do
66+
resume_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
67+
end
68+
assert_includes out_resume, "AnywhereCache #{anywhere_cache_name} running"
7569

76-
# it "Disable Anywhere cache" do
77-
# out, _err = capture_io do
78-
# disable_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
79-
# end
80-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} disabled"
81-
# end
70+
out_disable, _err = capture_io do
71+
disable_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
72+
end
73+
assert_includes out_disable, "AnywhereCache #{anywhere_cache_name} disabled"
74+
end
75+
end
8276

83-
# it "Update Anywhere cache" do
84-
# out, _err = capture_io do
85-
# update_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
86-
# end
87-
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
88-
# end
89-
end
77+
def count_anywhere_caches bucket_name
78+
sleep 900
79+
storage_control_client = Google::Cloud::Storage::Control.storage_control
80+
# Set project to "_" to signify global bucket
81+
parent = "projects/_/buckets/#{bucket_name}"
82+
request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
83+
parent: parent
84+
)
85+
result = storage_control_client.list_anywhere_caches request
86+
result.response.anywhere_caches.count
87+
end

google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def create_anywhere_cache bucket_name:, zone:
2424

2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
2728
parent = "projects/_/buckets/#{bucket_name}"
2829
name = "#{parent}/anywhereCaches/#{zone}"
2930

@@ -40,24 +41,21 @@ def create_anywhere_cache bucket_name:, zone:
4041
# The cache is created in the specified bucket.
4142
begin
4243
operation = storage_control_client.create_anywhere_cache request
43-
if operation.response? == false
44-
45-
puts "********************AnywhereCache operation created - #{operation.name}"
46-
if !operation.done?
47-
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
48-
name: name
49-
)
44+
if operation.class == Gapic::Operation
45+
puts "AnywhereCache operation created - #{operation.name}"
46+
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
47+
name: name
48+
)
49+
result = storage_control_client.get_anywhere_cache get_request
50+
until result.state == "running"
51+
sleep 1800 # Wait for 1/2 hour before checking again
5052
result = storage_control_client.get_anywhere_cache get_request
51-
while result.state == "creating"
52-
sleep 1800 # Wait for 1/2 hour before checking again
53-
result = storage_control_client.get_anywhere_cache get_request
54-
puts "********************AnywhereCache operation refreshed"
55-
puts "********************AnywhereCache operation status check retried"
56-
end
53+
puts "AnywhereCache status check retried"
5754
end
55+
puts "AnywhereCache created - #{result.name}"
56+
else
57+
puts "AnywhereCache create operation failed"
5858
end
59-
puts "********************AnywhereCache create operation completed - #{operation.name}"
60-
6159
rescue StandardError => e
6260
puts "Error creating AnywhereCache: #{e.message}"
6361
end

google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ def disable_anywhere_cache bucket_name:, anywhere_cache_id:
2424

2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
2728
parent = "projects/_/buckets/#{bucket_name}"
28-
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
2930

3031
# Create a request.
3132
request = Google::Cloud::Storage::Control::V2::DisableAnywhereCacheRequest.new(
@@ -35,7 +36,7 @@ def disable_anywhere_cache bucket_name:, anywhere_cache_id:
3536
# The cache can be re-enabled later.
3637
begin
3738
result = storage_control_client.disable_anywhere_cache request
38-
puts "AnywhereCache #{result.name} #{result.state}"
39+
puts "AnywhereCache #{result.name} #{result.state} "
3940
rescue StandardError => e
4041
puts "Error disabling AnywhereCache: #{e.message}"
4142
end

google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def get_anywhere_cache bucket_name:, anywhere_cache_id:
2424

2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
2728
parent = "projects/_/buckets/#{bucket_name}"
28-
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
2930

30-
# Create a request. Replace the placeholder values with actual data.
31+
# Create a request.
3132
request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
3233
name: name
3334
)
@@ -37,7 +38,7 @@ def get_anywhere_cache bucket_name:, anywhere_cache_id:
3738

3839
begin
3940
result = storage_control_client.get_anywhere_cache request
40-
puts "AnywhereCache fetched - #{result.name}"
41+
puts "AnywhereCache #{result.name} fetched"
4142
rescue StandardError => e
4243
puts "Error fetching AnywhereCache: #{e.message}"
4344
end

google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def list_anywhere_caches bucket_name:
2121

2222
# Create a client object. The client can be reused for multiple calls.
2323
storage_control_client = Google::Cloud::Storage::Control.storage_control
24+
# Set project to "_" to signify global bucket
2425
parent = "projects/_/buckets/#{bucket_name}"
2526

27+
# Create a request.
2628
request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
2729
parent: parent
2830
)
@@ -31,7 +33,7 @@ def list_anywhere_caches bucket_name:
3133
begin
3234
result = storage_control_client.list_anywhere_caches request
3335
result.response.anywhere_caches.each do |item|
34-
puts item.name
36+
puts "AnywhereCache #{item.name} found in list"
3537
end
3638
rescue StandardError => e
3739
puts "Error listing AnywhereCaches: #{e.message}"

google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def pause_anywhere_cache bucket_name:, anywhere_cache_id:
2424

2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
2728
parent = "projects/_/buckets/#{bucket_name}"
28-
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
2930

30-
# Create a request. Replace the placeholder values with actual data.
31+
# Create a request.
3132
request = Google::Cloud::Storage::Control::V2::PauseAnywhereCacheRequest.new(
3233
name: name
3334
)

google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def resume_anywhere_cache bucket_name:, anywhere_cache_id:
2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
2727
parent = "projects/_/buckets/#{bucket_name}"
28+
# Set project to "_" to signify global bucket
2829
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
2930

3031
# Create a request.

google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414

1515
# [START storage_control_update_anywhere_cache]
1616
require "google/cloud/storage/control"
17+
require "google/cloud/storage/control/v2"
1718

1819
def update_anywhere_cache bucket_name:, anywhere_cache_id:
1920
# The ID of your GCS bucket
2021
# bucket_name = "your-unique-bucket-name"
2122
# A value that, along with the bucket's name, uniquely identifies the cache
2223
# anywhere_cache_id = "us-east1-b"
23-
require "google/cloud/storage/control/v2"
2424

2525
# Create a client object. The client can be reused for multiple calls.
2626
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
2728
parent = "projects/_/buckets/#{bucket_name}"
2829
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
30+
ttl_in_seconds= 7200
2931

3032
anywhere_cache = Google::Cloud::Storage::Control::V2::AnywhereCache.new(
3133
name: name,
32-
ttl: 7200
34+
ttl: ttl_in_seconds
3335
)
3436
mask = Google::Protobuf::FieldMask.new paths: ["ttl"]
3537
# Create a request.
@@ -40,8 +42,17 @@ def update_anywhere_cache bucket_name:, anywhere_cache_id:
4042
# The request updates the cache in the specified bucket.
4143
# The cache is identified by the specified ID.
4244
begin
43-
result = storage_control_client.update_anywhere_cache request
44-
puts "AnywhereCache updated - #{result.name}"
45+
operation = storage_control_client.update_anywhere_cache request
46+
if operation.class == Gapic::Operation
47+
puts "AnywhereCache operation created - #{operation.name}"
48+
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
49+
name: name
50+
)
51+
result = storage_control_client.get_anywhere_cache get_request
52+
puts "AnywhereCache #{result.name} updated" if operation.class == Gapic::Operation
53+
else
54+
puts "AnywhereCache update operation failed"
55+
end
4556
rescue StandardError => e
4657
puts "Error updating AnywhereCache: #{e.message}"
4758
end

0 commit comments

Comments
 (0)