diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-12 16:26:10 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-12 16:26:10 +0000 |
commit | 6653ccc011dec86e5140a5d09ea3b2357eab6714 (patch) | |
tree | 897193f37bcd98152a0ac214f80a3c4cfe1047c5 /spec/lib/gitlab/etag_caching | |
parent | bff35a05aed6a31380a73c39113808fd262c2c37 (diff) | |
download | gitlab-ce-13.10.0-rc41.tar.gz |
Add latest changes from gitlab-org/gitlab@13-10-stable-eev13.10.0-rc41
Diffstat (limited to 'spec/lib/gitlab/etag_caching')
-rw-r--r-- | spec/lib/gitlab/etag_caching/router/graphql_spec.rb | 50 | ||||
-rw-r--r-- | spec/lib/gitlab/etag_caching/router/restful_spec.rb | 124 | ||||
-rw-r--r-- | spec/lib/gitlab/etag_caching/router_spec.rb | 147 | ||||
-rw-r--r-- | spec/lib/gitlab/etag_caching/store_spec.rb | 84 |
4 files changed, 280 insertions, 125 deletions
diff --git a/spec/lib/gitlab/etag_caching/router/graphql_spec.rb b/spec/lib/gitlab/etag_caching/router/graphql_spec.rb new file mode 100644 index 00000000000..d151dcba413 --- /dev/null +++ b/spec/lib/gitlab/etag_caching/router/graphql_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::EtagCaching::Router::Graphql do + it 'matches pipelines endpoint' do + result = match_route('/api/graphql', 'pipelines/id/1') + + expect(result).to be_present + expect(result.name).to eq 'pipelines_graph' + end + + it 'has a valid feature category for every route', :aggregate_failures do + feature_categories = YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).to_set + + described_class::ROUTES.each do |route| + expect(feature_categories).to include(route.feature_category), "#{route.name} has a category of #{route.feature_category}, which is not valid" + end + end + + def match_route(path, header) + described_class.match( + double(path_info: path, + headers: { 'X-GITLAB-GRAPHQL-RESOURCE-ETAG' => header })) + end + + describe '.cache_key' do + let(:path) { '/api/graphql' } + let(:header_value) { 'pipelines/id/1' } + let(:headers) do + { 'X-GITLAB-GRAPHQL-RESOURCE-ETAG' => header_value }.compact + end + + subject do + described_class.cache_key(double(path: path, headers: headers)) + end + + it 'uses request path and headers as cache key' do + is_expected.to eq '/api/graphql:pipelines/id/1' + end + + context 'when the header is missing' do + let(:header_value) {} + + it 'does not raise errors' do + is_expected.to eq '/api/graphql' + end + end + end +end diff --git a/spec/lib/gitlab/etag_caching/router/restful_spec.rb b/spec/lib/gitlab/etag_caching/router/restful_spec.rb new file mode 100644 index 00000000000..877789b320f --- /dev/null +++ b/spec/lib/gitlab/etag_caching/router/restful_spec.rb @@ -0,0 +1,124 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::EtagCaching::Router::Restful do + it 'matches issue notes endpoint' do + result = match_route('/my-group/and-subgroup/here-comes-the-project/noteable/issue/1/notes') + + expect(result).to be_present + expect(result.name).to eq 'issue_notes' + end + + it 'matches MR notes endpoint' do + result = match_route('/my-group/and-subgroup/here-comes-the-project/noteable/merge_request/1/notes') + + expect(result).to be_present + expect(result.name).to eq 'merge_request_notes' + end + + it 'matches issue title endpoint' do + result = match_route('/my-group/my-project/-/issues/123/realtime_changes') + + expect(result).to be_present + expect(result.name).to eq 'issue_title' + end + + it 'matches with a project name that includes a suffix of create' do + result = match_route('/group/test-create/-/issues/123/realtime_changes') + + expect(result).to be_present + expect(result.name).to eq 'issue_title' + end + + it 'matches with a project name that includes a prefix of create' do + result = match_route('/group/create-test/-/issues/123/realtime_changes') + + expect(result).to be_present + expect(result.name).to eq 'issue_title' + end + + it 'matches project pipelines endpoint' do + result = match_route('/my-group/my-project/-/pipelines.json') + + expect(result).to be_present + expect(result.name).to eq 'project_pipelines' + end + + it 'matches commit pipelines endpoint' do + result = match_route('/my-group/my-project/-/commit/aa8260d253a53f73f6c26c734c72fdd600f6e6d4/pipelines.json') + + expect(result).to be_present + expect(result.name).to eq 'commit_pipelines' + end + + it 'matches new merge request pipelines endpoint' do + result = match_route('/my-group/my-project/-/merge_requests/new.json') + + expect(result).to be_present + expect(result.name).to eq 'new_merge_request_pipelines' + end + + it 'matches merge request pipelines endpoint' do + result = match_route('/my-group/my-project/-/merge_requests/234/pipelines.json') + + expect(result).to be_present + expect(result.name).to eq 'merge_request_pipelines' + end + + it 'matches build endpoint' do + result = match_route('/my-group/my-project/builds/234.json') + + expect(result).to be_present + expect(result.name).to eq 'project_build' + end + + it 'does not match blob with confusing name' do + result = match_route('/my-group/my-project/-/blob/master/pipelines.json') + + expect(result).to be_blank + end + + it 'matches the cluster environments path' do + result = match_route('/my-group/my-project/-/clusters/47/environments') + + expect(result).to be_present + expect(result.name).to eq 'cluster_environments' + end + + it 'matches the environments path' do + result = match_route('/my-group/my-project/environments.json') + + expect(result).to be_present + expect(result.name).to eq 'environments' + end + + it 'matches pipeline#show endpoint' do + result = match_route('/my-group/my-project/-/pipelines/2.json') + + expect(result).to be_present + expect(result.name).to eq 'project_pipeline' + end + + it 'has a valid feature category for every route', :aggregate_failures do + feature_categories = YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).to_set + + described_class::ROUTES.each do |route| + expect(feature_categories).to include(route.feature_category), "#{route.name} has a category of #{route.feature_category}, which is not valid" + end + end + + def match_route(path) + described_class.match(double(path_info: path)) + end + + describe '.cache_key' do + subject do + described_class.cache_key(double(path: '/my-group/my-project/builds/234.json')) + end + + it 'uses request path as cache key' do + is_expected.to eq '/my-group/my-project/builds/234.json' + end + end +end diff --git a/spec/lib/gitlab/etag_caching/router_spec.rb b/spec/lib/gitlab/etag_caching/router_spec.rb index dbd9cc230f1..c748ee00721 100644 --- a/spec/lib/gitlab/etag_caching/router_spec.rb +++ b/spec/lib/gitlab/etag_caching/router_spec.rb @@ -3,136 +3,33 @@ require 'spec_helper' RSpec.describe Gitlab::EtagCaching::Router do - it 'matches issue notes endpoint' do - result = described_class.match( - '/my-group/and-subgroup/here-comes-the-project/noteable/issue/1/notes' - ) - - expect(result).to be_present - expect(result.name).to eq 'issue_notes' - end - - it 'matches MR notes endpoint' do - result = described_class.match( - '/my-group/and-subgroup/here-comes-the-project/noteable/merge_request/1/notes' - ) - - expect(result).to be_present - expect(result.name).to eq 'merge_request_notes' - end - - it 'matches issue title endpoint' do - result = described_class.match( - '/my-group/my-project/-/issues/123/realtime_changes' - ) - - expect(result).to be_present - expect(result.name).to eq 'issue_title' - end - - it 'matches with a project name that includes a suffix of create' do - result = described_class.match( - '/group/test-create/-/issues/123/realtime_changes' - ) - - expect(result).to be_present - expect(result.name).to eq 'issue_title' - end - - it 'matches with a project name that includes a prefix of create' do - result = described_class.match( - '/group/create-test/-/issues/123/realtime_changes' - ) - - expect(result).to be_present - expect(result.name).to eq 'issue_title' - end - - it 'matches project pipelines endpoint' do - result = described_class.match( - '/my-group/my-project/-/pipelines.json' - ) - - expect(result).to be_present - expect(result.name).to eq 'project_pipelines' - end - - it 'matches commit pipelines endpoint' do - result = described_class.match( - '/my-group/my-project/-/commit/aa8260d253a53f73f6c26c734c72fdd600f6e6d4/pipelines.json' - ) - - expect(result).to be_present - expect(result.name).to eq 'commit_pipelines' - end - - it 'matches new merge request pipelines endpoint' do - result = described_class.match( - '/my-group/my-project/-/merge_requests/new.json' - ) - - expect(result).to be_present - expect(result.name).to eq 'new_merge_request_pipelines' - end - - it 'matches merge request pipelines endpoint' do - result = described_class.match( - '/my-group/my-project/-/merge_requests/234/pipelines.json' - ) - - expect(result).to be_present - expect(result.name).to eq 'merge_request_pipelines' - end - - it 'matches build endpoint' do - result = described_class.match( - '/my-group/my-project/builds/234.json' - ) - - expect(result).to be_present - expect(result.name).to eq 'project_build' - end - - it 'does not match blob with confusing name' do - result = described_class.match( - '/my-group/my-project/-/blob/master/pipelines.json' - ) - - expect(result).to be_blank - end + describe '.match', :aggregate_failures do + context 'with RESTful routes' do + it 'matches project pipelines endpoint' do + result = match_route('/my-group/my-project/-/pipelines.json') + + expect(result).to be_present + expect(result.name).to eq 'project_pipelines' + expect(result.router).to eq Gitlab::EtagCaching::Router::Restful + end + end - it 'matches the cluster environments path' do - result = described_class.match( - '/my-group/my-project/-/clusters/47/environments' - ) + context 'with GraphQL routes' do + it 'matches pipelines endpoint' do + result = match_route('/api/graphql', 'pipelines/id/12') - expect(result).to be_present - expect(result.name).to eq 'cluster_environments' + expect(result).to be_present + expect(result.name).to eq 'pipelines_graph' + expect(result.router).to eq Gitlab::EtagCaching::Router::Graphql + end + end end - it 'matches the environments path' do - result = described_class.match( - '/my-group/my-project/environments.json' - ) + def match_route(path, header = nil) + headers = { 'X-GITLAB-GRAPHQL-RESOURCE-ETAG' => header }.compact - expect(result).to be_present - expect(result.name).to eq 'environments' - end - - it 'matches pipeline#show endpoint' do - result = described_class.match( - '/my-group/my-project/-/pipelines/2.json' + described_class.match( + double(path_info: path, headers: headers) ) - - expect(result).to be_present - expect(result.name).to eq 'project_pipeline' - end - - it 'has a valid feature category for every route', :aggregate_failures do - feature_categories = YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).to_set - - described_class::ROUTES.each do |route| - expect(feature_categories).to include(route.feature_category), "#{route.name} has a category of #{route.feature_category}, which is not valid" - end end end diff --git a/spec/lib/gitlab/etag_caching/store_spec.rb b/spec/lib/gitlab/etag_caching/store_spec.rb new file mode 100644 index 00000000000..46195e64715 --- /dev/null +++ b/spec/lib/gitlab/etag_caching/store_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::EtagCaching::Store, :clean_gitlab_redis_shared_state do + let(:store) { described_class.new } + + describe '#get' do + subject { store.get(key) } + + context 'with invalid keys' do + let(:key) { 'a' } + + it 'raises errors' do + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).and_call_original + + expect { subject }.to raise_error Gitlab::EtagCaching::Store::InvalidKeyError + end + + it 'does not raise errors in production' do + expect(store).to receive(:skip_validation?).and_return true + expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception) + + subject + end + end + + context 'with GraphQL keys' do + let(:key) { '/api/graphql:pipelines/id/5' } + + it 'returns a stored value' do + etag = store.touch(key) + + is_expected.to eq(etag) + end + end + + context 'with RESTful keys' do + let(:key) { '/my-group/my-project/builds/234.json' } + + it 'returns a stored value' do + etag = store.touch(key) + + is_expected.to eq(etag) + end + end + end + + describe '#touch' do + subject { store.touch(key) } + + context 'with invalid keys' do + let(:key) { 'a' } + + it 'raises errors' do + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).and_call_original + + expect { subject }.to raise_error Gitlab::EtagCaching::Store::InvalidKeyError + end + end + + context 'with GraphQL keys' do + let(:key) { '/api/graphql:pipelines/id/5' } + + it 'stores and returns a value' do + etag = store.touch(key) + + expect(etag).to be_present + expect(store.get(key)).to eq(etag) + end + end + + context 'with RESTful keys' do + let(:key) { '/my-group/my-project/builds/234.json' } + + it 'stores and returns a value' do + etag = store.touch(key) + + expect(etag).to be_present + expect(store.get(key)).to eq(etag) + end + end + end +end |