BigW Consortium Gitlab

backup_rake_spec.rb 7.61 KB
Newer Older
Hugo Duksis committed
1 2 3 4
require 'spec_helper'
require 'rake'

describe 'gitlab:app namespace rake task' do
5 6
  let(:enable_registry) { true }

Hugo Duksis committed
7
  before :all do
8 9 10 11
    Rake.application.rake_require 'tasks/gitlab/task_helpers'
    Rake.application.rake_require 'tasks/gitlab/backup'
    Rake.application.rake_require 'tasks/gitlab/shell'
    Rake.application.rake_require 'tasks/gitlab/db'
12

Hugo Duksis committed
13 14
    # empty task as env is already loaded
    Rake::Task.define_task :environment
15 16 17

    # We need this directory to run `gitlab:backup:create` task
    FileUtils.mkdir_p('public/uploads')
Hugo Duksis committed
18 19
  end

20 21 22 23
  before do
    stub_container_registry_config(enabled: enable_registry)
  end

24 25 26 27 28
  def run_rake_task(task_name)
    Rake::Task[task_name].reenable
    Rake.application.invoke_task task_name
  end

29
  def reenable_backup_sub_tasks
30
    %w{db repo uploads builds artifacts lfs registry}.each do |subtask|
31 32 33 34
      Rake::Task["gitlab:backup:#{subtask}:create"].reenable
    end
  end

Hugo Duksis committed
35 36 37
  describe 'backup_restore' do
    before do
      # avoid writing task output to spec progress
38
      allow($stdout).to receive :write
Hugo Duksis committed
39 40 41 42
    end

    context 'gitlab version' do
      before do
43 44 45 46 47 48 49 50
        allow(Dir).to receive(:glob).and_return([])
        allow(Dir).to receive(:chdir)
        allow(File).to receive(:exists?).and_return(true)
        allow(Kernel).to receive(:system).and_return(true)
        allow(FileUtils).to receive(:cp_r).and_return(true)
        allow(FileUtils).to receive(:mv).and_return(true)
        allow(Rake::Task["gitlab:shell:setup"]).
          to receive(:invoke).and_return(true)
51
        ENV['force'] = 'yes'
Hugo Duksis committed
52 53
      end

54
      let(:gitlab_version) { Gitlab::VERSION }
Hugo Duksis committed
55

Johannes Schleifenbaum committed
56
      it 'should fail on mismatch' do
57
        allow(YAML).to receive(:load_file).
58
          and_return({ gitlab_version: "not #{gitlab_version}" })
59 60 61

        expect { run_rake_task('gitlab:backup:restore') }.
          to raise_error(SystemExit)
Hugo Duksis committed
62 63
      end

64
      it 'should invoke restoration on match' do
65
        allow(YAML).to receive(:load_file).
66
          and_return({ gitlab_version: gitlab_version })
67 68 69 70 71 72 73
        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:repo:restore']).to receive(:invoke)
        expect(Rake::Task['gitlab:backup:builds:restore']).to receive(:invoke)
        expect(Rake::Task['gitlab:backup:uploads:restore']).to receive(:invoke)
        expect(Rake::Task['gitlab:backup:artifacts:restore']).to receive(:invoke)
        expect(Rake::Task['gitlab:backup:lfs:restore']).to receive(:invoke)
74
        expect(Rake::Task['gitlab:backup:registry:restore']).to receive(:invoke)
75
        expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke)
76
        expect { run_rake_task('gitlab:backup:restore') }.not_to raise_error
Hugo Duksis committed
77 78 79 80
      end
    end

  end # backup_restore task
81 82 83 84 85 86

  describe 'backup_create' do
    def tars_glob
      Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
    end

87
    def create_backup
88
      FileUtils.rm tars_glob
89 90 91 92

      # Redirect STDOUT and run the rake task
      orig_stdout = $stdout
      $stdout = StringIO.new
93
      reenable_backup_sub_tasks
94
      run_rake_task('gitlab:backup:create')
95
      reenable_backup_sub_tasks
96 97
      $stdout = orig_stdout

98
      @backup_tar = tars_glob.first
99 100
    end

101
    before do
102 103 104
      create_backup
    end

105
    after do
