GitLab steht Mittwoch, den 08. Juli, zwischen 09:00 und 13:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

build_spec.rb 96.3 KB
Newer Older
1 2
require 'spec_helper'

3
describe Ci::Build do
4
  set(:user) { create(:user) }
5 6
  set(:group) { create(:group, :access_requestable) }
  set(:project) { create(:project, :repository, group: group) }
7

8
  set(:pipeline) do
9 10 11 12 13 14
    create(:ci_pipeline, project: project,
                         sha: project.commit.id,
                         ref: project.default_branch,
                         status: 'success')
  end

15 16
  let(:build) { create(:ci_build, pipeline: pipeline) }

17 18 19
  it { is_expected.to belong_to(:runner) }
  it { is_expected.to belong_to(:trigger_request) }
  it { is_expected.to belong_to(:erased_by) }
20
  it { is_expected.to have_many(:trace_sections)}
21
  it { is_expected.to have_one(:deployment) }
22
  it { is_expected.to have_one(:runner_session)}
23 24 25
  it { is_expected.to validate_presence_of(:ref) }
  it { is_expected.to respond_to(:has_trace?) }
  it { is_expected.to respond_to(:trace) }
26

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
27 28
  it { is_expected.to be_a(ArtifactMigratable) }

29 30 31 32 33 34 35
  describe 'associations' do
    it 'has a bidirectional relationship with projects' do
      expect(described_class.reflect_on_association(:project).has_inverse?).to eq(:builds)
      expect(Project.reflect_on_association(:builds).has_inverse?).to eq(:project)
    end
  end

36 37 38 39 40 41 42 43 44 45
  describe 'callbacks' do
    context 'when running after_create callback' do
      it 'triggers asynchronous build hooks worker' do
        expect(BuildHooksWorker).to receive(:perform_async)

        create(:ci_build)
      end
    end
  end

46 47 48 49 50 51 52 53 54 55 56 57 58 59
  describe 'status' do
    context 'when transitioning to any state from running' do
      it 'removes runner_session' do
        %w(success drop cancel).each do |event|
          build = FactoryBot.create(:ci_build, :running, :with_runner_session, pipeline: pipeline)

          build.fire_events!(event)

          expect(build.reload.runner_session).to be_nil
        end
      end
    end
  end

60 61 62 63 64 65 66 67 68 69 70 71
  describe '.manual_actions' do
    let!(:manual_but_created) { create(:ci_build, :manual, status: :created, pipeline: pipeline) }
    let!(:manual_but_succeeded) { create(:ci_build, :manual, status: :success, pipeline: pipeline) }
    let!(:manual_action) { create(:ci_build, :manual, pipeline: pipeline) }

    subject { described_class.manual_actions }

    it { is_expected.to include(manual_action) }
    it { is_expected.to include(manual_but_succeeded) }
    it { is_expected.not_to include(manual_but_created) }
  end

72 73
  describe '.ref_protected' do
    subject { described_class.ref_protected }
Shinya Maeda's avatar
Shinya Maeda committed
74

75 76 77 78 79 80 81
    context 'when protected is true' do
      let!(:job) { create(:ci_build, :protected) }

      it { is_expected.to include(job) }
    end

    context 'when protected is false' do
Shinya Maeda's avatar
Shinya Maeda committed
82
      let!(:job) { create(:ci_build) }
83 84 85 86

      it { is_expected.not_to include(job) }
    end

Shinya Maeda's avatar
Shinya Maeda committed
87
    context 'when protected is nil' do
88 89 90 91 92
      let!(:job) { create(:ci_build) }

      before do
        job.update_attribute(:protected, nil)
      end
93 94 95

      it { is_expected.not_to include(job) }
    end
Shinya Maeda's avatar
Shinya Maeda committed
96 97
  end

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
  describe '.with_artifacts_archive' do
    subject { described_class.with_artifacts_archive }

    context 'when job does not have an archive' do
      let!(:job) { create(:ci_build) }

      it 'does not return the job' do
        is_expected.not_to include(job)
      end
    end

    context 'when job has a legacy archive' do
      let!(:job) { create(:ci_build, :legacy_artifacts) }

      it 'returns the job' do
        is_expected.to include(job)
      end
    end

    context 'when job has a job artifact archive' do
      let!(:job) { create(:ci_build, :artifacts) }

      it 'returns the job' do
        is_expected.to include(job)
      end
    end

    context 'when job has a job artifact trace' do
      let!(:job) { create(:ci_build, :trace_artifact) }

      it 'does not return the job' do
        is_expected.not_to include(job)
      end
    end
  end

134 135 136 137 138 139 140 141 142 143 144 145 146 147
  describe '.with_live_trace' do
    subject { described_class.with_live_trace }

    context 'when build has live trace' do
      let!(:build) { create(:ci_build, :success, :trace_live) }

      it 'selects the build' do
        is_expected.to eq([build])
      end
    end

    context 'when build does not have live trace' do
      let!(:build) { create(:ci_build, :success, :trace_artifact) }

148
      it 'does not select the build' do
149 150 151 152 153
        is_expected.to be_empty
      end
    end
  end

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
  describe '.with_test_reports' do
    subject { described_class.with_test_reports }

    context 'when build has a test report' do
      let!(:build) { create(:ci_build, :success, :test_reports) }

      it 'selects the build' do
        is_expected.to eq([build])
      end
    end

    context 'when build does not have test reports' do
      let!(:build) { create(:ci_build, :success, :trace_artifact) }

      it 'does not select the build' do
        is_expected.to be_empty
      end
    end

    context 'when there are multiple builds with test reports' do
      let!(:builds) { create_list(:ci_build, 5, :success, :test_reports) }

      it 'does not execute a query for selecting job artifact one by one' do
        recorded = ActiveRecord::QueryRecorder.new do
          subject.each do |build|
