diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /spec/requests/api/graphql/ci | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'spec/requests/api/graphql/ci')
-rw-r--r-- | spec/requests/api/graphql/ci/config_spec.rb | 6 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/config_variables_spec.rb | 93 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/group_variables_spec.rb | 12 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/instance_variables_spec.rb | 2 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/jobs_spec.rb | 31 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/project_variables_spec.rb | 4 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/runner_spec.rb | 163 | ||||
-rw-r--r-- | spec/requests/api/graphql/ci/runners_spec.rb | 18 |
8 files changed, 292 insertions, 37 deletions
diff --git a/spec/requests/api/graphql/ci/config_spec.rb b/spec/requests/api/graphql/ci/config_spec.rb index 5f8a895b16e..960fda80dd9 100644 --- a/spec/requests/api/graphql/ci/config_spec.rb +++ b/spec/requests/api/graphql/ci/config_spec.rb @@ -173,7 +173,7 @@ RSpec.describe 'Query.ciConfig' do { "name" => "docker", "size" => 1, - "jobs" => + "jobs" => { "nodes" => [ { @@ -206,7 +206,7 @@ RSpec.describe 'Query.ciConfig' do { "name" => "deploy_job", "size" => 1, - "jobs" => + "jobs" => { "nodes" => [ { @@ -332,7 +332,7 @@ RSpec.describe 'Query.ciConfig' do "only" => { "refs" => %w[branches tags] }, "when" => "on_success", "tags" => [], - "needs" => { "nodes" => [] } } + "needs" => { "nodes" => [] } } ] } } diff --git a/spec/requests/api/graphql/ci/config_variables_spec.rb b/spec/requests/api/graphql/ci/config_variables_spec.rb new file mode 100644 index 00000000000..2b5a5d0dc93 --- /dev/null +++ b/spec/requests/api/graphql/ci/config_variables_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Query.project(fullPath).ciConfigVariables(sha)' do + include GraphqlHelpers + include ReactiveCachingHelpers + + let_it_be(:content) do + File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) + end + + let_it_be(:project) { create(:project, :custom_repo, :public, files: { '.gitlab-ci.yml' => content }) } + let_it_be(:user) { create(:user) } + + let(:service) { Ci::ListConfigVariablesService.new(project, user) } + let(:sha) { project.repository.commit.sha } + + let(:query) do + %( + query { + project(fullPath: "#{project.full_path}") { + ciConfigVariables(sha: "#{sha}") { + key + value + description + } + } + } + ) + end + + context 'when the user has the correct permissions' do + before do + project.add_maintainer(user) + allow(Ci::ListConfigVariablesService) + .to receive(:new) + .and_return(service) + end + + context 'when the cache is not empty' do + before do + synchronous_reactive_cache(service) + end + + it 'returns the CI variables for the config' do + expect(service) + .to receive(:execute) + .with(sha) + .and_call_original + + post_graphql(query, current_user: user) + + expect(graphql_data.dig('project', 'ciConfigVariables')).to contain_exactly( + { + 'key' => 'DB_NAME', + 'value' => 'postgres', + 'description' => nil + }, + { + 'key' => 'ENVIRONMENT_VAR', + 'value' => 'env var value', + 'description' => 'env var description' + } + ) + end + end + + context 'when the cache is empty' do + it 'returns nothing' do + post_graphql(query, current_user: user) + + expect(graphql_data.dig('project', 'ciConfigVariables')).to be_nil + end + end + end + + context 'when the user is not authorized' do + before do + project.add_guest(user) + allow(Ci::ListConfigVariablesService) + .to receive(:new) + .and_return(service) + synchronous_reactive_cache(service) + end + + it 'returns nothing' do + post_graphql(query, current_user: user) + + expect(graphql_data.dig('project', 'ciConfigVariables')).to be_nil + end + end +end diff --git a/spec/requests/api/graphql/ci/group_variables_spec.rb b/spec/requests/api/graphql/ci/group_variables_spec.rb index 5ea6646ec2c..7baf26c7648 100644 --- a/spec/requests/api/graphql/ci/group_variables_spec.rb +++ b/spec/requests/api/graphql/ci/group_variables_spec.rb @@ -13,6 +13,7 @@ RSpec.describe 'Query.group(fullPath).ciVariables' do query { group(fullPath: "#{group.full_path}") { ciVariables { + limit nodes { id key @@ -35,11 +36,18 @@ RSpec.describe 'Query.group(fullPath).ciVariables' do end it "returns the group's CI variables" do - variable = create(:ci_group_variable, group: group, key: 'TEST_VAR', value: 'test', - masked: false, protected: true, raw: true, environment_scope: 'staging') + variable = create(:ci_group_variable, + group: group, + key: 'TEST_VAR', + value: 'test', + masked: false, + protected: true, + raw: true, + environment_scope: 'staging') post_graphql(query, current_user: user) + expect(graphql_data.dig('group', 'ciVariables', 'limit')).to be(200) expect(graphql_data.dig('group', 'ciVariables', 'nodes')).to contain_exactly({ 'id' => variable.to_global_id.to_s, 'key' => 'TEST_VAR', diff --git a/spec/requests/api/graphql/ci/instance_variables_spec.rb b/spec/requests/api/graphql/ci/instance_variables_spec.rb index c5c88697bf4..cd6b2de98a1 100644 --- a/spec/requests/api/graphql/ci/instance_variables_spec.rb +++ b/spec/requests/api/graphql/ci/instance_variables_spec.rb @@ -29,7 +29,7 @@ RSpec.describe 'Query.ciVariables' do it "returns the instance's CI variables" do variable = create(:ci_instance_variable, key: 'TEST_VAR', value: 'test', - masked: false, protected: true, raw: true) + masked: false, protected: true, raw: true) post_graphql(query, current_user: user) diff --git a/spec/requests/api/graphql/ci/jobs_spec.rb b/spec/requests/api/graphql/ci/jobs_spec.rb index 8c4ab13fc35..fa8fb1d54aa 100644 --- a/spec/requests/api/graphql/ci/jobs_spec.rb +++ b/spec/requests/api/graphql/ci/jobs_spec.rb @@ -335,4 +335,35 @@ RSpec.describe 'Query.project.pipeline' do end end end + + context 'when querying jobs for multiple projects' do + let(:query) do + %( + query { + projects { + nodes { + jobs { + nodes { + name + } + } + } + } + } + ) + end + + before do + create_list(:project, 2).each do |project| + project.add_developer(user) + create(:ci_build, project: project) + end + end + + it 'returns an error' do + post_graphql(query, current_user: user) + + expect_graphql_errors_to_include [/"jobs" field can be requested only for 1 Project\(s\) at a time./] + end + end end diff --git a/spec/requests/api/graphql/ci/project_variables_spec.rb b/spec/requests/api/graphql/ci/project_variables_spec.rb index e61f146b24c..d49a4a7e768 100644 --- a/spec/requests/api/graphql/ci/project_variables_spec.rb +++ b/spec/requests/api/graphql/ci/project_variables_spec.rb @@ -13,6 +13,7 @@ RSpec.describe 'Query.project(fullPath).ciVariables' do query { project(fullPath: "#{project.full_path}") { ciVariables { + limit nodes { id key @@ -36,10 +37,11 @@ RSpec.describe 'Query.project(fullPath).ciVariables' do it "returns the project's CI variables" do variable = create(:ci_variable, project: project, key: 'TEST_VAR', value: 'test', - masked: false, protected: true, raw: true, environment_scope: 'production') + masked: false, protected: true, raw: true, environment_scope: 'production') post_graphql(query, current_user: user) + expect(graphql_data.dig('project', 'ciVariables', 'limit')).to be(200) expect(graphql_data.dig('project', 'ciVariables', 'nodes')).to contain_exactly({ 'id' => variable.to_global_id.to_s, 'key' => 'TEST_VAR', diff --git a/spec/requests/api/graphql/ci/runner_spec.rb b/spec/requests/api/graphql/ci/runner_spec.rb index e17a83d8e47..bd90753f9ad 100644 --- a/spec/requests/api/graphql/ci/runner_spec.rb +++ b/spec/requests/api/graphql/ci/runner_spec.rb @@ -9,24 +9,53 @@ RSpec.describe 'Query.runner(id)' do let_it_be(:group) { create(:group) } let_it_be(:active_instance_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, executor_type: :custom, - maintenance_note: '**Test maintenance note**') + 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, + executor_type: :custom, + maintenance_note: '**Test maintenance note**') end let_it_be(:inactive_instance_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) + 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 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, - access_level: 0, tag_list: %w[tag1 tag2], run_untagged: true, executor_type: :shell) + 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, + access_level: 0, + tag_list: %w[tag1 tag2], + run_untagged: true, + executor_type: :shell) end - let_it_be(:active_project_runner) { create(:ci_runner, :project) } + let_it_be(:project1) { create(:project) } + let_it_be(:active_project_runner) { create(:ci_runner, :project, projects: [project1]) } shared_examples 'runner details fetch' do let(:query) do @@ -159,8 +188,16 @@ RSpec.describe 'Query.runner(id)' do 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) + 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 @@ -187,7 +224,6 @@ RSpec.describe 'Query.runner(id)' do end describe 'ownerProject' do - let_it_be(:project1) { create(:project) } let_it_be(:project2) { create(:project) } let_it_be(:runner1) { create(:ci_runner, :project, projects: [project2, project1]) } let_it_be(:runner2) { create(:ci_runner, :project, projects: [project1, project2]) } @@ -301,7 +337,6 @@ RSpec.describe 'Query.runner(id)' do end 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') } @@ -394,6 +429,8 @@ RSpec.describe 'Query.runner(id)' do 'jobs' => nil, # returning jobs not allowed for more than 1 runner (see RunnerJobsResolver) 'projectCount' => nil, 'projects' => nil) + + expect_graphql_errors_to_include [/"jobs" field can be requested only for 1 CiRunner\(s\) at a time./] end end end @@ -472,8 +509,8 @@ RSpec.describe 'Query.runner(id)' do <<~QUERY { instance_runner1: #{runner_query(active_instance_runner)} - project_runner1: #{runner_query(active_project_runner)} group_runner1: #{runner_query(active_group_runner)} + project_runner1: #{runner_query(active_project_runner)} } QUERY end @@ -493,12 +530,13 @@ RSpec.describe 'Query.runner(id)' do it 'does not execute more queries per runner', :aggregate_failures do # warm-up license cache and so on: - post_graphql(double_query, current_user: user) + personal_access_token = create(:personal_access_token, user: user) + args = { current_user: user, token: { personal_access_token: personal_access_token } } + post_graphql(double_query, **args) - control = ActiveRecord::QueryRecorder.new { post_graphql(single_query, current_user: user) } + control = ActiveRecord::QueryRecorder.new { post_graphql(single_query, **args) } - expect { post_graphql(double_query, current_user: user) } - .not_to exceed_query_limit(control) + expect { post_graphql(double_query, **args) }.not_to exceed_query_limit(control) expect(graphql_data.count).to eq 6 expect(graphql_data).to match( @@ -528,4 +566,91 @@ RSpec.describe 'Query.runner(id)' do )) end end + + describe 'sorting and pagination' do + let(:query) do + <<~GQL + query($id: CiRunnerID!, $projectSearchTerm: String, $n: Int, $cursor: String) { + runner(id: $id) { + #{fields} + } + } + GQL + end + + before do + post_graphql(query, current_user: user, variables: variables) + end + + context 'with project search term' do + let_it_be(:project1) { create(:project, description: 'abc') } + let_it_be(:project2) { create(:project, description: 'def') } + let_it_be(:project_runner) do + create(:ci_runner, :project, projects: [project1, project2]) + end + + let(:variables) { { id: project_runner.to_global_id.to_s, n: n, project_search_term: search_term } } + + let(:fields) do + <<~QUERY + projects(search: $projectSearchTerm, first: $n, after: $cursor) { + count + nodes { + id + } + pageInfo { + hasPreviousPage + startCursor + endCursor + hasNextPage + } + } + QUERY + end + + let(:projects_data) { graphql_data_at('runner', 'projects') } + + context 'set to empty string' do + let(:search_term) { '' } + + context 'with n = 1' do + let(:n) { 1 } + + it_behaves_like 'a working graphql query' + + it 'returns paged result' do + expect(projects_data).not_to be_nil + expect(projects_data['count']).to eq 2 + expect(projects_data['pageInfo']['hasNextPage']).to eq true + end + end + + context 'with n = 2' do + let(:n) { 2 } + + it 'returns non-paged result' do + expect(projects_data).not_to be_nil + expect(projects_data['count']).to eq 2 + expect(projects_data['pageInfo']['hasNextPage']).to eq false + end + end + end + + context 'set to partial match' do + let(:search_term) { 'def' } + + context 'with n = 1' do + let(:n) { 1 } + + it_behaves_like 'a working graphql query' + + it 'returns paged result with no additional pages' do + expect(projects_data).not_to be_nil + expect(projects_data['count']).to eq 1 + expect(projects_data['pageInfo']['hasNextPage']).to eq false + end + end + end + end + end end diff --git a/spec/requests/api/graphql/ci/runners_spec.rb b/spec/requests/api/graphql/ci/runners_spec.rb index 749f6839cb5..3054b866812 100644 --- a/spec/requests/api/graphql/ci/runners_spec.rb +++ b/spec/requests/api/graphql/ci/runners_spec.rb @@ -69,15 +69,6 @@ RSpec.describe 'Query.runners' do it_behaves_like 'a working graphql query returning expected runner' end - - context 'runner_type is PROJECT_TYPE and status is NEVER_CONTACTED' do - let(:runner_type) { 'PROJECT_TYPE' } - let(:status) { 'NEVER_CONTACTED' } - - let!(:expected_runner) { project_runner } - - it_behaves_like 'a working graphql query returning expected runner' - end end describe 'pagination' do @@ -141,8 +132,13 @@ RSpec.describe 'Group.runners' do describe 'edges' do let_it_be(:runner) do - create(:ci_runner, :group, active: false, version: 'def', revision: '456', - description: 'Project runner', groups: [group], ip_address: '127.0.0.1') + create(:ci_runner, :group, + active: false, + version: 'def', + revision: '456', + description: 'Project runner', + groups: [group], + ip_address: '127.0.0.1') end let(:query) do |