File: gitlab_backup_cli_restore_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (82 lines) | stat: -rw-r--r-- 3,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'gitlab-backup-cli restore subcommand', type: :thor do
  subject(:cli) { Gitlab::Backup::Cli::Runner }

  let(:context) { build_fake_context }

  let(:expected_help_output) do
    <<~COMMAND
      Restore commands:
        gitlab-backup-cli restore all BACKUP_ID   # Restores a backup including repositories, database and local files
        gitlab-backup-cli restore help [COMMAND]  # Describe subcommands or one specific subcommand

      Options:
        [--backup-bucket=BACKUP_BUCKET]                                                    # When backing up object storage, this is the bucket to backup to
        [--wait-for-completion], [--no-wait-for-completion], [--skip-wait-for-completion]  # Wait for object storage backups to complete
                                                                                           # Default: true
        [--registry-bucket=REGISTRY_BUCKET]                                                # When backing up registry from object storage, this is the source bucket
        [--service-account-file=SERVICE_ACCOUNT_FILE]                                      # JSON file containing the Google service account credentials
                                                                                           # Default: /etc/gitlab/backup-account-credentials.json

    COMMAND
  end

  context 'with gitlab-backup-cli restore' do
    it 'returns a list of supported commands' do
      expect { cli.start(%w[restore]) }.to output(expected_help_output).to_stdout
    end
  end

  context 'with gitlab-backup-cli restore help' do
    it 'returns a list of supported commands' do
      expect { cli.start(%w[restore help]) }.to output(expected_help_output).to_stdout
    end
  end

  context 'with gitlab-backup-cli restore all', :silence_output do
    let(:restore_subcommand) { Gitlab::Backup::Cli::Commands::RestoreSubcommand }
    let(:executor) { Gitlab::Backup::Cli::RestoreExecutor }
    let(:backup_id) { "1715018771_2024_05_06_17.0.0-pre" }

    before do
      allow(Gitlab::Backup::Cli).to receive(:rails_environment!)

      expect_next_instance_of(restore_subcommand) do |instance|
        allow(instance).to receive(:build_context).and_return(context)
      end
    end

    it 'delegates restore execution to restore executor' do
      # Simulate real execution
      expect_next_instance_of(executor) do |instance|
        expect(instance).to receive(:execute)
      end

      expected_backup_output = /
        .* Restoring\ GitLab\ backup\ #{Regexp.escape(backup_id)}
        .* GitLab\ restoration\ of\ backup\ #{Regexp.escape(backup_id)}\ finished
        .*
      /mx

      expect { cli.start(%W[restore all #{backup_id}]) }.to output(expected_backup_output).to_stdout
    end

    it 'displays an error message when an error is raised' do
      backup_error = Gitlab::Backup::Cli::Error.new('Custom error message')

      # Simulate an error during execution
      expect_next_instance_of(executor) do |instance|
        expect(instance).to receive(:execute).and_raise(backup_error)
      end

      expected_output = /.*GitLab Backup failed: Custom error message.*\)/m

      expect do
        cli.start(%W[restore all #{backup_id}])
      end.to raise_error(SystemExit) { |err| expect(err.status).to eq(1) }.and output(expected_output).to_stderr
    end
  end
end