179
            build.job_artifacts.map { |a| a.file.exists? }
180 181 182 183 184 185 186 187
          end
        end

        expect(recorded.count).to eq(2)
      end
    end
  end

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  describe '#actionize' do
    context 'when build is a created' do
      before do
        build.update_column(:status, :created)
      end

      it 'makes build a manual action' do
        expect(build.actionize).to be true
        expect(build.reload).to be_manual
      end
    end

    context 'when build is not created' do
      before do
        build.update_column(:status, :pending)
      end

      it 'does not change build status' do
        expect(build.actionize).to be false
        expect(build.reload).to be_pending
      end
    end
  end

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
  describe '#schedulable?' do
    subject { build.schedulable? }

    context 'when build is schedulable' do
      let(:build) { create(:ci_build, :created, :schedulable, project: project) }

      it { expect(subject).to be_truthy }
    end

    context 'when build is not schedulable' do
      let(:build) { create(:ci_build, :created, project: project) }

      it { expect(subject).to be_falsy }
    end
  end

  describe '#schedule' do
    subject { build.schedule }

    before do
      project.add_developer(user)
    end

    let(:build) { create(:ci_build, :created, :schedulable, user: user, project: project) }

    it 'transits to scheduled' do
Shinya Maeda's avatar
Shinya Maeda committed
238 239
      allow(Ci::BuildScheduleWorker).to receive(:perform_at)

240 241 242 243 244 245
      subject

      expect(build).to be_scheduled
    end

    it 'updates scheduled_at column' do
Shinya Maeda's avatar
Shinya Maeda committed
246 247
      allow(Ci::BuildScheduleWorker).to receive(:perform_at)

248 249 250 251 252 253 254 255
      subject

      expect(build.scheduled_at).not_to be_nil
    end

    it 'schedules BuildScheduleWorker at the right time' do
      Timecop.freeze do
        expect(Ci::BuildScheduleWorker)
256
          .to receive(:perform_at).with(be_like_time(1.minute.since), build.id)
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

        subject
      end
    end
  end

  describe '#unschedule' do
    subject { build.unschedule }

    context 'when build is scheduled' do
      let(:build) { create(:ci_build, :scheduled, pipeline: pipeline) }

      it 'cleans scheduled_at column' do
        subject

        expect(build.scheduled_at).to be_nil
      end

      it 'transits to manual' do
        subject

        expect(build).to be_manual
      end
    end

    context 'when build is not scheduled' do
      let(:build) { create(:ci_build, :created, pipeline: pipeline) }

      it 'does not transit status' do
        subject

        expect(build).to be_created
      end
    end
  end

  describe '#options_scheduled_at' do
    subject { build.options_scheduled_at }

    let(:build) { build_stubbed(:ci_build, options: option) }

    context 'when start_in is 1 day' do
      let(:option) { { start_in: '1 day' } }

      it 'returns date after 1 day' do
        Timecop.freeze do
          is_expected.to eq(1.day.since)
        end
      end
    end

    context 'when start_in is 1 week' do
      let(:option) { { start_in: '1 week' } }

      it 'returns date after 1 week' do
        Timecop.freeze do
          is_expected.to eq(1.week.since)
        end
      end
    end
  end

  describe '#enqueue_scheduled' do
    subject { build.enqueue_scheduled }

    context 'when build is scheduled and the right time has not come yet' do
      let(:build) { create(:ci_build, :scheduled, pipeline: pipeline) }

      it 'does not transits the status' do
        subject

        expect(build).to be_scheduled
      end
    end

    context 'when build is scheduled and the right time has already come' do
      let(:build) { create(:ci_build, :expired_scheduled, pipeline: pipeline) }

      it 'cleans scheduled_at column' do
        subject

        expect(build.scheduled_at).to be_nil
      end

      it 'transits to pending' do
        subject

        expect(build).to be_pending
      end
    end
  end

349 350 351 352 353 354 355 356
  describe '#any_runners_online?' do
    subject { build.any_runners_online? }

    context 'when no runners' do
      it { is_expected.to be_falsey }
    end

    context 'when there are runners' do
357
      let(:runner) { create(:ci_runner, :project, projects: [build.project]) }
358 359

      before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
360
        runner.update(contacted_at: 1.second.ago)
361 362 363 364 365
      end

      it { is_expected.to be_truthy }

      it 'that is inactive' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
366
        runner.update(active: false)
367 368 369 370
        is_expected.to be_falsey
      end

      it 'that is not online' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
371
        runner.update(contacted_at: nil)
372 373 374 375 376 377 378 379 380 381 382
        is_expected.to be_falsey
      end

      it 'that cannot handle build' do
        expect_any_instance_of(Ci::Runner).to receive(:can_pick?).and_return(false)
        is_expected.to be_falsey
      end
    end
  end

  describe '#artifacts?' do
383
    subject { build.artifacts? }
384

385
    context 'when new artifacts are used' do
386 387
      context 'artifacts archive does not exist' do
        let(:build) { create(:ci_build) }
388

389 390 391 392
        it { is_expected.to be_falsy }
      end

      context 'artifacts archive exists' do
393 394
        let(:build) { create(:ci_build, :artifacts) }

395 396 397
        it { is_expected.to be_truthy }

        context 'is expired' do
398
          let(:build) { create(:ci_build, :artifacts, :expired) }
399 400 401 402

          it { is_expected.to be_falsy }
        end
      end
403 404
    end

405 406 407 408
    context 'when legacy artifacts are used' do
      let(:build) { create(:ci_build, :legacy_artifacts) }

      subject { build.artifacts? }
