diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2017-09-01 09:49:28 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2017-09-01 09:49:28 +0300 |
commit | 6f96ccaa7dd53a7462b86a0ebe8af66afde86aa2 (patch) | |
tree | fa19cdd2b032ee81c38363b8e5b76624712fe55d /spec | |
parent | 8713afe61fb1beaff4d550a60b88d274c47006ea (diff) | |
parent | eaf60bb5441190e2ffcf219b3169bda2237d57cd (diff) |
Merge branch 'gitaly-shell-redis' into 'master'
Implement /internal/post_receive unified endpoint for PostReceive tasks
See merge request !13916
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/reference_counter_spec.rb | 37 | ||||
-rw-r--r-- | spec/requests/api/internal_spec.rb | 89 |
2 files changed, 126 insertions, 0 deletions
diff --git a/spec/lib/gitlab/reference_counter_spec.rb b/spec/lib/gitlab/reference_counter_spec.rb new file mode 100644 index 00000000000..b2344d1870a --- /dev/null +++ b/spec/lib/gitlab/reference_counter_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe Gitlab::ReferenceCounter do + let(:redis) { double('redis') } + let(:reference_counter_key) { "git-receive-pack-reference-counter:project-1" } + let(:reference_counter) { described_class.new('project-1') } + + before do + allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis) + end + + it 'increases and set the expire time of a reference count for a path' do + expect(redis).to receive(:incr).with(reference_counter_key) + expect(redis).to receive(:expire).with(reference_counter_key, + described_class::REFERENCE_EXPIRE_TIME) + expect(reference_counter.increase).to be(true) + end + + it 'decreases the reference count for a path' do + allow(redis).to receive(:decr).and_return(0) + expect(redis).to receive(:decr).with(reference_counter_key) + expect(reference_counter.decrease).to be(true) + end + + it 'warns if attempting to decrease a counter with a value of one or less, and resets the counter' do + expect(redis).to receive(:decr).and_return(-1) + expect(redis).to receive(:del) + expect(Rails.logger).to receive(:warn).with("Reference counter for project-1" \ + " decreased when its value was less than 1. Reseting the counter.") + expect(reference_counter.decrease).to be(true) + end + + it 'get the reference count for a path' do + allow(redis).to receive(:get).and_return(1) + expect(reference_counter.value).to be(1) + end +end diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index e9c30dba8d4..a6c804fb2b3 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -660,6 +660,95 @@ describe API::Internal do # end # end + describe 'POST /internal/post_receive' do + let(:gl_repository) { "project-#{project.id}" } + let(:identifier) { 'key-123' } + let(:reference_counter) { double('ReferenceCounter') } + + let(:valid_params) do + { + gl_repository: gl_repository, + secret_token: secret_token, + identifier: identifier, + changes: changes + } + end + + let(:changes) do + "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/new_branch" + end + + before do + project.team << [user, :developer] + end + + it 'enqueues a PostReceive worker job' do + expect(PostReceive).to receive(:perform_async) + .with(gl_repository, identifier, changes) + + post api("/internal/post_receive"), valid_params + end + + it 'decreases the reference counter and returns the result' do + expect(Gitlab::ReferenceCounter).to receive(:new).with(gl_repository) + .and_return(reference_counter) + expect(reference_counter).to receive(:decrease).and_return(true) + + post api("/internal/post_receive"), valid_params + + expect(json_response['reference_counter_decreased']).to be(true) + end + + it 'returns link to create new merge request' do + post api("/internal/post_receive"), valid_params + + expect(json_response['merge_request_urls']).to match [{ + "branch_name" => "new_branch", + "url" => "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch", + "new_merge_request" => true + }] + end + + it 'returns empty array if printing_merge_request_link_enabled is false' do + project.update!(printing_merge_request_link_enabled: false) + + post api("/internal/post_receive"), valid_params + + expect(json_response['merge_request_urls']).to eq([]) + end + + context 'broadcast message exists' do + let!(:broadcast_message) { create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now ) } + + it 'returns one broadcast message' do + post api("/internal/post_receive"), valid_params + + expect(response).to have_http_status(200) + expect(json_response['broadcast_message']).to eq(broadcast_message.message) + end + end + + context 'broadcast message does not exist' do + it 'returns empty string' do + post api("/internal/post_receive"), valid_params + + expect(response).to have_http_status(200) + expect(json_response['broadcast_message']).to eq(nil) + end + end + + context 'nil broadcast message' do + it 'returns empty string' do + allow(BroadcastMessage).to receive(:current).and_return(nil) + + post api("/internal/post_receive"), valid_params + + expect(response).to have_http_status(200) + expect(json_response['broadcast_message']).to eq(nil) + end + end + end + def project_with_repo_path(path) double().tap do |fake_project| allow(fake_project).to receive_message_chain('repository.path_to_repo' => path) |