Commit 3243cdc4 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Squashed commit of the following:

commit 637c9558
Author: Pawel Chojnacki <pawel@chojnacki.ws>
Date:   Mon Feb 26 12:40:40 2018 +0100

    Fix failing test, when deployment platform is not bound to a cluster.

commit 0feeddaa
Author: Pawel Chojnacki <pawel@chojnacki.ws>
Date:   Sat Feb 24 01:06:08 2018 +0100

    drop the ! from synchronize_service_state! + remove unused scope

commit 22e2cad9
Author: Pawel Chojnacki <pawel@chojnacki.ws>
Date:   Sat Feb 24 00:56:50 2018 +0100

    Use deployment platform to find cluster with prometheus application
parent 77a5c7a6
...@@ -51,8 +51,6 @@ module Clusters ...@@ -51,8 +51,6 @@ module Clusters
scope :enabled, -> { where(enabled: true) } scope :enabled, -> { where(enabled: true) }
scope :disabled, -> { where(enabled: false) } scope :disabled, -> { where(enabled: false) }
scope :for_environment, -> (env) { where(environment_scope: ['*', '', env.slug]) }
def status_name def status_name
if provider if provider
provider.status_name provider.status_name
......
...@@ -241,12 +241,12 @@ class Environment < ActiveRecord::Base ...@@ -241,12 +241,12 @@ class Environment < ActiveRecord::Base
end end
def cluster_prometheus_adapter def cluster_prometheus_adapter
# sort results by descending order based on environment_scope being longer return unless project.deployment_platform.respond_to?(:cluster)
# thus more closely matching environment slug
clusters = project.clusters.enabled.for_environment(self).sort_by { |c| c.environment_scope&.length }.reverse!
cluster = clusters&.detect { |cluster| cluster.application_prometheus&.installed? } cluster = project.deployment_platform.cluster
cluster&.application_prometheus return unless cluster.application_prometheus&.installed?
cluster.application_prometheus
end end
private private
......
...@@ -9,7 +9,7 @@ class PrometheusService < MonitoringService ...@@ -9,7 +9,7 @@ class PrometheusService < MonitoringService
validates :api_url, url: true validates :api_url, url: true
end end
before_save :synchronize_service_state! before_save :synchronize_service_state
after_save :clear_reactive_cache! after_save :clear_reactive_cache!
...@@ -82,7 +82,7 @@ class PrometheusService < MonitoringService ...@@ -82,7 +82,7 @@ class PrometheusService < MonitoringService
private private
def synchronize_service_state! def synchronize_service_state
self.active = prometheus_installed? || manual_configuration? self.active = prometheus_installed? || manual_configuration?
true true
......
require 'spec_helper' require 'spec_helper'
describe Environment do describe Environment do
set(:project) { create(:project) } let(:project) { create(:project) }
subject(:environment) { create(:environment, project: project) } subject(:environment) { create(:environment, project: project) }
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
...@@ -724,10 +724,7 @@ describe Environment do ...@@ -724,10 +724,7 @@ describe Environment do
end end
describe '#prometheus_adapter' do describe '#prometheus_adapter' do
let!(:cluster_for_all) { create(:cluster, environment_scope: '*', projects: [project]) } let(:cluster) { create(:cluster, :provided_by_user, environment_scope: '*', projects: [project]) }
let!(:cluster_for_dev) { create(:cluster, environment_scope: 'dev', projects: [project]) }
let!(:prometheus_for_dev) { create(:clusters_applications_prometheus, :installed, cluster: cluster_for_dev) }
context 'prometheus service can execute queries' do context 'prometheus service can execute queries' do
let(:prometheus_service) { double(:prometheus_service, can_query?: true) } let(:prometheus_service) { double(:prometheus_service, can_query?: true) }
...@@ -744,53 +741,17 @@ describe Environment do ...@@ -744,53 +741,17 @@ describe Environment do
context "prometheus service can't execute queries" do context "prometheus service can't execute queries" do
let(:prometheus_service) { double(:prometheus_service, can_query?: false) } let(:prometheus_service) { double(:prometheus_service, can_query?: false) }
context 'with cluster for all environments with prometheus installed' do context 'with cluster with prometheus installed' do
let!(:prometheus_for_all) { create(:clusters_applications_prometheus, :installed, cluster: cluster_for_all) } let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
context 'without environment supplied' do
it 'returns application handling all environments' do
expect(environment.prometheus_adapter).to eq(prometheus_for_all)
end
end
context 'with dev environment supplied' do
let!(:environment) { create(:environment, project: project, name: 'dev') }
it 'returns dev cluster prometheus application' do it 'returns application handling all environments' do
expect(environment.prometheus_adapter).to eq(prometheus_for_dev) expect(environment.prometheus_adapter).to eq(prometheus)
end
end
context 'with prod environment supplied' do
let!(:environment) { create(:environment, project: project, name: 'prod') }
it 'returns application handling all environments' do
expect(environment.prometheus_adapter).to eq(prometheus_for_all)
end
end end
end end
context 'with cluster for all environments without prometheus installed' do context 'with cluster without prometheus installed' do
context 'without environment supplied' do it 'returns nil' do
it 'returns nil' do expect(environment.prometheus_adapter).to be_nil
expect(environment.prometheus_adapter).to be_nil
end
end
context 'with dev environment supplied' do
let!(:environment) { create(:environment, project: project, name: 'dev') }
it 'returns dev cluster prometheus application' do
expect(environment.prometheus_adapter).to eq(prometheus_for_dev)
end
end
context 'with prod environment supplied' do
let!(:environment) { create(:environment, project: project, name: 'prod') }
it 'returns nil' do
expect(environment.prometheus_adapter).to be_nil
end
end end
end end
end end
......
...@@ -221,7 +221,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -221,7 +221,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end end
end end
describe '#synchronize_service_state! before_save callback' do describe '#synchronize_service_state before_save callback' do
context 'no clusters with prometheus are installed' do context 'no clusters with prometheus are installed' do
context 'when service is inactive' do context 'when service is inactive' do
before do before do
......
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