409

Micaël Bergeron's avatar
Micaël Bergeron committed
410 411 412 413 414 415
      context 'is expired' do
        let(:build) { create(:ci_build, :legacy_artifacts, :expired) }

        it { is_expected.to be_falsy }
      end

416 417
      context 'artifacts archive does not exist' do
        let(:build) { create(:ci_build) }
418

419 420 421
        it { is_expected.to be_falsy }
      end

422
      context 'artifacts archive exists' do
423 424
        let(:build) { create(:ci_build, :legacy_artifacts) }

425 426 427 428
        it { is_expected.to be_truthy }
      end
    end
  end
429

430 431
  describe '#browsable_artifacts?' do
    subject { build.browsable_artifacts? }
432

433 434
    context 'artifacts metadata does not exist' do
      before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
435
        build.update(legacy_artifacts_metadata: nil)
436
      end
437 438 439 440 441 442 443 444

      it { is_expected.to be_falsy }
    end

    context 'artifacts metadata does exists' do
      let(:build) { create(:ci_build, :artifacts) }

      it { is_expected.to be_truthy }
445 446 447 448 449 450 451
    end
  end

  describe '#artifacts_expired?' do
    subject { build.artifacts_expired? }

    context 'is expired' do
452 453 454
      before do
        build.update(artifacts_expire_at: Time.now - 7.days)
      end
455 456 457 458 459

      it { is_expected.to be_truthy }
    end

    context 'is not expired' do
460 461 462
      before do
        build.update(artifacts_expire_at: Time.now + 7.days)
      end
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

      it { is_expected.to be_falsey }
    end
  end

  describe '#artifacts_metadata?' do
    subject { build.artifacts_metadata? }
    context 'artifacts metadata does not exist' do
      it { is_expected.to be_falsy }
    end

    context 'artifacts archive is a zip file and metadata exists' do
      let(:build) { create(:ci_build, :artifacts) }
      it { is_expected.to be_truthy }
    end
  end

  describe '#artifacts_expire_in' do
    subject { build.artifacts_expire_in }
    it { is_expected.to be_nil }

    context 'when artifacts_expire_at is specified' do
      let(:expire_at) { Time.now + 7.days }

487 488 489
      before do
        build.artifacts_expire_at = expire_at
      end
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

      it { is_expected.to be_within(5).of(expire_at - Time.now) }
    end
  end

  describe '#artifacts_expire_in=' do
    subject { build.artifacts_expire_in }

    it 'when assigning valid duration' do
      build.artifacts_expire_in = '7 days'

      is_expected.to be_within(10).of(7.days.to_i)
    end

    it 'when assigning invalid duration' do
      expect { build.artifacts_expire_in = '7 elephants' }.to raise_error(ChronicDuration::DurationParseError)
      is_expected.to be_nil
    end

509
    it 'when resetting value' do
510 511
      build.artifacts_expire_in = nil

512 513 514 515 516 517
      is_expected.to be_nil
    end

    it 'when setting to 0' do
      build.artifacts_expire_in = '0'

518 519 520 521 522 523 524 525 526 527
      is_expected.to be_nil
    end
  end

  describe '#commit' do
    it 'returns commit pipeline has been created for' do
      expect(build.commit).to eq project.commit
    end
  end

528 529 530 531 532 533 534 535 536 537
  describe '#cache' do
    let(:options) { { cache: { key: "key", paths: ["public"], policy: "pull-push" } } }

    subject { build.cache }

    context 'when build has cache' do
      before do
        allow(build).to receive(:options).and_return(options)
      end

538
      context 'when project has jobs_cache_index' do
539
        before do
540
          allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(1)
541 542
        end

543
        it { is_expected.to be_an(Array).and all(include(key: "key-1")) }
544 545
      end

546
      context 'when project does not have jobs_cache_index' do
547
        before do
548
          allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(nil)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        end

        it { is_expected.to eq([options[:cache]]) }
      end
    end

    context 'when build does not have cache' do
      before do
        allow(build).to receive(:options).and_return({})
      end

      it { is_expected.to eq([nil]) }
    end
  end

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  describe '#depends_on_builds' do
    let!(:build) { create(:ci_build, pipeline: pipeline, name: 'build', stage_idx: 0, stage: 'build') }
    let!(:rspec_test) { create(:ci_build, pipeline: pipeline, name: 'rspec', stage_idx: 1, stage: 'test') }
    let!(:rubocop_test) { create(:ci_build, pipeline: pipeline, name: 'rubocop', stage_idx: 1, stage: 'test') }
    let!(:staging) { create(:ci_build, pipeline: pipeline, name: 'staging', stage_idx: 2, stage: 'deploy') }

    it 'expects to have no dependents if this is first build' do
      expect(build.depends_on_builds).to be_empty
    end

    it 'expects to have one dependent if this is test' do
      expect(rspec_test.depends_on_builds.map(&:id)).to contain_exactly(build.id)
    end

    it 'expects to have all builds from build and test stage if this is last' do
      expect(staging.depends_on_builds.map(&:id)).to contain_exactly(build.id, rspec_test.id, rubocop_test.id)
    end

    it 'expects to have retried builds instead the original ones' do
583
      project.add_developer(user)
584

585
      retried_rspec = described_class.retry(rspec_test, user)
586 587 588

      expect(staging.depends_on_builds.map(&:id))
        .to contain_exactly(build.id, retried_rspec.id, rubocop_test.id)
589 590 591
    end
  end

592 593
  describe '#triggered_by?' do
    subject { build.triggered_by?(user) }
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

    context 'when user is owner' do
      let(:build) { create(:ci_build, pipeline: pipeline, user: user) }

      it { is_expected.to be_truthy }
    end

    context 'when user is not owner' do
      let(:another_user) { create(:user) }
      let(:build) { create(:ci_build, pipeline: pipeline, user: another_user) }

      it { is_expected.to be_falsy }
    end
  end

609 610 611 612 613 614 615
  describe '#detailed_status' do
    it 'returns a detailed status' do
      expect(build.detailed_status(user))
        .to be_a Gitlab::Ci::Status::Build::Cancelable
    end
  end

616
  describe '#coverage_regex' do
617 618
    subject { build.coverage_regex }

619
    context 'when project has build_coverage_regex set' do
620 621
      let(:project_regex) { '\(\d+\.\d+\) covered' }

622
      before do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
623
        project.update_column(:build_coverage_regex, project_regex)
624
      end
625 626

      context 'and coverage_regex attribute is not set' do
627
        it { is_expected.to eq(project_regex) }
628 629 630
      end

      context 'but coverage_regex attribute is also set' do
631
        let(:build_regex) { 'Code coverage: \d+\.\d+' }
632

633 634 635 636
        before do
          build.coverage_regex = build_regex
        end

637
        it { is_expected.to eq(build_regex) }
638 639 640 641 642 643 644 645 646
      end
    end

    context 'when neither project nor build has coverage regex set' do
      it { is_expected.to be_nil }
    end
  end

  describe '#update_coverage' do
647
    context "regarding coverage_regex's value," do
648
      before do
649
        build.coverage_regex = '\(\d+.\d+\%\) covered'
650 651 652 653 654 655 656 657 658 659
        build.trace.set('Coverage 1033 / 1051 LOC (98.29%) covered')
      end

      it "saves the correct extracted coverage value" do
        expect(build.update_coverage).to be(true)
        expect(build.coverage).to eq(98.29)
      end
    end
  end

660
  describe '#parse_trace_sections!' do
661 662 663 664 665
    it 'calls ExtractSectionsFromBuildTraceService' do
      expect(Ci::ExtractSectionsFromBuildTraceService)
          .to receive(:new).with(project, build.user).once.and_call_original
      expect_any_instance_of(Ci::ExtractSectionsFromBuildTraceService)
        .to receive(:execute).with(build).once
666

667
      build.parse_trace_sections!
668 669 670
    end
  end

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
  describe '#trace' do
    subject { build.trace }

    it { is_expected.to be_a(Gitlab::Ci::Trace) }
  end

  describe '#has_trace?' do
    subject { build.has_trace? }

    it "expect to call exist? method" do
      expect_any_instance_of(Gitlab::Ci::Trace).to receive(:exist?)
        .and_return(true)

      is_expected.to be(true)
    end
  end

688 689
  describe '#has_job_artifacts?' do
    subject { build.has_job_artifacts? }
690

691 692
    context 'when build has a job artifact' do
      let(:build) { create(:ci_build, :artifacts) }
693 694 695 696

      it { is_expected.to be_truthy }
    end

697 698
    context 'when build does not have job artifacts' do
      let(:build) { create(:ci_build, :legacy_artifacts) }
699 700 701 702 703

      it { is_expected.to be_falsy }
    end
  end

Shinya Maeda's avatar
Shinya Maeda committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  describe '#has_old_trace?' do
    subject { build.has_old_trace? }

    context 'when old trace exists' do
      before do
        build.update_column(:trace, 'old trace')
      end

      it { is_expected.to be_truthy }
    end

    context 'when old trace does not exist' do
      it { is_expected.to be_falsy }
    end
  end

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  describe '#trace=' do
    it "expect to fail trace=" do
      expect { build.trace = "new" }.to raise_error(NotImplementedError)
    end
  end

  describe '#old_trace' do
    subject { build.old_trace }

    before do
      build.update_column(:trace, 'old trace')
    end

    it "expect to receive data from database" do
      is_expected.to eq('old trace')
    end
  end

  describe '#erase_old_trace!' do
Shinya Maeda's avatar
Shinya Maeda committed
739
    subject { build.erase_old_trace! }
740

Shinya Maeda's avatar
Shinya Maeda committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    context 'when old trace exists' do
      before do
        build.update_column(:trace, 'old trace')
      end

      it "erases old trace" do
        subject

        expect(build.old_trace).to be_nil
      end

      it "executes UPDATE query" do
        recorded = ActiveRecord::QueryRecorder.new { subject }

        expect(recorded.log.select { |l| l.match?(/UPDATE.*ci_builds/) }.count).to eq(1)
      end
757 758
    end

Shinya Maeda's avatar
Shinya Maeda committed
759 760 761
    context 'when old trace does not exist' do
      it 'does not execute UPDATE query' do
        recorded = ActiveRecord::QueryRecorder.new { subject }
762

Shinya Maeda's avatar
Shinya Maeda committed
763 764
        expect(recorded.log.select { |l| l.match?(/UPDATE.*ci_builds/) }.count).to eq(0)
      end
765 766 767 768 769 770 771
    end
  end

  describe '#hide_secrets' do
    let(:subject) { build.hide_secrets(data) }

    context 'hide runners token' do
772
      let(:data) { "new #{project.runners_token} data"}
773

774
      it { is_expected.to match(/^new x+ data$/) }
775 776 777
    end

    context 'hide build token' do
778
      let(:data) { "new #{build.token} data"}
779

780
      it { is_expected.to match(/^new x+ data$/) }
