From 2b0b53cddd7d57ca5dd93437fdffefd7a07af91e Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 20 Dec 2016 11:00:56 +0100 Subject: [PATCH] Add tests for stage API endpoint --- .../projects/pipelines_controller.rb | 4 +-- app/models/ci/pipeline.rb | 5 ++++ app/models/ci/stage.rb | 4 +++ .../projects/pipelines/pipelines_spec.rb | 29 +++++++++++++++++++ spec/models/ci/pipeline_spec.rb | 20 +++++++++++++ spec/models/ci/stage_spec.rb | 11 +++++++ 6 files changed, 70 insertions(+), 3 deletions(-) diff --git a/app/controllers/projects/pipelines_controller.rb b/app/controllers/projects/pipelines_controller.rb index 0147072b0f1..cc347922c6a 100644 --- a/app/controllers/projects/pipelines_controller.rb +++ b/app/controllers/projects/pipelines_controller.rb @@ -42,9 +42,7 @@ class Projects::PipelinesController < Projects::ApplicationController end def stage - @stage = pipeline.stages.find do |stage| - stage.name == params[:stage] - end + @stage = pipeline.stage(params[:stage]) return not_found unless @stage respond_to do |format| diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 48354cdbefb..f2f6453b3b9 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -116,6 +116,11 @@ module Ci where.not(duration: nil).sum(:duration) end + def stage(name) + stage = Ci::Stage.new(self, name: name) + stage unless stage.statuses_count.zero? + end + def stages_count statuses.select(:stage).distinct.count end diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb index 7ef59445d77..d035eda6df5 100644 --- a/app/models/ci/stage.rb +++ b/app/models/ci/stage.rb @@ -18,6 +18,10 @@ module Ci name end + def statuses_count + @statuses_count ||= statuses.count + end + def status @status ||= statuses.latest.status end diff --git a/spec/features/projects/pipelines/pipelines_spec.rb b/spec/features/projects/pipelines/pipelines_spec.rb index f3731698a18..e1c6b4c115c 100644 --- a/spec/features/projects/pipelines/pipelines_spec.rb +++ b/spec/features/projects/pipelines/pipelines_spec.rb @@ -152,6 +152,35 @@ describe "Pipelines" do end end + describe 'GET /:project/pipelines/stage?name=stage' do + let!(:pipeline) do + create(:ci_empty_pipeline, project: project, ref: 'master', + status: 'running') + end + + context 'when accessing existing stage' do + let!(:build) do + create(:ci_build, pipeline: pipeline, stage: 'build') + end + + before do + visit stage_namespace_project_pipeline_path( + project.namespace, project, pipeline, format: :json, stage: 'build') + end + + it { expect(page).to have_http_status(:ok) } + end + + context 'when accessing unknown stage' do + before do + visit stage_namespace_project_pipeline_path( + project.namespace, project, pipeline, format: :json, stage: 'test') + end + + it { expect(page).to have_http_status(:not_found) } + end + end + describe 'POST /:project/pipelines' do let(:project) { create(:project) } diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 52dd41065e9..67cc3e6be68 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -175,6 +175,26 @@ describe Ci::Pipeline, models: true do end end + describe '#stage' do + subject { pipeline.stage('test') } + + context 'with status in stage' do + let!(:status) { create(:commit_status, pipeline: pipeline, stage: 'test') } + + it 'return stage object' do + is_expected.to be_a(Ci::Stage) + end + end + + context 'without status in stage' do + let!(:status) { create(:commit_status, pipeline: pipeline, stage: 'build') } + + it 'return stage object' do + is_expected.to be_nil + end + end + end + describe 'state machine' do let(:current) { Time.now.change(usec: 0) } let(:build) { create_build('build1', 0) } diff --git a/spec/models/ci/stage_spec.rb b/spec/models/ci/stage_spec.rb index 8fff38f7cda..d8dce0f1cc6 100644 --- a/spec/models/ci/stage_spec.rb +++ b/spec/models/ci/stage_spec.rb @@ -28,6 +28,17 @@ describe Ci::Stage, models: true do end end + describe '#statuses_count' do + let!(:stage_build) { create_job(:ci_build) } + let!(:other_build) { create_job(:ci_build, stage: 'other stage') } + + subject { stage.statuses_count } + + it "statuses only from current stage" do + is_expected.to eq(1) + end + end + describe '#builds' do let!(:stage_build) { create_job(:ci_build) } let!(:commit_status) { create_job(:commit_status) }