BigW Consortium Gitlab

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

3 4 5
module TestEnv
  extend self

6 7
  # When developing the seed repository, comment out the branch you will modify.
  BRANCH_SHA = {
8 9
    'not-merged-branch'                  => 'b83d6e3',
    'branch-merged'                      => '498214d',
10
    'empty-branch'                       => '7efb185',
11
    'ends-with.json'                     => '98b0d8b',
12 13 14 15 16 17 18
    'flatten-dir'                        => 'e56497b',
    'feature'                            => '0b4bc9a',
    'feature_conflict'                   => 'bb5206f',
    'fix'                                => '48f0be4',
    'improve/awesome'                    => '5937ac0',
    'markdown'                           => '0ed8c6c',
    'lfs'                                => 'be93687',
19
    'master'                             => 'b83d6e3',
20
    'merge-test'                         => '5937ac0',
21 22 23 24 25
    "'test'"                             => 'e56497b',
    'orphaned-branch'                    => '45127a9',
    'binary-encoding'                    => '7b1cf43',
    'gitattributes'                      => '5a62481',
    'expand-collapse-diffs'              => '4842455',
26
    'symlink-expand-diff'                => '81e6355',
27 28 29
    'expand-collapse-files'              => '025db92',
    'expand-collapse-lines'              => '238e82d',
    'video'                              => '8879059',
30
    'add-balsamiq-file'                  => 'b89b56d',
31
    'crlf-diff'                          => '5938907',
Sean McGivern committed
32
    'conflict-start'                     => '824be60',
33 34
    'conflict-resolvable'                => '1450cd6',
    'conflict-binary-file'               => '259a6fb',
Sean McGivern committed
35
    'conflict-contains-conflict-markers' => '78a3086',
36
    'conflict-missing-side'              => 'eb227b3',
37
    'conflict-non-utf8'                  => 'd0a293c',
38
    'conflict-too-large'                 => '39fa04f',
39
    'deleted-image-test'                 => '6c17798',
40
    'wip'                                => 'b9238ee',
41
    'csv'                                => '3dd0896',
42
    'v1.1.0'                             => 'b83d6e3',
Sam Rose committed
43 44
    'add-ipython-files'                  => '6d85bb69',
    'add-pdf-file'                       => 'e774ebd3'
45
  }.freeze
46

47 48 49 50
  # gitlab-test-fork is a fork of gitlab-fork, but we don't necessarily
  # need to keep all the branches in sync.
  # We currently only need a subset of the branches
  FORKED_BRANCH_SHA = {
51 52 53 54
    'add-submodule-version-bump' => '3f547c0',
    'master'                     => '5937ac0',
    'remove-submodule'           => '2a33e0c',
    'conflict-resolvable-fork'   => '404fa3f'
55
  }.freeze
56

57 58
  # Test environment
  #
59
  # See gitlab.yml.example test section for paths
60
  #
61
  def init(opts = {})
62 63 64
    # Disable mailer for spinach tests
    disable_mailer if opts[:mailer] == false

Robert Speicher committed
65
    clean_test_path
66

67 68 69
    # Setup GitLab shell for test instance
    setup_gitlab_shell

70 71
    setup_gitaly if Gitlab::GitalyClient.enabled?

72 73
    # Create repository for FactoryGirl.create(:project)
    setup_factory_repo
74

75 76
    # Create repository for FactoryGirl.create(:forked_project_with_submodules)
    setup_forked_repo
77 78
  end

79 80 81 82
  def cleanup
    stop_gitaly
  end

83
  def disable_mailer
84 85
    allow_any_instance_of(NotificationService).to receive(:mailer).
      and_return(double.as_null_object)
86
  end
87

88
  def enable_mailer
89 90
    allow_any_instance_of(NotificationService).to receive(:mailer).
      and_call_original
91 92
  end

93
  def disable_pre_receive
94
    allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, nil])
95 96
  end

Robert Speicher committed
97 98 99 100 101 102 103
  # Clean /tmp/tests
  #
  # Keeps gitlab-shell and gitlab-test
  def clean_test_path
    tmp_test_path = Rails.root.join('tmp', 'tests', '**')

    Dir[tmp_test_path].each do |entry|
104
      unless File.basename(entry) =~ /\A(gitaly|gitlab-(shell|test|test_bare|test-fork|test-fork_bare))\z/
Robert Speicher committed
105 106 107
        FileUtils.rm_rf(entry)
      end
    end
108 109 110 111

    FileUtils.mkdir_p(repos_path)
    FileUtils.mkdir_p(backup_path)
    FileUtils.mkdir_p(pages_path)
Robert Speicher committed
112 113
  end

114
  def setup_gitlab_shell
115
    unless File.directory?(Gitlab.config.gitlab_shell.path)
116 117 118
      unless system('rake', 'gitlab:shell:install')
        raise 'Can`t clone gitlab-shell'
      end
Robert Speicher committed
119
    end
120
  end
121

122
  def setup_gitaly
123
    socket_path = Gitlab::GitalyClient.address('default').sub(/\Aunix:/, '')
124 125 126 127 128 129
    gitaly_dir = File.dirname(socket_path)

    unless File.directory?(gitaly_dir) || system('rake', "gitlab:gitaly:install[#{gitaly_dir}]")
      raise "Can't clone gitaly"
    end

