Skip to content

Commit 7f33f1e

Browse files
author
remi Taylor
committed
Initial Stackdriver Logging sample specs
1 parent c96e94d commit 7f33f1e

2 files changed

Lines changed: 214 additions & 5 deletions

File tree

logging/sample.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def create_log_sink
4141
gcloud = Gcloud.new "my-gcp-project-id"
4242
logging = gcloud.logging
4343
storage = gcloud.storage
44-
bucket = storage.create_bucket "my-logs-bucket"
44+
bucket = storage.create_bucket "my-logs-bucket"
4545

4646
# Grant owner permission to Cloud Logging service
4747
email = "cloud-logs@google.com"
@@ -58,10 +58,11 @@ def update_log_sink
5858
gcloud = Gcloud.new "my-gcp-project-id"
5959
logging = gcloud.logging
6060
storage = gcloud.storage
61-
bucket = storage.bucket "my-logs-bucket"
61+
bucket = storage.bucket "new-destination-bucket"
62+
sink = logging.sink "my-sink"
6263

63-
sink = logging.sink "my-sink"
6464
sink.destination = "storage.googleapis.com/#{bucket.id}"
65+
6566
sink.save
6667
# [END update_log_sink]
6768
end
@@ -134,5 +135,3 @@ def write_log_entry_using_ruby_logger
134135
logger.info "Log message"
135136
# [END write_log_entry_using_ruby_logger]
136137
end
137-
138-
write_log_entry_using_ruby_logger

logging/spec/sample_spec.rb

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,216 @@
1414

1515
require_relative "../sample"
1616
require "rspec"
17+
require "gcloud"
1718

