summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/ci
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-05-19 15:44:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-05-19 15:44:42 +0000
commit4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch)
tree5423a1c7516cffe36384133ade12572cf709398d /spec/requests/api/graphql/ci
parente570267f2f6b326480d284e0164a6464ba4081bc (diff)
downloadgitlab-ce-4555e1b21c365ed8303ffb7a3325d773c9b8bf31.tar.gz
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/requests/api/graphql/ci')
-rw-r--r--spec/requests/api/graphql/ci/job_spec.rb13
-rw-r--r--spec/requests/api/graphql/ci/pipelines_spec.rb124
-rw-r--r--spec/requests/api/graphql/ci/runner_spec.rb144
-rw-r--r--spec/requests/api/graphql/ci/runners_spec.rb114
-rw-r--r--spec/requests/api/graphql/ci/template_spec.rb48
5 files changed, 442 insertions, 1 deletions
diff --git a/spec/requests/api/graphql/ci/job_spec.rb b/spec/requests/api/graphql/ci/job_spec.rb
index 78f7d3e149b..b0514a0a963 100644
--- a/spec/requests/api/graphql/ci/job_spec.rb
+++ b/spec/requests/api/graphql/ci/job_spec.rb
@@ -5,6 +5,10 @@ require 'spec_helper'
RSpec.describe 'Query.project(fullPath).pipelines.job(id)' do
include GraphqlHelpers
+ around do |example|
+ travel_to(Time.current) { example.run }
+ end
+
let_it_be(:user) { create_default(:user) }
let_it_be(:project) { create(:project, :repository, :public) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
@@ -35,13 +39,20 @@ RSpec.describe 'Query.project(fullPath).pipelines.job(id)' do
let(:terminal_type) { 'CiJob' }
it 'retrieves scalar fields' do
+ job_2.update!(
+ created_at: 40.seconds.ago,
+ queued_at: 32.seconds.ago,
+ started_at: 30.seconds.ago,
+ finished_at: 5.seconds.ago
+ )
post_graphql(query, current_user: user)
expect(graphql_data_at(*path)).to match a_hash_including(
'id' => global_id_of(job_2),
'name' => job_2.name,
'allowFailure' => job_2.allow_failure,
- 'duration' => job_2.duration,
+ 'duration' => 25,
+ 'queuedDuration' => 2.0,
'status' => job_2.status.upcase
)
end
diff --git a/spec/requests/api/graphql/ci/pipelines_spec.rb b/spec/requests/api/graphql/ci/pipelines_spec.rb
index 7933251b8e9..f207636283f 100644
--- a/spec/requests/api/graphql/ci/pipelines_spec.rb
+++ b/spec/requests/api/graphql/ci/pipelines_spec.rb
@@ -8,6 +8,130 @@ RSpec.describe 'Query.project(fullPath).pipelines' do
let_it_be(:project) { create(:project, :repository, :public) }
let_it_be(:user) { create(:user) }
+ around do |example|
+ travel_to(Time.current) { example.run }
+ end
+
+ describe 'duration fields' do
+ let_it_be(:pipeline) do
+ create(:ci_pipeline, project: project)
+ end
+
+ let(:query_path) do
+ [
+ [:project, { full_path: project.full_path }],
+ [:pipelines],
+ [:nodes]
+ ]
+ end
+
+ let(:query) do
+ wrap_fields(query_graphql_path(query_path, 'queuedDuration duration'))
+ end
+
+ before do
+ pipeline.update!(
+ created_at: 1.minute.ago,
+ started_at: 55.seconds.ago
+ )
+ create(:ci_build, :success,
+ pipeline: pipeline,
+ started_at: 55.seconds.ago,
+ finished_at: 10.seconds.ago)
+ pipeline.update_duration
+ pipeline.save!
+
+ post_graphql(query, current_user: user)
+ end
+
+ it 'includes the duration fields' do
+ path = query_path.map(&:first)
+ expect(graphql_data_at(*path, :queued_duration)).to eq [5.0]
+ expect(graphql_data_at(*path, :duration)).to eq [45]
+ end
+ end
+
+ describe '.stages' do
+ let_it_be(:project) { create(:project, :repository) }
+ let_it_be(:pipeline) { create(:ci_empty_pipeline, project: project) }
+ let_it_be(:stage) { create(:ci_stage_entity, pipeline: pipeline, project: project) }
+ let_it_be(:other_stage) { create(:ci_stage_entity, pipeline: pipeline, project: project, name: 'other') }
+
+ let(:first_n) { var('Int') }
+ let(:query_path) do
+ [
+ [:project, { full_path: project.full_path }],
+ [:pipelines],
+ [:nodes],
+ [:stages, { first: first_n }],
+ [:nodes]
+ ]
+ end
+
+ let(:query) do
+ with_signature([first_n], wrap_fields(query_graphql_path(query_path, :name)))
+ end
+
+ before_all do
+ # see app/services/ci/ensure_stage_service.rb to explain why we use stage_id
+ create(:ci_build, pipeline: pipeline, stage_id: stage.id, name: 'linux: [foo]')
+ create(:ci_build, pipeline: pipeline, stage_id: stage.id, name: 'linux: [bar]')
+ create(:ci_build, pipeline: pipeline, stage_id: other_stage.id, name: 'linux: [baz]')
+ end
+
+ it 'is null if the user is a guest' do
+ project.add_guest(user)
+
+ post_graphql(query, current_user: user, variables: first_n.with(1))
+
+ expect(graphql_data_at(:project, :pipelines, :nodes)).to contain_exactly a_hash_including('stages' => be_nil)
+ end
+
+ it 'is present if the user has reporter access' do
+ project.add_reporter(user)
+
+ post_graphql(query, current_user: user)
+
+ expect(graphql_data_at(:project, :pipelines, :nodes, :stages, :nodes, :name))
+ .to contain_exactly(eq(stage.name), eq(other_stage.name))
+ end
+
+ describe '.groups' do
+ let(:query_path) do
+ [
+ [:project, { full_path: project.full_path }],
+ [:pipelines],
+ [:nodes],
+ [:stages],
+ [:nodes],
+ [:groups],
+ [:nodes]
+ ]
+ end
+
+ let(:query) do
+ wrap_fields(query_graphql_path(query_path, :name))
+ end
+
+ it 'is empty if the user is a guest' do
+ project.add_guest(user)
+
+ post_graphql(query, current_user: user)
+
+ expect(graphql_data_at(:project, :pipelines, :nodes, :stages, :nodes, :groups)).to be_empty
+ end
+
+ it 'is present if the user has reporter access' do
+ project.add_reporter(user)
+
+ post_graphql(query, current_user: user)
+
+ expect(graphql_data_at(:project, :pipelines, :nodes, :stages, :nodes, :groups, :nodes, :name))
+ .to contain_exactly('linux', 'linux')
+ end
+ end
+ end
+
describe '.jobs' do
let(:first_n) { var('Int') }
let(:query_path) do
diff --git a/spec/requests/api/graphql/ci/runner_spec.rb b/spec/requests/api/graphql/ci/runner_spec.rb
new file mode 100644
index 00000000000..e1f84d23209
--- /dev/null
+++ b/spec/requests/api/graphql/ci/runner_spec.rb
@@ -0,0 +1,144 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe 'Query.runner(id)' do
+ include GraphqlHelpers
+
+ let_it_be(:user) { create_default(:user, :admin) }
+
+ let_it_be(:active_runner) do
+ create(:ci_runner, :instance, description: 'Runner 1', contacted_at: 2.hours.ago,
+ active: true, version: 'adfe156', revision: 'a', locked: true, ip_address: '127.0.0.1', maximum_timeout: 600,
+ access_level: 0, tag_list: %w[tag1 tag2], run_untagged: true)
+ end
+
+ let_it_be(:inactive_runner) do
+ create(:ci_runner, :instance, description: 'Runner 2', contacted_at: 1.day.ago, active: false,
+ version: 'adfe157', revision: 'b', ip_address: '10.10.10.10', access_level: 1, run_untagged: true)
+ end
+
+ def get_runner(id)
+ case id
+ when :active_runner
+ active_runner
+ when :inactive_runner
+ inactive_runner
+ end
+ end
+
+ shared_examples 'runner details fetch' do |runner_id|
+ let(:query) do
+ wrap_fields(query_graphql_path(query_path, all_graphql_fields_for('CiRunner')))
+ end
+
+ let(:query_path) do
+ [
+ [:runner, { id: get_runner(runner_id).to_global_id.to_s }]
+ ]
+ end
+
+ it 'retrieves expected fields' do
+ post_graphql(query, current_user: user)
+
+ runner_data = graphql_data_at(:runner)
+ expect(runner_data).not_to be_nil
+
+ runner = get_runner(runner_id)
+ expect(runner_data).to match a_hash_including(
+ 'id' => "gid://gitlab/Ci::Runner/#{runner.id}",
+ 'description' => runner.description,
+ 'contactedAt' => runner.contacted_at&.iso8601,
+ 'version' => runner.version,
+ 'shortSha' => runner.short_sha,
+ 'revision' => runner.revision,
+ 'locked' => runner.locked,
+ 'active' => runner.active,
+ 'status' => runner.status.to_s.upcase,
+ 'maximumTimeout' => runner.maximum_timeout,
+ 'accessLevel' => runner.access_level.to_s.upcase,
+ 'runUntagged' => runner.run_untagged,
+ 'ipAddress' => runner.ip_address,
+ 'runnerType' => 'INSTANCE_TYPE'
+ )
+ expect(runner_data['tagList']).to match_array runner.tag_list
+ end
+ end
+
+ shared_examples 'retrieval by unauthorized user' do |runner_id|
+ let(:query) do
+ wrap_fields(query_graphql_path(query_path, all_graphql_fields_for('CiRunner')))
+ end
+
+ let(:query_path) do
+ [
+ [:runner, { id: get_runner(runner_id).to_global_id.to_s }]
+ ]
+ end
+
+ it 'returns null runner' do
+ post_graphql(query, current_user: user)
+
+ expect(graphql_data_at(:runner)).to be_nil
+ end
+ end
+
+ describe 'for active runner' do
+ it_behaves_like 'runner details fetch', :active_runner
+ end
+
+ describe 'for inactive runner' do
+ it_behaves_like 'runner details fetch', :inactive_runner
+ end
+
+ describe 'by regular user' do
+ let(:user) { create_default(:user) }
+
+ it_behaves_like 'retrieval by unauthorized user', :active_runner
+ end
+
+ describe 'by unauthenticated user' do
+ let(:user) { nil }
+
+ it_behaves_like 'retrieval by unauthorized user', :active_runner
+ end
+
+ describe 'Query limits' do
+ def runner_query(runner)
+ <<~SINGLE
+ runner(id: "#{runner.to_global_id}") {
+ #{all_graphql_fields_for('CiRunner')}
+ }
+ SINGLE
+ end
+
+ let(:single_query) do
+ <<~QUERY
+ {
+ active: #{runner_query(active_runner)}
+ }
+ QUERY
+ end
+
+ let(:double_query) do
+ <<~QUERY
+ {
+ active: #{runner_query(active_runner)}
+ inactive: #{runner_query(inactive_runner)}
+ }
+ QUERY
+ end
+
+ it 'does not execute more queries per runner', :aggregate_failures do
+ # warm-up license cache and so on:
+ post_graphql(single_query, current_user: user)
+
+ control = ActiveRecord::QueryRecorder.new { post_graphql(single_query, current_user: user) }
+
+ expect { post_graphql(double_query, current_user: user) }
+ .not_to exceed_query_limit(control)
+ expect(graphql_data_at(:active)).not_to be_nil
+ expect(graphql_data_at(:inactive)).not_to be_nil
+ end
+ end
+end
diff --git a/spec/requests/api/graphql/ci/runners_spec.rb b/spec/requests/api/graphql/ci/runners_spec.rb
new file mode 100644
index 00000000000..778fe5b129e
--- /dev/null
+++ b/spec/requests/api/graphql/ci/runners_spec.rb
@@ -0,0 +1,114 @@
+# frozen_string_literal: true
+require 'spec_helper'
+
+RSpec.describe 'Query.runners' do
+ include GraphqlHelpers
+
+ let_it_be(:current_user) { create_default(:user, :admin) }
+
+ describe 'Query.runners' do
+ let_it_be(:project) { create(:project, :repository, :public) }
+ let_it_be(:instance_runner) { create(:ci_runner, :instance, version: 'abc', revision: '123', description: 'Instance runner', ip_address: '127.0.0.1') }
+ let_it_be(:project_runner) { create(:ci_runner, :project, active: false, version: 'def', revision: '456', description: 'Project runner', projects: [project], ip_address: '127.0.0.1') }
+
+ let(:runners_graphql_data) { graphql_data['runners'] }
+
+ let(:params) { {} }
+
+ let(:fields) do
+ <<~QUERY
+ nodes {
+ #{all_graphql_fields_for('CiRunner')}
+ }
+ QUERY
+ end
+
+ let(:query) do
+ %(
+ query {
+ runners(type:#{runner_type},status:#{status}) {
+ #{fields}
+ }
+ }
+ )
+ end
+
+ before do
+ post_graphql(query, current_user: current_user)
+ end
+
+ shared_examples 'a working graphql query returning expected runner' do
+ it_behaves_like 'a working graphql query'
+
+ it 'returns expected runner' do
+ expect(runners_graphql_data['nodes'].map { |n| n['id'] }).to contain_exactly(expected_runner.to_global_id.to_s)
+ end
+ end
+
+ context 'runner_type is INSTANCE_TYPE and status is ACTIVE' do
+ let(:runner_type) { 'INSTANCE_TYPE' }
+ let(:status) { 'ACTIVE' }
+
+ let!(:expected_runner) { instance_runner }
+
+ it_behaves_like 'a working graphql query returning expected runner'
+ end
+
+ context 'runner_type is PROJECT_TYPE and status is NOT_CONNECTED' do
+ let(:runner_type) { 'PROJECT_TYPE' }
+ let(:status) { 'NOT_CONNECTED' }
+
+ let!(:expected_runner) { project_runner }
+
+ it_behaves_like 'a working graphql query returning expected runner'
+ end
+ end
+
+ describe 'pagination' do
+ let(:data_path) { [:runners] }
+
+ def pagination_query(params)
+ graphql_query_for(:runners, params, "#{page_info} nodes { id }")
+ end
+
+ def pagination_results_data(runners)
+ runners.map { |runner| GitlabSchema.parse_gid(runner['id'], expected_type: ::Ci::Runner).model_id.to_i }
+ end
+
+ let_it_be(:runners) do
+ common_args = {
+ version: 'abc',
+ revision: '123',
+ ip_address: '127.0.0.1'
+ }
+
+ [
+ create(:ci_runner, :instance, created_at: 4.days.ago, contacted_at: 3.days.ago, **common_args),
+ create(:ci_runner, :instance, created_at: 30.hours.ago, contacted_at: 1.day.ago, **common_args),
+ create(:ci_runner, :instance, created_at: 1.day.ago, contacted_at: 1.hour.ago, **common_args),
+ create(:ci_runner, :instance, created_at: 2.days.ago, contacted_at: 2.days.ago, **common_args),
+ create(:ci_runner, :instance, created_at: 3.days.ago, contacted_at: 1.second.ago, **common_args)
+ ]
+ end
+
+ context 'when sorted by contacted_at ascending' do
+ let(:ordered_runners) { runners.sort_by(&:contacted_at) }
+
+ it_behaves_like 'sorted paginated query' do
+ let(:sort_param) { :CONTACTED_ASC }
+ let(:first_param) { 2 }
+ let(:expected_results) { ordered_runners.map(&:id) }
+ end
+ end
+
+ context 'when sorted by created_at' do
+ let(:ordered_runners) { runners.sort_by(&:created_at).reverse }
+
+ it_behaves_like 'sorted paginated query' do
+ let(:sort_param) { :CREATED_DESC }
+ let(:first_param) { 2 }
+ let(:expected_results) { ordered_runners.map(&:id) }
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/graphql/ci/template_spec.rb b/spec/requests/api/graphql/ci/template_spec.rb
new file mode 100644
index 00000000000..1bbef7d7f30
--- /dev/null
+++ b/spec/requests/api/graphql/ci/template_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+require 'spec_helper'
+
+RSpec.describe 'Querying CI template' do
+ include GraphqlHelpers
+
+ let_it_be(:project) { create(:project, :public) }
+ let_it_be(:user) { create(:user) }
+
+ let(:query) do
+ <<~QUERY
+ {
+ project(fullPath: "#{project.full_path}") {
+ name
+ ciTemplate(name: "#{template_name}") {
+ name
+ content
+ }
+ }
+ }
+ QUERY
+ end
+
+ before do
+ post_graphql(query, current_user: user)
+ end
+
+ context 'when the template exists' do
+ let(:template_name) { 'Android' }
+
+ it_behaves_like 'a working graphql query'
+
+ it 'returns correct data' do
+ expect(graphql_data.dig('project', 'ciTemplate', 'name')).to eq(template_name)
+ expect(graphql_data.dig('project', 'ciTemplate', 'content')).not_to be_blank
+ end
+ end
+
+ context 'when the template does not exist' do
+ let(:template_name) { 'doesnotexist' }
+
+ it_behaves_like 'a working graphql query'
+
+ it 'returns correct data' do
+ expect(graphql_data.dig('project', 'ciTemplate')).to eq(nil)
+ end
+ end
+end