File: gitlab_backup_cli_backup_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 (76 lines) | stat: -rw-r--r-- 3,179 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
# frozen_string_literal: true

require 'spec_helper'

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

  let(:context) { build_fake_context }

  let(:expected_help_output) do
    <<~COMMAND
      Backup commands:
        gitlab-backup-cli backup all             # Creates a backup including repositories, database and local files
        gitlab-backup-cli backup 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 backup' do
    it 'returns a list of supported commands' do
      expect { cli.start(%w[backup]) }.to output(expected_help_output).to_stdout
    end
  end

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

  context 'with gitlab-backup-cli backup all', :silence_output do
    let(:backup_subcommand) { Gitlab::Backup::Cli::Commands::BackupSubcommand }
    let(:executor) { Gitlab::Backup::Cli::BackupExecutor }

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

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

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

      expected_backup_output = /.*Starting GitLab backup.*Backup finished:.*/m
      expect { cli.start(%w[backup all]) }.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[backup all])
      end.to raise_error(SystemExit) { |err| expect(err.status).to eq(1) }.and output(expected_output).to_stderr
    end
  end
end