From 00c8c190b0b1694f2512e2842208d8bb5d05b2d1 Mon Sep 17 00:00:00 2001 From: Igor Drozdov <idrozdov@gitlab.com> Date: Mon, 24 Jul 2023 19:53:11 +0200 Subject: [PATCH] Use stub_env instead of stubbing ENV const --- lib/backup/gitaly_backup.rb | 6 +++++- spec/lib/backup/database_spec.rb | 14 +++++++------- spec/lib/backup/gitaly_backup_spec.rb | 5 +---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/backup/gitaly_backup.rb b/lib/backup/gitaly_backup.rb index 53c998efd716..bb9f45121e9b 100644 --- a/lib/backup/gitaly_backup.rb +++ b/lib/backup/gitaly_backup.rb @@ -107,7 +107,11 @@ def build_env 'SSL_CERT_FILE' => Gitlab::X509::Certificate.default_cert_file, 'SSL_CERT_DIR' => Gitlab::X509::Certificate.default_cert_dir, 'GITALY_SERVERS' => gitaly_servers_encoded - }.merge(ENV) + }.merge(current_env) + end + + def current_env + ENV end def started? diff --git a/spec/lib/backup/database_spec.rb b/spec/lib/backup/database_spec.rb index dd8a4a14531c..e0ca090d96a3 100644 --- a/spec/lib/backup/database_spec.rb +++ b/spec/lib/backup/database_spec.rb @@ -272,14 +272,13 @@ end context 'with PostgreSQL settings defined in the environment' do - let(:cmd) { %W[#{Gem.ruby} -e] + ["$stderr.puts ENV.to_h.select { |k, _| k.start_with?('PG') }"] } let(:config) { YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))['test'] } before do - stub_const 'ENV', ENV.to_h.merge({ + stub_env(ENV.to_h.merge({ 'GITLAB_BACKUP_PGHOST' => 'test.example.com', 'PGPASSWORD' => 'donotchange' - }) + })) end it 'overrides default config values' do @@ -289,12 +288,13 @@ expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) end + expect(ENV).to receive(:[]=).with('PGHOST', 'test.example.com') + expect(ENV).not_to receive(:[]=).with('PGPASSWORD', anything) + subject.restore(backup_dir) - expect(output).to include(%("PGHOST"=>"test.example.com")) - expect(output).to include(%("PGPASSWORD"=>"donotchange")) - expect(output).to include(%("PGPORT"=>"#{config['port']}")) if config['port'] - expect(output).to include(%("PGUSER"=>"#{config['username']}")) if config['username'] + expect(ENV['PGPORT']).to eq(config['port']) if config['port'] + expect(ENV['PGUSER']).to eq(config['username']) if config['username'] end end diff --git a/spec/lib/backup/gitaly_backup_spec.rb b/spec/lib/backup/gitaly_backup_spec.rb index 172fc28dd3e5..40e835e9dc8e 100644 --- a/spec/lib/backup/gitaly_backup_spec.rb +++ b/spec/lib/backup/gitaly_backup_spec.rb @@ -132,11 +132,8 @@ ) end - before do - stub_const('ENV', ssl_env) - end - it 'passes through SSL envs' do + expect(subject).to receive(:current_env).and_return(ssl_env) expect(Open3).to receive(:popen2).with(expected_env, anything, 'create', '-path', anything, '-layout', 'pointer', '-id', backup_id).and_call_original subject.start(:create, destination, backup_id: backup_id) -- GitLab