1819
describe "Logging sample" do
20+
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"]
25+
end
26+
27+
before :each do
28+
cleanup!
29+
allow(Gcloud).to receive(:new).and_call_original
30+
allow(Gcloud).to receive(:new).with("my-gcp-project-id").and_return(@gcloud)
31+
allow(@gcloud).to receive(:logging).and_return(@logging)
32+
allow(@gcloud).to receive(:storage).and_return(@storage)
33+
allow(@storage).to receive(:bucket).with("my-logs-bucket").and_return(@bucket)
34+
allow(@storage).to receive(:create_bucket).and_return(@bucket)
35+
end
36+
37+
def cleanup!
38+
@logging.sink("my-sink")&.delete
39+
end
40+
41+
it "can create logging client" do
42+
expect(create_logging_client).to be_a Gcloud::Logging::Project
43+
expect(create_logging_client.project).to eq ENV["GCLOUD_PROJECT"]
44+
end
45+
46+
it "can list log sinks" do
47+
expect { list_log_sinks }.not_to output(/my-sink/).to_stdout
48+
49+
create_log_sink
50+
51+
expect { list_log_sinks }.to output(/my-sink/).to_stdout
52+
end
53+
54+
it "can create log sink" do
55+
expect(@logging.sink "my-sink").to be nil
56+
57+
create_log_sink
58+
59+
expect(@logging.sink "my-sink").not_to be nil
60+
end
61+
62+
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)
66+
67+
create_log_sink
68+
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+
)
75+
76+
update_log_sink
77+
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+
)
84+
end
85+
86+
it "can delete log sink" do
87+
create_log_sink
88+
89+
expect { list_log_sinks }.to output(/my-sink/).to_stdout
90+
91+
delete_log_sink
92+
93+
expect { list_log_sinks }.not_to output(/my-sink/).to_stdout
94+
end
95+
96+
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+
101+
write_log_entry
102+
103+
# Wait for entry to be queryable
104+
sleep 3
105+
106+
# Sample queries for entries for "gae_app" resources.
107+
# The test project may not have App Engine resources.
108+
# Instead, add a project log entry and change the filter string called.
109+
allow(@logging).to receive(:entries).
110+
with(filter: %{resource.type = "gae_app"}).
111+
and_wrap_original do |m, *args|
112+
m.call(
113+
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
114+
order: "timestamp desc"
115+
)
116+
end
117+
118+
timestamp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [^\\\\]+"
119+
log_name = "projects/#{project_id}/logs/my_application_log"
120+
payload = '"Log message"'
121+
122+
expect { list_log_entries }.to output(
123+
%r{\[#{timestamp}\] #{log_name} #{payload}}
124+
).to_stdout
125+
end
126+
127+
it "can write log entry" do
128+
project_id = ENV["GCLOUD_PROJECT"]
129+
current_time = Time.now.to_f
130+
131+
# Log entries refer to a particular resource
132+
# Mock the resource to refer to the test project being used
133+
# Also append the current time to the log message for asserting existence
134+
allow(@logging).to receive(:write_entries).and_wrap_original do |m, entry|
135+
# Verify entry payload and resource from sample
136+
expect(entry.payload).to eq "Log message"
137+
expect(entry.resource.type).to eq "gae_app"
138+
expect(entry.resource.labels[:module_id]).to eq "default"
139+
expect(entry.resource.labels[:version_id]).to eq "20160101t163030"
140+
141+
# Update entry to log to test resource
142+
entry.payload += " - current time #{current_time}"
143+
entry.resource.type = "project"
144+
entry.resource.labels.clear
145+
entry.resource.labels[:project_id] = project_id
146+
147+
m.call(entry)
148+
end
149+
150+
entries = @logging.entries(
151+
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
152+
order: "timestamp desc"
153+
)
154+
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
155+
expect(entry).to be nil
156+
157+
write_log_entry
158+
159+
# Wait for entry to be queryable
160+
sleep 3
161+
162+
entries = @logging.entries(
163+
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
164+
order: "timestamp desc"
165+
)
166+
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
167+
expect(entry).not_to be nil
168+
expect(entry.payload).to eq "Log message - current time #{current_time}"
169+
expect(entry.severity).to eq :NOTICE
170+
expect(entry.log_name).to eq(
171+
"projects/#{project_id}/logs/my_application_log"
172+
)
173+
end
174+
175+
it "can delete log"
176+
177+
it "can write log entry using Ruby Logger" do
178+
project_id = ENV["GCLOUD_PROJECT"]
179+
current_time = Time.now.to_f
180+
181+
entries = @logging.entries(
182+
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
183+
order: "timestamp desc"
184+
)
185+
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
186+
expect(entry).to be nil
187+
188+
# Hooked up to real test project
189+
logger = @logging.logger(
190+
"my_application_log",
191+
@logging.resource("project", project_id: ENV["GCLOUD_PROJECT"])
192+
)
193+
194+
# Log entries refer to a particular resource
195+
# Mock the resource to refer to the test project being used
196+
# Also append the current time to the log message for asserting existence
197+
allow(@logging).to receive(:logger) do |name, resource|
198+
# Verify entry payload and resource from sample
199+
expect(name).to eq "my_application_log"
200+
expect(resource.type).to eq "gae_app"
201+
expect(resource.labels[:module_id]).to eq "default"
202+
expect(resource.labels[:version_id]).to eq "20160101t163030"
203+
logger
204+
end
205+
206+
# Append unique string to logged message for assertion
207+
allow(logger).to receive(:info).and_wrap_original do |m, message|
208+
message += " - current time #{current_time}"
209+
m.call message
210+
end
211+
212+
write_log_entry_using_ruby_logger
213+
214+
# Wait for entry to be queryable
215+
sleep 5
216+
217+
entries = @logging.entries(
218+
filter: %{logName = "projects/#{project_id}/logs/my_application_log"},
219+
order: "timestamp desc"
220+
)
221+
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
222+
expect(entry).not_to be nil
223+
expect(entry.payload).to eq "Log message - current time #{current_time}"
224+
expect(entry.severity).to eq :INFO
225+
expect(entry.log_name).to eq(
226+
"projects/#{project_id}/logs/my_application_log"
227+
)
228+
end
19229
end

0 commit comments

Comments
 (0)