2021-09-04 01:27:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe Mutations::Ci::Runner::Update, feature_category: :runner_fleet do
|
2021-09-04 01:27:46 +05:30
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
2022-10-11 01:57:18 +05:30
|
|
|
let_it_be(:project1) { create(:project) }
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:project2) { create(:project) }
|
|
|
|
|
|
|
|
let(:runner) do
|
|
|
|
create(:ci_runner, :project, projects: [project1, project2], active: true, locked: false, run_untagged: true)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
let(:current_ctx) { { current_user: user } }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:mutated_runner) { response[:runner] }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
let(:mutation_params) do
|
|
|
|
{
|
|
|
|
id: runner.to_global_id,
|
|
|
|
description: 'updated description'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(described_class).to require_graphql_authorizations(:update_runner) }
|
|
|
|
|
|
|
|
describe '#resolve' do
|
2022-10-11 01:57:18 +05:30
|
|
|
subject(:response) do
|
2021-09-04 01:27:46 +05:30
|
|
|
sync(resolve(described_class, args: mutation_params, ctx: current_ctx))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot admin the runner' do
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'generates an error' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
2022-10-11 01:57:18 +05:30
|
|
|
response
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when required arguments are missing' do
|
|
|
|
let(:mutation_params) { {} }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect { response }.to raise_error(ArgumentError, "Arguments must be provided: id")
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-08 21:46:49 +05:30
|
|
|
context 'when user can update runner' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-05-08 21:46:49 +05:30
|
|
|
let(:original_projects) { [project1, project2] }
|
|
|
|
let(:projects_with_maintainer_access) { original_projects }
|
|
|
|
|
|
|
|
let(:current_ctx) { { current_user: user } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
projects_with_maintainer_access.each { |project| project.add_maintainer(user) }
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'with valid arguments' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:mutation_params) do
|
|
|
|
{
|
|
|
|
id: runner.to_global_id,
|
|
|
|
description: 'updated description',
|
|
|
|
maintenance_note: 'updated maintenance note',
|
|
|
|
maximum_timeout: 900,
|
|
|
|
access_level: 'ref_protected',
|
|
|
|
active: false,
|
|
|
|
locked: true,
|
|
|
|
run_untagged: false,
|
|
|
|
tag_list: %w(tag1 tag2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it 'updates runner with correct values' do
|
|
|
|
expected_attributes = mutation_params.except(:id, :tag_list)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
response
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(response[:errors]).to be_empty
|
|
|
|
expect(response[:runner]).to be_an_instance_of(Ci::Runner)
|
|
|
|
expect(response[:runner]).to have_attributes(expected_attributes)
|
|
|
|
expect(response[:runner].tag_list).to contain_exactly(*mutation_params[:tag_list])
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(runner.reload).to have_attributes(expected_attributes)
|
|
|
|
expect(runner.tag_list).to contain_exactly(*mutation_params[:tag_list])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
context 'with associatedProjects argument' do
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:project3) { create(:project) }
|
2023-05-08 21:46:49 +05:30
|
|
|
let_it_be(:project4) { create(:project) }
|
|
|
|
|
|
|
|
let(:new_projects) { [project3, project4] }
|
|
|
|
let(:mutation_params) do
|
|
|
|
{
|
|
|
|
id: runner.to_global_id,
|
|
|
|
description: 'updated description',
|
|
|
|
associated_projects: new_projects.map { |project| project.to_global_id.to_s }
|
|
|
|
}
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
context 'with id set to project runner' do
|
2023-05-08 21:46:49 +05:30
|
|
|
let(:projects_with_maintainer_access) { original_projects + new_projects }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
it 'updates runner attributes and project relationships', :aggregate_failures do
|
2023-05-08 21:46:49 +05:30
|
|
|
setup_service_expectations
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
expected_attributes = mutation_params.except(:id, :associated_projects)
|
|
|
|
|
|
|
|
response
|
|
|
|
|
|
|
|
expect(response[:errors]).to be_empty
|
|
|
|
expect(response[:runner]).to be_an_instance_of(Ci::Runner)
|
|
|
|
expect(response[:runner]).to have_attributes(expected_attributes)
|
|
|
|
expect(runner.reload).to have_attributes(expected_attributes)
|
2023-05-08 21:46:49 +05:30
|
|
|
expect(runner.projects).to match_array([project1] + new_projects)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
2023-05-08 21:46:49 +05:30
|
|
|
context 'with missing permissions on one of the new projects' do
|
|
|
|
let(:projects_with_maintainer_access) { original_projects + [project3] }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
it 'does not update runner', :aggregate_failures do
|
2023-05-08 21:46:49 +05:30
|
|
|
setup_service_expectations
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
expected_attributes = mutation_params.except(:id, :associated_projects)
|
|
|
|
|
|
|
|
response
|
|
|
|
|
2023-05-08 21:46:49 +05:30
|
|
|
expect(response[:errors]).to match_array(['user is not authorized to add runners to project'])
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(response[:runner]).to be_nil
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(runner.reload).not_to have_attributes(expected_attributes)
|
2023-05-08 21:46:49 +05:30
|
|
|
expect(runner.projects).to match_array(original_projects)
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an empty list of projects' do
|
2023-05-08 21:46:49 +05:30
|
|
|
let(:new_projects) { [] }
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
it 'removes project relationships', :aggregate_failures do
|
2023-05-08 21:46:49 +05:30
|
|
|
setup_service_expectations
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
response
|
|
|
|
|
|
|
|
expect(response[:errors]).to be_empty
|
|
|
|
expect(response[:runner]).to be_an_instance_of(Ci::Runner)
|
|
|
|
expect(runner.reload.projects).to contain_exactly(project1)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-08 21:46:49 +05:30
|
|
|
context 'with id set to instance runner', :enable_admin_mode do
|
|
|
|
let_it_be(:user) { create(:user, :admin) }
|
|
|
|
let_it_be(:runner) { create(:ci_runner, :instance) }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
it 'raises error', :aggregate_failures do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
|
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 21:46:49 +05:30
|
|
|
|
|
|
|
def setup_service_expectations
|
|
|
|
expect_next_instance_of(
|
|
|
|
::Ci::Runners::SetRunnerAssociatedProjectsService,
|
|
|
|
{
|
|
|
|
runner: runner,
|
|
|
|
current_user: user,
|
|
|
|
project_ids: new_projects.map(&:id)
|
|
|
|
}
|
|
|
|
) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with non-existing project ID in associatedProjects argument' do
|
|
|
|
let(:mutation_params) do
|
|
|
|
{
|
|
|
|
id: runner.to_global_id,
|
2022-11-25 23:54:43 +05:30
|
|
|
associated_projects: ["gid://gitlab/Project/#{non_existing_record_id}"]
|
2022-10-11 01:57:18 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change associated projects' do
|
|
|
|
expected_attributes = mutation_params.except(:id, :associated_projects)
|
|
|
|
|
|
|
|
response
|
|
|
|
|
|
|
|
expect(response[:errors]).to be_empty
|
|
|
|
expect(response[:runner]).to be_an_instance_of(Ci::Runner)
|
|
|
|
expect(response[:runner]).to have_attributes(expected_attributes)
|
|
|
|
expect(runner.reload).to have_attributes(expected_attributes)
|
|
|
|
expect(runner.projects).to match_array([project1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
context 'with out-of-range maximum_timeout and missing tag_list' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:mutation_params) do
|
|
|
|
{
|
|
|
|
id: runner.to_global_id,
|
|
|
|
maximum_timeout: 100,
|
|
|
|
run_untagged: false
|
|
|
|
}
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns a descriptive error' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(response[:runner]).to be_nil
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(response[:errors]).to contain_exactly(
|
2021-09-04 01:27:46 +05:30
|
|
|
'Maximum timeout needs to be at least 10 minutes',
|
|
|
|
'Tags list can not be empty when runner is not allowed to pick untagged jobs'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
context 'with too long maintenance note' do
|
|
|
|
it 'returns a descriptive error' do
|
|
|
|
mutation_params[:maintenance_note] = '1' * 1025
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(response[:runner]).to be_nil
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(response[:errors]).to contain_exactly(
|
2022-07-23 23:45:48 +05:30
|
|
|
'Maintenance note is too long (maximum is 1024 characters)'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|