Commit ed439d2d authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'jv-rspec-workhorse-setup-speed' into 'master'

Speed up Rspec workhorse setup

See merge request gitlab-org/gitlab!49741
parents 99e6ee96 4bc96d3a
...@@ -44,6 +44,24 @@ module Gitlab ...@@ -44,6 +44,24 @@ module Gitlab
def get_config_path(dir) def get_config_path(dir)
File.join(dir, 'config.toml') File.join(dir, 'config.toml')
end end
def compile_into(dir)
command = %W[#{make} -C #{Rails.root.join('workhorse')} install PREFIX=#{File.absolute_path(dir)}]
make_out, make_status = Gitlab::Popen.popen(command)
unless make_status == 0
warn make_out
raise 'workhorse make failed'
end
# 'make install' puts the binaries in #{dir}/bin but the init script expects them in dir
FileUtils.mv(Dir["#{dir}/bin/*"], dir)
end
def make
_, which_status = Gitlab::Popen.popen(%w[which gmake])
which_status == 0 ? 'gmake' : 'make'
end
end end
end end
......
...@@ -26,14 +26,7 @@ namespace :gitlab do ...@@ -26,14 +26,7 @@ namespace :gitlab do
args.with_defaults(repo: 'https://gitlab.com/gitlab-org/gitlab-workhorse.git') args.with_defaults(repo: 'https://gitlab.com/gitlab-org/gitlab-workhorse.git')
checkout_or_clone_version(version: 'workhorse-move-notice', repo: args.repo, target_dir: args.dir, clone_opts: %w[--depth 1]) checkout_or_clone_version(version: 'workhorse-move-notice', repo: args.repo, target_dir: args.dir, clone_opts: %w[--depth 1])
_, which_status = Gitlab::Popen.popen(%w[which gmake]) Gitlab::SetupHelper::Workhorse.compile_into(args.dir)
make = which_status == 0 ? 'gmake' : 'make'
command = %W[#{make} -C #{Rails.root.join('workhorse')} install PREFIX=#{File.absolute_path(args.dir)}]
run_command!(command)
# 'make install' puts the binaries in #{args.dir}/bin but the init script expects them in args.dir
FileUtils.mv(Dir["#{args.dir}/bin/*"], args.dir)
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::SetupHelper::Workhorse do
describe '.make' do
subject { described_class.make }
context 'when there is a gmake' do
it 'returns gmake' do
expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['/usr/bin/gmake', 0])
expect(subject).to eq 'gmake'
end
end
context 'when there is no gmake' do
it 'returns make' do
expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['', 1])
expect(subject).to eq 'make'
end
end
end
end
...@@ -241,15 +241,14 @@ module TestEnv ...@@ -241,15 +241,14 @@ module TestEnv
end end
def setup_workhorse def setup_workhorse
install_workhorse_args = [workhorse_dir, workhorse_url].compact.join(',') start = Time.now
puts "\n==> Setting up GitLab Workhorse..."
component_timed_setup(
'GitLab Workhorse', FileUtils.rm_rf(workhorse_dir)
install_dir: workhorse_dir, Gitlab::SetupHelper::Workhorse.compile_into(workhorse_dir)
version: Gitlab::Workhorse.version, Gitlab::SetupHelper::Workhorse.create_configuration(workhorse_dir, nil)
task: "gitlab:workhorse:install[#{install_workhorse_args}]") do
Gitlab::SetupHelper::Workhorse.create_configuration(workhorse_dir, nil) puts " GitLab Workhorse set up in #{Time.now - start} seconds...\n"
end
end end
def workhorse_dir def workhorse_dir
......
...@@ -9,9 +9,13 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do ...@@ -9,9 +9,13 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do
describe 'install' do describe 'install' do
let(:repo) { 'https://gitlab.com/gitlab-org/gitlab-workhorse.git' } let(:repo) { 'https://gitlab.com/gitlab-org/gitlab-workhorse.git' }
let(:clone_path) { Rails.root.join('tmp/tests/gitlab-workhorse').to_s } let(:clone_path) { Dir.mktmpdir('gitlab:workhorse:install-rake-test') }
let(:workhorse_source) { Rails.root.join('workhorse').to_s } let(:workhorse_source) { Rails.root.join('workhorse').to_s }
after do
FileUtils.rm_rf(clone_path)
end
context 'no dir given' do context 'no dir given' do
it 'aborts and display a help message' do it 'aborts and display a help message' do
# avoid writing task output to spec progress # avoid writing task output to spec progress
...@@ -20,7 +24,7 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do ...@@ -20,7 +24,7 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do
end end
end end
context 'when an underlying Git command fail' do context 'when an underlying Git command fails' do
it 'aborts and display a help message' do it 'aborts and display a help message' do
expect(main_object) expect(main_object)
.to receive(:checkout_or_clone_version).and_raise 'Git error' .to receive(:checkout_or_clone_version).and_raise 'Git error'
...@@ -29,47 +33,26 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do ...@@ -29,47 +33,26 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do
end end
end end
describe 'checkout or clone' do it 'clones the origin and creates a gitlab-workhorse binary' do
it 'calls checkout_or_clone_version with the right arguments' do FileUtils.rm_rf(clone_path)
expect(main_object)
.to receive(:checkout_or_clone_version).with(version: 'workhorse-move-notice', repo: repo, target_dir: clone_path, clone_opts: %w[--depth 1]) Dir.mktmpdir('fake-workhorse-origin') do |workhorse_origin|
[
run_rake_task('gitlab:workhorse:install', clone_path) %W[git init -q #{workhorse_origin}],
end %W[git -C #{workhorse_origin} checkout -q -b workhorse-move-notice],
end %W[touch #{workhorse_origin}/proof-that-repo-got-cloned],
%W[git -C #{workhorse_origin} add .],
describe 'gmake/make' do %W[git -C #{workhorse_origin} commit -q -m init],
before do %W[git -C #{workhorse_origin} checkout -q -b master]
FileUtils.mkdir_p(clone_path) ].each do |cmd|
end raise "#{cmd.join(' ')} failed" unless system(*cmd)
context 'gmake is available' do
before do
expect(main_object).to receive(:checkout_or_clone_version)
allow(Object).to receive(:run_command!).with(['gmake']).and_return(true)
end end
it 'calls gmake in the gitlab-workhorse directory' do run_rake_task('gitlab:workhorse:install', clone_path, File.join(workhorse_origin, '.git'))
expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['/usr/bin/gmake', 0])
expect(main_object).to receive(:run_command!).with(["gmake", "-C", workhorse_source, "install", "PREFIX=#{clone_path}"]).and_return(true)
run_rake_task('gitlab:workhorse:install', clone_path)
end
end end
context 'gmake is not available' do expect(File.exist?(File.join(clone_path, 'proof-that-repo-got-cloned'))).to be true
before do expect(File.executable?(File.join(clone_path, 'gitlab-workhorse'))).to be true
expect(main_object).to receive(:checkout_or_clone_version)
allow(main_object).to receive(:run_command!).with(['make']).and_return(true)
end
it 'calls make in the gitlab-workhorse directory' do
expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['', 42])
expect(main_object).to receive(:run_command!).with(["make", "-C", workhorse_source, "install", "PREFIX=#{clone_path}"]).and_return(true)
run_rake_task('gitlab:workhorse:install', clone_path)
end
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment