summaryrefslogtreecommitdiff
path: root/spec/workers/repository_check/single_repository_worker_spec.rb
blob: ebd03a80826877c4f8e95255fae3d5219c7db951 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'spec_helper'
require 'fileutils'

describe RepositoryCheck::SingleRepositoryWorker do
  subject(:worker) { described_class.new }

  it 'skips when the project repo is empty' do
    project = create(:project, :wiki_disabled)

    expect(worker).not_to receive(:git_fsck)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'succeeds when the project repo is valid' do
    project = create(:project, :repository, :wiki_disabled)

    expect(worker).to receive(:git_fsck).and_call_original

    expect do
      worker.perform(project.id)
    end.to change { project.reload.last_repository_check_at }

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'fails when the project is not empty and a broken repository' do
    project = create(:project, :repository)
    break_project(project)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'fails if the wiki repository is broken' do
    project = create(:project, :wiki_enabled)
    project.create_wiki

    # Test sanity: everything should be fine before the wiki repo is broken
    worker.perform(project.id)
    expect(project.reload.last_repository_check_failed).to eq(false)

    break_wiki(project)
    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'skips wikis when disabled' do
    project = create(:project, :wiki_disabled)
    # Make sure the test would fail if the wiki repo was checked
    break_wiki(project)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'creates missing wikis' do
    project = create(:project, :wiki_enabled)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'does not create a wiki if the main repo does not exist at all' do
    project = create(:project, :repository)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.path)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)

    subject.perform(project.id)

    expect(Gitlab::Shell.new.exists?(project.repository_storage_path, project.wiki.path)).to eq(false)
  end

  def break_wiki(project)
    break_repo(wiki_path(project))
  end

  def wiki_path(project)
    project.wiki.repository.path_to_repo
  end

  def break_project(project)
    break_repo(project.repository.path_to_repo)
  end

  def break_repo(repo)
    # Create or replace blob ffffffffffffffffffffffffffffffffffffffff with an empty file
    # This will make the repo invalid, _and_ 'git init' cannot fix it.
    path = File.join(repo, 'objects', 'ff')
    file = File.join(path, 'ffffffffffffffffffffffffffffffffffffff')

    FileUtils.mkdir_p(path)
    FileUtils.rm_f(file)
    FileUtils.touch(file)
  end
end