Skip to content

Commit dbde800

Browse files
author
remi Taylor
committed
Add specs for Google Cloud Storage bucket samples
1 parent 0c5e0c4 commit dbde800

5 files changed

Lines changed: 84 additions & 26 deletions

File tree

storage/Gemfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
source "https://rubygems.org"
1616

17-
# Google Cloud Client Library for Ruby
18-
gem "gcloud"
17+
gem "google-cloud-storage"
1918

2019
group :test do
2120
gem "rake"

storage/Gemfile.lock

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ GEM
77
digest-crc (0.4.1)
88
faraday (0.9.2)
99
multipart-post (>= 1.2, < 3)
10-
gcloud (0.12.2)
11-
digest-crc (~> 0.4)
12-
google-api-client (~> 0.9.11)
13-
google-protobuf (= 3.0.0.alpha.5.0.5.1)
14-
grpc (= 1.0.0.pre1)
15-
mime-types (>= 2.4, < 4.0)
16-
zonefile (~> 1.04)
17-
google-api-client (0.9.11)
10+
google-api-client (0.9.13)
1811
addressable (~> 2.3)
1912
googleauth (~> 0.5)
2013
httpclient (~> 2.7)
@@ -23,8 +16,11 @@ GEM
2316
mime-types (>= 1.6)
2417
representable (~> 2.3.0)
2518
retriable (~> 2.0)
26-
thor (~> 0.19)
27-
google-protobuf (3.0.0.alpha.5.0.5.1)
19+
google-cloud-core (0.20.1)
20+
google-cloud-storage (0.20.0)
21+
digest-crc (~> 0.4)
22+
google-api-client (~> 0.9.11)
23+
google-cloud-core (~> 0.20.0)
2824
googleauth (0.5.1)
2925
faraday (~> 0.9)
3026
jwt (~> 1.4)
@@ -33,17 +29,14 @@ GEM
3329
multi_json (~> 1.11)
3430
os (~> 0.9)
3531
signet (~> 0.7)
36-
grpc (1.0.0.pre1)
37-
google-protobuf (~> 3.0.0.alpha.5.0.3)
38-
googleauth (~> 0.5.1)
39-
httpclient (2.8.2.2)
32+
httpclient (2.8.2.3)
4033
hurley (0.2)
4134
jwt (1.5.4)
4235
little-plugger (1.1.4)
4336
logging (2.1.0)
4437
little-plugger (~> 1.1)
4538
multi_json (~> 1.10)
46-
memoist (0.14.0)
39+
memoist (0.15.0)
4740
mime-types (3.1)
4841
mime-types-data (~> 3.2015)
4942
mime-types-data (3.2016.0521)
@@ -83,16 +76,14 @@ GEM
8376
faraday (~> 0.9)
8477
jwt (~> 1.5)
8578
multi_json (~> 1.10)
86-
thor (0.19.1)
8779
uber (0.0.15)
8880
unicode-display_width (1.1.0)
89-
zonefile (1.04)
9081

9182
PLATFORMS
9283
ruby
9384

9485
DEPENDENCIES
95-
gcloud
86+
google-cloud-storage
9687
rake
9788
rspec
9889
rubocop

storage/buckets.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def list_buckets project_id:
1616
# [START list_buckets]
1717
# project_id = "Your Google Cloud project ID"
1818

19-
require "gcloud"
19+
require "google/cloud"
2020

21-
gcloud = Gcloud.new project_id
21+
gcloud = Google::Cloud.new project_id
2222
storage = gcloud.storage
2323

2424
storage.buckets.each do |bucket|
@@ -32,9 +32,9 @@ def create_bucket project_id:, bucket_name:
3232
# project_id = "Your Google Cloud project ID"
3333
# bucket_name = "Name of Google Cloud Storage bucket to create"
3434

35-
require "gcloud"
35+
require "google/cloud"
3636

37-
gcloud = Gcloud.new project_id
37+
gcloud = Google::Cloud.new project_id
3838
storage = gcloud.storage
3939
bucket = storage.create_bucket bucket_name
4040

@@ -47,9 +47,9 @@ def delete_bucket project_id:, bucket_name:
4747
# project_id = "Your Google Cloud project ID"
4848
# bucket_name = "Name of your Google Cloud Storage bucket to delete"
4949

50-
require "gcloud"
50+
require "google/cloud"
5151

52-
gcloud = Gcloud.new project_id
52+
gcloud = Google::Cloud.new project_id
5353
storage = gcloud.storage
5454
bucket = storage.bucket bucket_name
5555

storage/spec/buckets_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require_relative "../buckets"
2+
require "rspec"
3+
require "google/cloud"
4+
5+
RSpec.describe "Google Cloud Storage bucket management" do
6+
7+
before :all do
8+
@project_id = ENV["GOOGLE_PROJECT_ID"]
9+
@bucket_name = ENV["STORAGE_BUCKET"]
10+
@gcloud = Google::Cloud.new @project_id
11+
@storage = @gcloud.storage
12+
end
13+
14+
before do
15+
@storage.create_bucket @bucket_name unless @storage.bucket @bucket_name
16+
end
17+
18+
after :all do
19+
# Other tests assume that this bucket exists,
20+
# so create it before exiting this spec suite
21+
@storage.create_bucket @bucket_name unless @storage.bucket(@bucket_name)
22+
end
23+
24+
def delete_bucket!
25+
bucket = @storage.bucket @bucket_name
26+
27+
if bucket
28+
bucket.files.each &:delete until bucket.files.empty?
29+
bucket.delete
30+
end
31+
end
32+
33+
example "list buckets" do
34+
expect {
35+
list_buckets project_id: @project_id
36+
}.to output(
37+
/#{@bucket_name}/
38+
).to_stdout
39+
end
40+
41+
example "create bucket" do
42+
delete_bucket!
43+
44+
expect(@storage.bucket @bucket_name).to be nil
45+
46+
expect {
47+
create_bucket project_id: @project_id,
48+
bucket_name: @bucket_name
49+
}.to output(
50+
"Created bucket: #{@bucket_name}\n"
51+
).to_stdout
52+
53+
expect(@storage.bucket @bucket_name).not_to be nil
54+
end
55+
56+
example "delete bucket" do
57+
expect(@storage.bucket @bucket_name).not_to be nil
58+
59+
expect {
60+
delete_bucket project_id: @project_id, bucket_name: @bucket_name
61+
}.to output(
62+
"Deleted bucket: #{@bucket_name}\n"
63+
).to_stdout
64+
65+
expect(@storage.bucket @bucket_name).to be nil
66+
end
67+
68+
end

0 commit comments

Comments
 (0)