Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable release detection when SDK is not configured to send events #1471

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move release detection test out of configuration specs
  • Loading branch information
st0012 committed Jun 11, 2021
commit f57d8e222eb98bbe453587c6ee11f2e9e6065776
135 changes: 0 additions & 135 deletions sentry-ruby/spec/sentry/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,141 +154,6 @@
end
end

context 'being initialized without a release' do
let(:fake_root) { "/tmp/sentry/" }

before do
allow(File).to receive(:directory?).and_return(false)
allow_any_instance_of(described_class).to receive(:project_root).and_return(fake_root)
end

it 'defaults to nil' do
expect(subject.release).to eq(nil)
end

it 'uses `SENTRY_RELEASE` env variable' do
ENV['SENTRY_RELEASE'] = 'v1'

expect(subject.release).to eq('v1')

ENV.delete('SENTRY_CURRENT_ENV')
end

context "when git is available" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with(".git").and_return(true)
end
it 'gets release from git' do
allow(Sentry).to receive(:`).with("git rev-parse --short HEAD 2>&1").and_return("COMMIT_SHA")

expect(subject.release).to eq('COMMIT_SHA')
end
end

context "when Capistrano is available" do
let(:revision) { "2019010101000" }

before do
Dir.mkdir(fake_root) unless Dir.exist?(fake_root)
File.write(filename, file_content)
end

after do
File.delete(filename)
Dir.delete(fake_root)
end

context "when the REVISION file is present" do
let(:filename) do
File.join(fake_root, "REVISION")
end
let(:file_content) { revision }

it "gets release from the REVISION file" do
expect(subject.release).to eq(revision)
end
end

context "when the revisions.log file is present" do
let(:filename) do
File.join(fake_root, "..", "revisions.log")
end
let(:file_content) do
"Branch master (at COMMIT_SHA) deployed as release #{revision} by alice"
end

it "gets release from the REVISION file" do
expect(subject.release).to eq(revision)
end
end
end

context "when running on heroku" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with("/etc/heroku").and_return(true)
end

context "when it's on heroku ci" do
it "returns nil" do
begin
original_ci_val = ENV["CI"]
ENV["CI"] = "true"

expect(subject.release).to eq(nil)
ensure
ENV["CI"] = original_ci_val
end
end
end

context "when it's not on heroku ci" do
around do |example|
begin
original_ci_val = ENV["CI"]
ENV["CI"] = nil

example.run
ensure
ENV["CI"] = original_ci_val
end
end

it "returns nil + logs an warning if HEROKU_SLUG_COMMIT is not set" do
logger = double("logger")
expect(::Sentry::Logger).to receive(:new).and_return(logger)
expect(logger).to receive(:warn).with(Sentry::LOGGER_PROGNAME) { described_class::HEROKU_DYNO_METADATA_MESSAGE }

expect(described_class.new.release).to eq(nil)
end

it "returns HEROKU_SLUG_COMMIT" do
begin
ENV["HEROKU_SLUG_COMMIT"] = "REVISION"

expect(subject.release).to eq("REVISION")
ensure
ENV["HEROKU_SLUG_COMMIT"] = nil
end
end
end

context "when having an error detecting the release" do
it "logs the error" do
string_io = StringIO.new
logger = Logger.new(string_io)
allow_any_instance_of(described_class).to receive(:logger).and_return(logger)
allow_any_instance_of(described_class).to receive(:detect_release_from_git).and_raise(TypeError.new)

subject

expect(string_io.string).to include("ERROR -- sentry: Error detecting release: TypeError")
end
end
end
end

describe "config: backtrace_cleanup_callback" do
it "defaults to nil" do
expect(subject.backtrace_cleanup_callback).to eq(nil)
Expand Down
149 changes: 149 additions & 0 deletions sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,153 @@
expect(described_class.get_current_scope.user).to eq(id: 1)
end
end

describe 'release detection' do
let(:fake_root) { "/tmp/sentry/" }

before do
allow(File).to receive(:directory?).and_return(false)
allow_any_instance_of(Sentry::Configuration).to receive(:project_root).and_return(fake_root)
end

subject { described_class.configuration }

it 'defaults to nil' do
described_class.init
expect(described_class.configuration.release).to eq(nil)
end

it 'uses `SENTRY_RELEASE` env variable' do
ENV['SENTRY_RELEASE'] = 'v1'

described_class.init
expect(described_class.configuration.release).to eq('v1')

ENV.delete('SENTRY_CURRENT_ENV')
end

context "when git is available" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with(".git").and_return(true)
end
it 'gets release from git' do
allow(Sentry).to receive(:`).with("git rev-parse --short HEAD 2>&1").and_return("COMMIT_SHA")

described_class.init
expect(described_class.configuration.release).to eq('COMMIT_SHA')
end
end

context "when Capistrano is available" do
let(:revision) { "2019010101000" }

before do
Dir.mkdir(fake_root) unless Dir.exist?(fake_root)
File.write(filename, file_content)
end

after do
File.delete(filename)
Dir.delete(fake_root)
end

context "when the REVISION file is present" do
let(:filename) do
File.join(fake_root, "REVISION")
end
let(:file_content) { revision }

it "gets release from the REVISION file" do
described_class.init
expect(described_class.configuration.release).to eq(revision)
end
end

context "when the revisions.log file is present" do
let(:filename) do
File.join(fake_root, "..", "revisions.log")
end
let(:file_content) do
"Branch master (at COMMIT_SHA) deployed as release #{revision} by alice"
end

it "gets release from the REVISION file" do
described_class.init
expect(described_class.configuration.release).to eq(revision)
end
end
end

context "when running on heroku" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with("/etc/heroku").and_return(true)
end

context "when it's on heroku ci" do
it "returns nil" do
begin
original_ci_val = ENV["CI"]
ENV["CI"] = "true"

described_class.init
expect(described_class.configuration.release).to eq(nil)
ensure
ENV["CI"] = original_ci_val
end
end
end

context "when it's not on heroku ci" do
around do |example|
begin
original_ci_val = ENV["CI"]
ENV["CI"] = nil

example.run
ensure
ENV["CI"] = original_ci_val
end
end

it "returns nil + logs an warning if HEROKU_SLUG_COMMIT is not set" do
string_io = StringIO.new
logger = Logger.new(string_io)
allow_any_instance_of(Sentry::Configuration).to receive(:logger).and_return(logger)

described_class.init
expect(described_class.configuration.release).to eq(nil)
expect(string_io.string).to include(Sentry::Configuration::HEROKU_DYNO_METADATA_MESSAGE)
end

it "returns HEROKU_SLUG_COMMIT" do
begin
ENV["HEROKU_SLUG_COMMIT"] = "REVISION"

described_class.init
expect(described_class.configuration.release).to eq("REVISION")
ensure
ENV["HEROKU_SLUG_COMMIT"] = nil
end
end
end

context "when having an error detecting the release" do
it "logs the error" do
string_io = StringIO.new
logger = Logger.new(string_io)
allow_any_instance_of(Sentry::Configuration).to receive(:logger).and_return(logger)
allow_any_instance_of(Sentry::Configuration).to receive(:detect_release_from_git).and_raise(TypeError.new)

described_class.init do |config|
config.logger = logger
end

expect(string_io.string).to include("ERROR -- sentry: Error detecting release: TypeError")
end
end
end
end

end