781 782 783
    end
  end

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
  describe 'state transition as a deployable' do
    let!(:build) { create(:ci_build, :start_review_app) }
    let(:deployment) { build.deployment }
    let(:environment) { deployment.environment }

    it 'has deployments record with created status' do
      expect(deployment).to be_created
      expect(environment.name).to eq('review/master')
    end

    context 'when transits to running' do
      before do
        build.run!
      end

      it 'transits deployment status to running' do
        expect(deployment).to be_running
      end
    end

    context 'when transits to success' do
      before do
        allow(Deployments::SuccessWorker).to receive(:perform_async)
        build.success!
      end

      it 'transits deployment status to success' do
        expect(deployment).to be_success
      end
    end

    context 'when transits to failed' do
      before do
        build.drop!
      end

      it 'transits deployment status to failed' do
        expect(deployment).to be_failed
      end
    end

    context 'when transits to skipped' do
      before do
        build.skip!
      end

      it 'transits deployment status to canceled' do
        expect(deployment).to be_canceled
      end
    end

    context 'when transits to canceled' do
      before do
        build.cancel!
      end

      it 'transits deployment status to canceled' do
        expect(deployment).to be_canceled
      end
    end
  end

  describe '#on_stop' do
    subject { build.on_stop }

    context 'when a job has a specification that it can be stopped from the other job' do
      let(:build) { create(:ci_build, :start_review_app) }

      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(:build) { create(:ci_build) }

      it 'returns nil' do
        is_expected.to be_nil
      end
    end
  end

866
  describe 'deployment' do
867 868 869 870 871
    describe '#has_deployment?' do
      subject { build.has_deployment? }

      context 'when build has a deployment' do
        let!(:deployment) { create(:deployment, deployable: build) }
872

873 874
        it { is_expected.to be_truthy }
      end
875

876 877
      context 'when build does not have a deployment' do
        it { is_expected.to be_falsy }
878 879 880 881 882 883 884 885
      end
    end

    describe '#outdated_deployment?' do
      subject { build.outdated_deployment? }

      context 'when build succeeded' do
        let(:build) { create(:ci_build, :success) }
886
        let!(:deployment) { create(:deployment, :success, deployable: build) }
887 888 889 890 891 892

        context 'current deployment is latest' do
          it { is_expected.to be_falsey }
        end

        context 'current deployment is not latest on environment' do
893
          let!(:deployment2) { create(:deployment, :success, environment: deployment.environment) }
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

          it { is_expected.to be_truthy }
        end
      end

      context 'when build failed' do
        let(:build) { create(:ci_build, :failed) }

        it { is_expected.to be_falsey }
      end
    end
  end

  describe 'environment' do
    describe '#has_environment?' do
      subject { build.has_environment? }

      context 'when environment is defined' do
        before do
          build.update(environment: 'review')
        end

        it { is_expected.to be_truthy }
      end

      context 'when environment is not defined' do
        before do
          build.update(environment: nil)
        end

        it { is_expected.to be_falsey }
      end
    end

    describe '#expanded_environment_name' do
      subject { build.expanded_environment_name }

931
      context 'when environment uses $CI_COMMIT_REF_NAME' do
932 933 934
        let(:build) do
          create(:ci_build,
                 ref: 'master',
935
                 environment: 'review/$CI_COMMIT_REF_NAME')
936 937 938 939 940 941 942 943 944 945 946 947 948 949
        end

        it { is_expected.to eq('review/master') }
      end

      context 'when environment uses yaml_variables containing symbol keys' do
        let(:build) do
          create(:ci_build,
                 yaml_variables: [{ key: :APP_HOST, value: 'host' }],
                 environment: 'review/$APP_HOST')
        end

        it { is_expected.to eq('review/host') }
      end
950 951 952 953 954 955 956 957

      context 'when using persisted variables' do
        let(:build) do
          create(:ci_build, environment: 'review/x$CI_BUILD_ID')
        end

        it { is_expected.to eq('review/x') }
      end
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    end

    describe '#starts_environment?' do
      subject { build.starts_environment? }

      context 'when environment is defined' do
        before do
          build.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
            build.update(options: { environment: { action: 'start' } } )
          end

          it { is_expected.to be_truthy }
        end
      end

      context 'when environment is not defined' do
        before do
          build.update(environment: nil)
        end

        it { is_expected.to be_falsey }
      end
    end

    describe '#stops_environment?' do
      subject { build.stops_environment? }

      context 'when environment is defined' do
        before do
          build.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
            build.update(options: { environment: { action: 'stop' } } )
          end

          it { is_expected.to be_truthy }
        end
      end

      context 'when environment is not defined' do
        before do
          build.update(environment: nil)
        end

        it { is_expected.to be_falsey }
      end
    end
  end

  describe 'erasable build' do
    shared_examples 'erasable' do
      it 'removes artifact file' do
        expect(build.artifacts_file.exists?).to be_falsy
      end

      it 'removes artifact metadata file' do
        expect(build.artifacts_metadata.exists?).to be_falsy
      end

1031 1032
      it 'removes all job_artifacts' do
        expect(build.job_artifacts.count).to eq(0)
1033 1034
      end

1035
      it 'erases build trace in trace file' do
1036
        expect(build).not_to have_trace
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
      end

      it 'sets erased to true' do
        expect(build.erased?).to be true
      end

      it 'sets erase date' do
        expect(build.erased_at).not_to be_falsy
      end
    end

    context 'build is not erasable' do
      let!(:build) { create(:ci_build) }

      describe '#erase' do
        subject { build.erase }

        it { is_expected.to be false }
      end

      describe '#erasable?' do
        subject { build.erasable? }
1059

1060 1061 1062 1063 1064
        it { is_expected.to eq false }
      end
    end

    context 'build is erasable' do
1065
      context 'new artifacts' do
1066
        let!(:build) { create(:ci_build, :test_reports, :trace_artifact, :success, :artifacts) }
1067

1068 1069
        describe '#erase' do
          before do
1070
            build.erase(erased_by: erased_by)
1071
          end
1072

1073
          context 'erased by user' do
1074
            let!(:erased_by) { create(:user, username: 'eraser') }
1075

1076
            include_examples 'erasable'
1077

1078
            it 'records user who erased a build' do
1079
              expect(build.erased_by).to eq erased_by
1080
            end
1081 1082
          end

1083
          context 'erased by system' do
1084
            let(:erased_by) { nil }
1085

1086
            include_examples 'erasable'
1087

1088 1089 1090
            it 'does not set user who erased a build' do
              expect(build.erased_by).to be_nil
            end
1091 1092 1093
          end
        end

1094 1095 1096 1097
        describe '#erasable?' do
          subject { build.erasable? }
          it { is_expected.to be_truthy }
        end
1098

1099
        describe '#erased?' do
1100
          let!(:build) { create(:ci_build, :trace_artifact, :success, :artifacts) }
1101
          subject { build.erased? }
1102

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
          context 'job has not been erased' do
            it { is_expected.to be_falsey }
          end

          context 'job has been erased' do
            before do
              build.erase
            end

            it { is_expected.to be_truthy }
          end
1114 1115
        end

1116 1117 1118
        context 'metadata and build trace are not available' do
          let!(:build) { create(:ci_build, :success, :artifacts) }

1119
          before do
1120
            build.remove_artifacts_metadata!
1121 1122
          end

1123 1124 1125 1126 1127
          describe '#erase' do
            it 'does not raise error' do
              expect { build.erase }.not_to raise_error
            end
          end
1128 1129
        end
      end
1130
    end
1131

1132 1133 1134
    context 'old artifacts' do
      context 'build is erasable' do
        context 'new artifacts' do
1135
          let!(:build) { create(:ci_build, :trace_artifact, :success, :legacy_artifacts) }
1136

1137 1138
          describe '#erase' do
            before do
1139
              build.erase(erased_by: erased_by)
1140
            end
1141

1142
            context 'erased by user' do
1143
              let!(:erased_by) { create(:user, username: 'eraser') }
1144 1145 1146 1147

              include_examples 'erasable'

              it 'records user who erased a build' do
1148
                expect(build.erased_by).to eq erased_by
1149 1150 1151 1152
              end
            end

            context 'erased by system' do
1153
              let(:erased_by) { nil }
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

              include_examples 'erasable'

              it 'does not set user who erased a build' do
                expect(build.erased_by).to be_nil
              end
            end
          end

          describe '#erasable?' do
            subject { build.erasable? }
            it { is_expected.to be_truthy }
          end

          describe '#erased?' do
1169
            let!(:build) { create(:ci_build, :trace_artifact, :success, :legacy_artifacts) }
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
            subject { build.erased? }

            context 'job has not been erased' do
              it { is_expected.to be_falsey }
            end

            context 'job has been erased' do
              before do
                build.erase
              end

              it { is_expected.to be_truthy }
            end
          end

          context 'metadata and build trace are not available' do
            let!(:build) { create(:ci_build, :success, :legacy_artifacts) }

            before do
              build.remove_artifacts_metadata!
            end

            describe '#erase' do
              it 'does not raise error' do
                expect { build.erase }.not_to raise_error
              end
            end
1197 1198 1199 1200 1201 1202
          end
        end
      end
    end
  end

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
  describe '#erase_erasable_artifacts!' do
    let!(:build) { create(:ci_build, :success) }

    subject { build.erase_erasable_artifacts! }

    before do
      Ci::JobArtifact.file_types.keys.each do |file_type|
        create(:ci_job_artifact, job: build, file_type: file_type, file_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS[file_type.to_sym])
      end
    end

    it "erases erasable artifacts" do
      subject

      expect(build.job_artifacts.erasable).to be_empty
    end

    it "keeps non erasable artifacts" do
      subject

      Ci::JobArtifact::NON_ERASABLE_FILE_TYPES.each do |file_type|
        expect(build.send("job_artifacts_#{file_type}")).not_to be_nil
      end
    end
  end

1229 1230 1231
  describe '#first_pending' do
    let!(:first) { create(:ci_build, pipeline: pipeline, status: 'pending', created_at: Date.yesterday) }
    let!(:second) { create(:ci_build, pipeline: pipeline, status: 'pending') }
1232
    subject { described_class.first_pending }
1233

1234
    it { is_expected.to be_a(described_class) }
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    it('returns with the first pending build') { is_expected.to eq(first) }
  end

  describe '#failed_but_allowed?' do
    subject { build.failed_but_allowed? }

    context 'when build is not allowed to fail' do
      before do
        build.allow_failure = false
      end

      context 'and build.status is success' do
        before do
          build.status = 'success'
        end

        it { is_expected.to be_falsey }
      end

      context 'and build.status is failed' do
        before do
          build.status = 'failed'
        end

        it { is_expected.to be_falsey }
      end
    end

    context 'when build is allowed to fail' do
      before do
        build.allow_failure = true
      end

      context 'and build.status is success' do
        before do
          build.status = 'success'
        end

        it { is_expected.to be_falsey }
      end

1276
      context 'and build status is failed' do
1277 1278 1279 1280 1281 1282
        before do
          build.status = 'failed'
        end

        it { is_expected.to be_truthy }
      end
1283 1284 1285 1286 1287 1288 1289 1290

      context 'when build is a manual action' do
        before do
          build.status = 'manual'
        end

        it { is_expected.to be_falsey }
      end
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    end
  end

  describe 'flags' do
    describe '#cancelable?' do
      subject { build }

      context 'when build is cancelable' do
        context 'when build is pending' do
          it { is_expected.to be_cancelable }
        end

        context 'when build is running' do
          before do
            build.run!
          end

          it { is_expected.to be_cancelable }
        end
