Add test checking if prometheus integration is enabled after prometheus is installed

This commit is contained in:
Pawel Chojnacki 2018-01-04 02:45:57 +01:00
parent 249c9a8cf6
commit 80d4c0675f
8 changed files with 65 additions and 24 deletions

View file

@ -10,6 +10,15 @@ module Clusters
default_value_for :version, VERSION
state_machine :status do
after_transition any => [:installed] do |application|
application.cluster.projects.each do |project|
# raise "exe"
project.prometheus_service&.update(active: true)
end
end
end
def chart
'stable/prometheus'
end

View file

@ -1,3 +1,7 @@
module Gitlab
PrometheusError = Class.new(StandardError)
end
class PrometheusService < MonitoringService
include ReactiveService
@ -8,7 +12,6 @@ class PrometheusService < MonitoringService
# Access to prometheus is directly through the API
prop_accessor :api_url
boolean_accessor :manual_configuration
boolean_accessor :prometheus_installed
with_options presence: true, if: :manual_configuration? do
validates :api_url, url: true
@ -18,10 +21,9 @@ class PrometheusService < MonitoringService
after_save :clear_reactive_cache!
def initialize_properties
if properties.nil?
self.properties = { prometheus_installed: false }
self.properties = { }
end
end
@ -54,7 +56,6 @@ class PrometheusService < MonitoringService
}
]
},
{
type: 'text',
name: 'api_url',
@ -126,6 +127,10 @@ class PrometheusService < MonitoringService
end
end
def prometheus_installed?
cluster_with_prometheus.present?
end
private
def cluster_with_prometheus(environment_id = nil)
@ -135,7 +140,7 @@ class PrometheusService < MonitoringService
project.clusters.enabled.select { |c| c.environment_scope == '*' || c.environment_scope == '' }
end
clusters.detect { |cluster| cluster.application_prometheus.installed? }
clusters.detect { |cluster| cluster.application_prometheus&.installed? }
end
def rename_data_to_metrics(metrics)
@ -144,7 +149,8 @@ class PrometheusService < MonitoringService
end
def synchronize_service_state!
self.active = prometheus_installed? || manual_configuration? || cluster_with_prometheus.present?
self.prometheus_installed = !manual_configuration? && cluster_with_prometheus.present?
self.active = prometheus_installed? || self.manual_configuration?
true
end
end

View file

@ -0,0 +1,10 @@
.row.prepend-top-default.append-bottom-default
%p
- unless @service.manual_configuration?
- if @service.prometheus_installed?
= link_to 'Manage installed Prometheus', project_clusters_path(@project), class: 'btn btn-cancel'
- else
= link_to 'Install Prometheus', project_clusters_path(@project), class: 'btn btn-cancel'
- else
To automatically install prometheus disable manual configuration

View file

@ -2,22 +2,20 @@ module Gitlab
module Prometheus
module Queries
class EnvironmentQuery < BaseQuery
def query(environment_id)
::Environment.find_by(id: environment_id).try do |environment|
environment_slug = environment.slug
timeframe_start = 8.hours.ago.to_f
timeframe_end = Time.now.to_f
def query
environment_slug = environment.slug
timeframe_start = 8.hours.ago.to_f
timeframe_end = Time.now.to_f
memory_query = raw_memory_usage_query(environment_slug)
cpu_query = raw_cpu_usage_query(environment_slug)
memory_query = raw_memory_usage_query(environment_slug)
cpu_query = raw_cpu_usage_query(environment_slug)
{
memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end),
memory_current: client_query(memory_query, time: timeframe_end),
cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end),
cpu_current: client_query(cpu_query, time: timeframe_end)
}
end
{
memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end),
memory_current: client_query(memory_query, time: timeframe_end),
cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end),
cpu_current: client_query(cpu_query, time: timeframe_end)
}
end
end
end

View file

@ -4,7 +4,7 @@ module Gitlab
class MatchedMetricsQuery < BaseQuery
MAX_QUERY_ITEMS = 40.freeze
def query
def query(_ = nil)
groups_data.map do |group, data|
{
group: group.name,

View file

@ -7,7 +7,7 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do
include_examples 'additional metrics query' do
let(:deployment) { create(:deployment, environment: environment) }
let(:query_params) { [deployment.id] }
let(:query_params) { [environment.id, deployment.id] }
it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything,

View file

@ -31,7 +31,7 @@ describe Gitlab::Prometheus::Queries::DeploymentQuery do
expect(client).to receive(:query).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment-slug"}[30m])) * 100',
time: stop_time)
expect(subject.query(deployment.id)).to eq(memory_values: nil, memory_before: nil, memory_after: nil,
expect(subject.query(environment.id, deployment.id)).to eq(memory_values: nil, memory_before: nil, memory_after: nil,
cpu_values: nil, cpu_before: nil, cpu_after: nil)
end
end

View file

@ -6,6 +6,24 @@ describe Clusters::Applications::Prometheus do
include_examples 'cluster application specs', described_class
describe 'transition to installed' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, projects: [project]) }
let(:prometheus_service) { double('prometheus_service') }
subject { create(:clusters_applications_prometheus, :installing, cluster: cluster) }
before do
allow(project).to receive(:prometheus_service).and_return prometheus_service
end
it 'ensures Prometheus service is activated' do
expect(prometheus_service).to receive(:update).with(active: true)
subject.make_installed
end
end
describe "#chart_values_file" do
subject { create(:clusters_applications_prometheus).chart_values_file }