summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/templates/terraform_latest_gitlab_ci_yaml_spec.rb
blob: 6ae51f9783b0813a14981d8801c49e82244e77f0 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Terraform.latest.gitlab-ci.yml' do
  before do
    allow(Gitlab::Template::GitlabCiYmlTemplate).to receive(:excluded_patterns).and_return([])
  end

  subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Terraform.latest') }

  describe 'the created pipeline' do
    let(:default_branch) { project.default_branch_or_main }
    let(:pipeline_branch) { default_branch }
    let_it_be(:project) { create(:project, :repository, create_branch: 'patch-1') }
    let_it_be(:user) { project.first_owner }
    let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch ) }
    let(:pipeline) { service.execute(:push).payload }
    let(:build_names) { pipeline.builds.pluck(:name) }

    before do
      stub_ci_pipeline_yaml_file(template.content)
      allow_next_instance_of(Ci::BuildScheduleWorker) do |instance|
        allow(instance).to receive(:perform).and_return(true)
      end
      allow(project).to receive(:default_branch).and_return(default_branch)
    end

    context 'on master branch' do
      it 'creates init, validate and build jobs', :aggregate_failures do
        expect(pipeline.errors).to be_empty
        expect(build_names).to include('validate', 'build', 'deploy')
      end
    end

    context 'outside the master branch' do
      let(:pipeline_branch) { 'patch-1' }

      it 'does not creates a deploy and a test job', :aggregate_failures do
        expect(pipeline.errors).to be_empty
        expect(build_names).not_to include('deploy')
      end
    end

    context 'on merge request' do
      let(:pipeline_branch) { 'patch-1' }
      let(:mr_service) { MergeRequests::CreatePipelineService.new(project: project, current_user: user) }
      let(:merge_request) { create(:merge_request, :simple, source_project: project, source_branch: pipeline_branch ) }
      let(:mr_pipeline) { mr_service.execute(merge_request).payload }
      let(:mr_build_names) { mr_pipeline.builds.pluck(:name) }
      let(:branch_service) { Ci::CreatePipelineService.new(project, user, ref: merge_request.source_branch ) }
      let(:branch_pipeline) { branch_service.execute(:push).payload }
      let(:branch_build_names) { branch_pipeline.builds.pluck(:name) }

      # This is needed so that the terraform artifacts and sast_iac artifacts
      # are both available in the MR
      it 'creates a pipeline with the terraform and sast_iac jobs' do
        expect(mr_pipeline).to be_merge_request_event
        expect(mr_pipeline.errors.full_messages).to be_empty
        expect(mr_build_names).to include('kics-iac-sast', 'validate', 'build')
      end

      it 'does not creates a deploy', :aggregate_failures do
        expect(mr_build_names).not_to include('deploy')
      end

      it 'does not create a branch pipeline', :aggregate_failures do
        expect(branch_build_names).to be_empty
        expect(branch_pipeline.errors.full_messages).to match_array(["No stages / jobs for this pipeline."])
      end
    end
  end
end