Jacopo's avatar
Jacopo committed
1310 1311 1312 1313 1314 1315

        context 'when build is created' do
          let(:build) { create(:ci_build, :created) }

          it { is_expected.to be_cancelable }
        end
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
      end

      context 'when build is not cancelable' do
        context 'when build is successful' do
          before do
            build.success!
          end

          it { is_expected.not_to be_cancelable }
        end

        context 'when build is failed' do
          before do
            build.drop!
          end

          it { is_expected.not_to be_cancelable }
        end
      end
    end

    describe '#retryable?' do
      subject { build }

      context 'when build is retryable' do
        context 'when build is successful' do
          before do
            build.success!
          end

          it { is_expected.to be_retryable }
        end

        context 'when build is failed' do
          before do
            build.drop!
          end

          it { is_expected.to be_retryable }
        end

        context 'when build is canceled' do
          before do
            build.cancel!
          end

          it { is_expected.to be_retryable }
        end
      end

      context 'when build is not retryable' do
        context 'when build is running' do
          before do
            build.run!
          end

          it { is_expected.not_to be_retryable }
        end

        context 'when build is skipped' do
          before do
            build.skip!
          end

          it { is_expected.not_to be_retryable }
        end
1382 1383 1384 1385 1386 1387 1388 1389

        context 'when build is degenerated' do
          before do
            build.degenerate!
          end

          it { is_expected.not_to be_retryable }
        end
1390 1391 1392
      end
    end

1393
    describe '#action?' do
1394 1395 1396 1397
      before do
        build.update(when: value)
      end

1398
      subject { build.action? }
1399 1400 1401 1402 1403 1404 1405

      context 'when is set to manual' do
        let(:value) { 'manual' }

        it { is_expected.to be_truthy }
      end

1406 1407 1408 1409 1410 1411
      context 'when is set to delayed' do
        let(:value) { 'delayed' }

        it { is_expected.to be_truthy }
      end

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
      context 'when set to something else' do
        let(:value) { 'something else' }

        it { is_expected.to be_falsey }
      end
    end
  end

  describe '#has_tags?' do
    context 'when build has tags' do
      subject { create(:ci_build, tag_list: ['tag']) }
1423

1424 1425 1426 1427 1428
      it { is_expected.to have_tags }
    end

    context 'when build does not have tags' do
      subject { create(:ci_build, tag_list: []) }
1429

1430 1431 1432 1433
      it { is_expected.not_to have_tags }
    end
  end

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  describe 'build auto retry feature' do
    describe '#retries_count' do
      subject { create(:ci_build, name: 'test', pipeline: pipeline) }

      context 'when build has been retried several times' do
        before do
          create(:ci_build, :retried, name: 'test', pipeline: pipeline)
          create(:ci_build, :retried, name: 'test', pipeline: pipeline)
        end

        it 'reports a correct retry count value' do
          expect(subject.retries_count).to eq 2
        end
      end

      context 'when build has not been retried' do
        it 'returns zero' do
          expect(subject.retries_count).to eq 0
        end
      end
    end

    describe '#retries_max' do
1457
      context 'with retries max config option' do
Markus Doits's avatar
Markus Doits committed
1458 1459
        subject { create(:ci_build, options: { retry: { max: 1 } }) }

1460
        it 'returns the number of configured max retries' do
Markus Doits's avatar
Markus Doits committed
1461 1462 1463 1464
          expect(subject.retries_max).to eq 1
        end
      end

1465
      context 'without retries max config option' do
1466 1467 1468 1469 1470 1471
        subject { create(:ci_build) }

        it 'returns zero' do
          expect(subject.retries_max).to eq 0
        end
      end
1472 1473 1474 1475 1476 1477 1478 1479

      context 'when build is degenerated' do
        subject { create(:ci_build, :degenerated) }

        it 'returns zero' do
          expect(subject.retries_max).to eq 0
        end
      end
1480 1481 1482 1483 1484 1485 1486 1487

      context 'with integer only config option' do
        subject { create(:ci_build, options: { retry: 1 }) }

        it 'returns the number of configured max retries' do
          expect(subject.retries_max).to eq 1
        end
      end
1488
    end
1489 1490

    describe '#retry_when' do
1491 1492
      context 'with retries when config option' do
        subject { create(:ci_build, options: { retry: { when: ['some_reason'] } }) }
1493

1494 1495
        it 'returns the configured when' do
          expect(subject.retry_when).to eq ['some_reason']
1496 1497 1498
        end
      end

1499
      context 'without retries when config option' do
1500 1501
        subject { create(:ci_build) }

1502
        it 'returns always array' do
1503
          expect(subject.retry_when).to eq ['always']
1504 1505
        end
      end
1506 1507 1508 1509 1510 1511 1512 1513

      context 'with integer only config option' do
        subject { create(:ci_build, options: { retry: 1 }) }

        it 'returns always array' do
          expect(subject.retry_when).to eq ['always']
        end
      end
1514
    end
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

    describe '#retry_failure?' do
      subject { create(:ci_build) }

      context 'when retries max is zero' do
        before do
          expect(subject).to receive(:retries_max).at_least(:once).and_return(0)
        end

        it 'returns false' do
          expect(subject.retry_failure?).to eq false
        end
      end

      context 'when retries max equals retries count' do
        before do
          expect(subject).to receive(:retries_max).at_least(:once).and_return(1)
          expect(subject).to receive(:retries_count).at_least(:once).and_return(1)
        end

        it 'returns false' do
          expect(subject.retry_failure?).to eq false
        end
      end

      context 'when retries max is higher than retries count' do
        before do
          expect(subject).to receive(:retries_max).at_least(:once).and_return(2)
          expect(subject).to receive(:retries_count).at_least(:once).and_return(1)
        end

        context 'and retry when is always' do
          before do
