| 
1 | 1 | require_relative 'spec_helper'  | 
2 | 2 | require_relative '../lib/gitlab_projects'  | 
 | 3 | +require_relative '../lib/gitlab_reference_counter'  | 
3 | 4 | 
 
  | 
4 | 5 | describe GitlabProjects do  | 
5 | 6 |  before do  | 
 | 
322 | 323 |  let(:pid) { 1234 }  | 
323 | 324 |  let(:branch_name) { 'master' }  | 
324 | 325 | 
 
  | 
 | 326 | + def stub_spawn(*args, wait: true, success: true)  | 
 | 327 | + expect(Process).to receive(:spawn).with(*args).and_return(pid)  | 
 | 328 | + expect(Process).to receive(:wait2).with(pid).and_return([pid, double(success?: success)]) if wait  | 
 | 329 | + end  | 
 | 330 | + | 
 | 331 | + def stub_env(args = {})  | 
 | 332 | + original = ENV.to_h  | 
 | 333 | + args.each { |k, v| ENV[k] = v }  | 
 | 334 | + yield  | 
 | 335 | + ensure  | 
 | 336 | + ENV.replace(original)  | 
 | 337 | + end  | 
 | 338 | + | 
 | 339 | + def stub_tempfile(name, *args)  | 
 | 340 | + file = StringIO.new  | 
 | 341 | + allow(file).to receive(:close!)  | 
 | 342 | + allow(file).to receive(:path).and_return(name)  | 
 | 343 | + | 
 | 344 | + expect(Tempfile).to receive(:new).with(*args).and_return(file)  | 
 | 345 | + | 
 | 346 | + file  | 
 | 347 | + end  | 
 | 348 | + | 
