1# frozen_string_literal: true 2 3require 'spec_helper' 4 5RSpec.describe Ci::ExpirePipelineCacheService do 6 let_it_be(:user) { create(:user) } 7 let_it_be(:project) { create(:project) } 8 let_it_be(:pipeline) { create(:ci_pipeline, project: project) } 9 10 subject { described_class.new } 11 12 describe '#execute' do 13 it 'invalidates Etag caching for project pipelines path' do 14 pipelines_path = "/#{project.full_path}/-/pipelines.json" 15 new_mr_pipelines_path = "/#{project.full_path}/-/merge_requests/new.json" 16 pipeline_path = "/#{project.full_path}/-/pipelines/#{pipeline.id}.json" 17 graphql_pipeline_path = "/api/graphql:pipelines/id/#{pipeline.id}" 18 graphql_pipeline_sha_path = "/api/graphql:pipelines/sha/#{pipeline.sha}" 19 graphql_project_on_demand_scan_counts_path = "/api/graphql:on_demand_scan/counts/#{project.full_path}" 20 21 expect_touched_etag_caching_paths( 22 pipelines_path, 23 new_mr_pipelines_path, 24 pipeline_path, 25 graphql_pipeline_path, 26 graphql_pipeline_sha_path, 27 graphql_project_on_demand_scan_counts_path 28 ) 29 30 subject.execute(pipeline) 31 end 32 33 it 'invalidates Etag caching for merge request pipelines if pipeline runs on any commit of that source branch' do 34 merge_request = create(:merge_request, :with_detached_merge_request_pipeline) 35 project = merge_request.target_project 36 37 merge_request_pipelines_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/pipelines.json" 38 merge_request_widget_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/cached_widget.json" 39 40 expect_touched_etag_caching_paths( 41 merge_request_pipelines_path, 42 merge_request_widget_path 43 ) 44 45 subject.execute(merge_request.all_pipelines.last) 46 end 47 48 it 'updates the cached status for a project' do 49 expect(Gitlab::Cache::Ci::ProjectPipelineStatus).to receive(:update_for_pipeline).with(pipeline) 50 51 subject.execute(pipeline) 52 end 53 54 context 'destroyed pipeline' do 55 let(:project_with_repo) { create(:project, :repository) } 56 let!(:pipeline_with_commit) { create(:ci_pipeline, :success, project: project_with_repo, sha: project_with_repo.commit.id) } 57 58 it 'clears the cache', :use_clean_rails_redis_caching do 59 create(:commit_status, :success, pipeline: pipeline_with_commit, ref: pipeline_with_commit.ref) 60 61 # Sanity check 62 expect(project_with_repo.pipeline_status.has_status?).to be_truthy 63 64 subject.execute(pipeline_with_commit, delete: true) 65 66 pipeline_with_commit.destroy! 67 68 # We need to reset lazy_latest_pipeline cache to simulate a new request 69 BatchLoader::Executor.clear_current 70 71 # Need to use find to avoid memoization 72 expect(Project.find(project_with_repo.id).pipeline_status.has_status?).to be_falsey 73 end 74 end 75 76 context 'when the pipeline is triggered by another pipeline' do 77 let(:source) { create(:ci_sources_pipeline, pipeline: pipeline) } 78 79 it 'updates the cache of dependent pipeline' do 80 dependent_pipeline_path = "/#{source.source_project.full_path}/-/pipelines/#{source.source_pipeline.id}.json" 81 82 expect_touched_etag_caching_paths(dependent_pipeline_path) 83 84 subject.execute(pipeline) 85 end 86 end 87 88 context 'when the pipeline triggered another pipeline' do 89 let(:build) { create(:ci_build, pipeline: pipeline) } 90 let(:source) { create(:ci_sources_pipeline, source_job: build) } 91 92 it 'updates the cache of dependent pipeline' do 93 dependent_pipeline_path = "/#{source.project.full_path}/-/pipelines/#{source.pipeline.id}.json" 94 95 expect_touched_etag_caching_paths(dependent_pipeline_path) 96 97 subject.execute(pipeline) 98 end 99 end 100 101 it 'does not do N+1 queries' do 102 subject.execute(pipeline) 103 104 control = ActiveRecord::QueryRecorder.new { subject.execute(pipeline) } 105 106 create(:ci_sources_pipeline, pipeline: pipeline) 107 create(:ci_sources_pipeline, source_job: create(:ci_build, pipeline: pipeline)) 108 109 expect { subject.execute(pipeline) }.not_to exceed_query_limit(control.count) 110 end 111 end 112 113 def expect_touched_etag_caching_paths(*paths) 114 expect_next_instance_of(Gitlab::EtagCaching::Store) do |store| 115 expect(store).to receive(:touch).and_wrap_original do |m, *args| 116 expect(args).to include(*paths) 117 118 m.call(*args) 119 end 120 end 121 end 122end 123