Commit f70d7e4e authored by Toon Claes's avatar Toon Claes

Refactor RepositoryCheck::SingleRepositoryWorker

A repo does not need pushes before it will be repo-checked. There are
other sources where commits can come from (e.g. import or webIDE).

In the test I made the method of breaking a repo more robust. And
added a few test examples.

See also gitlab-org/gitlab-ce#45046.
parent b07c4e3d
...@@ -14,18 +14,24 @@ module RepositoryCheck ...@@ -14,18 +14,24 @@ module RepositoryCheck
private private
def check(project) def check(project)
if has_pushes?(project) && !git_fsck(project.repository) check_repo(project) && check_wiki_repo(project)
false end
elsif project.wiki_enabled?
# Historically some projects never had their wiki repos initialized; def check_repo(project)
# this happens on project creation now. Let's initialize an empty repo return true if project.empty_repo?
# if it is not already there.
project.create_wiki git_fsck(project.repository)
end
git_fsck(project.wiki.repository)
else def check_wiki_repo(project)
true return true unless project.wiki_enabled?
end
# Historically some projects never had their wiki repos initialized;
# this happens on project creation now. Let's initialize an empty repo
# if it is not already there.
project.create_wiki
git_fsck(project.wiki.repository)
end end
def git_fsck(repository) def git_fsck(repository)
......
---
title: Small improvements to repository checks
merge_request: 18484
author:
type: changed
...@@ -2,44 +2,55 @@ require 'spec_helper' ...@@ -2,44 +2,55 @@ require 'spec_helper'
require 'fileutils' require 'fileutils'
describe RepositoryCheck::SingleRepositoryWorker do describe RepositoryCheck::SingleRepositoryWorker do
subject { described_class.new } subject(:worker) { described_class.new }
it 'passes when the project has no push events' do it 'skips when the project repo is empty' do
project = create(:project_empty_repo, :wiki_disabled) project = create(:project, :wiki_disabled)
project.events.destroy_all
break_repo(project)
subject.perform(project.id) expect(worker).not_to receive(:git_fsck)
worker.perform(project.id)
expect(project.reload.last_repository_check_failed).to eq(false) expect(project.reload.last_repository_check_failed).to eq(false)
end end
it 'fails when the project has push events and a broken repository' do it 'succeeds when the project repo is valid' do
project = create(:project_empty_repo) project = create(:project, :repository, :wiki_disabled)
create_push_event(project)
break_repo(project)
subject.perform(project.id) 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) expect(project.reload.last_repository_check_failed).to eq(true)
end end
it 'fails if the wiki repository is broken' do it 'fails if the wiki repository is broken' do
project = create(:project_empty_repo, :wiki_enabled) project = create(:project, :wiki_enabled)
project.create_wiki project.create_wiki
# Test sanity: everything should be fine before the wiki repo is broken # Test sanity: everything should be fine before the wiki repo is broken
subject.perform(project.id) worker.perform(project.id)
expect(project.reload.last_repository_check_failed).to eq(false) expect(project.reload.last_repository_check_failed).to eq(false)
break_wiki(project) break_wiki(project)
subject.perform(project.id) worker.perform(project.id)
expect(project.reload.last_repository_check_failed).to eq(true) expect(project.reload.last_repository_check_failed).to eq(true)
end end
it 'skips wikis when disabled' do it 'skips wikis when disabled' do
project = create(:project_empty_repo, :wiki_disabled) project = create(:project, :wiki_disabled)
# Make sure the test would fail if the wiki repo was checked # Make sure the test would fail if the wiki repo was checked
break_wiki(project) break_wiki(project)
...@@ -49,8 +60,8 @@ describe RepositoryCheck::SingleRepositoryWorker do ...@@ -49,8 +60,8 @@ describe RepositoryCheck::SingleRepositoryWorker do
end end
it 'creates missing wikis' do it 'creates missing wikis' do
project = create(:project_empty_repo, :wiki_enabled) project = create(:project, :wiki_enabled)
FileUtils.rm_rf(wiki_path(project)) Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)
subject.perform(project.id) subject.perform(project.id)
...@@ -58,34 +69,35 @@ describe RepositoryCheck::SingleRepositoryWorker do ...@@ -58,34 +69,35 @@ describe RepositoryCheck::SingleRepositoryWorker do
end end
it 'does not create a wiki if the main repo does not exist at all' do it 'does not create a wiki if the main repo does not exist at all' do
project = create(:project_empty_repo) project = create(:project, :repository)
create_push_event(project) Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.path)
FileUtils.rm_rf(project.repository.path_to_repo) Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)
FileUtils.rm_rf(wiki_path(project))
subject.perform(project.id) subject.perform(project.id)
expect(File.exist?(wiki_path(project))).to eq(false) expect(Gitlab::Shell.new.exists?(project.repository_storage_path, project.wiki.path)).to eq(false)
end end
def break_wiki(project) def break_wiki(project)
objects_dir = wiki_path(project) + '/objects' break_repo(wiki_path(project))
# Replace the /objects directory with a file so that the repo is
# invalid, _and_ 'git init' cannot fix it.
FileUtils.rm_rf(objects_dir)
FileUtils.touch(objects_dir) if File.directory?(wiki_path(project))
end end
def wiki_path(project) def wiki_path(project)
project.wiki.repository.path_to_repo project.wiki.repository.path_to_repo
end end
def create_push_event(project) def break_project(project)
project.events.create(action: Event::PUSHED, author_id: create(:user).id) break_repo(project.repository.path_to_repo)
end end
def break_repo(project) def break_repo(repo)
FileUtils.rm_rf(File.join(project.repository.path_to_repo, 'objects')) # 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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment