2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
require 'fileutils'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe RepositoryCheck::SingleRepositoryWorker do
|
2018-10-15 14:42:47 +05:30
|
|
|
subject(:worker) { described_class.new }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it 'skips when the project has no push events' do
|
|
|
|
project = create(:project, :repository, :wiki_disabled)
|
2020-06-23 00:09:42 +05:30
|
|
|
project.events.destroy_all # rubocop: disable Cop/DestroyAll
|
2018-10-15 14:42:47 +05:30
|
|
|
break_project(project)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
expect(worker).not_to receive(:git_fsck)
|
|
|
|
|
|
|
|
worker.perform(project.id)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
expect(project.reload.last_repository_check_failed).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when the project has push events and a broken repository' do
|
2018-10-15 14:42:47 +05:30
|
|
|
project = create(:project, :repository)
|
2016-06-22 15:30:34 +05:30
|
|
|
create_push_event(project)
|
2018-10-15 14:42:47 +05:30
|
|
|
break_project(project)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
worker.perform(project.id)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
expect(project.reload.last_repository_check_failed).to eq(true)
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it 'succeeds when the project repo is valid' do
|
|
|
|
project = create(:project, :repository, :wiki_disabled)
|
|
|
|
create_push_event(project)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
it 'fails if the wiki repository is broken' do
|
2018-10-15 14:42:47 +05:30
|
|
|
project = create(:project, :repository, :wiki_enabled)
|
2016-06-02 11:05:42 +05:30
|
|
|
project.create_wiki
|
2018-10-15 14:42:47 +05:30
|
|
|
create_push_event(project)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
# Test sanity: everything should be fine before the wiki repo is broken
|
2018-10-15 14:42:47 +05:30
|
|
|
worker.perform(project.id)
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(project.reload.last_repository_check_failed).to eq(false)
|
|
|
|
|
|
|
|
break_wiki(project)
|
2018-10-15 14:42:47 +05:30
|
|
|
worker.perform(project.id)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(project.reload.last_repository_check_failed).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips wikis when disabled' do
|
2018-10-15 14:42:47 +05:30
|
|
|
project = create(:project, :wiki_disabled)
|
2016-06-02 11:05:42 +05:30
|
|
|
# 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
|
2018-10-15 14:42:47 +05:30
|
|
|
project = create(:project, :wiki_enabled)
|
2019-12-26 22:10:19 +05:30
|
|
|
TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
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
|
2018-10-15 14:42:47 +05:30
|
|
|
project = create(:project, :repository)
|
2019-12-26 22:10:19 +05:30
|
|
|
TestEnv.rm_storage_dir(project.repository_storage, project.path)
|
|
|
|
TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
subject.perform(project.id)
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
expect(TestEnv.storage_dir_exists?(project.repository_storage, project.wiki.path)).to eq(false)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def create_push_event(project)
|
2020-06-23 00:09:42 +05:30
|
|
|
project.events.create(action: :pushed, author_id: create(:user).id)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def break_wiki(project)
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
break_repo(wiki_path(project))
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_path(project)
|
|
|
|
project.wiki.repository.path_to_repo
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def break_project(project)
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
break_repo(project.repository.path_to_repo)
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
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)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|