1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
# frozen_string_literal: true
# rubocop:disable Layout/LineLength
# rubocop:disable RSpec/ContextWording
RSpec.shared_examples 'a deployable job' do
it { is_expected.to have_one(:deployment) }
shared_examples 'calling proper BuildFinishedWorker' do
it 'calls Ci::BuildFinishedWorker' do
skip unless described_class == ::Ci::Build
expect(Ci::BuildFinishedWorker).to receive(:perform_async)
subject
end
end
describe '#has_outdated_deployment?' do
subject { job.has_outdated_deployment? }
let(:job) { create(factory_type, :created, :with_deployment, project: project, pipeline: pipeline, environment: 'production') }
context 'when job has no environment' do
let(:job) { create(factory_type, :created, pipeline: pipeline, environment: nil) }
it { expect(subject).to be_falsey }
end
context 'when deployment is not persisted' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:commits) { project.repository.commits('master', limit: 2) }
let_it_be(:environment) { create(:environment, project: project) }
let(:unpersisted_deployment) do
FactoryBot.build(
:deployment,
:success,
project: project,
environment: environment,
finished_at: 1.year.ago,
sha: nil # Required attribute, nil prevents the record from being persisted and getting an ID
)
end
let!(:last_deployment) do
create(:deployment, :success, project: project, environment: environment, sha: commits[1].sha)
end
it 'returns false to ignore the Build and not take any Deployment-related action' do
expect(unpersisted_deployment.job.has_outdated_deployment?).to eq(false)
end
end
context 'when project has forward deployment disabled' do
before do
project.ci_cd_settings.update!(forward_deployment_enabled: false)
end
it { expect(subject).to be_falsey }
end
context 'when job is not an outdated deployment' do
before do
allow(job.deployment).to receive(:older_than_last_successful_deployment?).and_return(false)
end
it { expect(subject).to be_falsey }
end
context 'when job is older than the latest deployment and still pending status' do
before do
allow(job.deployment).to receive(:older_than_last_successful_deployment?).and_return(true)
end
it { expect(subject).to be_truthy} # rubocop: disable Layout/SpaceInsideBlockBraces
end
context 'when job is older than the latest deployment but succeeded once' do
let(:job) { create(factory_type, :success, :with_deployment, project: project, pipeline: pipeline, environment: 'production') }
before do
allow(job.deployment).to receive(:older_than_last_successful_deployment?).and_return(true)
end
it 'returns false for allowing rollback' do
expect(subject).to be_falsey
end
context 'when forward_deployment_rollback_allowed option is disabled' do
before do
project.ci_cd_settings.update!(forward_deployment_rollback_allowed: false)
end
it 'returns true for disallowing rollback' do
expect(subject).to eq(true)
end
end
end
end
describe 'state transition as a deployable' do
subject { job.send(event) }
let!(:job) { create(factory_type, :with_deployment, :start_review_app, status: :pending, pipeline: pipeline) }
let(:deployment) { job.deployment }
let(:environment) { deployment.environment }
before do
allow(Deployments::LinkMergeRequestWorker).to receive(:perform_async)
allow(Deployments::HooksWorker).to receive(:perform_async)
end
it 'has deployments record with created status' do
expect(deployment).to be_created
expect(environment.name).to eq('review/master')
end
shared_examples_for 'avoid deadlock' do
it 'executes UPDATE in the right order' do
recorded = with_cross_database_modification_prevented do
ActiveRecord::QueryRecorder.new { subject }
end
index_for_build = recorded.log.index { |l| l.include?("UPDATE #{described_class.quoted_table_name}") }
index_for_deployment = recorded.log.index { |l| l.include?("UPDATE \"deployments\"") }
expect(index_for_build).to be < index_for_deployment
end
end
context 'when transits to running' do
let(:event) { :run! }
it_behaves_like 'avoid deadlock'
it 'transits deployment status to running' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_running
end
context 'when deployment is already running state' do
before do
job.deployment.success!
end
it 'does not change deployment status and tracks an error' do
expect(Gitlab::ErrorTracking)
.to receive(:track_exception).with(
instance_of(Deployment::StatusSyncError), deployment_id: deployment.id, job_id: job.id)
with_cross_database_modification_prevented do
expect { subject }.not_to change { deployment.reload.status }
end
end
end
end
context 'when transits to success' do
let(:event) { :success! }
before do
allow(Deployments::UpdateEnvironmentWorker).to receive(:perform_async)
allow(Deployments::HooksWorker).to receive(:perform_async)
end
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to success' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_success
end
end
context 'when transits to failed' do
let(:event) { :drop! }
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to failed' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_failed
end
end
context 'when transits to skipped' do
let(:event) { :skip! }
it_behaves_like 'avoid deadlock'
it 'transits deployment status to skipped' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_skipped
end
end
context 'when transits to canceled' do
let(:event) { :cancel! }
it_behaves_like 'avoid deadlock'
it_behaves_like 'calling proper BuildFinishedWorker'
it 'transits deployment status to canceled' do
with_cross_database_modification_prevented do
subject
end
expect(deployment).to be_canceled
end
end
# Mimic playing a manual job that needs another job.
# `needs + when:manual` scenario, see: https://gitlab.com/gitlab-org/gitlab/-/issues/347502
context 'when transits from skipped to created to running' do
before do
job.skip!
end
context 'during skipped to created' do
let(:event) { :process! }
it 'transitions to created' do
subject
expect(deployment).to be_created
end
end
context 'during created to running' do
let(:event) { :run! }
before do
job.process!
job.enqueue!
end
it 'transitions to running and calls webhook' do
freeze_time do
expect(Deployments::HooksWorker)
.to receive(:perform_async).with(hash_including({ 'deployment_id' => deployment.id, 'status' => 'running', 'status_changed_at' => Time.current.to_s }))
subject
end
expect(deployment).to be_running
end
end
end
end
describe '#on_stop' do
subject { job.on_stop }
context 'when a job has a specification that it can be stopped from the other job' do
let(:job) { create(factory_type, :start_review_app, pipeline: pipeline) }
it 'returns the other job name' do
is_expected.to eq('stop_review_app')
end
end
context 'when a job does not have environment information' do
let(:job) { create(factory_type, pipeline: pipeline) }
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#environment_tier_from_options' do
subject { job.environment_tier_from_options }
let(:job) { described_class.new(options: options) }
let(:options) { { environment: { deployment_tier: 'production' } } }
it { is_expected.to eq('production') }
context 'when options does not include deployment_tier' do
let(:options) { { environment: { name: 'production' } } }
it { is_expected.to be_nil }
end
end
describe '#environment_tier' do
subject { job.environment_tier }
let(:options) { { environment: { deployment_tier: 'production' } } }
let!(:environment) { create(:environment, name: 'production', tier: 'development', project: project) }
let(:job) { described_class.new(options: options, environment: 'production', project: project) }
it { is_expected.to eq('production') }
context 'when options does not include deployment_tier' do
let(:options) { { environment: { name: 'production' } } }
it 'uses tier from environment' do
is_expected.to eq('development')
end
context 'when persisted environment is absent' do
let(:environment) { nil }
it { is_expected.to be_nil }
end
end
end
describe '#environment_url' do
subject { job.environment_url }
let!(:job) { create(factory_type, :with_deployment, :deploy_to_production, pipeline: pipeline) }
it { is_expected.to eq('http://prd.example.com/$CI_JOB_NAME') }
context 'when options does not include url' do
before do
job.update!(options: { environment: { url: nil } })
job.persisted_environment.update!(external_url: 'http://prd.example.com/$CI_JOB_NAME')
end
it 'fetches from the persisted environment' do
expect_any_instance_of(::Environment) do |environment|
expect(environment).to receive(:external_url).once
end
is_expected.to eq('http://prd.example.com/$CI_JOB_NAME')
end
context 'when persisted environment is absent' do
before do
job.clear_memoization(:persisted_environment)
job.persisted_environment = nil
end
it { is_expected.to be_nil }
end
end
end
describe '#environment_slug' do
subject { job.environment_slug }
let!(:job) { create(factory_type, :with_deployment, :start_review_app, pipeline: pipeline) }
it { is_expected.to eq('review-master-8dyme2') }
context 'when persisted environment is absent' do
let!(:job) { create(factory_type, :start_review_app, pipeline: pipeline) }
it { is_expected.to be_nil }
end
end
describe 'environment' do
describe '#has_environment_keyword?' do
subject { job.has_environment_keyword? }
context 'when environment is defined' do
before do
job.update!(environment: 'review')
end
it { is_expected.to be_truthy }
end
context 'when environment is not defined' do
before do
job.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#expanded_environment_name' do
subject { job.expanded_environment_name }
context 'when environment uses $CI_COMMIT_REF_NAME' do
let(:job) do
create(
factory_type,
ref: 'master',
environment: 'review/$CI_COMMIT_REF_NAME',
pipeline: pipeline
)
end
it { is_expected.to eq('review/master') }
end
context 'when environment uses yaml_variables containing symbol keys' do
let(:job) do
create(
factory_type,
yaml_variables: [{ key: :APP_HOST, value: 'host' }],
environment: 'review/$APP_HOST',
pipeline: pipeline
)
end
it 'returns an expanded environment name with a list of variables' do
is_expected.to eq('review/host')
end
context 'when job metadata has already persisted the expanded environment name' do
before do
job.metadata.expanded_environment_name = 'review/foo'
end
it 'returns a persisted expanded environment name without a list of variables' do
expect(job).not_to receive(:simple_variables)
is_expected.to eq('review/foo')
end
end
end
context 'when using persisted variables' do
let(:job) do
create(factory_type, environment: 'review/x$CI_JOB_ID', pipeline: pipeline)
end
it { is_expected.to eq('review/x') }
end
context 'when environment name uses a nested variable' do
let(:yaml_variables) do
[
{ key: 'ENVIRONMENT_NAME', value: '${CI_COMMIT_REF_NAME}' }
]
end
let(:job) do
create(
factory_type,
ref: 'master',
yaml_variables: yaml_variables,
environment: 'review/$ENVIRONMENT_NAME',
pipeline: pipeline
)
end
it { is_expected.to eq('review/master') }
end
end
describe '#expanded_kubernetes_namespace' do
let(:job) { create(factory_type, environment: environment, options: options, pipeline: pipeline) }
subject { job.expanded_kubernetes_namespace }
context 'environment and namespace are not set' do
let(:environment) { nil }
let(:options) { nil }
it { is_expected.to be_nil }
end
context 'environment is specified' do
let(:environment) { 'production' }
context 'namespace is not set' do
let(:options) { nil }
it { is_expected.to be_nil }
end
context 'namespace is provided' do
let(:options) do
{
environment: {
name: environment,
kubernetes: {
namespace: namespace
}
}
}
end
context 'with a static value' do
let(:namespace) { 'production' }
it { is_expected.to eq namespace }
end
context 'with a dynamic value' do
let(:namespace) { 'deploy-$CI_COMMIT_REF_NAME' }
it { is_expected.to eq 'deploy-master' }
end
end
end
end
describe '#deployment_job?' do
subject { job.deployment_job? }
context 'when environment is defined' do
before do
job.update!(environment: 'review')
end
context 'no action is defined' do
it { is_expected.to be_truthy }
end
context 'and start action is defined' do
before do
job.update!(options: { environment: { action: 'start' } })
end
it { is_expected.to be_truthy }
end
end
context 'when environment is not defined' do
before do
job.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#stops_environment?' do
subject { job.stops_environment? }
context 'when environment is defined' do
before do
job.update!(environment: 'review')
end
context 'no action is defined' do
it { is_expected.to be_falsey }
end
context 'and stop action is defined' do
before do
job.update!(options: { environment: { action: 'stop' } })
end
it { is_expected.to be_truthy }
end
end
context 'when environment is not defined' do
before do
job.update!(environment: nil)
end
it { is_expected.to be_falsey }
end
end
end
describe '#persisted_environment' do
let!(:environment) do
create(:environment, project: project, name: "foo-#{project.default_branch}")
end
subject { job.persisted_environment }
context 'when referenced literally' do
let(:job) do
create(factory_type, pipeline: pipeline, environment: "foo-#{project.default_branch}")
end
it { is_expected.to eq(environment) }
end
context 'when referenced with a variable' do
let(:job) do
create(factory_type, pipeline: pipeline, environment: "foo-$CI_COMMIT_REF_NAME")
end
it { is_expected.to eq(environment) }
end
context 'when there is no environment' do
it { is_expected.to be_nil }
end
context 'when job has a stop environment' do
let(:job) { create(factory_type, :stop_review_app, pipeline: pipeline, environment: "foo-#{project.default_branch}") }
it 'expands environment name' do
expect(job).to receive(:expanded_environment_name).and_call_original
is_expected.to eq(environment)
end
end
end
describe '#deployment_status' do
context 'when job is a last deployment' do
let(:job) { create(factory_type, :success, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: job.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: job) }
it { expect(job.deployment_status).to eq(:last) }
end
context 'when there is a newer job with deployment' do
let(:job) { create(factory_type, :success, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: job.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: job) }
let!(:last_deployment) { create(:deployment, :success, environment: environment, project: environment.project) }
it { expect(job.deployment_status).to eq(:out_of_date) }
end
context 'when job with deployment has failed' do
let(:job) { create(factory_type, :failed, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: job.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: job) }
it { expect(job.deployment_status).to eq(:failed) }
end
context 'when job with deployment is running' do
let(:job) { create(factory_type, environment: 'production', pipeline: pipeline) }
let(:environment) { create(:environment, name: 'production', project: job.project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: job) }
it { expect(job.deployment_status).to eq(:creating) }
end
end
def factory_type
described_class.name.underscore.tr('/', '_')
end
end
# rubocop:enable Layout/LineLength
# rubocop:enable RSpec/ContextWording
|