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

lint_spec.rb « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3892df273bd51f8ca093291dba3cb56b0c9f1ece (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Lint do
  describe 'POST /ci/lint' do
    context 'with valid .gitlab-ci.yaml content' do
      let(:yaml_content) do
        File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
      end

      it 'passes validation' do
        post api('/ci/lint'), params: { content: yaml_content }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to be_an Hash
        expect(json_response['status']).to eq('valid')
        expect(json_response['errors']).to eq([])
      end

      it 'outputs expanded yaml content' do
        post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to have_key('merged_yaml')
      end
    end

    context 'with an invalid .gitlab_ci.yml' do
      context 'with invalid syntax' do
        let(:yaml_content) { 'invalid content' }

        it 'responds with errors about invalid syntax' do
          post api('/ci/lint'), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['status']).to eq('invalid')
          expect(json_response['errors']).to eq(['Invalid configuration format'])
        end

        it 'outputs expanded yaml content' do
          post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to have_key('merged_yaml')
        end
      end

      context 'with invalid configuration' do
        let(:yaml_content) { '{ image: "ruby:2.7",  services: ["postgres"] }' }

        it 'responds with errors about invalid configuration' do
          post api('/ci/lint'), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['status']).to eq('invalid')
          expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
        end

        it 'outputs expanded yaml content' do
          post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to have_key('merged_yaml')
        end
      end
    end

    context 'without the content parameter' do
      it 'responds with validation error about missing content' do
        post api('/ci/lint')

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response['error']).to eq('content is missing')
      end
    end
  end

  describe 'GET /projects/:id/ci/lint' do
    subject(:ci_lint) { get api("/projects/#{project.id}/ci/lint", api_user), params: { dry_run: dry_run } }

    let_it_be(:api_user) { create(:user) }
    let(:project) { create(:project, :repository) }
    let(:dry_run) { nil }

    RSpec.shared_examples 'valid config' do
      it 'passes validation' do
        ci_lint

        included_config = YAML.safe_load(included_content, [Symbol])
        root_config = YAML.safe_load(yaml_content, [Symbol])
        expected_yaml = included_config.merge(root_config).except(:include).to_yaml

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to be_an Hash
        expect(json_response['merged_yaml']).to eq(expected_yaml)
        expect(json_response['valid']).to eq(true)
        expect(json_response['errors']).to eq([])
      end
    end

    RSpec.shared_examples 'invalid config' do
      it 'responds with errors about invalid configuration' do
        ci_lint

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['merged_yaml']).to eq(yaml_content)
        expect(json_response['valid']).to eq(false)
        expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
      end
    end

    context 'when unauthenticated' do
      it 'returns authentication error' do
        ci_lint

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when authenticated as project member' do
      before do
        project.add_developer(api_user)
      end

      context 'with valid .gitlab-ci.yml content' do
        let(:yaml_content) do
          { include: { local: 'another-gitlab-ci.yml' }, test: { stage: 'test', script: 'echo 1' } }.to_yaml
        end

        let(:included_content) do
          { another_test: { stage: 'test', script: 'echo 1' } }.to_yaml
        end

        before do
          project.repository.create_file(
            project.creator,
            '.gitlab-ci.yml',
            yaml_content,
            message: 'Automatically created .gitlab-ci.yml',
            branch_name: 'master'
          )

          project.repository.create_file(
            project.creator,
            'another-gitlab-ci.yml',
            included_content,
            message: 'Automatically created another-gitlab-ci.yml',
            branch_name: 'master'
          )
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'valid config'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'valid config'
        end
      end

      context 'with invalid .gitlab-ci.yml content' do
        let(:yaml_content) do
          { image: 'ruby:2.7', services: ['postgres'] }.to_yaml
        end

        before do
          stub_ci_pipeline_yaml_file(yaml_content)
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'invalid config'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'invalid config'
        end
      end
    end
  end
end