From ad25ef1f0d7e85e2b9797f30721caab00f62dfc2 Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Mon, 15 Jan 2018 20:33:35 +0530 Subject: [PATCH 1/6] List available backups for restore --- lib/backup/manager.rb | 7 ++++++- spec/lib/backup/manager_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index 05aa79dc160..c6c6fca6b74 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -108,7 +108,12 @@ module Backup $progress.puts "Please make sure that file name ends with #{FILE_NAME_SUFFIX}" exit 1 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:' + # print list of available backups + backup_file_list.each do |item| + $progress.puts " " + item.gsub("#{FILE_NAME_SUFFIX}", "") + end + $progress.puts 'Please specify which one you want to restore:' $progress.puts 'rake gitlab:backup:restore BACKUP=timestamp_of_backup' exit 1 end diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index b68301a066a..cd498177b81 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -194,6 +194,13 @@ describe Backup::Manager do ) end + it 'prints the list of available backups' do + expect(progress).to have_received(:puts) + .with(a_string_matching('1451606400_2016_01_01_1.2.3')) + expect(progress).to have_received(:puts) + .with(a_string_matching('1451520000_2015_12_31')) + end + it 'fails the operation and prints an error' do expect { subject.unpack }.to raise_error SystemExit expect(progress).to have_received(:puts) From 295524bb53e4214d9869553cb01810bf928a775a Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Mon, 15 Jan 2018 21:50:24 +0530 Subject: [PATCH 2/6] Run the code to be tested --- spec/lib/backup/manager_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index cd498177b81..108523d8511 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -195,6 +195,7 @@ describe Backup::Manager do end it 'prints the list of available backups' do + subject.unpack expect(progress).to have_received(:puts) .with(a_string_matching('1451606400_2016_01_01_1.2.3')) expect(progress).to have_received(:puts) From 5a8cf78d5141406bdf6dbfa509b345612323d919 Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Mon, 15 Jan 2018 23:52:00 +0530 Subject: [PATCH 3/6] Move conditional to a separate method --- lib/backup/manager.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index c6c6fca6b74..f27ce4d2b2b 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -110,9 +110,7 @@ module Backup elsif backup_file_list.many? && ENV["BACKUP"].nil? $progress.puts 'Found more than one backup:' # print list of available backups - backup_file_list.each do |item| - $progress.puts " " + item.gsub("#{FILE_NAME_SUFFIX}", "") - end + $progress.puts " " + available_timestamps.join("\n ") $progress.puts 'Please specify which one you want to restore:' $progress.puts 'rake gitlab:backup:restore BACKUP=timestamp_of_backup' exit 1 @@ -174,6 +172,10 @@ module Backup @backup_file_list ||= Dir.glob("*#{FILE_NAME_SUFFIX}") end + def available_timestamps + @backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")} + end + def connect_to_remote_directory(connection_settings) # our settings use string keys, but Fog expects symbols connection = ::Fog::Storage.new(connection_settings.symbolize_keys) From dd1bf98cb3e337de1267eecbf5fb73059874ef2a Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Tue, 16 Jan 2018 16:35:20 +0530 Subject: [PATCH 4/6] Try one expectation only --- spec/lib/backup/manager_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index 108523d8511..08f50d411be 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -198,8 +198,6 @@ describe Backup::Manager do subject.unpack expect(progress).to have_received(:puts) .with(a_string_matching('1451606400_2016_01_01_1.2.3')) - expect(progress).to have_received(:puts) - .with(a_string_matching('1451520000_2015_12_31')) end it 'fails the operation and prints an error' do From a3633805829a5ce3e8e255bdc4408af2aa49a83f Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Tue, 16 Jan 2018 17:44:43 +0530 Subject: [PATCH 5/6] Imitate other specs --- spec/lib/backup/manager_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index 08f50d411be..52fc263436d 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -195,7 +195,7 @@ describe Backup::Manager do end it 'prints the list of available backups' do - subject.unpack + expect { subject.unpack }.to raise_error SystemExit expect(progress).to have_received(:puts) .with(a_string_matching('1451606400_2016_01_01_1.2.3')) end From 740d819e8777e70de5827428e711ed5ed81ab177 Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Tue, 16 Jan 2018 19:55:05 +0530 Subject: [PATCH 6/6] Check for both backup files --- spec/lib/backup/manager_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index 52fc263436d..5100f5737c2 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -197,7 +197,7 @@ describe Backup::Manager do it 'prints the list of available backups' do expect { subject.unpack }.to raise_error SystemExit expect(progress).to have_received(:puts) - .with(a_string_matching('1451606400_2016_01_01_1.2.3')) + .with(a_string_matching('1451606400_2016_01_01_1.2.3\n 1451520000_2015_12_31')) end it 'fails the operation and prints an error' do