test_env.rb 4.63 KB
Newer Older
1 2
require 'rspec/mocks'

3 4 5 6 7 8 9 10 11 12 13 14 15 16
module TestEnv
  extend self

  # Test environment
  #
  # all repositories and namespaces stored at
  # RAILS_APP/tmp/test-git-base-path
  #
  # Next shell methods are stubbed and return true
  # -  mv_repository
  # -  remove_repository
  # -  add_key
  # -  remove_key
  #
17
  def init(opts = {})
18 19
    RSpec::Mocks::setup(self)

20 21 22 23 24 25 26 27
    # Disable observers to improve test speed
    #
    # You can enable it in whole test case where needed by next string:
    #
    #   before(:each) { enable_observers }
    #
    disable_observers if opts[:observers] == false

28 29
    # Disable mailer for spinach tests
    disable_mailer if opts[:mailer] == false
30
    setup_stubs
31 32


33 34 35 36
    clear_test_repo_dir if opts[:init_repos] == true
    setup_test_repos(opts) if opts[:repos] == true
  end

37 38
  def enable_observers
    ActiveRecord::Base.observers.enable(:all)
39 40
  end

41 42
  def disable_observers
    ActiveRecord::Base.observers.disable(:all)
43 44
  end

45 46
  def disable_mailer
    NotificationService.any_instance.stub(mailer: double.as_null_object)
47
  end
48 49
  def enable_mailer
    NotificationService.any_instance.unstub(:mailer)
50 51 52 53 54
  end

  def setup_stubs()
    # Use tmp dir for FS manipulations
    repos_path = testing_path()
55
    GollumWiki.any_instance.stub(:init_repo) do |path|
56 57 58
      create_temp_repo(File.join(repos_path, "#{path}.git"))
    end

59 60 61 62 63 64
    Gitlab.config.gitlab_shell.stub(repos_path: repos_path)

    Gitlab.config.satellites.stub(path: satellite_path)

    Gitlab::Git::Repository.stub(repos_path: repos_path)

65
    Gitlab::Shell.any_instance.stub(
66
      add_repository: true,
67 68
      mv_repository: true,
      remove_repository: true,
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
69
      update_repository_head: true,
70 71 72 73
      add_key: true,
      remove_key: true
    )

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74
    Gitlab::Satellite::Satellite.any_instance.stub(
75 76 77 78
      exists?: true,
      destroy: true,
      create: true,
      lock_files_dir: repos_path
79 80 81
    )

    MergeRequest.any_instance.stub(
82
      check_if_can_be_merged: true
83 84
    )
    Repository.any_instance.stub(
85
      size: 12.45
86
    )
87
  end
88

89 90
  def clear_repo_dir(namespace, name)
    setup_stubs
91
    # Clean any .wiki.git that may have been created
92 93 94
    FileUtils.rm_rf File.join(testing_path(), "#{name}.wiki.git")
  end

95
  # Create a repo and it's satellite
96 97 98 99 100 101 102 103 104
  def create_repo(namespace, name)
    setup_stubs
    repo = repo(namespace, name)

    # Symlink tmp/repositories/gitlabhq to tmp/test-git-base-path/gitlabhq
    system("ln -s -f #{seed_repo_path()} #{repo}")
    create_satellite(repo, namespace, name)
  end

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
  private

  def testing_path
    Rails.root.join('tmp', 'test-git-base-path')
  end

  def seed_repo_path
    Rails.root.join('tmp', 'repositories', 'gitlabhq')
  end

  def seed_satellite_path
    Rails.root.join('tmp', 'satellite', 'gitlabhq')
  end

  def satellite_path
    "#{testing_path()}/satellite"
  end

  def repo(namespace, name)
    unless (namespace.nil? || namespace.path.nil? || namespace.path.strip.empty?)
      repo = File.join(testing_path(), "#{namespace.path}/#{name}.git")
    else
      repo = File.join(testing_path(), "#{name}.git")
    end
  end

  def satellite(namespace, name)
    unless (namespace.nil? || namespace.path.nil? || namespace.path.strip.empty?)
      satellite_repo = File.join(satellite_path, namespace.path, name)
    else
      satellite_repo = File.join(satellite_path, name)
    end
  end

  def setup_test_repos(opts ={})
    create_repo(nil, 'gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('')
    create_repo(nil, 'source_gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('source_')
    create_repo(nil, 'target_gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('target_')
  end

  def clear_test_repo_dir
    setup_stubs
    # Use tmp dir for FS manipulations
    repos_path = testing_path()
    # Remove tmp/test-git-base-path
    FileUtils.rm_rf Gitlab.config.gitlab_shell.repos_path

    # Recreate tmp/test-git-base-path
    FileUtils.mkdir_p Gitlab.config.gitlab_shell.repos_path

155
    # Since much more is happening in satellites
156 157 158
    FileUtils.mkdir_p Gitlab.config.satellites.path
  end

159 160 161
  # Create a testing satellite, and clone the source repo into it
  def create_satellite(source_repo, namespace, satellite_name)
    satellite_repo = satellite(namespace, satellite_name)
Izaak Alpert's avatar
Izaak Alpert committed
162 163
    # Symlink tmp/satellite/gitlabhq to tmp/test-git-base-path/satellite/gitlabhq, create the directory if it doesn't exist already
    satellite_dir = File.dirname(satellite_repo)
164 165
    FileUtils.mkdir_p(satellite_dir) unless File.exists?(satellite_dir)
    system("ln -s -f #{seed_satellite_path} #{satellite_repo}")
166 167 168 169 170 171 172 173
  end

  def create_temp_repo(path)
    FileUtils.mkdir_p path
    command = "git init --quiet --bare #{path};"
    system(command)
  end
end