2021-06-08 01:23:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe VersionCheck, :use_clean_rails_memory_store_caching do
|
|
|
|
include ReactiveCachingHelpers
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
describe '.url' do
|
|
|
|
it 'returns the correct URL' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(described_class.url).to match(%r{\A#{Regexp.escape(described_class.host)}/check\.json\?gitlab_info=\w+})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
context 'reactive cache properties' do
|
|
|
|
describe '.reactive_cache_refresh_interval' do
|
|
|
|
it 'returns 12.hours' do
|
|
|
|
expect(described_class.reactive_cache_refresh_interval).to eq(12.hours)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.reactive_cache_lifetime' do
|
|
|
|
it 'returns 7.days' do
|
|
|
|
expect(described_class.reactive_cache_lifetime).to eq(7.days)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
describe '#calculate_reactive_cache' do
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'response code is 200 with valid body' do
|
2022-01-26 12:08:38 +05:30
|
|
|
before do
|
|
|
|
stub_request(:get, described_class.url).to_return(status: 200, body: '{ "status": "success" }', headers: {})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the response object' do
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(described_class.new.calculate_reactive_cache).to eq({ "status" => "success" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'response code is 200 with invalid body' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, described_class.url).to_return(status: 200, body: '{ "invalid: json" }', headers: {})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error hash' do
|
|
|
|
expect(described_class.new.calculate_reactive_cache).to eq(
|
|
|
|
{ error: 'parsing version check response failed', status: 200 }
|
|
|
|
)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'response code is not 200' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, described_class.url).to_return(status: 500, body: nil, headers: {})
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'returns an error hash' do
|
|
|
|
expect(described_class.new.calculate_reactive_cache).to eq({ error: 'version check failed', status: 500 })
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#response' do
|
2023-03-04 22:38:38 +05:30
|
|
|
# see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/106254
|
|
|
|
context "with old string value in cache" do
|
2022-01-26 12:08:38 +05:30
|
|
|
before do
|
2023-03-04 22:38:38 +05:30
|
|
|
old_version_check = described_class.new
|
|
|
|
allow(old_version_check).to receive(:id).and_return(Gitlab::VERSION)
|
|
|
|
write_reactive_cache(old_version_check,
|
|
|
|
"{\"latest_stable_versions\":[],\"latest_version\":\"15.6.2\",\"severity\":\"success\",\"details\":\"\"}"
|
|
|
|
)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'returns nil' do
|
|
|
|
version_check = described_class.new
|
|
|
|
expect(version_check.response).to be_nil
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
# see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/106254
|
|
|
|
context "with non-hash value in cache" do
|
|
|
|
it 'returns nil and invalidates the reactive cache' do
|
|
|
|
version_check = described_class.new
|
|
|
|
stub_reactive_cache(version_check,
|
|
|
|
"{\"latest_stable_versions\":[],\"latest_version\":\"15.6.2\",\"severity\":\"success\",\"details\":\"\"}"
|
|
|
|
)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(version_check).to receive(:refresh_reactive_cache!).and_call_original
|
|
|
|
expect(version_check.response).to be_nil
|
|
|
|
expect(read_reactive_cache(version_check)).to be_nil
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'cache returns value' do
|
|
|
|
it 'returns the response object' do
|
|
|
|
version_check = described_class.new
|
|
|
|
data = { status: 'success' }
|
|
|
|
stub_reactive_cache(version_check, data)
|
|
|
|
|
|
|
|
expect(version_check.response).to eq(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cache returns error' do
|
|
|
|
it 'returns nil and invalidates the reactive cache' do
|
|
|
|
version_check = described_class.new
|
|
|
|
stub_reactive_cache(version_check, error: 'version check failed')
|
|
|
|
|
|
|
|
expect(version_check).to receive(:refresh_reactive_cache!).and_call_original
|
|
|
|
expect(version_check.response).to be_nil
|
|
|
|
expect(read_reactive_cache(version_check)).to be_nil
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|