Commit f6cc29ed authored by Rémy Coutable's avatar Rémy Coutable

Don't persist ApplicationSetting in test env

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent b038c530
...@@ -9,7 +9,9 @@ module Gitlab ...@@ -9,7 +9,9 @@ module Gitlab
end end
def ensure_application_settings! def ensure_application_settings!
if connect_to_db? return fake_application_settings unless connect_to_db?
unless ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
begin begin
settings = ::ApplicationSetting.current settings = ::ApplicationSetting.current
# In case Redis isn't running or the Redis UNIX socket file is not available # In case Redis isn't running or the Redis UNIX socket file is not available
...@@ -20,15 +22,23 @@ module Gitlab ...@@ -20,15 +22,23 @@ module Gitlab
settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration? settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
end end
settings || fake_application_settings settings || in_memory_application_settings
end end
def sidekiq_throttling_enabled? def sidekiq_throttling_enabled?
current_application_settings.sidekiq_throttling_enabled? current_application_settings.sidekiq_throttling_enabled?
end end
def in_memory_application_settings
@in_memory_application_settings ||= ApplicationSetting.new(ApplicationSetting::DEFAULTS)
# In case migrations the application_settings table is not created yet,
# we fallback to a simple OpenStruct
rescue ActiveRecord::StatementInvalid
fake_application_settings
end
def fake_application_settings def fake_application_settings
ApplicationSetting.new(ApplicationSetting::DEFAULTS) OpenStruct.new(ApplicationSetting::DEFAULTS)
end end
private private
......
module Gitlab module Gitlab
module GithubImport module GithubImport
class ProjectCreator class ProjectCreator
include Gitlab::CurrentSettings
attr_reader :repo, :name, :namespace, :current_user, :session_data, :type attr_reader :repo, :name, :namespace, :current_user, :session_data, :type
def initialize(repo, name, namespace, current_user, session_data, type: 'github') def initialize(repo, name, namespace, current_user, session_data, type: 'github')
...@@ -34,7 +36,7 @@ module Gitlab ...@@ -34,7 +36,7 @@ module Gitlab
end end
def visibility_level def visibility_level
repo.private ? Gitlab::VisibilityLevel::PRIVATE : ApplicationSetting.current.default_project_visibility repo.private ? Gitlab::VisibilityLevel::PRIVATE : current_application_settings.default_project_visibility
end end
# #
......
require 'spec_helper' require 'spec_helper'
describe HealthCheckController do describe HealthCheckController do
include StubENV
let(:token) { current_application_settings.health_check_access_token } let(:token) { current_application_settings.health_check_access_token }
let(:json_response) { JSON.parse(response.body) } let(:json_response) { JSON.parse(response.body) }
let(:xml_response) { Hash.from_xml(response.body)['hash'] } let(:xml_response) { Hash.from_xml(response.body)['hash'] }
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
describe 'GET #index' do describe 'GET #index' do
context 'when services are up but NO access token' do context 'when services are up but NO access token' do
it 'returns a not found page' do it 'returns a not found page' do
......
require 'rails_helper' require 'rails_helper'
feature 'Admin disables Git access protocol', feature: true do feature 'Admin disables Git access protocol', feature: true do
include StubENV
let(:project) { create(:empty_project, :empty_repo) } let(:project) { create(:empty_project, :empty_repo) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
background do background do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
login_as(admin) login_as(admin)
end end
......
require 'spec_helper' require 'spec_helper'
feature "Admin Health Check", feature: true do feature "Admin Health Check", feature: true do
include StubENV
include WaitForAjax include WaitForAjax
before do before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
login_as :admin login_as :admin
end end
...@@ -12,11 +14,12 @@ feature "Admin Health Check", feature: true do ...@@ -12,11 +14,12 @@ feature "Admin Health Check", feature: true do
visit admin_health_check_path visit admin_health_check_path
end end
it { page.has_text? 'Health Check' }
it { page.has_text? 'Health information can be retrieved' }
it 'has a health check access token' do it 'has a health check access token' do
page.has_text? 'Health Check'
page.has_text? 'Health information can be retrieved'
token = current_application_settings.health_check_access_token token = current_application_settings.health_check_access_token
expect(page).to have_content("Access token is #{token}") expect(page).to have_content("Access token is #{token}")
expect(page).to have_selector('#health-check-token', text: token) expect(page).to have_selector('#health-check-token', text: token)
end end
......
require 'spec_helper' require 'spec_helper'
describe "Admin Runners" do describe "Admin Runners" do
include StubENV
before do before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
login_as :admin login_as :admin
end end
......
require 'spec_helper' require 'spec_helper'
feature 'Admin updates settings', feature: true do feature 'Admin updates settings', feature: true do
before(:each) do include StubENV
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
login_as :admin login_as :admin
visit admin_application_settings_path visit admin_application_settings_path
end end
......
require 'rails_helper' require 'rails_helper'
feature 'Admin uses repository checks', feature: true do feature 'Admin uses repository checks', feature: true do
before { login_as :admin } include StubENV
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
login_as :admin
end
scenario 'to trigger a single check' do scenario 'to trigger a single check' do
project = create(:empty_project) project = create(:empty_project)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::CurrentSettings do describe Gitlab::CurrentSettings do
include StubENV
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
describe '#current_application_settings' do describe '#current_application_settings' do
it 'attempts to use cached values first' do context 'with DB available' do
before do
allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(true) allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(true)
expect(ApplicationSetting).to receive(:current).and_return(::ApplicationSetting.create_from_defaults)
expect(ApplicationSetting).not_to receive(:last)
expect(current_application_settings).to be_a(ApplicationSetting)
end end
it 'does not attempt to connect to DB or Redis' do it 'attempts to use cached values first' do
allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(false) expect(ApplicationSetting).to receive(:current)
expect(ApplicationSetting).not_to receive(:current)
expect(ApplicationSetting).not_to receive(:last) expect(ApplicationSetting).not_to receive(:last)
expect(current_application_settings).to eq fake_application_settings expect(current_application_settings).to be_a(ApplicationSetting)
end end
it 'falls back to DB if Redis returns an empty value' do it 'falls back to DB if Redis returns an empty value' do
allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(true)
expect(ApplicationSetting).to receive(:last).and_call_original expect(ApplicationSetting).to receive(:last).and_call_original
expect(current_application_settings).to be_a(ApplicationSetting) expect(current_application_settings).to be_a(ApplicationSetting)
end end
it 'falls back to DB if Redis fails' do it 'falls back to DB if Redis fails' do
allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(true)
expect(ApplicationSetting).to receive(:current).and_raise(::Redis::BaseError) expect(ApplicationSetting).to receive(:current).and_raise(::Redis::BaseError)
expect(ApplicationSetting).to receive(:last).and_call_original expect(ApplicationSetting).to receive(:last).and_call_original
expect(current_application_settings).to be_a(ApplicationSetting) expect(current_application_settings).to be_a(ApplicationSetting)
end end
end end
context 'with DB unavailable' do
before do
allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(false)
end
it 'returns an in-memory ApplicationSetting object' do
expect(ApplicationSetting).not_to receive(:current)
expect(ApplicationSetting).not_to receive(:last)
expect(current_application_settings).to be_a(OpenStruct)
end
end
context 'when ENV["IN_MEMORY_APPLICATION_SETTINGS"] is true' do
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'true')
end
it 'returns an in-memory ApplicationSetting object' do
expect(ApplicationSetting).not_to receive(:current)
expect(ApplicationSetting).not_to receive(:last)
expect(current_application_settings).to be_a(ApplicationSetting)
expect(current_application_settings).not_to be_persisted
end
end
end
end end
...@@ -337,8 +337,7 @@ describe API::Internal, api: true do ...@@ -337,8 +337,7 @@ describe API::Internal, api: true do
context 'ssh access has been disabled' do context 'ssh access has been disabled' do
before do before do
settings = ::ApplicationSetting.create_from_defaults stub_application_setting(enabled_git_access_protocol: 'http')
settings.update_attribute(:enabled_git_access_protocol, 'http')
end end
it 'rejects the SSH push' do it 'rejects the SSH push' do
...@@ -360,8 +359,7 @@ describe API::Internal, api: true do ...@@ -360,8 +359,7 @@ describe API::Internal, api: true do
context 'http access has been disabled' do context 'http access has been disabled' do
before do before do
settings = ::ApplicationSetting.create_from_defaults stub_application_setting(enabled_git_access_protocol: 'ssh')
settings.update_attribute(:enabled_git_access_protocol, 'ssh')
end end
it 'rejects the HTTP push' do it 'rejects the HTTP push' do
...@@ -383,8 +381,7 @@ describe API::Internal, api: true do ...@@ -383,8 +381,7 @@ describe API::Internal, api: true do
context 'web actions are always allowed' do context 'web actions are always allowed' do
it 'allows WEB push' do it 'allows WEB push' do
settings = ::ApplicationSetting.create_from_defaults stub_application_setting(enabled_git_access_protocol: 'ssh')
settings.update_attribute(:enabled_git_access_protocol, 'ssh')
project.team << [user, :developer] project.team << [user, :developer]
push(key, project, 'web') push(key, project, 'web')
......
...@@ -2,6 +2,7 @@ require './spec/simplecov_env' ...@@ -2,6 +2,7 @@ require './spec/simplecov_env'
SimpleCovEnv.start! SimpleCovEnv.start!
ENV["RAILS_ENV"] ||= 'test' ENV["RAILS_ENV"] ||= 'test'
ENV["IN_MEMORY_APPLICATION_SETTINGS"] = 'true'
require File.expand_path("../../config/environment", __FILE__) require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails' require 'rspec/rails'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment