2021-06-08 01:23:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Query.runner(id)' do
|
|
|
|
include GraphqlHelpers
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
let_it_be(:user) { create(:user, :admin) }
|
2021-11-18 22:05:49 +05:30
|
|
|
let_it_be(:group) { create(:group) }
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
let_it_be(:active_instance_runner) do
|
2021-06-08 01:23:25 +05:30
|
|
|
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,
|
2022-03-02 08:16:31 +05:30
|
|
|
access_level: 0, tag_list: %w[tag1 tag2], run_untagged: true, executor_type: :custom)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
let_it_be(:inactive_instance_runner) do
|
2021-06-08 01:23:25 +05:30
|
|
|
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
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
let_it_be(:active_group_runner) do
|
|
|
|
create(:ci_runner, :group, groups: [group], description: 'Group runner 1', contacted_at: 2.hours.ago,
|
|
|
|
active: true, version: 'adfe156', revision: 'a', locked: true, ip_address: '127.0.0.1', maximum_timeout: 600,
|
2022-03-02 08:16:31 +05:30
|
|
|
access_level: 0, tag_list: %w[tag1 tag2], run_untagged: true, executor_type: :shell)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
let_it_be(:active_project_runner) { create(:ci_runner, :project) }
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def get_runner(id)
|
|
|
|
case id
|
2021-09-30 23:02:18 +05:30
|
|
|
when :active_instance_runner
|
|
|
|
active_instance_runner
|
|
|
|
when :inactive_instance_runner
|
|
|
|
inactive_instance_runner
|
2021-11-18 22:05:49 +05:30
|
|
|
when :active_group_runner
|
|
|
|
active_group_runner
|
2022-04-04 11:22:00 +05:30
|
|
|
when :active_project_runner
|
|
|
|
active_project_runner
|
2021-06-08 01:23:25 +05:30
|
|
|
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(
|
2022-04-04 11:22:00 +05:30
|
|
|
'id' => runner.to_global_id.to_s,
|
2021-06-08 01:23:25 +05:30
|
|
|
'description' => runner.description,
|
2022-03-02 08:16:31 +05:30
|
|
|
'createdAt' => runner.created_at&.iso8601,
|
2021-06-08 01:23:25 +05:30
|
|
|
'contactedAt' => runner.contacted_at&.iso8601,
|
|
|
|
'version' => runner.version,
|
|
|
|
'shortSha' => runner.short_sha,
|
|
|
|
'revision' => runner.revision,
|
2021-10-27 15:23:28 +05:30
|
|
|
'locked' => false,
|
2021-06-08 01:23:25 +05:30
|
|
|
'active' => runner.active,
|
2022-04-04 11:22:00 +05:30
|
|
|
'paused' => !runner.active,
|
2022-01-26 12:08:38 +05:30
|
|
|
'status' => runner.status('14.5').to_s.upcase,
|
2021-06-08 01:23:25 +05:30
|
|
|
'maximumTimeout' => runner.maximum_timeout,
|
|
|
|
'accessLevel' => runner.access_level.to_s.upcase,
|
|
|
|
'runUntagged' => runner.run_untagged,
|
|
|
|
'ipAddress' => runner.ip_address,
|
2021-10-27 15:23:28 +05:30
|
|
|
'runnerType' => runner.instance_type? ? 'INSTANCE_TYPE' : 'PROJECT_TYPE',
|
2022-03-02 08:16:31 +05:30
|
|
|
'executorName' => runner.executor_type&.dasherize,
|
2021-09-30 23:02:18 +05:30
|
|
|
'jobCount' => 0,
|
2022-04-04 11:22:00 +05:30
|
|
|
'jobs' => a_hash_including("count" => 0, "nodes" => [], "pageInfo" => anything),
|
2021-11-18 22:05:49 +05:30
|
|
|
'projectCount' => nil,
|
|
|
|
'adminUrl' => "http://localhost/admin/runners/#{runner.id}",
|
|
|
|
'userPermissions' => {
|
|
|
|
'readRunner' => true,
|
|
|
|
'updateRunner' => true,
|
|
|
|
'deleteRunner' => true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(runner_data['tagList']).to match_array runner.tag_list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'retrieval with no admin url' 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(
|
2022-04-04 11:22:00 +05:30
|
|
|
'id' => runner.to_global_id.to_s,
|
2021-11-18 22:05:49 +05:30
|
|
|
'adminUrl' => nil
|
2021-06-08 01:23:25 +05:30
|
|
|
)
|
|
|
|
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
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'runner details fetch', :active_instance_runner
|
|
|
|
|
|
|
|
context 'when tagList is not requested' do
|
|
|
|
let(:query) do
|
|
|
|
wrap_fields(query_graphql_path(query_path, 'id'))
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:query_path) do
|
|
|
|
[
|
|
|
|
[:runner, { id: active_instance_runner.to_global_id.to_s }]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not retrieve tagList' do
|
|
|
|
post_graphql(query, current_user: user)
|
|
|
|
|
|
|
|
runner_data = graphql_data_at(:runner)
|
|
|
|
expect(runner_data).not_to be_nil
|
|
|
|
expect(runner_data).not_to include('tagList')
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe 'for project runner' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(is_locked: [true, false])
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:project_runner) do
|
|
|
|
create(:ci_runner, :project, description: 'Runner 3', contacted_at: 1.day.ago, active: false, locked: is_locked,
|
|
|
|
version: 'adfe157', revision: 'b', ip_address: '10.10.10.10', access_level: 1, run_untagged: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:query) do
|
|
|
|
wrap_fields(query_graphql_path(query_path, all_graphql_fields_for('CiRunner')))
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:query_path) do
|
|
|
|
[
|
|
|
|
[:runner, { id: project_runner.to_global_id.to_s }]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves correct locked value' do
|
|
|
|
post_graphql(query, current_user: user)
|
|
|
|
|
|
|
|
runner_data = graphql_data_at(:runner)
|
|
|
|
|
|
|
|
expect(runner_data).to match a_hash_including(
|
2022-04-04 11:22:00 +05:30
|
|
|
'id' => project_runner.to_global_id.to_s,
|
2021-10-27 15:23:28 +05:30
|
|
|
'locked' => is_locked
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
describe 'for inactive runner' do
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'runner details fetch', :inactive_instance_runner
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
describe 'for group runner request' do
|
|
|
|
let(:query) do
|
|
|
|
%(
|
|
|
|
query {
|
|
|
|
runner(id: "#{active_group_runner.to_global_id}") {
|
|
|
|
groups {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves groups field with expected value' do
|
|
|
|
post_graphql(query, current_user: user)
|
|
|
|
|
|
|
|
runner_data = graphql_data_at(:runner, :groups)
|
|
|
|
expect(runner_data).to eq 'nodes' => [{ 'id' => group.to_global_id.to_s }]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
describe 'for runner with status' do
|
|
|
|
let_it_be(:stale_runner) { create(:ci_runner, description: 'Stale runner 1', created_at: 3.months.ago) }
|
|
|
|
let_it_be(:never_contacted_instance_runner) { create(:ci_runner, description: 'Missing runner 1', created_at: 1.month.ago, contacted_at: nil) }
|
|
|
|
|
|
|
|
let(:status_fragment) do
|
|
|
|
%(
|
|
|
|
status
|
|
|
|
legacyStatusWithExplicitVersion: status(legacyMode: "14.5")
|
|
|
|
newStatus: status(legacyMode: null)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:query) do
|
|
|
|
%(
|
|
|
|
query {
|
|
|
|
staleRunner: runner(id: "#{stale_runner.to_global_id}") { #{status_fragment} }
|
|
|
|
pausedRunner: runner(id: "#{inactive_instance_runner.to_global_id}") { #{status_fragment} }
|
|
|
|
neverContactedInstanceRunner: runner(id: "#{never_contacted_instance_runner.to_global_id}") { #{status_fragment} }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves status fields with expected values' do
|
|
|
|
post_graphql(query, current_user: user)
|
|
|
|
|
|
|
|
stale_runner_data = graphql_data_at(:stale_runner)
|
|
|
|
expect(stale_runner_data).to match a_hash_including(
|
|
|
|
'status' => 'NOT_CONNECTED',
|
|
|
|
'legacyStatusWithExplicitVersion' => 'NOT_CONNECTED',
|
|
|
|
'newStatus' => 'STALE'
|
|
|
|
)
|
|
|
|
|
|
|
|
paused_runner_data = graphql_data_at(:paused_runner)
|
|
|
|
expect(paused_runner_data).to match a_hash_including(
|
|
|
|
'status' => 'PAUSED',
|
|
|
|
'legacyStatusWithExplicitVersion' => 'PAUSED',
|
|
|
|
'newStatus' => 'OFFLINE'
|
|
|
|
)
|
|
|
|
|
|
|
|
never_contacted_instance_runner_data = graphql_data_at(:never_contacted_instance_runner)
|
|
|
|
expect(never_contacted_instance_runner_data).to match a_hash_including(
|
|
|
|
'status' => 'NOT_CONNECTED',
|
|
|
|
'legacyStatusWithExplicitVersion' => 'NOT_CONNECTED',
|
|
|
|
'newStatus' => 'NEVER_CONTACTED'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
describe 'for multiple runners' do
|
|
|
|
let_it_be(:project1) { create(:project, :test_repo) }
|
|
|
|
let_it_be(:project2) { create(:project, :test_repo) }
|
|
|
|
let_it_be(:project_runner1) { create(:ci_runner, :project, projects: [project1, project2], description: 'Runner 1') }
|
|
|
|
let_it_be(:project_runner2) { create(:ci_runner, :project, projects: [], description: 'Runner 2') }
|
|
|
|
|
|
|
|
let!(:job) { create(:ci_build, runner: project_runner1) }
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'requesting projects and counts for projects and jobs' do
|
|
|
|
let(:jobs_fragment) do
|
|
|
|
%(
|
|
|
|
jobs {
|
|
|
|
count
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:query) do
|
|
|
|
%(
|
|
|
|
query {
|
|
|
|
projectRunner1: runner(id: "#{project_runner1.to_global_id}") {
|
|
|
|
projectCount
|
|
|
|
jobCount
|
2022-04-04 11:22:00 +05:30
|
|
|
#{jobs_fragment}
|
|
|
|
projects {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
|
|
|
projectRunner2: runner(id: "#{project_runner2.to_global_id}") {
|
|
|
|
projectCount
|
|
|
|
jobCount
|
2022-04-04 11:22:00 +05:30
|
|
|
#{jobs_fragment}
|
|
|
|
projects {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
|
|
|
activeInstanceRunner: runner(id: "#{active_instance_runner.to_global_id}") {
|
|
|
|
projectCount
|
|
|
|
jobCount
|
2022-04-04 11:22:00 +05:30
|
|
|
#{jobs_fragment}
|
|
|
|
projects {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2021-11-18 22:05:49 +05:30
|
|
|
project_runner2.runner_projects.clear
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
post_graphql(query, current_user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves expected fields' do
|
|
|
|
runner1_data = graphql_data_at(:project_runner1)
|
|
|
|
runner2_data = graphql_data_at(:project_runner2)
|
|
|
|
runner3_data = graphql_data_at(:active_instance_runner)
|
|
|
|
|
|
|
|
expect(runner1_data).to match a_hash_including(
|
|
|
|
'jobCount' => 1,
|
2022-04-04 11:22:00 +05:30
|
|
|
'jobs' => a_hash_including(
|
|
|
|
"count" => 1,
|
|
|
|
"nodes" => [{ "id" => job.to_global_id.to_s, "status" => job.status.upcase }]
|
|
|
|
),
|
|
|
|
'projectCount' => 2,
|
|
|
|
'projects' => {
|
|
|
|
'nodes' => [
|
|
|
|
{ 'id' => project1.to_global_id.to_s },
|
|
|
|
{ 'id' => project2.to_global_id.to_s }
|
|
|
|
]
|
|
|
|
})
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(runner2_data).to match a_hash_including(
|
|
|
|
'jobCount' => 0,
|
2022-04-04 11:22:00 +05:30
|
|
|
'jobs' => nil, # returning jobs not allowed for more than 1 runner (see RunnerJobsResolver)
|
|
|
|
'projectCount' => 0,
|
|
|
|
'projects' => {
|
|
|
|
'nodes' => []
|
|
|
|
})
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(runner3_data).to match a_hash_including(
|
|
|
|
'jobCount' => 0,
|
2022-04-04 11:22:00 +05:30
|
|
|
'jobs' => nil, # returning jobs not allowed for more than 1 runner (see RunnerJobsResolver)
|
|
|
|
'projectCount' => nil,
|
|
|
|
'projects' => nil)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'by regular user' do
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:user) { create(:user) }
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'on instance runner' do
|
|
|
|
it_behaves_like 'retrieval by unauthorized user', :active_instance_runner
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on group runner' do
|
|
|
|
it_behaves_like 'retrieval by unauthorized user', :active_group_runner
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on project runner' do
|
|
|
|
it_behaves_like 'retrieval by unauthorized user', :active_project_runner
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
describe 'by non-admin user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_user(user, Gitlab::Access::OWNER)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'retrieval with no admin url', :active_group_runner
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
describe 'by unauthenticated user' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'retrieval by unauthorized user', :active_instance_runner
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Query limits' do
|
|
|
|
def runner_query(runner)
|
|
|
|
<<~SINGLE
|
|
|
|
runner(id: "#{runner.to_global_id}") {
|
2021-09-30 23:02:18 +05:30
|
|
|
#{all_graphql_fields_for('CiRunner', excluded: excluded_fields)}
|
2021-06-08 01:23:25 +05:30
|
|
|
}
|
|
|
|
SINGLE
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Currently excluding a known N+1 issue, see https://gitlab.com/gitlab-org/gitlab/-/issues/334759
|
|
|
|
let(:excluded_fields) { %w[jobCount] }
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:single_query) do
|
|
|
|
<<~QUERY
|
|
|
|
{
|
2021-09-30 23:02:18 +05:30
|
|
|
active: #{runner_query(active_instance_runner)}
|
2021-06-08 01:23:25 +05:30
|
|
|
}
|
|
|
|
QUERY
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:double_query) do
|
|
|
|
<<~QUERY
|
|
|
|
{
|
2021-09-30 23:02:18 +05:30
|
|
|
active: #{runner_query(active_instance_runner)}
|
|
|
|
inactive: #{runner_query(inactive_instance_runner)}
|
2021-06-08 01:23:25 +05:30
|
|
|
}
|
|
|
|
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
|