Welcome to mirror list, hosted at ThFree Co, Russian Federation.

components_project_spec.rb « catalog « ci « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f739c244a5dda67253de2ca09c14d879dc34f24 (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::Catalog::ComponentsProject, feature_category: :pipeline_composition do
  using RSpec::Parameterized::TableSyntax

  let_it_be(:project) { create(:project, :catalog_resource_with_components) }
  let_it_be(:catalog_resource) { create(:ci_catalog_resource, project: project) }

  let(:components_project) { described_class.new(project, project.default_branch) }

  describe '#fetch_component_paths' do
    context 'when there are invalid paths' do
      let(:project) do
        create(:project, :small_repo, description: 'description',
          files: { 'templates/secrets.yml' => '',
                   'tests/test.yml' => 'this is invalid',
                   'README.md' => 'this is not ok' }
        )
      end

      it 'does not retrieve the invalid path(s) and only retrieves the valid one(s)' do
        paths = components_project.fetch_component_paths(project.default_branch)

        expect(paths).to contain_exactly('templates/secrets.yml')
      end
    end

    it 'retrieves all the valid paths for components' do
      paths = components_project.fetch_component_paths(project.default_branch)

      expect(paths).to contain_exactly(
        'templates/blank-yaml.yml', 'templates/dast/template.yml', 'templates/secret-detection.yml',
        'templates/template.yml'
      )
    end

    it 'does not fetch more paths than the limit' do
      paths = components_project.fetch_component_paths(project.default_branch, limit: 1)

      expect(paths.size).to eq(1)
    end
  end

  describe '#extract_component_name' do
    context 'with invalid component path' do
      it 'raises an error' do
        expect(components_project.extract_component_name('not-template/this-is-wrong.yml')).to be_nil
      end
    end

    context 'with valid component paths' do
      where(:path, :name) do
        'templates/secret-detection.yml'         | 'secret-detection'
        'templates/secret_detection.yml'         | 'secret_detection'
        'templates/secret_detection123.yml'      | 'secret_detection123'
        'templates/secret-detection-123.yml'     | 'secret-detection-123'
        'templates/dast/template.yml'            | 'dast'
        'templates/d-a-s_t/template.yml'         | 'd-a-s_t'
        'templates/template.yml'                 | 'template'
        'templates/blank-yaml.yml'               | 'blank-yaml'
      end

      with_them do
        it 'extracts the component name from the path' do
          expect(components_project.extract_component_name(path)).to eq(name)
        end
      end
    end
  end

  describe '#extract_inputs' do
    context 'with valid inputs' do
      it 'extracts the inputs from a blob' do
        blob = "spec:\n inputs:\n  website:\n---\nimage: alpine_1"

        expect(components_project.extract_inputs(blob)).to eq({ website: nil })
      end
    end

    context 'with invalid inputs' do
      it 'raises InvalidFormatError' do
        blob = "spec:\n inputs:\n  website:\n---\nsome: invalid: string"

        expect do
          components_project.extract_inputs(blob)
        end.to raise_error(::Gitlab::Config::Loader::FormatError,
          /mapping values are not allowed in this context/)
      end
    end
  end

  describe '#fetch_component' do
    where(:component_name, :content, :path) do
      'secret-detection' | "spec:\n inputs:\n  website:\n---\nimage: alpine_1" | 'templates/secret-detection.yml'
      'dast'             | 'image: alpine_2'                                   | 'templates/dast/template.yml'
      'template'         | 'image: alpine_3'                                   | 'templates/template.yml'
      'blank-yaml'       | ''                                                  | 'templates/blank-yaml.yml'
      'non/exist'        | nil                                                 | nil
    end

    with_them do
      it 'fetches the content for a component' do
        data = components_project.fetch_component(component_name)

        expect(data.path).to eq(path)
        expect(data.content).to eq(content)
      end
    end
  end
end