Welcome to mirror list, hosted at ThFree Co, Russian Federation.

single_repository_worker_spec.rb « repository_check « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8db262598bef5dfb55d354d04e60ab0450245cf (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
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

require 'spec_helper'
require 'fileutils'

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

  before do
    allow(::Gitlab::Git::Repository).to receive(:new).and_call_original
  end

  it 'skips when the project has no push events' do
    project = create(:project, :repository, :wiki_disabled)
    project.events.destroy_all # rubocop: disable Cop/DestroyAll

    repository = instance_double(::Gitlab::Git::Repository)
    allow(::Gitlab::Git::Repository).to receive(:new)
      .with(project.repository_storage, "#{project.disk_path}.git", anything, anything, container: project)
      .and_return(repository)

    worker.perform(project.id)

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

  it 'fails when the project has push events and a broken repository' do
    project = create(:project, :repository)
    create_push_event(project)

    repository = project.repository.raw
    expect(repository).to receive(:fsck).and_raise(::Gitlab::Git::Repository::GitError)
    expect(::Gitlab::Git::Repository).to receive(:new)
      .with(project.repository_storage, "#{project.disk_path}.git", anything, anything, container: project)
      .and_return(repository)

    worker.perform(project.id)

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

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

    repository = project.repository.raw
    expect(repository).to receive(:fsck).and_call_original
    expect(::Gitlab::Git::Repository).to receive(:new)
      .with(project.repository_storage, "#{project.disk_path}.git", anything, anything, container: project)
      .and_return(repository)

    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 if the wiki repository is broken' do
    project = create(:project, :repository, :wiki_enabled)
    project.create_wiki
    create_push_event(project)

    # 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)

    repository = project.wiki.repository.raw
    expect(repository).to receive(:fsck).and_raise(::Gitlab::Git::Repository::GitError)
    expect(::Gitlab::Git::Repository).to receive(:new)
      .with(project.repository_storage, "#{project.disk_path}.wiki.git", anything, anything, container: project.wiki)
      .and_return(repository)

    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
    repository = instance_double(::Gitlab::Git::Repository)
    allow(::Gitlab::Git::Repository).to receive(:new)
      .with(project.repository_storage, "#{project.disk_path}.wiki.git", anything, anything, container: project)
      .and_return(repository)

    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)
    project.wiki.repository.raw.remove

    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)
    project.repository.raw.remove
    project.wiki.repository.raw.remove

    subject.perform(project.id)

    expect(TestEnv.storage_dir_exists?(project.repository_storage, project.wiki.path)).to eq(false)
  end

  def create_push_event(project)
    project.events.create!(action: :pushed, author_id: create(:user).id)
  end
end