Commit 953a1094 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Added Ci::Stage specs

parent 401c155e
...@@ -104,6 +104,10 @@ module Ci ...@@ -104,6 +104,10 @@ module Ci
statuses.select(:stage).distinct.count statuses.select(:stage).distinct.count
end end
def stages_name
statuses.order(:stage_idx).distinct.pluck(:stage)
end
def stages def stages
status_sql = statuses.latest.where('stage=sg.stage').status_sql status_sql = statuses.latest.where('stage=sg.stage').status_sql
......
...@@ -22,7 +22,7 @@ module Gitlab ...@@ -22,7 +22,7 @@ module Gitlab
sha: pipeline.sha, sha: pipeline.sha,
before_sha: pipeline.before_sha, before_sha: pipeline.before_sha,
status: pipeline.status, status: pipeline.status,
stages: pipeline.stages.map(&:name), stages: pipeline.stages_name,
created_at: pipeline.created_at, created_at: pipeline.created_at,
finished_at: pipeline.finished_at, finished_at: pipeline.finished_at,
duration: pipeline.duration duration: pipeline.duration
......
FactoryGirl.define do
factory :ci_stage, class: Ci::Stage do
transient do
name 'test'
status nil
pipeline factory: :ci_empty_pipeline
end
initialize_with do
Ci::Stage.new(pipeline, name: name, status: status)
end
end
end
...@@ -21,7 +21,7 @@ describe Gitlab::Ci::Status::Pipeline::Factory do ...@@ -21,7 +21,7 @@ describe Gitlab::Ci::Status::Pipeline::Factory do
Gitlab::Ci::Status.const_get(core_status.capitalize)) Gitlab::Ci::Status.const_get(core_status.capitalize))
end end
it 'extends core status with common pipeline methods' do it 'extends core status with common stage methods' do
expect(status).to have_details expect(status).to have_details
expect(status).not_to have_action expect(status).not_to have_action
expect(status.details_path) expect(status.details_path)
...@@ -45,7 +45,7 @@ describe Gitlab::Ci::Status::Pipeline::Factory do ...@@ -45,7 +45,7 @@ describe Gitlab::Ci::Status::Pipeline::Factory do
.to be_a Gitlab::Ci::Status::Pipeline::SuccessWithWarnings .to be_a Gitlab::Ci::Status::Pipeline::SuccessWithWarnings
end end
it 'extends core status with common pipeline methods' do it 'extends core status with common stage methods' do
expect(status).to have_details expect(status).to have_details
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Status::Stage::Common do describe Gitlab::Ci::Status::Stage::Common do
let(:pipeline) { create(:ci_pipeline) } let(:pipeline) { create(:ci_empty_pipeline) }
let(:stage) { Ci::Stage.new(pipeline, name: 'test') } let(:stage) { build(:ci_stage, pipeline: pipeline, name: 'test') }
subject do subject do
Class.new(Gitlab::Ci::Status::Core) Class.new(Gitlab::Ci::Status::Core)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Status::Stage::Factory do describe Gitlab::Ci::Status::Stage::Factory do
let(:pipeline) { create(:ci_pipeline) } let(:pipeline) { create(:ci_empty_pipeline) }
let(:stage) { Ci::Stage.new(pipeline, name: 'test') } let(:stage) { build(:ci_stage, pipeline: pipeline, name: 'test') }
subject do subject do
described_class.new(stage) described_class.new(stage)
...@@ -15,8 +15,10 @@ describe Gitlab::Ci::Status::Stage::Factory do ...@@ -15,8 +15,10 @@ describe Gitlab::Ci::Status::Stage::Factory do
context 'when stage has a core status' do context 'when stage has a core status' do
HasStatus::AVAILABLE_STATUSES.each do |core_status| HasStatus::AVAILABLE_STATUSES.each do |core_status|
context "when core status is #{core_status}" do context "when core status is #{core_status}" do
let!(:build) do before do
create(:ci_build, pipeline: pipeline, stage: 'test', status: core_status) create(:ci_build, pipeline: pipeline, stage: 'test', status: core_status)
create(:commit_status, pipeline: pipeline, stage: 'test', status: core_status)
create(:ci_build, pipeline: pipeline, stage: 'build', status: :failed)
end end
it "fabricates a core status #{core_status}" do it "fabricates a core status #{core_status}" do
...@@ -24,7 +26,7 @@ describe Gitlab::Ci::Status::Stage::Factory do ...@@ -24,7 +26,7 @@ describe Gitlab::Ci::Status::Stage::Factory do
Gitlab::Ci::Status.const_get(core_status.capitalize)) Gitlab::Ci::Status.const_get(core_status.capitalize))
end end
it 'extends core status with common pipeline methods' do it 'extends core status with common stage methods' do
expect(status).to have_details expect(status).to have_details
expect(status.details_path).to include "pipelines/#{pipeline.id}" expect(status.details_path).to include "pipelines/#{pipeline.id}"
expect(status.details_path).to include "##{stage.name}" expect(status.details_path).to include "##{stage.name}"
......
...@@ -142,6 +142,10 @@ describe Ci::Pipeline, models: true do ...@@ -142,6 +142,10 @@ describe Ci::Pipeline, models: true do
expect(pipeline.stages_count).to eq(3) expect(pipeline.stages_count).to eq(3)
end end
it 'returns a valid names of stages' do
expect(pipeline.stages_name).to eq(['build', 'test', 'deploy'])
end
context 'stages with statuses' do context 'stages with statuses' do
let(:statuses) do let(:statuses) do
subject.map do |stage| subject.map do |stage|
......
require 'spec_helper'
describe Ci::Stage, models: true do
let(:stage) { build(:ci_stage) }
let(:pipeline) { stage.pipeline }
let(:stage_name) { stage.name }
describe '#expectations' do
subject { stage }
it { is_expected.to include_module(StaticModel) }
it { is_expected.to respond_to(:pipeline) }
it { is_expected.to respond_to(:name) }
it { is_expected.to delegate_method(:project).to(:pipeline) }
end
describe '#statuses' do
let!(:stage_build) { create_job(:ci_build) }
let!(:commit_status) { create_job(:commit_status) }
let!(:other_build) { create_job(:ci_build, stage: 'other stage') }
subject { stage.statuses }
it "returns only matching statuses" do
is_expected.to contain_exactly(stage_build, commit_status)
end
end
describe '#builds' do
let!(:stage_build) { create_job(:ci_build) }
let!(:commit_status) { create_job(:commit_status) }
subject { stage.builds }
it "returns only builds" do
is_expected.to contain_exactly(stage_build)
end
end
describe '#status' do
subject { stage.status }
context 'if status is already defined' do
let(:stage) { build(:ci_stage, status: 'success') }
it "returns defined status" do
is_expected.to eq('success')
end
end
context 'if status has to be calculated' do
let!(:stage_build) { create_job(:ci_build, status: :failed) }
it "returns status of a build" do
is_expected.to eq('failed')
end
context 'and builds are retried' do
let!(:new_build) { create_job(:ci_build, status: :success) }
it "returns status of latest build" do
is_expected.to eq('success')
end
end
end
end
describe '#detailed_status' do
subject { stage.detailed_status }
context 'when build is created' do
let!(:stage_build) { create_job(:ci_build, status: :created) }
it 'returns detailed status for created stage' do
expect(subject.text).to eq 'created'
end
end
context 'when build is pending' do
let!(:stage_build) { create_job(:ci_build, status: :pending) }
it 'returns detailed status for pending stage' do
expect(subject.text).to eq 'pending'
end
end
context 'when build is running' do
let!(:stage_build) { create_job(:ci_build, status: :running) }
it 'returns detailed status for running stage' do
expect(subject.text).to eq 'running'
end
end
context 'when build is successful' do
let!(:stage_build) { create_job(:ci_build, status: :success) }
it 'returns detailed status for successful stage' do
expect(subject.text).to eq 'passed'
end
end
context 'when build is failed' do
let!(:stage_build) { create_job(:ci_build, status: :failed) }
it 'returns detailed status for failed stage' do
expect(subject.text).to eq 'failed'
end
end
context 'when build is canceled' do
let!(:stage_build) { create_job(:ci_build, status: :canceled) }
it 'returns detailed status for canceled stage' do
expect(subject.text).to eq 'canceled'
end
end
context 'when build is skipped' do
let!(:stage_build) { create_job(:ci_build, status: :skipped) }
it 'returns detailed status for skipped stage' do
expect(subject.text).to eq 'skipped'
end
end
end
def create_job(type, status: 'success', stage: stage_name)
create(type, pipeline: pipeline, stage: stage, status: status)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment