debian-mirror-gitlab/spec/commands/metrics_server/metrics_server_spec.rb

79 lines
2.1 KiB
Ruby
Raw Normal View History

2022-01-26 12:08:38 +05:30
# frozen_string_literal: true
require 'spec_helper'
require_relative '../../../metrics_server/metrics_server'
# End-to-end tests for the metrics server process we use to serve metrics
# from forking applications (Sidekiq, Puma) to the Prometheus scraper.
RSpec.describe 'bin/metrics-server', :aggregate_failures do
let(:config_file) { Tempfile.new('gitlab.yml') }
let(:config) do
{
'test' => {
'monitoring' => {
2022-04-04 11:22:00 +05:30
'web_exporter' => {
'address' => 'localhost',
'enabled' => true,
'port' => 3807
},
2022-01-26 12:08:38 +05:30
'sidekiq_exporter' => {
'address' => 'localhost',
'enabled' => true,
'port' => 3807
}
}
}
}
end
2022-04-04 11:22:00 +05:30
%w(puma sidekiq).each do |target|
context "with a running server targeting #{target}" do
let(:metrics_dir) { Dir.mktmpdir }
2022-03-02 08:16:31 +05:30
2022-04-04 11:22:00 +05:30
before do
# We need to send a request to localhost
WebMock.allow_net_connect!
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
config_file.write(YAML.dump(config))
config_file.close
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
@pid = MetricsServer.spawn(target, metrics_dir: metrics_dir, gitlab_config: config_file.path, wipe_metrics_dir: true)
end
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
after do
webmock_enable!
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
if @pid
pgrp = Process.getpgid(@pid)
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
Timeout.timeout(10) do
Process.kill('TERM', -pgrp)
Process.waitpid(@pid)
end
2022-01-26 12:08:38 +05:30
2022-04-04 11:22:00 +05:30
expect(Gitlab::ProcessManagement.process_alive?(@pid)).to be(false)
end
rescue Errno::ESRCH => _
# 'No such process' means the process died before
ensure
config_file.unlink
FileUtils.rm_rf(metrics_dir, secure: true)
2022-01-26 12:08:38 +05:30
end
2022-04-04 11:22:00 +05:30
it 'serves /metrics endpoint' do
expect do
Timeout.timeout(10) do
http_ok = false
until http_ok
sleep 1
response = Gitlab::HTTP.try_get("http://localhost:3807/metrics", allow_local_requests: true)
http_ok = response&.success?
end
2022-01-26 12:08:38 +05:30
end
2022-04-04 11:22:00 +05:30
end.not_to raise_error
end
2022-01-26 12:08:38 +05:30
end
end
end