Skip to content

Commit c7ca10c

Browse files
author
remi Taylor
committed
Clean up Stackdriver Logging specs
1 parent 7f33f1e commit c7ca10c

1 file changed

Lines changed: 73 additions & 54 deletions

File tree

logging/spec/sample_spec.rb

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,68 @@
1717
require "gcloud"
1818

1919
describe "Logging sample" do
20+
21+
# Simple wait method. Test for condition 5 times, delaying 1 second each time
22+
def wait_until times: 5, delay: 1, &condition
23+
times.times do
24+
return if condition.call
25+
sleep delay
26+
end
27+
raise "Condition not met. Waited #{times} times with #{delay} sec delay"
28+
end
29+
30+
# Returns entries logged to "my_application_log" in the test project
31+
def my_application_log_entries
32+
@logging.entries(
33+
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
34+
order: "timestamp desc"
35+
)
36+
end
37+
38+
# Tests require environment variables:
39+
#
40+
# GCLOUD_PROJECT ID of your Google Cloud Platform project
41+
# BUCKET Name of Google Cloud Storage bucket to use for log sink
42+
# ALT_BUCKET Name of an alternative bucket to also use for log sink
43+
#
2044
before :all do
21-
@gcloud = Gcloud.new ENV["GCLOUD_PROJECT"]
22-
@logging = @gcloud.logging
23-
@storage = @gcloud.storage
24-
@bucket = @storage.bucket ENV["GCLOUD_PROJECT"]
45+
@project_id = ENV["GCLOUD_PROJECT"]
46+
@gcloud = Gcloud.new @project_id
47+
@logging = @gcloud.logging
48+
@storage = @gcloud.storage
49+
@bucket = @storage.bucket ENV["BUCKET"]
50+
@alt_bucket = @storage.bucket ENV["ALT_BUCKET"]
51+
52+
# Cloud Logging needs owner permissions on the buckets used
53+
@bucket.acl.add_owner "group-cloud-logs@google.com"
54+
@alt_bucket.acl.add_owner "group-cloud-logs@google.com"
2555
end
2656

57+
# Sample code uses project ID "my-gcp-project-id" and bucket
58+
# names "my-logs-bucket" and "new-destination-bucket"
59+
#
60+
# Stub calls to Gcloud library to use our test project and storage buckets
2761
before :each do
2862
cleanup!
2963
allow(Gcloud).to receive(:new).and_call_original
3064
allow(Gcloud).to receive(:new).with("my-gcp-project-id").and_return(@gcloud)
3165
allow(@gcloud).to receive(:logging).and_return(@logging)
3266
allow(@gcloud).to receive(:storage).and_return(@storage)
33-
allow(@storage).to receive(:bucket).with("my-logs-bucket").and_return(@bucket)
3467
allow(@storage).to receive(:create_bucket).and_return(@bucket)
68+
allow(@storage).to receive(:bucket).with("my-logs-bucket").
69+
and_return(@bucket)
70+
allow(@storage).to receive(:bucket).with("new-destination-bucket").
71+
and_return(@alt_bucket)
3572
end
3673

74+
# Delete log sink used by code samples if the test created one
3775
def cleanup!
3876
@logging.sink("my-sink")&.delete
3977
end
4078

4179
it "can create logging client" do
4280
expect(create_logging_client).to be_a Gcloud::Logging::Project
43-
expect(create_logging_client.project).to eq ENV["GCLOUD_PROJECT"]
81+
expect(create_logging_client.project).to eq @project_id
4482
end
4583

4684
it "can list log sinks" do
@@ -60,27 +98,16 @@ def cleanup!
6098
end
6199

62100
it "can update log sink" do
63-
different_bucket = Gcloud.new.storage.bucket ENV["ALTERNATE_BUCKET"]
64-
allow(@storage).to receive(:bucket).with("new-destination-bucket").
65-
and_return(different_bucket)
101+
original_destination = "storage.googleapis.com/#{@bucket.id}"
102+
updated_destination = "storage.googleapis.com/#{@alt_bucket.id}"
66103

67104
create_log_sink
68105

69-
expect(@logging.sink("my-sink").destination).to eq(
70-
"storage.googleapis.com/#{@bucket.id}"
71-
)
72-
expect(@logging.sink("my-sink").destination).not_to eq(
73-
"storage.googleapis.com/#{different_bucket.id}"
74-
)
106+
expect(@logging.sink("my-sink").destination).to eq original_destination
75107

76108
update_log_sink
77109