1548
            expect(subject).to receive(:retry_when).at_least(:once).and_return(['always'])
1549 1550 1551 1552 1553 1554 1555
          end

          it 'returns true' do
            expect(subject.retry_failure?).to eq true
          end
        end

1556
        context 'and retry when includes the failure_reason' do
1557
          before do
1558 1559
            expect(subject).to receive(:failure_reason).at_least(:once).and_return('some_reason')
            expect(subject).to receive(:retry_when).at_least(:once).and_return(['some_reason'])
1560 1561
          end

1562 1563
          it 'returns true' do
            expect(subject.retry_failure?).to eq true
1564 1565 1566
          end
        end

1567
        context 'and retry when does not include failure_reason' do
1568
          before do
1569 1570
            expect(subject).to receive(:failure_reason).at_least(:once).and_return('some_reason')
            expect(subject).to receive(:retry_when).at_least(:once).and_return(['some', 'other failure'])
1571 1572
          end

1573 1574
          it 'returns false' do
            expect(subject.retry_failure?).to eq false
1575 1576 1577 1578
          end
        end
      end
    end
1579 1580
  end

1581 1582 1583
  describe '#keep_artifacts!' do
    let(:build) { create(:ci_build, artifacts_expire_at: Time.now + 7.days) }

1584 1585
    subject { build.keep_artifacts! }

1586
    it 'to reset expire_at' do
1587
      subject
1588 1589 1590

      expect(build.artifacts_expire_at).to be_nil
    end
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

    context 'when having artifacts files' do
      let!(:artifact) { create(:ci_job_artifact, job: build, expire_in: '7 days') }

      it 'to reset dependent objects' do
        subject

        expect(artifact.reload.expire_at).to be_nil
      end
    end
1601 1602
  end

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
  describe '#artifacts_file_for_type' do
    let(:build) { create(:ci_build, :artifacts) }
    let(:file_type) { :archive }

    subject { build.artifacts_file_for_type(file_type) }

    it 'queries artifacts for type' do
      expect(build).to receive_message_chain(:job_artifacts, :find_by).with(file_type: Ci::JobArtifact.file_types[file_type])

      subject
    end
  end

1616 1617
  describe '#merge_request' do
    def create_mr(build, pipeline, factory: :merge_request, created_at: Time.now)
Kamil Trzciński's avatar
Kamil Trzciński committed
1618
      create(factory, source_project: pipeline.project,
1619 1620 1621
                      target_project: pipeline.project,
                      source_branch: build.ref,
                      created_at: created_at)
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
    end

    context 'when a MR has a reference to the pipeline' do
      before do
        @merge_request = create_mr(build, pipeline, factory: :merge_request)

        commits = [double(id: pipeline.sha)]
        allow(@merge_request).to receive(:commits).and_return(commits)
        allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request])
      end

      it 'returns the single associated MR' do
        expect(build.merge_request.id).to eq(@merge_request.id)
      end
    end

    context 'when there is not a MR referencing the pipeline' do
      it 'returns nil' do
        expect(build.merge_request).to be_nil
      end
    end
1643

1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
    context 'when more than one MR have a reference to the pipeline' do
      before do
        @merge_request = create_mr(build, pipeline, factory: :merge_request)
        @merge_request.close!
        @merge_request2 = create_mr(build, pipeline, factory: :merge_request)

        commits = [double(id: pipeline.sha)]
        allow(@merge_request).to receive(:commits).and_return(commits)
        allow(@merge_request2).to receive(:commits).and_return(commits)
        allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request, @merge_request2])
      end

      it 'returns the first MR' do
        expect(build.merge_request.id).to eq(@merge_request.id)
      end
    end
1660

1661 1662 1663 1664 1665 1666
    context 'when a Build is created after the MR' do
      before do
        @merge_request = create_mr(build, pipeline, factory: :merge_request_with_diffs)
        pipeline2 = create(:ci_pipeline, project: project)
        @build2 = create(:ci_build, pipeline: pipeline2)

1667
        allow(@merge_request).to receive(:commit_shas)
1668
          .and_return([pipeline.sha, pipeline2.sha])
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
        allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request])
      end

      it 'returns the current MR' do
        expect(@build2.merge_request.id).to eq(@merge_request.id)
      end
    end
  end

  describe '#options' do
    let(:options) do
      {
        image: "ruby:2.1",
        services: [
          "postgres"
        ]
      }
    end

    it 'contains options' do
      expect(build.options).to eq(options)
    end
  end

Shinya Maeda's avatar
Shinya Maeda committed
1693
  describe '#other_manual_actions' do
1694 1695 1696
    let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
    let!(:other_build) { create(:ci_build, :manual, pipeline: pipeline, name: 'other action') }

Shinya Maeda's avatar
Shinya Maeda committed
1697
    subject { build.other_manual_actions }
1698

1699
    before do
1700
      project.add_developer(user)
1701 1702
    end

1703 1704 1705 1706 1707
    it 'returns other actions' do
      is_expected.to contain_exactly(other_build)
    end

    context 'when build is retried' do
1708
      let!(:new_build) { described_class.retry(build, user) }
1709 1710 1711 1712 1713 1714 1715

      it 'does not return any of them' do
        is_expected.not_to include(build, new_build)
      end
    end

    context 'when other build is retried' do