From 623eb68195d51ea50e09970771442d992ff19a4a Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Thu, 16 Nov 2017 17:12:33 +0100 Subject: Add new API endpoint - list jobs of a specified runner --- spec/requests/api/runners_spec.rb | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'spec') diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index fe38a7b3251..95e5dd91c12 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -354,6 +354,98 @@ describe API::Runners do end end + describe 'GET /runners/:id/jobs' do + let!(:job_1) { create(:ci_build) } + let!(:job_2) { create(:ci_build, :running, runner: shared_runner) } + let!(:job_3) { create(:ci_build, :failed, runner: shared_runner) } + let!(:job_4) { create(:ci_build, :running, runner: specific_runner) } + let!(:job_5) { create(:ci_build, :failed, runner: specific_runner) } + + context 'admin user' do + context 'when runner exists' do + context 'when runner is shared' do + it 'return jobs' do + get api("/runners/#{shared_runner.id}/jobs", admin) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response.length).to eq(1) + expect(json_response[0]).to include('id' => job_2.id) + end + end + + context 'when runner is specific' do + it 'return jobs' do + get api("/runners/#{specific_runner.id}/jobs", admin) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response.length).to eq(1) + expect(json_response[0]).to include('id' => job_4.id) + end + end + end + + context "when runner doesn't exist" do + it 'returns 404' do + get api('/runners/9999/jobs', admin) + + expect(response).to have_gitlab_http_status(404) + end + end + end + + context "runner project's administrative user" do + context 'when runner exists' do + context 'when runner is shared' do + it 'returns 403' do + get api("/runners/#{shared_runner.id}/jobs", user) + + expect(response).to have_gitlab_http_status(403) + end + end + + context 'when runner is specific' do + it 'return jobs' do + get api("/runners/#{specific_runner.id}/jobs", user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response.length).to eq(1) + expect(json_response[0]).to include('id' => job_4.id) + end + end + end + + context "when runner doesn't exist" do + it 'returns 404' do + get api('/runners/9999/jobs', user) + + expect(response).to have_gitlab_http_status(404) + end + end + end + + context 'other authorized user' do + it 'does not return jobs' do + get api("/runners/#{specific_runner.id}/jobs", user2) + + expect(response).to have_gitlab_http_status(403) + end + end + + context 'unauthorized user' do + it 'does not return jobs' do + get api("/runners/#{specific_runner.id}/jobs") + + expect(response).to have_gitlab_http_status(401) + end + end + end + describe 'GET /projects/:id/runners' do context 'authorized user with master privileges' do it "returns project's runners" do -- cgit v1.2.1 From 8d3e80692cbeea06dd28a052554f0c262004e18d Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Thu, 16 Nov 2017 19:44:14 +0100 Subject: Add information about project --- spec/requests/api/runners_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index 95e5dd91c12..d68fa13af1e 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -356,10 +356,10 @@ describe API::Runners do describe 'GET /runners/:id/jobs' do let!(:job_1) { create(:ci_build) } - let!(:job_2) { create(:ci_build, :running, runner: shared_runner) } - let!(:job_3) { create(:ci_build, :failed, runner: shared_runner) } - let!(:job_4) { create(:ci_build, :running, runner: specific_runner) } - let!(:job_5) { create(:ci_build, :failed, runner: specific_runner) } + let!(:job_2) { create(:ci_build, :running, runner: shared_runner, project: project) } + let!(:job_3) { create(:ci_build, :failed, runner: shared_runner, project: project) } + let!(:job_4) { create(:ci_build, :running, runner: specific_runner, project: project) } + let!(:job_5) { create(:ci_build, :failed, runner: specific_runner, project: project) } context 'admin user' do context 'when runner exists' do -- cgit v1.2.1 From b7ed102ea601fb4c6f65c5a982058f8c92883d31 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 21 Nov 2017 12:37:07 +0100 Subject: Allow filtering by 'status' --- spec/requests/api/runners_spec.rb | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index d68fa13af1e..1ffbb06803f 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -370,8 +370,8 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers - expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_2.id) + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) end end @@ -382,8 +382,29 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) + end + end + + context 'when valid status is provided' do + it 'return filtered jobs' do + get api("/runners/#{specific_runner.id}/jobs?status=failed", admin) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response).to be_an(Array) expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_4.id) + expect(json_response.first).to include('id' => job_5.id) + end + end + + context 'when invalid status is provided' do + it 'return 404' do + get api("/runners/#{specific_runner.id}/jobs?status=non-existing", admin) + + expect(response).to have_gitlab_http_status(404) end end end @@ -414,8 +435,29 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) + end + end + + context 'when valid status is provided' do + it 'return filtered jobs' do + get api("/runners/#{specific_runner.id}/jobs?status=failed", user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response).to be_an(Array) expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_4.id) + expect(json_response.first).to include('id' => job_5.id) + end + end + + context 'when invalid status is provided' do + it 'return 404' do + get api("/runners/#{specific_runner.id}/jobs?status=non-existing", user) + + expect(response).to have_gitlab_http_status(404) end end end -- cgit v1.2.1 From c0e7261068f0ead50f125b43157c05d93af73379 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 21 Nov 2017 12:49:25 +0100 Subject: Use 'set' instead of 'let!' when it's possible --- spec/requests/api/runners_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index 1ffbb06803f..80a8495808d 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -355,7 +355,7 @@ describe API::Runners do end describe 'GET /runners/:id/jobs' do - let!(:job_1) { create(:ci_build) } + set(:job_1) { create(:ci_build) } let!(:job_2) { create(:ci_build, :running, runner: shared_runner, project: project) } let!(:job_3) { create(:ci_build, :failed, runner: shared_runner, project: project) } let!(:job_4) { create(:ci_build, :running, runner: specific_runner, project: project) } -- cgit v1.2.1 From 13a902a9a42c0909ad8c6790c040447a9e12211f Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 27 Nov 2017 22:59:01 +0100 Subject: Refactorize jobs finding logic --- spec/finders/runner_jobs_finder_spec.rb | 39 +++++++++++++++++++++++++++++++++ spec/requests/api/runners_spec.rb | 8 +++---- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 spec/finders/runner_jobs_finder_spec.rb (limited to 'spec') diff --git a/spec/finders/runner_jobs_finder_spec.rb b/spec/finders/runner_jobs_finder_spec.rb new file mode 100644 index 00000000000..4275b1a7ff1 --- /dev/null +++ b/spec/finders/runner_jobs_finder_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe RunnerJobsFinder do + let(:project) { create(:project) } + let(:runner) { create(:ci_runner, :shared) } + + subject { described_class.new(runner, params).execute } + + describe '#execute' do + context 'when params is empty' do + let(:params) { {} } + let!(:job) { create(:ci_build, runner: runner, project: project) } + let!(:job1) { create(:ci_build, project: project) } + + it 'returns all jobs assigned to Runner' do + is_expected.to match_array(job) + is_expected.not_to match_array(job1) + end + end + + context 'when params contains status' do + HasStatus::AVAILABLE_STATUSES.each do |target_status| + context "when status is #{target_status}" do + let(:params) { { status: target_status } } + let!(:job) { create(:ci_build, runner: runner, project: project, status: target_status) } + + before do + exception_status = HasStatus::AVAILABLE_STATUSES - [target_status] + create(:ci_build, runner: runner, project: project, status: exception_status.first) + end + + it 'returns matched job' do + is_expected.to eq([job]) + end + end + end + end + end +end diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index 80a8495808d..ec5cad4f4fd 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -401,10 +401,10 @@ describe API::Runners do end context 'when invalid status is provided' do - it 'return 404' do + it 'return 400' do get api("/runners/#{specific_runner.id}/jobs?status=non-existing", admin) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(400) end end end @@ -454,10 +454,10 @@ describe API::Runners do end context 'when invalid status is provided' do - it 'return 404' do + it 'return 400' do get api("/runners/#{specific_runner.id}/jobs?status=non-existing", user) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(400) end end end -- cgit v1.2.1