78-
expect(@logging.sink("my-sink").destination).not_to eq(
79-
"storage.googleapis.com/#{@bucket.id}"
80-
)
81-
expect(@logging.sink("my-sink").destination).to eq(
82-
"storage.googleapis.com/#{different_bucket.id}"
83-
)
110+
expect(@logging.sink("my-sink").destination).to eq updated_destination
84111
end
85112

86113
it "can delete log sink" do
@@ -94,29 +121,22 @@ def cleanup!
94121
end
95122

96123
it "can list log entries" do
97-
# TODO move to constant
98-
# TODO move log name to constant as well
99-
project_id = ENV["GCLOUD_PROJECT"]
100-
101124
write_log_entry
102125

103-
# Wait for entry to be queryable
104-
sleep 3
105-
106-
# Sample queries for entries for "gae_app" resources.
126+
# The code sample queries for entries for "gae_app" resources.
107127
# The test project may not have App Engine resources.
108128
# Instead, add a project log entry and change the filter string called.
109129
allow(@logging).to receive(:entries).
110130
with(filter: %{resource.type = "gae_app"}).
111131
and_wrap_original do |m, *args|
112132
m.call(
113-
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
133+
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
114134
order: "timestamp desc"
115135
)
116136
end
117137

118138
timestamp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [^\\\\]+"
119-
log_name = "projects/#{project_id}/logs/my_application_log"
139+
log_name = "projects/#{@project_id}/logs/my_application_log"
120140
payload = '"Log message"'
121141

122142
expect { list_log_entries }.to output(
@@ -125,7 +145,6 @@ def cleanup!
125145
end
126146

127147
it "can write log entry" do
128-
project_id = ENV["GCLOUD_PROJECT"]
129148
current_time = Time.now.to_f
130149

131150
# Log entries refer to a particular resource
@@ -142,44 +161,43 @@ def cleanup!
142161
entry.payload += " - current time #{current_time}"
143162
entry.resource.type = "project"
144163
entry.resource.labels.clear
145-
entry.resource.labels[:project_id] = project_id
164+
entry.resource.labels[:project_id] = @project_id
146165

147-
m.call(entry)
166+
m.call entry
148167
end
149168

150-
entries = @logging.entries(
151-
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
152-
order: "timestamp desc"
153-
)
169+
entries = my_application_log_entries
154170
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
155171
expect(entry).to be nil
156172

157173
write_log_entry
158174

159175
# Wait for entry to be queryable
160-
sleep 3
176+
wait_until do
177+
my_application_log_entries.any? do |e|
178+
e.payload == "Log message - current time #{current_time}"
179+
end
180+
end
161181

162-
entries = @logging.entries(
163-
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
164-
order: "timestamp desc"
165-
)
182+
entries = my_application_log_entries
166183
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
167184
expect(entry).not_to be nil
168185
expect(entry.payload).to eq "Log message - current time #{current_time}"
169186
expect(entry.severity).to eq :NOTICE
170187
expect(entry.log_name).to eq(
171-
"projects/#{project_id}/logs/my_application_log"
188+
"projects/#{@project_id}/logs/my_application_log"
172189
)
173190
end
174191

175-
it "can delete log"
192+
it "can delete log" do
193+
expect { delete_log }.not_to raise_error
194+
end
176195

177196
it "can write log entry using Ruby Logger" do
178-
project_id = ENV["GCLOUD_PROJECT"]
179197
current_time = Time.now.to_f
180198

181199
entries = @logging.entries(
182-
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
200+
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
183201
order: "timestamp desc"
184202
)
185203
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
@@ -188,7 +206,7 @@ def cleanup!
188206
# Hooked up to real test project
189207
logger = @logging.logger(
190208
"my_application_log",
191-
@logging.resource("project", project_id: ENV["GCLOUD_PROJECT"])
209+
@logging.resource("project", project_id: @project_id)
192210
)
193211

194212
# Log entries refer to a particular resource
@@ -212,18 +230,19 @@ def cleanup!
212230
write_log_entry_using_ruby_logger
213231

214232
# Wait for entry to be queryable
215-
sleep 5
233+
wait_until do
234+
my_application_log_entries.any? do |e|
235+
e.payload == "Log message - current time #{current_time}"
236+
end
237+
end
216238

217-
entries = @logging.entries(
218-
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
219-
order: "timestamp desc"
220-
)
239+
entries = my_application_log_entries
221240
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
222241
expect(entry).not_to be nil
223242
expect(entry.payload).to eq "Log message - current time #{current_time}"
224243
expect(entry.severity).to eq :INFO
225244
expect(entry.log_name).to eq(
226-
"projects/#{project_id}/logs/my_application_log"
245+
"projects/#{@project_id}/logs/my_application_log"
227246
)
228247
end
229248
end

0 commit comments

Comments
 (0)