diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-16 15:06:17 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-16 15:06:17 +0000 |
commit | 00c78fb814d7ce00989ac04edd6cdaa3239da284 (patch) | |
tree | f04920f08eb4e481ce27bd1d96862676dff735dc /spec/lib | |
parent | d2ffc30fd583e86d4122bb5061098f4f3ca7b3f1 (diff) | |
download | gitlab-ce-00c78fb814d7ce00989ac04edd6cdaa3239da284.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/cluster/mixins/puma_cluster_spec.rb | 93 | ||||
-rw-r--r-- | spec/lib/gitlab/cluster/mixins/unicorn_http_server_spec.rb | 109 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/exporter/web_exporter_spec.rb | 64 | ||||
-rw-r--r-- | spec/lib/gitlab/submodule_links_spec.rb | 18 |
5 files changed, 289 insertions, 11 deletions
diff --git a/spec/lib/gitlab/cluster/mixins/puma_cluster_spec.rb b/spec/lib/gitlab/cluster/mixins/puma_cluster_spec.rb new file mode 100644 index 00000000000..038b72075ad --- /dev/null +++ b/spec/lib/gitlab/cluster/mixins/puma_cluster_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# For easier debugging set `PUMA_DEBUG=1` + +describe Gitlab::Cluster::Mixins::PumaCluster do + PUMA_STARTUP_TIMEOUT = 30 + + context 'when running Puma in Cluster-mode' do + %i[USR1 USR2 INT HUP].each do |signal| + it "for #{signal} does execute phased restart block" do + with_puma(workers: 1) do |pid| + Process.kill(signal, pid) + + child_pid, child_status = Process.wait2(pid) + expect(child_pid).to eq(pid) + expect(child_status).to be_exited + expect(child_status.exitstatus).to eq(140) + end + end + end + end + + private + + def with_puma(workers:, timeout: PUMA_STARTUP_TIMEOUT) + with_puma_config(workers: workers) do |puma_rb| + cmdline = [ + "bundle", "exec", "puma", + "-C", puma_rb, + "-I", Rails.root.to_s + ] + + IO.popen(cmdline) do |process| + # wait for process to start: + # [2123] * Listening on tcp://127.0.0.1:0 + wait_for_output(process, /Listening on/, timeout: timeout) + consume_output(process) + + yield(process.pid) + ensure + Process.kill(:KILL, process.pid) unless process.eof? + end + end + end + + def with_puma_config(workers:) + Dir.mktmpdir do |dir| + File.write "#{dir}/puma.rb", <<-EOF + require './lib/gitlab/cluster/lifecycle_events' + require './lib/gitlab/cluster/mixins/puma_cluster' + + workers #{workers} + bind "tcp://127.0.0.1:0" + preload_app! + + app -> (env) { [404, {}, ['']] } + + Puma::Cluster.prepend(#{described_class}) + + Gitlab::Cluster::LifecycleEvents.on_before_phased_restart do + exit(140) + end + + # redirect stderr to stdout + $stderr.reopen($stdout) + EOF + + yield("#{dir}/puma.rb") + end + end + + def wait_for_output(process, output, timeout:) + Timeout.timeout(timeout) do + loop do + line = process.readline + puts "PUMA_DEBUG: #{line}" if ENV['PUMA_DEBUG'] + break if line =~ output + end + end + end + + def consume_output(process) + Thread.new do + loop do + line = process.readline + puts "PUMA_DEBUG: #{line}" if ENV['PUMA_DEBUG'] + end + rescue + end + end +end diff --git a/spec/lib/gitlab/cluster/mixins/unicorn_http_server_spec.rb b/spec/lib/gitlab/cluster/mixins/unicorn_http_server_spec.rb new file mode 100644 index 00000000000..43176e38b2b --- /dev/null +++ b/spec/lib/gitlab/cluster/mixins/unicorn_http_server_spec.rb @@ -0,0 +1,109 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# For easier debugging set `UNICORN_DEBUG=1` + +describe Gitlab::Cluster::Mixins::UnicornHttpServer do + UNICORN_STARTUP_TIMEOUT = 10 + + context 'when running Unicorn' do + %i[USR2].each do |signal| + it "for #{signal} does execute phased restart block" do + with_unicorn(workers: 1) do |pid| + Process.kill(signal, pid) + + child_pid, child_status = Process.wait2(pid) + expect(child_pid).to eq(pid) + expect(child_status).to be_exited + expect(child_status.exitstatus).to eq(140) + end + end + end + + %i[QUIT TERM INT].each do |signal| + it "for #{signal} does not execute phased restart block" do + with_unicorn(workers: 1) do |pid| + Process.kill(signal, pid) + + child_pid, child_status = Process.wait2(pid) + expect(child_pid).to eq(pid) + expect(child_status).to be_exited + expect(child_status.exitstatus).to eq(0) + end + end + end + end + + private + + def with_unicorn(workers:, timeout: UNICORN_STARTUP_TIMEOUT) + with_unicorn_configs(workers: workers) do |unicorn_rb, config_ru| + cmdline = [ + "bundle", "exec", "unicorn", + "-I", Rails.root.to_s, + "-c", unicorn_rb, + config_ru + ] + + IO.popen(cmdline) do |process| + # wait for process to start: + # I, [2019-10-15T13:21:27.565225 #3089] INFO -- : master process ready + wait_for_output(process, /master process ready/, timeout: timeout) + consume_output(process) + + yield(process.pid) + ensure + Process.kill(:KILL, process.pid) unless process.eof? + end + end + end + + def with_unicorn_configs(workers:) + Dir.mktmpdir do |dir| + File.write "#{dir}/unicorn.rb", <<-EOF + require './lib/gitlab/cluster/lifecycle_events' + require './lib/gitlab/cluster/mixins/unicorn_http_server' + + worker_processes #{workers} + listen "127.0.0.1:0" + preload_app true + + Unicorn::HttpServer.prepend(#{described_class}) + + Gitlab::Cluster::LifecycleEvents.on_before_phased_restart do + exit(140) + end + + # redirect stderr to stdout + $stderr.reopen($stdout) + EOF + + File.write "#{dir}/config.ru", <<-EOF + run -> (env) { [404, {}, ['']] } + EOF + + yield("#{dir}/unicorn.rb", "#{dir}/config.ru") + end + end + + def wait_for_output(process, output, timeout:) + Timeout.timeout(timeout) do + loop do + line = process.readline + puts "UNICORN_DEBUG: #{line}" if ENV['UNICORN_DEBUG'] + break if line =~ output + end + end + end + + def consume_output(process) + Thread.new do + loop do + line = process.readline + puts "UNICORN_DEBUG: #{line}" if ENV['UNICORN_DEBUG'] + end + rescue + end + end +end diff --git a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb index 0376da13595..47ec69e2f45 100644 --- a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb +++ b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb @@ -19,15 +19,11 @@ describe Gitlab::Metrics::Exporter::BaseExporter do BindAddress: anything, Logger: anything, AccessLog: anything - ).and_wrap_original do |m, *args| - m.call(DoNotListen: true, Logger: args.first[:Logger]) - end - - allow_any_instance_of(::WEBrick::HTTPServer).to receive(:start) + ).and_call_original allow(settings).to receive(:enabled).and_return(true) - allow(settings).to receive(:port).and_return(8082) - allow(settings).to receive(:address).and_return('localhost') + allow(settings).to receive(:port).and_return(0) + allow(settings).to receive(:address).and_return('127.0.0.1') end after do @@ -61,6 +57,8 @@ describe Gitlab::Metrics::Exporter::BaseExporter do m.call(DoNotListen: true, Logger: args.first[:Logger]) end + allow_any_instance_of(::WEBrick::HTTPServer).to receive(:start) + exporter.start.join end end @@ -89,14 +87,14 @@ describe Gitlab::Metrics::Exporter::BaseExporter do describe 'when exporter is running' do before do - exporter.start.join + exporter.start end describe '#start' do it "doesn't start running server" do expect_any_instance_of(::WEBrick::HTTPServer).not_to receive(:start) - expect { exporter.start.join }.not_to change { exporter.thread? } + expect { exporter.start }.not_to change { exporter.thread? } end end diff --git a/spec/lib/gitlab/metrics/exporter/web_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/web_exporter_spec.rb new file mode 100644 index 00000000000..99349934e63 --- /dev/null +++ b/spec/lib/gitlab/metrics/exporter/web_exporter_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Metrics::Exporter::WebExporter do + let(:exporter) { described_class.new } + + context 'when blackout seconds is used' do + let(:blackout_seconds) { 0 } + let(:readiness_probe) { exporter.send(:readiness_probe).execute } + + before do + stub_config( + monitoring: { + web_exporter: { + enabled: true, + port: 0, + address: '127.0.0.1', + blackout_seconds: blackout_seconds + } + } + ) + + exporter.start + end + + after do + exporter.stop + end + + context 'when running server' do + it 'readiness probe returns succesful status' do + expect(readiness_probe.http_status).to eq(200) + expect(readiness_probe.json).to include(status: 'ok') + expect(readiness_probe.json).to include('web_exporter' => [{ 'status': 'ok' }]) + end + end + + context 'when blackout seconds is 10s' do + let(:blackout_seconds) { 10 } + + it 'readiness probe returns a failure status' do + # during sleep we check the status of readiness probe + expect(exporter).to receive(:sleep).with(10) do + expect(readiness_probe.http_status).to eq(503) + expect(readiness_probe.json).to include(status: 'failed') + expect(readiness_probe.json).to include('web_exporter' => [{ 'status': 'failed' }]) + end + + exporter.stop + end + end + + context 'when blackout is disabled' do + let(:blackout_seconds) { 0 } + + it 'readiness probe returns a failure status' do + expect(exporter).not_to receive(:sleep) + + exporter.stop + end + end + end +end diff --git a/spec/lib/gitlab/submodule_links_spec.rb b/spec/lib/gitlab/submodule_links_spec.rb index d4420c5b513..f0c8825de74 100644 --- a/spec/lib/gitlab/submodule_links_spec.rb +++ b/spec/lib/gitlab/submodule_links_spec.rb @@ -8,7 +8,9 @@ describe Gitlab::SubmoduleLinks do let(:links) { described_class.new(repo) } describe '#for' do - subject { links.for(submodule_item, 'ref') } + let(:ref) { 'ref' } + + subject { links.for(submodule_item, ref) } context 'when there is no .gitmodules file' do before do @@ -35,8 +37,20 @@ describe Gitlab::SubmoduleLinks do stub_urls({ 'gitlab-foss' => 'git@gitlab.com:gitlab-org/gitlab-foss.git' }) end - it 'returns links' do + it 'returns links and caches the by ref' do expect(subject).to eq(['https://gitlab.com/gitlab-org/gitlab-foss', 'https://gitlab.com/gitlab-org/gitlab-foss/tree/hash']) + + cache_store = links.instance_variable_get("@cache_store") + + expect(cache_store[ref]).to eq({ "gitlab-foss" => "git@gitlab.com:gitlab-org/gitlab-foss.git" }) + end + + context 'when ref name contains a dash' do + let(:ref) { 'signed-commits' } + + it 'returns links' do + expect(subject).to eq(['https://gitlab.com/gitlab-org/gitlab-foss', 'https://gitlab.com/gitlab-org/gitlab-foss/tree/hash']) + end end end end |