130
    start_gitaly(gitaly_dir)
131 132
  end

133
  def start_gitaly(gitaly_dir)
134
    gitaly_exec = File.join(gitaly_dir, 'gitaly')
135
    gitaly_config = File.join(gitaly_dir, 'config.toml')
136 137
    log_file = Rails.root.join('log/gitaly-test.log').to_s
    @gitaly_pid = spawn(gitaly_exec, gitaly_config, [:out, :err] => log_file)
138 139 140 141 142 143 144 145
  end

  def stop_gitaly
    return unless @gitaly_pid

    Process.kill('KILL', @gitaly_pid)
  end

146
  def setup_factory_repo
147 148 149 150 151 152 153 154 155 156 157 158 159
    setup_repo(factory_repo_path, factory_repo_path_bare, factory_repo_name,
               BRANCH_SHA)
  end

  # This repo has a submodule commit that is not present in the main test
  # repository.
  def setup_forked_repo
    setup_repo(forked_repo_path, forked_repo_path_bare, forked_repo_name,
               FORKED_BRANCH_SHA)
  end

  def setup_repo(repo_path, repo_path_bare, repo_name, branch_sha)
    clone_url = "https://gitlab.com/gitlab-org/#{repo_name}.git"
160

161
    unless File.directory?(repo_path)
162
      system(*%W(#{Gitlab.config.git.bin_path} clone -q #{clone_url} #{repo_path}))
163 164
    end

165
    set_repo_refs(repo_path, branch_sha)
166

167 168 169 170
    unless File.directory?(repo_path_bare)
      # We must copy bare repositories because we will push to them.
      system(git_env, *%W(#{Gitlab.config.git.bin_path} clone -q --bare #{repo_path} #{repo_path_bare}))
    end
171 172
  end

173
  def copy_repo(project)
174
    base_repo_path = File.expand_path(factory_repo_path_bare)
175
    target_repo_path = File.expand_path(project.repository_storage_path + "/#{project.full_path}.git")
176 177 178
    FileUtils.mkdir_p(target_repo_path)
    FileUtils.cp_r("#{base_repo_path}/.", target_repo_path)
    FileUtils.chmod_R 0755, target_repo_path
179
    set_repo_refs(target_repo_path, BRANCH_SHA)
180 181
  end

182
  def repos_path
183
    Gitlab.config.repositories.storages.default['path']
184
  end
185

186 187 188 189
  def backup_path
    Gitlab.config.backup.path
  end

190 191 192 193
  def pages_path
    Gitlab.config.pages.path
  end

194 195
  def copy_forked_repo_with_submodules(project)
    base_repo_path = File.expand_path(forked_repo_path_bare)
196
    target_repo_path = File.expand_path(project.repository_storage_path + "/#{project.full_path}.git")
197 198 199
    FileUtils.mkdir_p(target_repo_path)
    FileUtils.cp_r("#{base_repo_path}/.", target_repo_path)
    FileUtils.chmod_R 0755, target_repo_path
200
    set_repo_refs(target_repo_path, FORKED_BRANCH_SHA)
201 202
  end

203 204 205 206
  # When no cached assets exist, manually hit the root path to create them
  #
  # Otherwise they'd be created by the first test, often timing out and
  # causing a transient test failure
207
  def eager_load_driver_server
208 209
    return unless defined?(Capybara)

210
    puts "Starting the Capybara driver server..."
211
    Capybara.current_session.visit '/'
212 213
  end

214 215 216
  private

  def factory_repo_path
217 218 219 220
    @factory_repo_path ||= Rails.root.join('tmp', 'tests', factory_repo_name)
  end

  def factory_repo_path_bare
Robert Speicher committed
221
    "#{factory_repo_path}_bare"
222 223 224 225 226
  end

  def factory_repo_name
    'gitlab-test'
  end
227

228 229 230 231 232 233 234 235 236 237 238 239
  def forked_repo_path
    @forked_repo_path ||= Rails.root.join('tmp', 'tests', forked_repo_name)
  end

  def forked_repo_path_bare
    "#{forked_repo_path}_bare"
  end

  def forked_repo_name
    'gitlab-test-fork'
  end

240 241 242
  # Prevent developer git configurations from being persisted to test
  # repositories
  def git_env
243
    { 'GIT_TEMPLATE_DIR' => '' }
244
  end
245 246

  def set_repo_refs(repo_path, branch_sha)
247 248 249 250 251 252 253
    instructions = branch_sha.map {|branch, sha| "update refs/heads/#{branch}\x00#{sha}\x00" }.join("\x00") << "\x00"
    update_refs = %W(#{Gitlab.config.git.bin_path} update-ref --stdin -z)
    reset = proc do
      IO.popen(update_refs, "w") {|io| io.write(instructions) }
      $?.success?
    end

254
    Dir.chdir(repo_path) do
255 256 257 258
      # Try to reset without fetching to avoid using the network.
      unless reset.call
        raise 'Could not fetch test seed repository.' unless system(*%W(#{Gitlab.config.git.bin_path} fetch origin))
        raise 'The fetched test seed does not contain the required revision.' unless reset.call
259 260 261
      end
    end
  end
262
end