BigW Consortium Gitlab

git_garbage_collect_worker_spec.rb 5.01 KB
Newer Older
1 2
require 'fileutils'

3 4 5
require 'spec_helper'

describe GitGarbageCollectWorker do
6
  let(:project) { create(:project, :repository) }
7 8
  let(:shell) { Gitlab::Shell.new }

9
  subject { described_class.new }
10 11

  describe "#perform" do
12 13 14
    shared_examples 'flushing ref caches' do |gitaly|
      it "flushes ref caches when the task if 'gc'" do
        expect(subject).to receive(:command).with(:gc).and_return([:the, :command])
15

16 17 18 19 20 21 22
        if gitaly
          expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:garbage_collect)
            .and_return(nil)
        else
          expect(Gitlab::Popen).to receive(:popen)
            .with([:the, :command], project.repository.path_to_repo).and_return(["", 0])
        end
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
        expect_any_instance_of(Repository).to receive(:after_create_branch).and_call_original
        expect_any_instance_of(Repository).to receive(:branch_names).and_call_original
        expect_any_instance_of(Repository).to receive(:branch_count).and_call_original
        expect_any_instance_of(Repository).to receive(:has_visible_content?).and_call_original

        subject.perform(project.id)
      end
    end

    context "with Gitaly turned on" do
      it_should_behave_like 'flushing ref caches', true
    end

    context "with Gitaly turned off", skip_gitaly_mock: true do
      it_should_behave_like 'flushing ref caches', false
    end

    context "repack_full" do
      it "calls Gitaly" do
        expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:repack_full)
          .and_return(nil)

        subject.perform(project.id, :full_repack)
      end
    end

    context "repack_incremental" do
      it "calls Gitaly" do
        expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:repack_incremental)
          .and_return(nil)

        subject.perform(project.id, :incremental_repack)
      end
57
    end
58 59

    shared_examples 'gc tasks' do
60 61 62
      before do
        allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled)
      end
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

      it 'incremental repack adds a new packfile' do
        create_objects(project)
        before_packs = packs(project)

        expect(before_packs.count).to be >= 1

        subject.perform(project.id, 'incremental_repack')
        after_packs = packs(project)

        # Exactly one new pack should have been created
        expect(after_packs.count).to eq(before_packs.count + 1)

        # Previously existing packs are still around
        expect(before_packs & after_packs).to eq(before_packs)
      end

      it 'full repack consolidates into 1 packfile' do
        create_objects(project)
        subject.perform(project.id, 'incremental_repack')
        before_packs = packs(project)

        expect(before_packs.count).to be >= 2

        subject.perform(project.id, 'full_repack')
        after_packs = packs(project)

        expect(after_packs.count).to eq(1)

        # Previously existing packs should be gone now
        expect(after_packs - before_packs).to eq(after_packs)

        expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled)
      end

      it 'gc consolidates into 1 packfile and updates packed-refs' do
        create_objects(project)
        before_packs = packs(project)
        before_packed_refs = packed_refs(project)

        expect(before_packs.count).to be >= 1

        subject.perform(project.id, 'gc')
        after_packed_refs = packed_refs(project)
        after_packs = packs(project)

        expect(after_packs.count).to eq(1)

        # Previously existing packs should be gone now
        expect(after_packs - before_packs).to eq(after_packs)

        # The packed-refs file should have been updated during 'git gc'
        expect(before_packed_refs).not_to eq(after_packed_refs)

        expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled)
      end
    end

    context 'with bitmaps enabled' do
      let(:bitmaps_enabled) { true }

      include_examples 'gc tasks'
    end

    context 'with bitmaps disabled' do
      let(:bitmaps_enabled) { false }

      include_examples 'gc tasks'
    end
  end

  # Create a new commit on a random new branch
  def create_objects(project)
    rugged = project.repository.rugged
    old_commit = rugged.branches.first.target
    new_commit_sha = Rugged::Commit.create(
      rugged,
      message: "hello world #{SecureRandom.hex(6)}",
141 142
      author: Gitlab::Git.committer_hash(email: 'foo@bar', name: 'baz'),
      committer: Gitlab::Git.committer_hash(email: 'foo@bar', name: 'baz'),
143
      tree: old_commit.tree,
144
      parents: [old_commit]
145
    )
146
    GitOperationService.new(nil, project.repository).send(
147
      :update_ref,
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      "refs/heads/#{SecureRandom.hex(6)}",
      new_commit_sha,
      Gitlab::Git::BLANK_SHA
    )
  end

  def packs(project)
    Dir["#{project.repository.path_to_repo}/objects/pack/*.pack"]
  end

  def packed_refs(project)
    path = "#{project.repository.path_to_repo}/packed-refs"
    FileUtils.touch(path)
    File.read(path)
  end

  def bitmap_path(pack)
    pack.sub(/\.pack\z/, '.bitmap')
166 167
  end
end