106 107 108
      FileUtils.rm(@backup_tar)
    end

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    context 'archive file permissions' do
      it 'should set correct permissions on the tar file' do
        expect(File.exist?(@backup_tar)).to be_truthy
        expect(File::Stat.new(@backup_tar).mode.to_s(8)).to eq('100600')
      end

      context 'with custom archive_permissions' do
        before do
          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

        it 'uses the custom permissions' do
          expect(File::Stat.new(@backup_tar).mode.to_s(8)).to eq('100651')
        end
      end
128 129 130 131
    end

    it 'should set correct permissions on the tar contents' do
      tar_contents, exit_status = Gitlab::Popen.popen(
132
        %W{tar -tvf #{@backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz lfs.tar.gz registry.tar.gz}
133 134 135
      )
      expect(exit_status).to eq(0)
      expect(tar_contents).to match('db/')
136
      expect(tar_contents).to match('uploads.tar.gz')
137
      expect(tar_contents).to match('repositories/')
138
      expect(tar_contents).to match('builds.tar.gz')
139
      expect(tar_contents).to match('artifacts.tar.gz')
140
      expect(tar_contents).to match('lfs.tar.gz')
141 142
      expect(tar_contents).to match('registry.tar.gz')
      expect(tar_contents).not_to match(/^.{4,9}[rwx].* (database.sql.gz|uploads.tar.gz|repositories|builds.tar.gz|artifacts.tar.gz|registry.tar.gz)\/$/)
143 144 145 146
    end

    it 'should delete temp directories' do
      temp_dirs = Dir.glob(
147
        File.join(Gitlab.config.backup.path, '{db,repositories,uploads,builds,artifacts,lfs,registry}')
148 149 150 151
      )

      expect(temp_dirs).to be_empty
    end
152 153 154 155 156 157 158 159 160 161 162 163

    context 'registry disabled' do
      let(:enable_registry) { false }

      it 'should not create registry.tar.gz' do
        tar_contents, exit_status = Gitlab::Popen.popen(
          %W{tar -tvf #{@backup_tar}}
        )
        expect(exit_status).to eq(0)
        expect(tar_contents).not_to match('registry.tar.gz')
      end
    end
164
  end # backup_create task
165 166 167 168 169 170 171 172 173

  describe "Skipping items" do
    def tars_glob
      Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
    end

    before :all do
      @origin_cd = Dir.pwd

174
      reenable_backup_sub_tasks
175

176
      FileUtils.rm tars_glob
177 178 179 180

      # Redirect STDOUT and run the rake task
      orig_stdout = $stdout
      $stdout = StringIO.new
181
      ENV["SKIP"] = "repositories,uploads"
182 183 184
      run_rake_task('gitlab:backup:create')
      $stdout = orig_stdout

185
      @backup_tar = tars_glob.first
186 187 188 189 190 191 192 193
    end

    after :all do
      FileUtils.rm(@backup_tar)
      Dir.chdir @origin_cd
    end

    it "does not contain skipped item" do
194
      tar_contents, _exit_status = Gitlab::Popen.popen(
195
        %W{tar -tvf #{@backup_tar} db uploads.tar.gz repositories builds.tar.gz artifacts.tar.gz lfs.tar.gz registry.tar.gz}
196 197 198
      )

      expect(tar_contents).to match('db/')
199 200
      expect(tar_contents).to match('uploads.tar.gz')
      expect(tar_contents).to match('builds.tar.gz')
201
      expect(tar_contents).to match('artifacts.tar.gz')
202
      expect(tar_contents).to match('lfs.tar.gz')
203
      expect(tar_contents).to match('registry.tar.gz')
204 205 206 207
      expect(tar_contents).not_to match('repositories/')
    end

    it 'does not invoke repositories restore' do
208
      allow(Rake::Task['gitlab:shell:setup']).
209
        to receive(:invoke).and_return(true)
210 211
      allow($stdout).to receive :write

212 213 214 215 216 217 218
      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:repo:restore']).not_to receive :invoke
      expect(Rake::Task['gitlab:backup:uploads:restore']).not_to receive :invoke
      expect(Rake::Task['gitlab:backup:builds:restore']).to receive :invoke
      expect(Rake::Task['gitlab:backup:artifacts:restore']).to receive :invoke
      expect(Rake::Task['gitlab:backup:lfs:restore']).to receive :invoke
219
      expect(Rake::Task['gitlab:backup:registry:restore']).to receive :invoke
220
      expect(Rake::Task['gitlab:shell:setup']).to receive :invoke
221
      expect { run_rake_task('gitlab:backup:restore') }.not_to raise_error
222 223
    end
  end
Hugo Duksis committed
224
end # gitlab:app namespace