From ebede2b3ff1c2b089529c4f9d6268641580e280b Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Fri, 12 May 2017 15:19:27 +0200 Subject: Use etag caching for environments JSON For the index view, the environments can now be requested every 15 seconds. Any transition state of a projects environments will trigger a cache invalidation action. Fixes gitlab-org/gitlab-ce#31701 --- spec/models/environment_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 12519de8636..768f557bb9f 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Environment, models: true do - let(:project) { create(:empty_project) } + set(:project) { create(:empty_project) } subject(:environment) { create(:environment, project: project) } it { is_expected.to belong_to(:project) } @@ -34,6 +34,14 @@ describe Environment, models: true do end end + describe 'state machine' do + it 'invalidates the cache after a change' do + expect(environment).to receive(:expire_etag_cache) + + environment.stop + end + end + describe '#nullify_external_url' do it 'replaces a blank url with nil' do env = build(:environment, external_url: "") -- cgit v1.2.1 From fb1b7b00f3f76e0c0ace91b5dfea63408c24de08 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 31 May 2017 11:20:36 +0200 Subject: Fix environment model specs related to protected actions --- spec/models/environment_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 12519de8636..9fbe19b04d5 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -227,7 +227,10 @@ describe Environment, models: true do context 'when user is allowed to stop environment' do before do - project.add_master(user) + project.add_developer(user) + + create(:protected_branch, :developers_can_merge, + name: 'master', project: project) end context 'when action did not yet finish' do -- cgit v1.2.1 From f4aa01053ed39a265ddfd9ee6e9618bd3406e59d Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Tue, 30 May 2017 23:56:26 +0200 Subject: Test etag cache key changing value --- spec/models/environment_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 768f557bb9f..b6162908e12 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -42,6 +42,18 @@ describe Environment, models: true do end end + describe '#expire_etag_cache' do + let(:store) { Gitlab::EtagCaching::Store.new } + + it 'changes the cached value' do + old_value = store.get(environment.etag_cache_key) + + environment.stop + + expect(store.get(environment.etag_cache_key)).not_to eq(old_value) + end + end + describe '#nullify_external_url' do it 'replaces a blank url with nil' do env = build(:environment, external_url: "") -- cgit v1.2.1 From 223d07b38315a68fe39df90a7675f043d8b7acaf Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Mon, 5 Jun 2017 18:38:09 +0200 Subject: Environments#additional_metrics tests --- spec/models/environment_spec.rb | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 12519de8636..12de9c9111b 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -409,6 +409,99 @@ describe Environment, models: true do end end + describe '#has_metrics?' do + subject { environment.has_metrics? } + + context 'when the enviroment is available' do + context 'with a deployment service' do + let(:project) { create(:prometheus_project) } + + context 'and a deployment' do + let!(:deployment) { create(:deployment, environment: environment) } + it { is_expected.to be_truthy } + end + + context 'but no deployments' do + it { is_expected.to be_falsy } + end + end + + context 'without a monitoring service' do + it { is_expected.to be_falsy } + end + end + + context 'when the environment is unavailable' do + let(:project) { create(:prometheus_project) } + + before do + environment.stop + end + + it { is_expected.to be_falsy } + end + end + + describe '#additional_metrics' do + let(:project) { create(:prometheus_project) } + subject { environment.additional_metrics } + + context 'when the environment has additional metrics' do + before do + allow(environment).to receive(:has_additional_metrics?).and_return(true) + end + + it 'returns the additional metrics from the deployment service' do + expect(project.monitoring_service).to receive(:reactive_query) + .with(Gitlab::Prometheus::Queries::AdditionalMetricsQuery.name, environment.id) + .and_return(:fake_metrics) + + is_expected.to eq(:fake_metrics) + end + end + + context 'when the environment does not have metrics' do + before do + allow(environment).to receive(:has_additional_metrics?).and_return(false) + end + + it { is_expected.to be_nil } + end + end + + describe '#has_additional_metrics??' do + subject { environment.has_metrics? } + + context 'when the enviroment is available' do + context 'with a deployment service' do + let(:project) { create(:prometheus_project) } + + context 'and a deployment' do + let!(:deployment) { create(:deployment, environment: environment) } + it { is_expected.to be_truthy } + end + + context 'but no deployments' do + it { is_expected.to be_falsy } + end + end + + context 'without a monitoring service' do + it { is_expected.to be_falsy } + end + end + + context 'when the environment is unavailable' do + let(:project) { create(:prometheus_project) } + + before do + environment.stop + end + + it { is_expected.to be_falsy } + end + end + describe '#slug' do it "is automatically generated" do expect(environment.slug).not_to be_nil -- cgit v1.2.1 From ccf89acc7145bb129f5666108854daa71a022827 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Tue, 6 Jun 2017 16:07:33 +0200 Subject: expand Namespaces:: and refactoring yaml parsing out of MetricGroup class --- spec/models/environment_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 12de9c9111b..e25d2bb6955 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -453,7 +453,7 @@ describe Environment, models: true do it 'returns the additional metrics from the deployment service' do expect(project.monitoring_service).to receive(:reactive_query) - .with(Gitlab::Prometheus::Queries::AdditionalMetricsQuery.name, environment.id) + .with(Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery.name, environment.id) .and_return(:fake_metrics) is_expected.to eq(:fake_metrics) -- cgit v1.2.1 From a7e1205219387a6d24c8579994f73a33b3028010 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Wed, 7 Jun 2017 02:36:59 +0200 Subject: Use explicit instance of prometheus service and add access methods to it --- spec/models/environment_spec.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index e25d2bb6955..8c9d093fc48 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -452,8 +452,8 @@ describe Environment, models: true do end it 'returns the additional metrics from the deployment service' do - expect(project.monitoring_service).to receive(:reactive_query) - .with(Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery.name, environment.id) + expect(environment.prometheus_service).to receive(:additional_environment_metrics) + .with(environment) .and_return(:fake_metrics) is_expected.to eq(:fake_metrics) @@ -470,7 +470,11 @@ describe Environment, models: true do end describe '#has_additional_metrics??' do - subject { environment.has_metrics? } + subject { environment.has_additional_metrics? } + + before do + + end context 'when the enviroment is available' do context 'with a deployment service' do -- cgit v1.2.1 From a924152219c1367bf494f3f387d050ac3ff2d7d3 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Wed, 7 Jun 2017 04:07:47 +0200 Subject: Remove unecessary before block --- spec/models/environment_spec.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'spec/models/environment_spec.rb') diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 8c9d093fc48..c8709409cea 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -472,10 +472,6 @@ describe Environment, models: true do describe '#has_additional_metrics??' do subject { environment.has_additional_metrics? } - before do - - end - context 'when the enviroment is available' do context 'with a deployment service' do let(:project) { create(:prometheus_project) } -- cgit v1.2.1