325 | 349 |  describe 'with default args' do  | 
326 | 350 |  let(:gl_projects) { build_gitlab_projects('fetch-remote', repos_path, project_name, remote_name, '600') }  | 
327 | 351 |  let(:cmd) { %W(git --git-dir=#{full_path} fetch #{remote_name} --prune --quiet --tags) }  | 
328 | 352 | 
 
  | 
329 | 353 |  it 'executes the command' do  | 
330 |  | - expect(Process).to receive(:spawn).with(*cmd).and_return(pid)  | 
331 |  | - expect(Process).to receive(:wait).with(pid)  | 
 | 354 | + stub_spawn({}, *cmd)  | 
332 | 355 | 
 
  | 
333 | 356 |  expect(gl_projects.exec).to be true  | 
334 | 357 |  end  | 
335 | 358 | 
 
  | 
336 | 359 |  it 'raises timeout' do  | 
 | 360 | + stub_spawn({}, *cmd, wait: false)  | 
337 | 361 |  expect(Timeout).to receive(:timeout).with(600).and_raise(Timeout::Error)  | 
338 |  | - expect(Process).to receive(:spawn).with(*cmd).and_return(pid)  | 
339 |  | - expect(Process).to receive(:wait)  | 
340 | 362 |  expect(Process).to receive(:kill).with('KILL', pid)  | 
 | 363 | + | 
341 | 364 |  expect(gl_projects.exec).to be false  | 
342 | 365 |  end  | 
343 | 366 |  end  | 
344 | 367 | 
 
  | 
345 | 368 |  describe 'with --force' do  | 
346 | 369 |  let(:gl_projects) { build_gitlab_projects('fetch-remote', repos_path, project_name, remote_name, '600', '--force') }  | 
 | 370 | + let(:env) { {} }  | 
347 | 371 |  let(:cmd) { %W(git --git-dir=#{full_path} fetch #{remote_name} --prune --quiet --force --tags) }  | 
348 | 372 | 
 
  | 
349 | 373 |  it 'executes the command with forced option' do  | 
350 |  | - expect(Process).to receive(:spawn).with(*cmd).and_return(pid)  | 
351 |  | - expect(Process).to receive(:wait).with(pid)  | 
 | 374 | + stub_spawn({}, *cmd)  | 
352 | 375 | 
 
  | 
353 | 376 |  expect(gl_projects.exec).to be true  | 
354 | 377 |  end  | 
 | 
359 | 382 |  let(:cmd) { %W(git --git-dir=#{full_path} fetch #{remote_name} --prune --quiet --no-tags) }  | 
360 | 383 | 
 
  | 
361 | 384 |  it 'executes the command' do  | 
362 |  | - expect(Process).to receive(:spawn).with(*cmd).and_return(pid)  | 
363 |  | - expect(Process).to receive(:wait).with(pid)  | 
 | 385 | + stub_spawn({}, *cmd)  | 
364 | 386 | 
 
  | 
365 | 387 |  expect(gl_projects.exec).to be true  | 
366 | 388 |  end  | 
367 | 389 |  end  | 
 | 390 | + | 
 | 391 | + describe 'with GITLAB_SHELL_SSH_KEY' do  | 
 | 392 | + let(:gl_projects) { build_gitlab_projects('fetch-remote', repos_path, project_name, remote_name, '600') }  | 
 | 393 | + let(:cmd) { %W(git --git-dir=#{full_path} fetch #{remote_name} --prune --quiet --tags) }  | 
 | 394 | + | 
 | 395 | + around(:each) do |example|  | 
 | 396 | + stub_env('GITLAB_SHELL_SSH_KEY' => 'SSH KEY') { example.run }  | 
 | 397 | + end  | 
 | 398 | + | 
 | 399 | + it 'sets GIT_SSH to a custom script' do  | 
 | 400 | + script = stub_tempfile('scriptFile', 'gitlab-shell-ssh-wrapper', mode: 0755)  | 
 | 401 | + key = stub_tempfile('/tmp files/keyFile', 'gitlab-shell-key-file', mode: 0400)  | 
 | 402 | + | 
 | 403 | + stub_spawn({ 'GIT_SSH' => 'scriptFile' }, *cmd)  | 
 | 404 | + | 
 | 405 | + expect(gl_projects.exec).to be true  | 
 | 406 | + | 
 | 407 | + expect(script.string).to eq("#!/bin/sh\nexec ssh '-oIdentityFile=\"/tmp files/keyFile\"' '-oIdentitiesOnly=\"true\"' \"$@\"")  | 
 | 408 | + expect(key.string).to eq('SSH KEY')  | 
 | 409 | + end  | 
 | 410 | + end  | 
 | 411 | + | 
 | 412 | + describe 'with GITLAB_SHELL_KNOWN_HOSTS' do  | 
 | 413 | + let(:gl_projects) { build_gitlab_projects('fetch-remote', repos_path, project_name, remote_name, '600') }  | 
 | 414 | + let(:cmd) { %W(git --git-dir=#{full_path} fetch #{remote_name} --prune --quiet --tags) }  | 
 | 415 | + | 
 | 416 | + around(:each) do |example|  | 
 | 417 | + stub_env('GITLAB_SHELL_KNOWN_HOSTS' => 'KNOWN HOSTS') { example.run }  | 
 | 418 | + end  | 
 | 419 | + | 
 | 420 | + it 'sets GIT_SSH to a custom script' do  | 
 | 421 | + script = stub_tempfile('scriptFile', 'gitlab-shell-ssh-wrapper', mode: 0755)  | 
 | 422 | + key = stub_tempfile('/tmp files/knownHosts', 'gitlab-shell-known-hosts', mode: 0400)  | 
 | 423 | + | 
 | 424 | + stub_spawn({ 'GIT_SSH' => 'scriptFile' }, *cmd)  | 
 | 425 | + | 
 | 426 | + expect(gl_projects.exec).to be true  | 
 | 427 | + | 
 | 428 | + expect(script.string).to eq("#!/bin/sh\nexec ssh '-oStrictHostKeyChecking=\"true\"' '-oUserKnownHostsFile=\"/tmp files/knownHosts\"' \"$@\"")  | 
 | 429 | + expect(key.string).to eq('KNOWN HOSTS')  | 
 | 430 | + end  | 
 | 431 | + end  | 
368 | 432 |  end  | 
369 | 433 | 
 
  | 
370 | 434 |  describe :import_project do  | 
 | 
0 commit comments