gitlab_projects_spec.rb 9.5 KB
Newer Older
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
require 'spec_helper'

describe Gitlab::Git::GitlabProjects do
  after do
    TestEnv.clean_test_path
  end

  let(:project) { create(:project, :repository) }

  if $VERBOSE
    let(:logger) { Logger.new(STDOUT) }
  else
    let(:logger) { double('logger').as_null_object }
  end

  let(:tmp_repos_path) { TestEnv.repos_path }
  let(:repo_name) { project.disk_path + '.git' }
  let(:tmp_repo_path) { File.join(tmp_repos_path, repo_name) }
  let(:gl_projects) { build_gitlab_projects(tmp_repos_path, repo_name) }

  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 '#mv_project' do
    let(:new_repo_path) { File.join(tmp_repos_path, 'repo.git') }

    it 'moves a repo directory' do
      expect(File.exist?(tmp_repo_path)).to be_truthy

      message = "Moving repository from <#{tmp_repo_path}> to <#{new_repo_path}>."
      expect(logger).to receive(:info).with(message)

      expect(gl_projects.mv_project('repo.git')).to be_truthy

      expect(File.exist?(tmp_repo_path)).to be_falsy
      expect(File.exist?(new_repo_path)).to be_truthy
    end

    it "fails if the source path doesn't exist" do
      expect(logger).to receive(:error).with("mv-project failed: source path <#{tmp_repos_path}/bad-src.git> does not exist.")

      result = build_gitlab_projects(tmp_repos_path, 'bad-src.git').mv_project('repo.git')
      expect(result).to be_falsy
    end

    it 'fails if the destination path already exists' do
      FileUtils.mkdir_p(File.join(tmp_repos_path, 'already-exists.git'))

      message = "mv-project failed: destination path <#{tmp_repos_path}/already-exists.git> already exists."
      expect(logger).to receive(:error).with(message)

      expect(gl_projects.mv_project('already-exists.git')).to be_falsy
    end
  end

  describe '#rm_project' do
    it 'removes a repo directory' do
      expect(File.exist?(tmp_repo_path)).to be_truthy
      expect(logger).to receive(:info).with("Removing repository <#{tmp_repo_path}>.")

      expect(gl_projects.rm_project).to be_truthy

      expect(File.exist?(tmp_repo_path)).to be_falsy
    end
  end

  describe '#push_branches' do
    let(:remote_name) { 'remote-name' }
    let(:branch_name) { 'master' }
    let(:cmd) { %W(git 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(git 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' }
    let(:force) { false }
    let(:tags) { true }
    let(:args) { { force: force, tags: tags }.merge(extra_args) }
    let(:extra_args) { {} }
    let(:cmd) { %W(git fetch #{remote_name} --prune --quiet --tags) }

    subject { gl_projects.fetch_remote(remote_name, 600, args) }

    def stub_tempfile(name, filename, opts = {})
      chmod = opts.delete(:chmod)
      file = StringIO.new

      allow(file).to receive(:close!)
      allow(file).to receive(:path).and_return(name)

      expect(Tempfile).to receive(:new).with(filename).and_return(file)
      expect(file).to receive(:chmod).with(chmod) if chmod

      file
    end

    context 'with default args' do
      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
    end

    context 'with --force' do
      let(:force) { true }
      let(:cmd) { %W(git fetch #{remote_name} --prune --quiet --force --tags) }

      it 'executes the command with forced option' do
        stub_spawn(cmd, 600, tmp_repo_path, {}, success: true)

        is_expected.to be_truthy
      end
    end

    context 'with --no-tags' do
      let(:tags) { false }
      let(:cmd) { %W(git fetch #{remote_name} --prune --quiet --no-tags) }

      it 'executes the command' do
        stub_spawn(cmd, 600, tmp_repo_path, {}, success: true)

        is_expected.to be_truthy
      end
    end

    describe 'with an SSH key' do
      let(:extra_args) { { ssh_key: 'SSH KEY' } }

      it 'sets GIT_SSH to a custom script' do
        script = stub_tempfile('scriptFile', 'gitlab-shell-ssh-wrapper', chmod: 0o755)
        key = stub_tempfile('/tmp files/keyFile', 'gitlab-shell-key-file', chmod: 0o400)

        stub_spawn(cmd, 600, tmp_repo_path, { 'GIT_SSH' => 'scriptFile' }, success: true)

        is_expected.to be_truthy

        expect(script.string).to eq("#!/bin/sh\nexec ssh '-oIdentityFile=\"/tmp files/keyFile\"' '-oIdentitiesOnly=\"yes\"' \"$@\"")
        expect(key.string).to eq('SSH KEY')
      end
    end

    describe 'with known_hosts data' do
      let(:extra_args) { { known_hosts: 'KNOWN HOSTS' } }

      it 'sets GIT_SSH to a custom script' do
        script = stub_tempfile('scriptFile', 'gitlab-shell-ssh-wrapper', chmod: 0o755)
        key = stub_tempfile('/tmp files/knownHosts', 'gitlab-shell-known-hosts', chmod: 0o400)

        stub_spawn(cmd, 600, tmp_repo_path, { 'GIT_SSH' => 'scriptFile' }, success: true)

        is_expected.to be_truthy

        expect(script.string).to eq("#!/bin/sh\nexec ssh '-oStrictHostKeyChecking=\"yes\"' '-oUserKnownHostsFile=\"/tmp files/knownHosts\"' \"$@\"")
        expect(key.string).to eq('KNOWN HOSTS')
      end
    end
  end

  describe '#import_project' do
    let(:project) { create(:project) }
    let(:import_url) { TestEnv.factory_repo_path_bare }
    let(:cmd) { %W(git clone --bare -- #{import_url} #{tmp_repo_path}) }
    let(:timeout) { 600 }

    subject { gl_projects.import_project(import_url, timeout) }

    context 'success import' do
      it 'imports a repo' do
        expect(File.exist?(File.join(tmp_repo_path, 'HEAD'))).to be_falsy

        message = "Importing project from <#{import_url}> to <#{tmp_repo_path}>."
        expect(logger).to receive(:info).with(message)

        is_expected.to be_truthy

        expect(File.exist?(File.join(tmp_repo_path, 'HEAD'))).to be_truthy
      end
    end

    context 'already exists' do
      it "doesn't import" do
        FileUtils.mkdir_p(tmp_repo_path)

        is_expected.to be_falsy
      end
    end

    context 'timeout' do
      it 'does not import a repo' do
        stub_spawn_timeout(cmd, timeout, nil)

        message = "Importing project from <#{import_url}> to <#{tmp_repo_path}> failed."
        expect(logger).to receive(:error).with(message)

        is_expected.to be_falsy

        expect(gl_projects.output).to eq("Timed out\n")
        expect(File.exist?(File.join(tmp_repo_path, 'HEAD'))).to be_falsy
      end
    end
  end

  describe '#fork_repository' do
    let(:dest_repos_path) { tmp_repos_path }
    let(:dest_repo_name) { File.join('@hashed', 'aa', 'bb', 'xyz.git') }
    let(:dest_repo) { File.join(dest_repos_path, dest_repo_name) }
    let(:dest_namespace) { File.dirname(dest_repo) }

    subject { gl_projects.fork_repository(dest_repos_path, dest_repo_name) }

    before do
      FileUtils.mkdir_p(dest_repos_path)
    end

    after do
      FileUtils.rm_rf(dest_repos_path)
    end

    it 'forks the repository' do
      message = "Forking repository from <#{tmp_repo_path}> to <#{dest_repo}>."
      expect(logger).to receive(:info).with(message)

      is_expected.to be_truthy

      expect(File.exist?(dest_repo)).to be_truthy
      expect(File.exist?(File.join(dest_repo, 'hooks', 'pre-receive'))).to be_truthy
      expect(File.exist?(File.join(dest_repo, 'hooks', 'post-receive'))).to be_truthy
    end

    it 'does not fork if a project of the same name already exists' do
      # create a fake project at the intended destination
      FileUtils.mkdir_p(dest_repo)

      # trying to fork again should fail as the repo already exists
      message = "fork-repository failed: destination repository <#{dest_repo}> already exists."
      expect(logger).to receive(:error).with(message)

      is_expected.to be_falsy
    end

    context 'different storages' do
      let(:dest_repos_path) { File.join(File.dirname(tmp_repos_path), 'alternative') }

      it 'forks the repo' do
        is_expected.to be_truthy

        expect(File.exist?(dest_repo)).to be_truthy
        expect(File.exist?(File.join(dest_repo, 'hooks', 'pre-receive'))).to be_truthy
        expect(File.exist?(File.join(dest_repo, 'hooks', 'post-receive'))).to be_truthy
      end
    end
  end

  def build_gitlab_projects(*args)
    described_class.new(
      *args,
      global_hooks_path: Gitlab.config.gitlab_shell.hooks_path,
      logger: logger
    )
  end

  def stub_spawn(*args, success: true)
    exitstatus = success ? 0 : nil
    expect(gl_projects).to receive(:popen_with_timeout).with(*args)
      .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
end