Commit 567baf0e authored by Rémy Coutable's avatar Rémy Coutable Committed by Winnie Hellmann

Stub ENV in the backup task spec file

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent be2c6657
...@@ -101,50 +101,50 @@ module Backup ...@@ -101,50 +101,50 @@ module Backup
end end
def unpack def unpack
Dir.chdir(backup_path) Dir.chdir(backup_path) do
# check for existing backups in the backup dir
# check for existing backups in the backup dir if backup_file_list.empty?
if backup_file_list.empty? $progress.puts "No backups found in #{backup_path}"
$progress.puts "No backups found in #{backup_path}" $progress.puts "Please make sure that file name ends with #{FILE_NAME_SUFFIX}"
$progress.puts "Please make sure that file name ends with #{FILE_NAME_SUFFIX}" exit 1
exit 1 elsif backup_file_list.many? && ENV["BACKUP"].nil?
elsif backup_file_list.many? && ENV["BACKUP"].nil? $progress.puts 'Found more than one backup, please specify which one you want to restore:'
$progress.puts 'Found more than one backup, please specify which one you want to restore:' $progress.puts 'rake gitlab:backup:restore BACKUP=timestamp_of_backup'
$progress.puts 'rake gitlab:backup:restore BACKUP=timestamp_of_backup' exit 1
exit 1 end
end
tar_file = if ENV['BACKUP'].present? tar_file = if ENV['BACKUP'].present?
"#{ENV['BACKUP']}#{FILE_NAME_SUFFIX}" "#{ENV['BACKUP']}#{FILE_NAME_SUFFIX}"
else else
backup_file_list.first backup_file_list.first
end end
unless File.exist?(tar_file) unless File.exist?(tar_file)
$progress.puts "The backup file #{tar_file} does not exist!" $progress.puts "The backup file #{tar_file} does not exist!"
exit 1 exit 1
end end
$progress.print 'Unpacking backup ... ' $progress.print 'Unpacking backup ... '
unless Kernel.system(*%W(tar -xf #{tar_file})) unless Kernel.system(*%W(tar -xf #{tar_file}))
$progress.puts 'unpacking backup failed'.color(:red) $progress.puts 'unpacking backup failed'.color(:red)
exit 1 exit 1
else else
$progress.puts 'done'.color(:green) $progress.puts 'done'.color(:green)
end end
ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0 ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0
# restoring mismatching backups can lead to unexpected problems # restoring mismatching backups can lead to unexpected problems
if settings[:gitlab_version] != Gitlab::VERSION if settings[:gitlab_version] != Gitlab::VERSION
$progress.puts 'GitLab version mismatch:'.color(:red) $progress.puts 'GitLab version mismatch:'.color(:red)
$progress.puts " Your current GitLab version (#{Gitlab::VERSION}) differs from the GitLab version in the backup!".color(:red) $progress.puts " Your current GitLab version (#{Gitlab::VERSION}) differs from the GitLab version in the backup!".color(:red)
$progress.puts ' Please switch to the following version and try again:'.color(:red) $progress.puts ' Please switch to the following version and try again:'.color(:red)
$progress.puts " version: #{settings[:gitlab_version]}".color(:red) $progress.puts " version: #{settings[:gitlab_version]}".color(:red)
$progress.puts $progress.puts
$progress.puts "Hint: git checkout v#{settings[:gitlab_version]}" $progress.puts "Hint: git checkout v#{settings[:gitlab_version]}"
exit 1 exit 1
end
end end
end end
......
...@@ -172,10 +172,6 @@ describe Backup::Manager do ...@@ -172,10 +172,6 @@ describe Backup::Manager do
end end
describe '#unpack' do describe '#unpack' do
before do
allow(Dir).to receive(:chdir)
end
context 'when there are no backup files in the directory' do context 'when there are no backup files in the directory' do
before do before do
allow(Dir).to receive(:glob).and_return([]) allow(Dir).to receive(:glob).and_return([])
......
...@@ -4,7 +4,15 @@ require 'rake' ...@@ -4,7 +4,15 @@ require 'rake'
describe 'gitlab:app namespace rake task' do describe 'gitlab:app namespace rake task' do
let(:enable_registry) { true } let(:enable_registry) { true }
before :all do def tars_glob
Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
end
def backup_tar
tars_glob.first
end
before(:all) do
Rake.application.rake_require 'tasks/gitlab/helpers' Rake.application.rake_require 'tasks/gitlab/helpers'
Rake.application.rake_require 'tasks/gitlab/backup' Rake.application.rake_require 'tasks/gitlab/backup'
Rake.application.rake_require 'tasks/gitlab/shell' Rake.application.rake_require 'tasks/gitlab/shell'
...@@ -19,9 +27,16 @@ describe 'gitlab:app namespace rake task' do ...@@ -19,9 +27,16 @@ describe 'gitlab:app namespace rake task' do
end end
before do before do
stub_env('force', 'yes')
FileUtils.rm(tars_glob, force: true)
reenable_backup_sub_tasks
stub_container_registry_config(enabled: enable_registry) stub_container_registry_config(enabled: enable_registry)
end end
after do
FileUtils.rm(tars_glob, force: true)
end
def run_rake_task(task_name) def run_rake_task(task_name)
Rake::Task[task_name].reenable Rake::Task[task_name].reenable
Rake.application.invoke_task task_name Rake.application.invoke_task task_name
...@@ -34,22 +49,15 @@ describe 'gitlab:app namespace rake task' do ...@@ -34,22 +49,15 @@ describe 'gitlab:app namespace rake task' do
end end
describe 'backup_restore' do describe 'backup_restore' do
before do
# avoid writing task output to spec progress
allow($stdout).to receive :write
end
context 'gitlab version' do context 'gitlab version' do
before do before do
allow(Dir).to receive(:glob).and_return(['1_gitlab_backup.tar']) allow(Dir).to receive(:glob).and_return(['1_gitlab_backup.tar'])
allow(Dir).to receive(:chdir)
allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:exist?).and_return(true)
allow(Kernel).to receive(:system).and_return(true) allow(Kernel).to receive(:system).and_return(true)
allow(FileUtils).to receive(:cp_r).and_return(true) allow(FileUtils).to receive(:cp_r).and_return(true)
allow(FileUtils).to receive(:mv).and_return(true) allow(FileUtils).to receive(:mv).and_return(true)
allow(Rake::Task["gitlab:shell:setup"]) allow(Rake::Task["gitlab:shell:setup"])
.to receive(:invoke).and_return(true) .to receive(:invoke).and_return(true)
ENV['force'] = 'yes'
end end
let(:gitlab_version) { Gitlab::VERSION } let(:gitlab_version) { Gitlab::VERSION }
...@@ -58,8 +66,9 @@ describe 'gitlab:app namespace rake task' do ...@@ -58,8 +66,9 @@ describe 'gitlab:app namespace rake task' do
allow(YAML).to receive(:load_file) allow(YAML).to receive(:load_file)
.and_return({ gitlab_version: "not #{gitlab_version}" }) .and_return({ gitlab_version: "not #{gitlab_version}" })
expect { run_rake_task('gitlab:backup:restore') } expect do
.to raise_error(SystemExit) expect { run_rake_task('gitlab:backup:restore') }.to output.to_stdout
end.to raise_error(SystemExit)
end end
it 'invokes restoration on match' do it 'invokes restoration on match' do
...@@ -75,44 +84,15 @@ describe 'gitlab:app namespace rake task' do ...@@ -75,44 +84,15 @@ describe 'gitlab:app namespace rake task' do
expect(Rake::Task['gitlab:backup:lfs:restore']).to receive(:invoke) expect(Rake::Task['gitlab:backup:lfs:restore']).to receive(:invoke)
expect(Rake::Task['gitlab:backup:registry:restore']).to receive(:invoke) expect(Rake::Task['gitlab:backup:registry:restore']).to receive(:invoke)
expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke) expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke)
expect { run_rake_task('gitlab:backup:restore') }.not_to raise_error expect { run_rake_task('gitlab:backup:restore') }.to output.to_stdout
end end
end end
end # backup_restore task end # backup_restore task
describe 'backup' do describe 'backup' do
before(:all) do before do
ENV['force'] = 'yes'
end
def tars_glob
Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
end
def create_backup
FileUtils.rm tars_glob
# This reconnect makes our project fixture disappear, breaking the restore. Stub it out. # This reconnect makes our project fixture disappear, breaking the restore. Stub it out.
allow(ActiveRecord::Base.connection).to receive(:reconnect!) allow(ActiveRecord::Base.connection).to receive(:reconnect!)
# Redirect STDOUT and run the rake task
orig_stdout = $stdout
$stdout = StringIO.new
reenable_backup_sub_tasks
run_rake_task('gitlab:backup:create')
reenable_backup_sub_tasks
$stdout = orig_stdout
@backup_tar = tars_glob.first
end
def restore_backup
orig_stdout = $stdout
$stdout = StringIO.new
reenable_backup_sub_tasks
run_rake_task('gitlab:backup:restore')
reenable_backup_sub_tasks
$stdout = orig_stdout
end end
describe 'backup creation and deletion using custom_hooks' do describe 'backup creation and deletion using custom_hooks' do
...@@ -120,27 +100,17 @@ describe 'gitlab:app namespace rake task' do ...@@ -120,27 +100,17 @@ describe 'gitlab:app namespace rake task' do
let(:user_backup_path) { "repositories/#{project.disk_path}" } let(:user_backup_path) { "repositories/#{project.disk_path}" }
before do before do
@origin_cd = Dir.pwd stub_env('SKIP', 'db')
path = File.join(project.repository.path_to_repo, 'custom_hooks')
path = File.join(project.repository.path_to_repo, filename)
FileUtils.mkdir_p(path) FileUtils.mkdir_p(path)
FileUtils.touch(File.join(path, "dummy.txt")) FileUtils.touch(File.join(path, "dummy.txt"))
ENV["SKIP"] = "db"
create_backup
end
after do
ENV["SKIP"] = ""
FileUtils.rm(@backup_tar)
Dir.chdir(@origin_cd)
end end
context 'project uses custom_hooks and successfully creates backup' do context 'project uses custom_hooks and successfully creates backup' do
let(:filename) { "custom_hooks" }
it 'creates custom_hooks.tar and project bundle' do it 'creates custom_hooks.tar and project bundle' do
tar_contents, exit_status = Gitlab::Popen.popen(%W{tar -tvf #{@backup_tar}}) expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
tar_contents, exit_status = Gitlab::Popen.popen(%W{tar -tvf #{backup_tar}})
expect(exit_status).to eq(0) expect(exit_status).to eq(0)
expect(tar_contents).to match(user_backup_path) expect(tar_contents).to match(user_backup_path)
...@@ -149,47 +119,43 @@ describe 'gitlab:app namespace rake task' do ...@@ -149,47 +119,43 @@ describe 'gitlab:app namespace rake task' do
end end
it 'restores files correctly' do it 'restores files correctly' do
restore_backup expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
expect { run_rake_task('gitlab:backup:restore') }.to output.to_stdout
expect(Dir.entries(File.join(project.repository.path, "custom_hooks"))).to include("dummy.txt") expect(Dir.entries(File.join(project.repository.path, 'custom_hooks'))).to include("dummy.txt")
end end
end end
end end
context 'tar creation' do context 'tar creation' do
before do
create_backup
end
after do
FileUtils.rm(@backup_tar)
end
context 'archive file permissions' do context 'archive file permissions' do
it 'sets correct permissions on the tar file' do it 'sets correct permissions on the tar file' do
expect(File.exist?(@backup_tar)).to be_truthy expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
expect(File::Stat.new(@backup_tar).mode.to_s(8)).to eq('100600')
expect(File.exist?(backup_tar)).to be_truthy
expect(File::Stat.new(backup_tar).mode.to_s(8)).to eq('100600')
end end
context 'with custom archive_permissions' do context 'with custom archive_permissions' do
before do before do
allow(Gitlab.config.backup).to receive(:archive_permissions).and_return(0651) allow(Gitlab.config.backup).to receive(:archive_permissions).and_return(0651)
# We created a backup in a before(:all) so it got the default permissions.
# We now need to do some work to create a _new_ backup file using our stub.
FileUtils.rm(@backup_tar)
create_backup
end end
it 'uses the custom permissions' do it 'uses the custom permissions' do
expect(File::Stat.new(@backup_tar).mode.to_s(8)).to eq('100651') expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
expect(File::Stat.new(backup_tar).mode.to_s(8)).to eq('100651')
end end
end end
end end
it 'sets correct permissions on the tar contents' do it 'sets correct permissions on the tar contents' do
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
tar_contents, exit_status = Gitlab::Popen.popen( tar_contents, exit_status = Gitlab::Popen.popen(
%W{tar -tvf #{@backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz registry.tar.gz} %W{tar -tvf #{backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz registry.tar.gz}
) )
expect(exit_status).to eq(0) expect(exit_status).to eq(0)
expect(tar_contents).to match('db/') expect(tar_contents).to match('db/')
expect(tar_contents).to match('uploads.tar.gz') expect(tar_contents).to match('uploads.tar.gz')
...@@ -203,6 +169,8 @@ describe 'gitlab:app namespace rake task' do ...@@ -203,6 +169,8 @@ describe 'gitlab:app namespace rake task' do
end end
it 'deletes temp directories' do it 'deletes temp directories' do
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
temp_dirs = Dir.glob( temp_dirs = Dir.glob(
File.join(Gitlab.config.backup.path, '{db,repositories,uploads,builds,artifacts,pages,lfs,registry}') File.join(Gitlab.config.backup.path, '{db,repositories,uploads,builds,artifacts,pages,lfs,registry}')
) )
...@@ -214,9 +182,12 @@ describe 'gitlab:app namespace rake task' do ...@@ -214,9 +182,12 @@ describe 'gitlab:app namespace rake task' do
let(:enable_registry) { false } let(:enable_registry) { false }
it 'does not create registry.tar.gz' do it 'does not create registry.tar.gz' do
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
tar_contents, exit_status = Gitlab::Popen.popen( tar_contents, exit_status = Gitlab::Popen.popen(
%W{tar -tvf #{@backup_tar}} %W{tar -tvf #{backup_tar}}
) )
expect(exit_status).to eq(0) expect(exit_status).to eq(0)
expect(tar_contents).not_to match('registry.tar.gz') expect(tar_contents).not_to match('registry.tar.gz')
end end
...@@ -232,37 +203,33 @@ describe 'gitlab:app namespace rake task' do ...@@ -232,37 +203,33 @@ describe 'gitlab:app namespace rake task' do
} }
end end
let(:project_a) { create(:project, :repository, repository_storage: 'default') }
let(:project_b) { create(:project, :repository, repository_storage: 'test_second_storage') }
before do before do
FileUtils.mkdir('tmp/tests/default_storage') # We only need a backup of the repositories for this test
FileUtils.mkdir('tmp/tests/custom_storage') stub_env('SKIP', 'db,uploads,builds,artifacts,lfs,registry')
FileUtils.mkdir(Settings.absolute('tmp/tests/default_storage'))
FileUtils.mkdir(Settings.absolute('tmp/tests/custom_storage'))
allow(Gitlab.config.repositories).to receive(:storages).and_return(storages) allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
# Create the projects now, after mocking the settings but before doing the backup
project_a
project_b
# Avoid asking gitaly about the root ref (which will fail beacuse of the # Avoid asking gitaly about the root ref (which will fail beacuse of the
# mocked storages) # mocked storages)
allow_any_instance_of(Repository).to receive(:empty_repo?).and_return(false) allow_any_instance_of(Repository).to receive(:empty_repo?).and_return(false)
# We only need a backup of the repositories for this test
ENV["SKIP"] = "db,uploads,builds,artifacts,lfs,registry"
create_backup
end end
after do after do
FileUtils.rm_rf('tmp/tests/default_storage') FileUtils.rm_rf(Settings.absolute('tmp/tests/default_storage'))
FileUtils.rm_rf('tmp/tests/custom_storage') FileUtils.rm_rf(Settings.absolute('tmp/tests/custom_storage'))
FileUtils.rm(@backup_tar) if @backup_tar
end end
it 'includes repositories in all repository storages' do it 'includes repositories in all repository storages' do
project_a = create(:project, :repository, repository_storage: 'default')
project_b = create(:project, :repository, repository_storage: 'test_second_storage')
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
tar_contents, exit_status = Gitlab::Popen.popen( tar_contents, exit_status = Gitlab::Popen.popen(
%W{tar -tvf #{@backup_tar} repositories} %W{tar -tvf #{backup_tar} repositories}
) )
expect(exit_status).to eq(0) expect(exit_status).to eq(0)
expect(tar_contents).to match("repositories/#{project_a.disk_path}.bundle") expect(tar_contents).to match("repositories/#{project_a.disk_path}.bundle")
expect(tar_contents).to match("repositories/#{project_b.disk_path}.bundle") expect(tar_contents).to match("repositories/#{project_b.disk_path}.bundle")
...@@ -271,35 +238,15 @@ describe 'gitlab:app namespace rake task' do ...@@ -271,35 +238,15 @@ describe 'gitlab:app namespace rake task' do
end # backup_create task end # backup_create task
describe "Skipping items" do describe "Skipping items" do
def tars_glob before do
Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar')) stub_env('SKIP', 'repositories,uploads')
end
before :all do
@origin_cd = Dir.pwd
reenable_backup_sub_tasks
FileUtils.rm tars_glob
# Redirect STDOUT and run the rake task
orig_stdout = $stdout
$stdout = StringIO.new
ENV["SKIP"] = "repositories,uploads"
run_rake_task('gitlab:backup:create')
$stdout = orig_stdout
@backup_tar = tars_glob.first
end
after :all do
FileUtils.rm(@backup_tar)
Dir.chdir @origin_cd
end end
it "does not contain skipped item" do it "does not contain skipped item" do
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
tar_contents, _exit_status = Gitlab::Popen.popen( tar_contents, _exit_status = Gitlab::Popen.popen(
%W{tar -tvf #{@backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz registry.tar.gz} %W{tar -tvf #{backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz registry.tar.gz}
) )
expect(tar_contents).to match('db/') expect(tar_contents).to match('db/')
...@@ -313,9 +260,10 @@ describe 'gitlab:app namespace rake task' do ...@@ -313,9 +260,10 @@ describe 'gitlab:app namespace rake task' do
end end
it 'does not invoke repositories restore' do it 'does not invoke repositories restore' do
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
allow(Rake::Task['gitlab:shell:setup']) allow(Rake::Task['gitlab:shell:setup'])
.to receive(:invoke).and_return(true) .to receive(:invoke).and_return(true)
allow($stdout).to receive :write
expect(Rake::Task['gitlab:db:drop_tables']).to receive :invoke expect(Rake::Task['gitlab:db:drop_tables']).to receive :invoke
expect(Rake::Task['gitlab:backup:db:restore']).to receive :invoke expect(Rake::Task['gitlab:backup:db:restore']).to receive :invoke
...@@ -327,38 +275,15 @@ describe 'gitlab:app namespace rake task' do ...@@ -327,38 +275,15 @@ describe 'gitlab:app namespace rake task' do
expect(Rake::Task['gitlab:backup:lfs:restore']).to receive :invoke expect(Rake::Task['gitlab:backup:lfs:restore']).to receive :invoke
expect(Rake::Task['gitlab:backup:registry:restore']).to receive :invoke expect(Rake::Task['gitlab:backup:registry:restore']).to receive :invoke
expect(Rake::Task['gitlab:shell:setup']).to receive :invoke expect(Rake::Task['gitlab:shell:setup']).to receive :invoke
expect { run_rake_task('gitlab:backup:restore') }.not_to raise_error expect { run_rake_task('gitlab:backup:restore') }.to output.to_stdout
end end
end end
describe "Human Readable Backup Name" do describe "Human Readable Backup Name" do
def tars_glob
Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
end
before :all do
@origin_cd = Dir.pwd
reenable_backup_sub_tasks
FileUtils.rm tars_glob
# Redirect STDOUT and run the rake task
orig_stdout = $stdout
$stdout = StringIO.new
run_rake_task('gitlab:backup:create')
$stdout = orig_stdout
@backup_tar = tars_glob.first
end
after :all do
FileUtils.rm(@backup_tar)
Dir.chdir @origin_cd
end
it 'name has human readable time' do it 'name has human readable time' do
expect(@backup_tar).to match(/\d+_\d{4}_\d{2}_\d{2}_\d+\.\d+\.\d+.*_gitlab_backup.tar$/) expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout
expect(backup_tar).to match(/\d+_\d{4}_\d{2}_\d{2}_\d+\.\d+\.\d+.*_gitlab_backup.tar$/)
end end
end end
end # gitlab:app namespace end # gitlab:app namespace
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