2020-04-30 15:09:46 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.shared_context 'Pipeline Processing Service Tests With Yaml' do
|
2020-04-30 15:09:46 +00:00
|
|
|
where(:test_file_path) do
|
|
|
|
Dir.glob(Rails.root.join('spec/services/ci/pipeline_processing/test_cases/*.yml'))
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:test_file) { YAML.load_file(test_file_path) }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:pipeline) { Ci::CreatePipelineService.new(project, user, ref: 'master').execute(:pipeline) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(YAML.dump(test_file['config']))
|
|
|
|
stub_not_protect_default_branch
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2020-05-08 12:09:37 +00:00
|
|
|
it 'follows transitions' do
|
2020-04-30 15:09:46 +00:00
|
|
|
expect(pipeline).to be_persisted
|
2020-05-08 12:09:37 +00:00
|
|
|
Sidekiq::Worker.drain_all # ensure that all async jobs are executed
|
|
|
|
check_expectation(test_file.dig('init', 'expect'), "init")
|
2020-04-30 15:09:46 +00:00
|
|
|
|
2020-05-08 12:09:37 +00:00
|
|
|
test_file['transitions'].each_with_index do |transition, idx|
|
2020-04-30 15:09:46 +00:00
|
|
|
event_on_jobs(transition['event'], transition['jobs'])
|
2020-05-08 12:09:37 +00:00
|
|
|
Sidekiq::Worker.drain_all # ensure that all async jobs are executed
|
|
|
|
check_expectation(transition['expect'], "transition:#{idx}")
|
2020-04-30 15:09:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-08 12:09:37 +00:00
|
|
|
def check_expectation(expectation, message)
|
|
|
|
expect(current_state.deep_stringify_keys).to eq(expectation), message
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_state
|
|
|
|
# reload pipeline and all relations
|
|
|
|
pipeline.reload
|
|
|
|
|
|
|
|
{
|
|
|
|
pipeline: pipeline.status,
|
|
|
|
stages: pipeline.ordered_stages.pluck(:name, :status).to_h,
|
|
|
|
jobs: pipeline.statuses.latest.pluck(:name, :status).to_h
|
|
|
|
}
|
2020-04-30 15:09:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def event_on_jobs(event, job_names)
|
2020-05-08 12:09:37 +00:00
|
|
|
statuses = pipeline.statuses.latest.by_name(job_names).to_a
|
|
|
|
expect(statuses.count).to eq(job_names.count) # ensure that we have the same counts
|
2020-04-30 15:09:46 +00:00
|
|
|
|
2020-05-08 12:09:37 +00:00
|
|
|
statuses.each { |status| status.public_send("#{event}!") }
|
2020-04-30 15:09:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|