pipeline_spec.rb 6.21 KB
Newer Older
1 2 3
require 'spec_helper'

describe Ci::Pipeline do
4 5 6 7 8 9 10
  let(:user) { create(:user) }
  set(:project) { create(:project) }

  let(:pipeline) do
    create(:ci_empty_pipeline, status: :created, project: project)
  end

11 12
  it { is_expected.to have_one(:chat_data) }

13 14 15 16 17 18
  describe '.failure_reasons' do
    it 'contains failure reasons about exceeded limits' do
      expect(described_class.failure_reasons)
        .to include 'activity_limit_exceeded', 'size_limit_exceeded'
    end
  end
19

20
  PIPELINE_ARTIFACTS_METHODS = [
21 22
    # codeclimate_artifact is deprecated and replaced with code_quality_artifact  (#5779)
    { method: :codeclimate_artifact, options: [Ci::Build::CODECLIMATE_FILE, 'codeclimate'] },
23
    { method: :codeclimate_artifact, options: [Ci::Build::CODECLIMATE_FILE, 'codequality'] },
24 25 26 27
    { method: :codeclimate_artifact, options: [Ci::Build::CODECLIMATE_FILE, 'code_quality'] },
    { method: :code_quality_artifact, options: [Ci::Build::CODE_QUALITY_FILE, 'codeclimate'] },
    { method: :code_quality_artifact, options: [Ci::Build::CODE_QUALITY_FILE, 'codequality'] },
    { method: :code_quality_artifact, options: [Ci::Build::CODE_QUALITY_FILE, 'code_quality'] },
28 29 30 31
    { method: :performance_artifact, options: [Ci::Build::PERFORMANCE_FILE, 'performance'] },
    { method: :sast_artifact, options: [Ci::Build::SAST_FILE, 'sast'] },
    { method: :dependency_scanning_artifact, options: [Ci::Build::DEPENDENCY_SCANNING_FILE, 'dependency_scanning'] },
    { method: :license_management_artifact, options: [Ci::Build::LICENSE_MANAGEMENT_FILE, 'license_management'] },
32
    # sast_container_artifact is deprecated and replaced with container_scanning_artifact (#5778)
33 34 35 36 37 38 39 40 41
    { method: :sast_container_artifact, options: [Ci::Build::SAST_CONTAINER_FILE, 'sast:container'] },
    { method: :sast_container_artifact, options: [Ci::Build::SAST_CONTAINER_FILE, 'container_scanning'] },
    { method: :container_scanning_artifact, options: [Ci::Build::CONTAINER_SCANNING_FILE, 'sast:container'] },
    { method: :container_scanning_artifact, options: [Ci::Build::CONTAINER_SCANNING_FILE, 'container_scanning'] },
    { method: :dast_artifact, options: [Ci::Build::DAST_FILE, 'dast'] }
  ].freeze

  PIPELINE_ARTIFACTS_METHODS.each do |method_test|
    method, options = method_test.values_at(:method, :options)
42
    describe method.to_s do
43 44
      context 'has corresponding job' do
        let!(:build) do
45 46
          filename, name = options

47 48 49
          create(
            :ci_build,
            :artifacts,
50
            name: name,
51 52 53 54 55
            pipeline: pipeline,
            options: {
              artifacts: {
                paths: [filename]
              }
56
            }
57 58
          )
        end
59

60
        it { expect(pipeline.send(method)).to eq(build) }
61 62
      end

63
      context 'no corresponding job' do
64 65 66
        before do
          create(:ci_build, pipeline: pipeline)
        end
67

68
        it { expect(pipeline.send(method)).to be_nil }
69
      end
70 71
    end
  end
72

73
  %w(sast dependency_scanning dast performance sast_container container_scanning codeclimate code_quality).each do |type|
74 75 76 77 78 79 80 81 82 83 84 85 86
    method = "has_#{type}_data?"

    describe "##{method}" do
      let(:artifact) { double(success?: true) }

      before do
        allow(pipeline).to receive(:"#{type}_artifact").and_return(artifact)
      end

      it { expect(pipeline.send(method.to_sym)).to be_truthy }
    end
  end

87
  %w(sast dependency_scanning dast performance sast_container container_scanning codeclimate code_quality).each do |type|
88 89 90 91 92 93 94 95 96 97 98
    method = "expose_#{type}_data?"

    describe "##{method}" do
      before do
        allow(pipeline).to receive(:"has_#{type}_data?").and_return(true)
        allow(pipeline.project).to receive(:feature_available?).and_return(true)
      end

      it { expect(pipeline.send(method.to_sym)).to be_truthy }
    end
  end
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

  describe '#with_security_reports scope' do
    let(:pipeline_1) { create(:ci_pipeline_without_jobs, project: project) }
    let(:pipeline_2) { create(:ci_pipeline_without_jobs, project: project) }
    let(:pipeline_3) { create(:ci_pipeline_without_jobs, project: project) }
    let(:pipeline_4) { create(:ci_pipeline_without_jobs, project: project) }
    let(:pipeline_5) { create(:ci_pipeline_without_jobs, project: project) }

    before do
      create(
        :ci_build,
        :success,
        :artifacts,
        name: 'sast',
        pipeline: pipeline_1,
        options: {
          artifacts: {
            paths: [Ci::Build::SAST_FILE]
          }
        }
      )
      create(
        :ci_build,
        :success,
        :artifacts,
        name: 'dependency_scanning',
        pipeline: pipeline_2,
        options: {
          artifacts: {
            paths: [Ci::Build::DEPENDENCY_SCANNING_FILE]
          }
        }
      )
      create(
        :ci_build,
        :success,
        :artifacts,
        name: 'container_scanning',
        pipeline: pipeline_3,
        options: {
          artifacts: {
            paths: [Ci::Build::CONTAINER_SCANNING_FILE]
          }
        }
      )
      create(
        :ci_build,
        :success,
        :artifacts,
        name: 'dast',
        pipeline: pipeline_4,
        options: {
          artifacts: {
            paths: [Ci::Build::DAST_FILE]
          }
        }
      )
      create(
        :ci_build,
        :success,
        :artifacts,
        name: 'foobar',
        pipeline: pipeline_5,
        options: {
          artifacts: {
            paths: ['foobar-report.json']
          }
        }
      )
    end

    it "returns pipeline with security reports" do
      expect(described_class.with_security_reports).to eq([pipeline_1, pipeline_2, pipeline_3, pipeline_4])
    end
  end
174 175

  context 'performance' do
176
    def create_build(job_name, filename)
177 178 179 180
      create(
        :ci_build,
        :artifacts,
        name: job_name,
181 182 183 184 185 186
        pipeline: pipeline,
        options: {
          artifacts: {
            paths: [filename]
          }
        }
187 188 189 190
      )
    end

    it 'does not perform extra queries when calling pipeline artifacts methods after the first' do
191 192
      create_build('codeclimate', 'codeclimate.json')
      create_build('dependency_scanning', 'gl-dependency-scanning-report.json')
193 194 195 196 197 198

      pipeline.code_quality_artifact

      expect { pipeline.dependency_scanning_artifact }.not_to exceed_query_limit(0)
    end
  end
199
end