Commit 32b09b88 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add minor refactoring

parent 0905fe4d
...@@ -509,10 +509,8 @@ module Ci ...@@ -509,10 +509,8 @@ module Ci
end end
def steps def steps
[ [Gitlab::Ci::Build::Step.from_commands(self),
Gitlab::Ci::Build::Step.from_commands(self), Gitlab::Ci::Build::Step.from_after_script(self)].compact
Gitlab::Ci::Build::Step.from_after_script(self)
].compact
end end
def image def image
......
...@@ -115,7 +115,7 @@ module API ...@@ -115,7 +115,7 @@ module API
end end
end end
desc 'Appends a patch to the job.trace' do desc 'Appends a patch to the job trace' do
http_codes [[202, 'Trace was patched'], http_codes [[202, 'Trace was patched'],
[400, 'Missing Content-Range header'], [400, 'Missing Content-Range header'],
[403, 'Forbidden'], [403, 'Forbidden'],
......
...@@ -21,8 +21,7 @@ module Gitlab ...@@ -21,8 +21,7 @@ module Gitlab
end end
def initialize(image) def initialize(image)
type = image.class @name = image
@name = image if type == String
end end
def valid? def valid?
......
...@@ -6,7 +6,9 @@ module Gitlab ...@@ -6,7 +6,9 @@ module Gitlab
WHEN_ON_SUCCESS = 'on_success'.freeze WHEN_ON_SUCCESS = 'on_success'.freeze
WHEN_ALWAYS = 'always'.freeze WHEN_ALWAYS = 'always'.freeze
attr_reader :name, :script, :timeout, :when, :allow_failure attr_reader :name
attr_writer :script
attr_accessor :timeout, :when, :allow_failure
class << self class << self
def from_commands(job) def from_commands(job)
...@@ -25,7 +27,7 @@ module Gitlab ...@@ -25,7 +27,7 @@ module Gitlab
step.script = after_script step.script = after_script
step.timeout = job.timeout step.timeout = job.timeout
step.when = WHEN_ALWAYS step.when = WHEN_ALWAYS
step.allow_failure_on step.allow_failure = true
end end
end end
end end
...@@ -35,20 +37,8 @@ module Gitlab ...@@ -35,20 +37,8 @@ module Gitlab
@allow_failure = false @allow_failure = false
end end
def script=(script) def script
@script = script.split("\n") @script.split("\n")
end
def timeout=(timeout)
@timeout = timeout
end
def when=(when_condition)
@when = when_condition
end
def allow_failure_on
@allow_failure = true
end end
end end
end end
......
...@@ -10,18 +10,27 @@ describe Gitlab::Ci::Build::Image do ...@@ -10,18 +10,27 @@ describe Gitlab::Ci::Build::Image do
let(:image_name) { 'ruby:2.1' } let(:image_name) { 'ruby:2.1' }
let(:job) { create(:ci_build, options: { image: image_name } ) } let(:job) { create(:ci_build, options: { image: image_name } ) }
it { is_expected.to be_kind_of(described_class) } it 'fabricates an object of the proper class' do
it { expect(subject.name).to eq(image_name) } is_expected.to be_kind_of(described_class)
end
it 'populates fabricated object with the proper name attribute' do
expect(subject.name).to eq(image_name)
end
context 'when image name is empty' do context 'when image name is empty' do
let(:image_name) { '' } let(:image_name) { '' }
it { is_expected.to eq(nil) } it 'does not fabricate an object' do
is_expected.to be_nil
end
end end
end end
context 'when image is not defined in job' do context 'when image is not defined in job' do
it { is_expected.to eq(nil) } it 'does not fabricate an object' do
is_expected.to be_nil
end
end end
end end
...@@ -32,21 +41,27 @@ describe Gitlab::Ci::Build::Image do ...@@ -32,21 +41,27 @@ describe Gitlab::Ci::Build::Image do
let(:service_image_name) { 'postgres' } let(:service_image_name) { 'postgres' }
let(:job) { create(:ci_build, options: { services: [service_image_name] }) } let(:job) { create(:ci_build, options: { services: [service_image_name] }) }
it { is_expected.to be_kind_of(Array) } it 'fabricates an non-empty array of objects' do
it { is_expected.not_to be_empty } is_expected.to be_kind_of(Array)
it { expect(subject[0].name).to eq(service_image_name) } is_expected.not_to be_empty
expect(subject.first.name).to eq(service_image_name)
end
context 'when service image name is empty' do context 'when service image name is empty' do
let(:service_image_name) { '' } let(:service_image_name) { '' }
it { is_expected.to be_kind_of(Array) } it 'fabricates an empty array' do
it { is_expected.to be_empty } is_expected.to be_kind_of(Array)
is_expected.to be_empty
end
end end
end end
context 'when services are not defined in job' do context 'when services are not defined in job' do
it { is_expected.to be_kind_of(Array) } it 'fabricates an empty array' do
it { is_expected.to be_empty } is_expected.to be_kind_of(Array)
is_expected.to be_empty
end
end end
end end
end end
...@@ -6,28 +6,34 @@ describe Gitlab::Ci::Build::Step do ...@@ -6,28 +6,34 @@ describe Gitlab::Ci::Build::Step do
describe '#from_commands' do describe '#from_commands' do
subject { described_class.from_commands(job) } subject { described_class.from_commands(job) }
it { expect(subject.name).to eq(:script) } it 'fabricates an object' do
it { expect(subject.script).to eq(['ls -la', 'date']) } expect(subject.name).to eq(:script)
it { expect(subject.timeout).to eq(job.timeout) } expect(subject.script).to eq(['ls -la', 'date'])
it { expect(subject.when).to eq('on_success') } expect(subject.timeout).to eq(job.timeout)
it { expect(subject.allow_failure).to be_falsey } expect(subject.when).to eq('on_success')
expect(subject.allow_failure).to be_falsey
end
end end
describe '#from_after_script' do describe '#from_after_script' do
subject { described_class.from_after_script(job) } subject { described_class.from_after_script(job) }
context 'when after_script is empty' do context 'when after_script is empty' do
it { is_expected.to be(nil) } it 'doesn not fabricate an object' do
is_expected.to be_nil
end
end end
context 'when after_script is not empty' do context 'when after_script is not empty' do
let(:job) { create(:ci_build, options: { after_script: "ls -la\ndate" }) } let(:job) { create(:ci_build, options: { after_script: "ls -la\ndate" }) }
it { expect(subject.name).to eq(:after_script) } it 'fabricates an object' do
it { expect(subject.script).to eq(['ls -la', 'date']) } expect(subject.name).to eq(:after_script)
it { expect(subject.timeout).to eq(job.timeout) } expect(subject.script).to eq(['ls -la', 'date'])
it { expect(subject.when).to eq('always') } expect(subject.timeout).to eq(job.timeout)
it { expect(subject.allow_failure).to be_truthy } expect(subject.when).to eq('always')
expect(subject.allow_failure).to be_truthy
end
end end
end end
end end
...@@ -155,7 +155,10 @@ describe API::Runner do ...@@ -155,7 +155,10 @@ describe API::Runner do
let(:project) { create(:empty_project, shared_runners_enabled: false) } let(:project) { create(:empty_project, shared_runners_enabled: false) }
let(:pipeline) { create(:ci_pipeline_without_jobs, project: project, ref: 'master') } let(:pipeline) { create(:ci_pipeline_without_jobs, project: project, ref: 'master') }
let(:runner) { create(:ci_runner) } let(:runner) { create(:ci_runner) }
let!(:job) { create(:ci_build, :artifacts, :extended_options, pipeline: pipeline, name: 'spinach', stage: 'test', stage_idx: 0, commands: "ls\ndate") } let!(:job) do
create(:ci_build, :artifacts, :extended_options,
pipeline: pipeline, name: 'spinach', stage: 'test', stage_idx: 0, commands: "ls\ndate")
end
before { project.runners << runner } before { project.runners << runner }
...@@ -283,6 +286,7 @@ describe API::Runner do ...@@ -283,6 +286,7 @@ describe API::Runner do
'project_id' => job.project.id, 'project_id' => job.project.id,
'project_name' => job.project.name } 'project_name' => job.project.name }
end end
let(:expected_git_info) do let(:expected_git_info) do
{ 'repo_url' => job.repo_url, { 'repo_url' => job.repo_url,
'ref' => job.ref, 'ref' => job.ref,
...@@ -290,6 +294,7 @@ describe API::Runner do ...@@ -290,6 +294,7 @@ describe API::Runner do
'before_sha' => job.before_sha, 'before_sha' => job.before_sha,
'ref_type' => 'branch' } 'ref_type' => 'branch' }
end end
let(:expected_steps) do let(:expected_steps) do
[{ 'name' => 'script', [{ 'name' => 'script',
'script' => %w(ls date), 'script' => %w(ls date),
...@@ -302,11 +307,13 @@ describe API::Runner do ...@@ -302,11 +307,13 @@ describe API::Runner do
'when' => 'always', 'when' => 'always',
'allow_failure' => true }] 'allow_failure' => true }]
end end
let(:expected_variables) do let(:expected_variables) do
[{ 'key' => 'CI_BUILD_NAME', 'value' => 'spinach', 'public' => true }, [{ 'key' => 'CI_BUILD_NAME', 'value' => 'spinach', 'public' => true },
{ 'key' => 'CI_BUILD_STAGE', 'value' => 'test', 'public' => true }, { 'key' => 'CI_BUILD_STAGE', 'value' => 'test', 'public' => true },
{ 'key' => 'DB_NAME', 'value' => 'postgres', 'public' => true }] { 'key' => 'DB_NAME', 'value' => 'postgres', 'public' => true }]
end end
let(:expected_artifacts) do let(:expected_artifacts) do
[{ 'name' => 'artifacts_file', [{ 'name' => 'artifacts_file',
'untracked' => false, 'untracked' => false,
...@@ -314,13 +321,14 @@ describe API::Runner do ...@@ -314,13 +321,14 @@ describe API::Runner do
'when' => 'always', 'when' => 'always',
'expire_in' => '7d' }] 'expire_in' => '7d' }]
end end
let(:expected_cache) do let(:expected_cache) do
[{ 'key' => 'cache_key', [{ 'key' => 'cache_key',
'untracked' => false, 'untracked' => false,
'paths' => ['vendor/*'] }] 'paths' => ['vendor/*'] }]
end end
it 'starts a job' do it 'picks a job' do
request_job info: { platform: :darwin } request_job info: { platform: :darwin }
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
......
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