|
| 1 | +# Copyright 2016 Google, Inc |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require "rspec" |
| 16 | +require "google/cloud" |
| 17 | + |
| 18 | +describe "Logging Quickstart" do |
| 19 | + |
| 20 | + # Simple wait method. Test for condition 5 times, delaying 1 second each time |
| 21 | + def wait_until times: 5, delay: 1, &condition |
| 22 | + times.times do |
| 23 | + return if condition.call |
| 24 | + sleep delay |
| 25 | + end |
| 26 | + raise "Condition not met. Waited #{times} times with #{delay} sec delay" |
| 27 | + end |
| 28 | + |
| 29 | + before do |
| 30 | + @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] |
| 31 | + @logging = @gcloud.logging |
| 32 | + @entry = @logging.entry |
| 33 | + @log_name = "projects/#{ENV["GOOGLE_CLOUD_PROJECT"]}/logs/" + |
| 34 | + "quickstart_log_#{Time.now.to_i}" |
| 35 | + |
| 36 | + @entry.log_name = @log_name |
| 37 | + end |
| 38 | + |
| 39 | + after do |
| 40 | + begin |
| 41 | + @logging.delete_log @log_name |
| 42 | + rescue Google::Cloud::NotFoundError |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + def test_log_entries |
| 47 | + @logging.entries filter: %Q{logName="#{@log_name}"} |
| 48 | + end |
| 49 | + |
| 50 | + it "logs a new entry" do |
| 51 | + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). |
| 52 | + and_return(@gcloud) |
| 53 | + expect(@gcloud).to receive(:logging).and_return(@logging) |
| 54 | + expect(@logging).to receive(:entry).and_return(@entry) |
| 55 | + allow(@entry).to receive(:log_name=).with("my-log") |
| 56 | + |
| 57 | + expect(test_log_entries).to be_empty |
| 58 | + |
| 59 | + expect { |
| 60 | + load File.expand_path("../quickstart.rb", __dir__) |
| 61 | + }.to output( |
| 62 | + "Logged Hello, world!\n" |
| 63 | + ).to_stdout |
| 64 | + |
| 65 | + entries = [] |
| 66 | + |
| 67 | + wait_until { entries = test_log_entries; entries.any? } |
| 68 | + |
| 69 | + expect(entries).not_to be_empty |
| 70 | + expect(entries.length).to eq 1 |
| 71 | + |
| 72 | + entry = entries.first |
| 73 | + expect(entry.payload).to eq "Hello, world!" |
| 74 | + end |
| 75 | +end |
| 76 | + |
0 commit comments