diff options
author | Alejandro Rodríguez <alejorro70@gmail.com> | 2018-10-05 03:45:41 +0300 |
---|---|---|
committer | Alejandro Rodríguez <alejorro70@gmail.com> | 2018-10-05 03:45:41 +0300 |
commit | f03038d301d4c928c425321bfab9b81f4cea58ad (patch) | |
tree | 0df3a5a0a335c0646b8ce59627bbd8e0c194b01a | |
parent | 75643882c8c5e1abb46fe29b5c4ddac3cc0dffeb (diff) | |
parent | 57ee55333267cb11e6acae224c77c84ded7d5d2b (diff) |
Merge branch 'sh-add-gitlab-projects-spec' into 'master'
Add missing specs for gitlab_projects.rb
See merge request gitlab-org/gitaly!897
-rw-r--r-- | ruby/lib/gitlab/git/gitlab_projects.rb | 62 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb | 49 |
2 files changed, 47 insertions, 64 deletions
diff --git a/ruby/lib/gitlab/git/gitlab_projects.rb b/ruby/lib/gitlab/git/gitlab_projects.rb index d5ad8c67e..14ea2c61c 100644 --- a/ruby/lib/gitlab/git/gitlab_projects.rb +++ b/ruby/lib/gitlab/git/gitlab_projects.rb @@ -66,22 +66,6 @@ module Gitlab end end - def shard_path - strong_memoize(:shard_path) do - Gitlab.config.repositories.storages.fetch(shard_name).legacy_disk_path - end - end - - # Import project via git clone --bare - # URL must be publicly cloneable - def import_project(source, timeout) - git_import_repository(source, timeout) - end - - def fork_repository(new_shard_name, new_repository_relative_path) - git_fork_repository(new_shard_name, new_repository_relative_path) - end - def fetch_remote(name, timeout, force:, tags:, ssh_key: nil, known_hosts: nil, prune: true) logger.info "Fetching remote #{name} for repository #{repository_absolute_path}." cmd = fetch_remote_command(name, tags, prune, force) @@ -224,52 +208,6 @@ module Gitlab cmd << (tags ? '--tags' : '--no-tags') end end - - def git_import_repository(source, timeout) - # Skip import if repo already exists - return false if File.exist?(repository_absolute_path) - - masked_source = mask_password_in_url(source) - - logger.info "Importing project from <#{masked_source}> to <#{repository_absolute_path}>." - cmd = %W(#{Gitlab.config.git.bin_path} clone --bare -- #{source} #{repository_absolute_path}) - - success = run_with_timeout(cmd, timeout, nil) - - unless success - logger.error("Importing project from <#{masked_source}> to <#{repository_absolute_path}> failed.") - FileUtils.rm_rf(repository_absolute_path) - return false - end - - Gitlab::Git::Repository.create_hooks(repository_absolute_path, global_hooks_path) - - # The project was imported successfully. - # Remove the origin URL since it may contain password. - remove_origin_in_repo - - true - end - - def git_fork_repository(new_shard_name, new_repository_relative_path) - from_path = repository_absolute_path - new_shard_path = Gitlab.config.repositories.storages.fetch(new_shard_name).legacy_disk_path - to_path = File.join(new_shard_path, new_repository_relative_path) - - # The repository cannot already exist - if File.exist?(to_path) - logger.error "fork-repository failed: destination repository <#{to_path}> already exists." - return false - end - - # Ensure the namepsace / hashed storage directory exists - FileUtils.mkdir_p(File.dirname(to_path), mode: 0770) - - logger.info "Forking repository from <#{from_path}> to <#{to_path}>." - cmd = %W(#{Gitlab.config.git.bin_path} clone --bare --no-local -- #{from_path} #{to_path}) - - run(cmd, nil) && Gitlab::Git::Repository.create_hooks(to_path, global_hooks_path) - end end end end diff --git a/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb b/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb index dfb0be80a..cc62e12a8 100644 --- a/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb +++ b/ruby/spec/lib/gitlab/git/gitlab_projects_spec.rb @@ -6,8 +6,9 @@ describe Gitlab::Git::GitlabProjects do let(:repository) { gitlab_git_from_gitaly(test_repo_read_only) } let(:repo_name) { 'gitlab-test.git' } let(:gl_projects) { build_gitlab_projects(DEFAULT_STORAGE_DIR, repo_name) } - let(:hooks_path) { '/tmp/test/hooks' } + let(:hooks_path) { File.join(tmp_repo_path, 'hooks') } let(:tmp_repo_path) { TEST_REPO_PATH } + let(:tmp_repos_path) { DEFAULT_STORAGE_DIR } if $VERBOSE let(:logger) { Logger.new(STDOUT) } @@ -29,6 +30,50 @@ describe Gitlab::Git::GitlabProjects do .and_return(["output", exitstatus]) end + def stub_spawn_timeout(*args) + expect(gl_projects).to receive(:popen_with_timeout).with(*args) + .and_raise(Timeout::Error) + end + + describe '#initialize' do + it { expect(gl_projects.shard_path).to eq(tmp_repos_path) } + it { expect(gl_projects.repository_relative_path).to eq(repo_name) } + it { expect(gl_projects.repository_absolute_path).to eq(File.join(tmp_repos_path, repo_name)) } + it { expect(gl_projects.logger).to eq(logger) } + end + + describe '#push_branches' do + let(:remote_name) { 'remote-name' } + let(:branch_name) { 'master' } + let(:cmd) { %W(#{Gitlab.config.git.bin_path} push -- #{remote_name} #{branch_name}) } + let(:force) { false } + + subject { gl_projects.push_branches(remote_name, 600, force, [branch_name]) } + + it 'executes the command' do + stub_spawn(cmd, 600, tmp_repo_path, success: true) + + is_expected.to be_truthy + end + + it 'fails' do + stub_spawn(cmd, 600, tmp_repo_path, success: false) + + is_expected.to be_falsy + end + + context 'with --force' do + let(:cmd) { %W(#{Gitlab.config.git.bin_path} push --force -- #{remote_name} #{branch_name}) } + let(:force) { true } + + it 'executes the command' do + stub_spawn(cmd, 600, tmp_repo_path, success: true) + + is_expected.to be_truthy + end + end + end + describe '#fetch_remote' do let(:remote_name) { 'remote-name' } let(:branch_name) { 'master' } @@ -61,7 +106,7 @@ describe Gitlab::Git::GitlabProjects do is_expected.to be_truthy end - it 'fails' do + it 'returns false if the command fails' do stub_spawn(cmd, 600, tmp_repo_path, {}, success: false) is_expected.to be_falsy |