summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-09 03:07:57 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-09 03:07:57 +0000
commit330eac18cef61a4f58b3601265f7b631d2311cd0 (patch)
tree87eec5d8c441581938ca908ce30199832e118b85 /spec
parent3359a5a56337b93cd34b9914b6468395bfb6c514 (diff)
downloadgitlab-ce-330eac18cef61a4f58b3601265f7b631d2311cd0.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/projects/environments/prometheus_api_controller_spec.rb4
-rw-r--r--spec/fixtures/api/schemas/remote_mirror.json26
-rw-r--r--spec/fixtures/api/schemas/remote_mirrors.json4
-rw-r--r--spec/lib/gitlab/prometheus/query_variables_spec.rb2
-rw-r--r--spec/requests/api/remote_mirrors_spec.rb41
-rw-r--r--spec/services/prometheus/proxy_variable_substitution_service_spec.rb143
6 files changed, 216 insertions, 4 deletions
diff --git a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
index b12964f8d8b..b8cf47cb1f2 100644
--- a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
+++ b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
@@ -63,9 +63,7 @@ describe Projects::Environments::PrometheusApiController do
context 'with nil query' do
let(:params_without_query) do
- params = environment_params
- params.delete(:query)
- params
+ environment_params.except(:query)
end
before do
diff --git a/spec/fixtures/api/schemas/remote_mirror.json b/spec/fixtures/api/schemas/remote_mirror.json
new file mode 100644
index 00000000000..416b0f080d9
--- /dev/null
+++ b/spec/fixtures/api/schemas/remote_mirror.json
@@ -0,0 +1,26 @@
+{
+ "type": "object",
+ "required": [
+ "id",
+ "enabled",
+ "url",
+ "update_status",
+ "last_update_at",
+ "last_update_started_at",
+ "last_successful_update_at",
+ "last_error",
+ "only_protected_branches"
+ ],
+ "properties": {
+ "id": { "type": "integer" },
+ "enabled": { "type": "boolean" },
+ "url": { "type": "string" },
+ "update_status": { "type": "string" },
+ "last_update_at": { "type": ["string", "null"] },
+ "last_update_started_at": { "type": ["string", "null"] },
+ "last_successful_update_at": { "type": ["string", "null"] },
+ "last_error": { "type": ["string", "null"] },
+ "only_protected_branches": { "type": "boolean" }
+ },
+ "additionalProperties": false
+}
diff --git a/spec/fixtures/api/schemas/remote_mirrors.json b/spec/fixtures/api/schemas/remote_mirrors.json
new file mode 100644
index 00000000000..3c4600c6caa
--- /dev/null
+++ b/spec/fixtures/api/schemas/remote_mirrors.json
@@ -0,0 +1,4 @@
+{
+ "type": "array",
+ "items": { "$ref": "remote_mirror.json" }
+}
diff --git a/spec/lib/gitlab/prometheus/query_variables_spec.rb b/spec/lib/gitlab/prometheus/query_variables_spec.rb
index 3f9b245a3fb..849265de513 100644
--- a/spec/lib/gitlab/prometheus/query_variables_spec.rb
+++ b/spec/lib/gitlab/prometheus/query_variables_spec.rb
@@ -14,7 +14,7 @@ describe Gitlab::Prometheus::QueryVariables do
it do
is_expected.to include(environment_filter:
- %{container_name!="POD",environment="#{slug}"})
+ %Q[container_name!="POD",environment="#{slug}"])
end
context 'without deployment platform' do
diff --git a/spec/requests/api/remote_mirrors_spec.rb b/spec/requests/api/remote_mirrors_spec.rb
new file mode 100644
index 00000000000..c5ba9bd223e
--- /dev/null
+++ b/spec/requests/api/remote_mirrors_spec.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe API::RemoteMirrors do
+ let_it_be(:user) { create(:user) }
+ let_it_be(:project) { create(:project, :repository, :remote_mirror) }
+
+ describe 'GET /projects/:id/remote_mirrors' do
+ let(:route) { "/projects/#{project.id}/remote_mirrors" }
+
+ it 'requires `admin_remote_mirror` permission' do
+ project.add_developer(user)
+
+ get api(route, user)
+
+ expect(response).to have_gitlab_http_status(:unauthorized)
+ end
+
+ it 'returns a list of remote mirrors' do
+ project.add_maintainer(user)
+
+ get api(route, user)
+
+ expect(response).to have_gitlab_http_status(:success)
+ expect(response).to match_response_schema('remote_mirrors')
+ end
+
+ context 'with the `remote_mirrors_api` feature disabled' do
+ before do
+ stub_feature_flags(remote_mirrors_api: false)
+ end
+
+ it 'responds with `not_found`' do
+ get api(route, user)
+
+ expect(response).to have_gitlab_http_status(:not_found)
+ end
+ end
+ end
+end
diff --git a/spec/services/prometheus/proxy_variable_substitution_service_spec.rb b/spec/services/prometheus/proxy_variable_substitution_service_spec.rb
new file mode 100644
index 00000000000..b1cdb8fd3ae
--- /dev/null
+++ b/spec/services/prometheus/proxy_variable_substitution_service_spec.rb
@@ -0,0 +1,143 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Prometheus::ProxyVariableSubstitutionService do
+ describe '#execute' do
+ let_it_be(:environment) { create(:environment) }
+
+ let(:params_keys) { { query: 'up{environment="%{ci_environment_slug}"}' } }
+ let(:params) { ActionController::Parameters.new(params_keys).permit! }
+ let(:result) { subject.execute }
+
+ subject { described_class.new(environment, params) }
+
+ shared_examples 'success' do
+ it 'replaces variables with values' do
+ expect(result[:status]).to eq(:success)
+ expect(result[:params][:query]).to eq(expected_query)
+ end
+ end
+
+ shared_examples 'error' do |message|
+ it 'returns error' do
+ expect(result[:status]).to eq(:error)
+ expect(result[:message]).to eq(message)
+ end
+ end
+
+ context 'does not alter params passed to the service' do
+ it do
+ subject.execute
+
+ expect(params).to eq(
+ ActionController::Parameters.new(
+ query: 'up{environment="%{ci_environment_slug}"}'
+ ).permit!
+ )
+ end
+ end
+
+ context 'with predefined variables' do
+ it_behaves_like 'success' do
+ let(:expected_query) { %Q[up{environment="#{environment.slug}"}] }
+ end
+
+ context 'with nil query' do
+ let(:params_keys) { {} }
+
+ it_behaves_like 'success' do
+ let(:expected_query) { nil }
+ end
+ end
+ end
+
+ context 'ruby template rendering' do
+ let(:params_keys) do
+ { query: 'up{env=%{ci_environment_slug},%{environment_filter}}' }
+ end
+
+ it_behaves_like 'success' do
+ let(:expected_query) do
+ "up{env=#{environment.slug},container_name!=\"POD\"," \
+ "environment=\"#{environment.slug}\"}"
+ end
+ end
+
+ context 'with multiple occurrences of variable in string' do
+ let(:params_keys) do
+ { query: 'up{env1=%{ci_environment_slug},env2=%{ci_environment_slug}}' }
+ end
+
+ it_behaves_like 'success' do
+ let(:expected_query) { "up{env1=#{environment.slug},env2=#{environment.slug}}" }
+ end
+ end
+
+ context 'with multiple variables in string' do
+ let(:params_keys) do
+ { query: 'up{env=%{ci_environment_slug},%{environment_filter}}' }
+ end
+
+ it_behaves_like 'success' do
+ let(:expected_query) do
+ "up{env=#{environment.slug}," \
+ "container_name!=\"POD\",environment=\"#{environment.slug}\"}"
+ end
+ end
+ end
+
+ context 'with unknown variables in string' do
+ let(:params_keys) { { query: 'up{env=%{env_slug}}' } }
+
+ it_behaves_like 'success' do
+ let(:expected_query) { 'up{env=%{env_slug}}' }
+ end
+ end
+
+ # This spec is needed if there are multiple keys in the context provided
+ # by `Gitlab::Prometheus::QueryVariables.call(environment)` which is
+ # passed to the Ruby `%` operator.
+ # If the number of keys in the context is one, there is no need for
+ # this spec.
+ context 'with extra variables in context' do
+ let(:params_keys) { { query: 'up{env=%{ci_environment_slug}}' } }
+
+ it_behaves_like 'success' do
+ let(:expected_query) { "up{env=#{environment.slug}}" }
+ end
+
+ it 'has more than one variable in context' do
+ expect(Gitlab::Prometheus::QueryVariables.call(environment).size).to be > 1
+ end
+ end
+
+ # The ruby % operator will not replace known variables if there are unknown
+ # variables also in the string. It doesn't raise an error
+ # (though the `sprintf` and `format` methods do).
+ context 'with unknown and known variables in string' do
+ let(:params_keys) do
+ { query: 'up{env=%{ci_environment_slug},other_env=%{env_slug}}' }
+ end
+
+ it_behaves_like 'success' do
+ let(:expected_query) { 'up{env=%{ci_environment_slug},other_env=%{env_slug}}' }
+ end
+ end
+
+ context 'when rendering raises error' do
+ context 'when TypeError is raised' do
+ let(:params_keys) { { query: '{% a %}' } }
+
+ it_behaves_like 'error', 'Malformed string'
+ end
+
+ context 'when ArgumentError is raised' do
+ let(:params_keys) { { query: '%<' } }
+
+ it_behaves_like 'error', 'Malformed string'
+ end
+ end
+ end
+ end
+end