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

file_variable_downstream_pipeline_spec.rb « 4_verify « api « features « specs « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c95a1f9fcd08cf90382bc9b4d9978e1253909b82 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# frozen_string_literal: true

module QA
  RSpec.describe 'Verify', :runner, product_group: :pipeline_security,
    feature_flag: { name: 'ci_prevent_file_var_expansion_downstream_pipeline', scope: :project },
    only: { subdomain: 'staging-canary' } do
    # Runs this test only in staging-canary to debug flakiness https://gitlab.com/gitlab-org/gitlab/-/issues/424903
    # We need to collect failure data, please don't quarantine for the time being
    describe 'Pipeline with file variables and downstream pipelines' do
      let(:random_string) { Faker::Alphanumeric.alphanumeric(number: 8) }
      let(:executor) { "qa-runner-#{Faker::Alphanumeric.alphanumeric(number: 8)}" }
      let!(:upstream_project) { create(:project, name: 'upstream-project-with-file-variables') }
      let!(:downstream_project) { create(:project, name: 'downstream-project') }

      let!(:upstream_project_runner) do
        Resource::ProjectRunner.fabricate! do |runner|
          runner.project = upstream_project
          runner.name = executor
          runner.tags = [executor]
        end
      end

      let!(:downstream_project_runner) do
        Resource::ProjectRunner.fabricate! do |runner|
          runner.project = downstream_project
          runner.name = "#{executor}-downstream"
          runner.tags = [executor]
        end
      end

      let(:upstream_project_files) do
        [
          {
            action: 'create',
            file_path: '.gitlab-ci.yml',
            content: <<~YAML
                  default:
                    tags: [#{executor}]

                  variables:
                    EXTRA_ARGS: "-f $TEST_PROJECT_FILE"
                    DOCKER_REMOTE_ARGS: --tlscacert="$DOCKER_CA_CERT"
                    EXTRACTED_CRT_FILE: ${DOCKER_CA_CERT}.crt
                    MY_FILE_VAR: $TEST_PROJECT_FILE

                  trigger_child:
                    trigger:
                      strategy: depend
                      include:
                        - local: child.yml

                  trigger_downstream_project:
                    trigger:
                      strategy: depend
                      project: #{downstream_project.path_with_namespace}

            YAML
          },
          {
            action: 'create',
            file_path: 'child.yml',
            content: <<~YAML
                  default:
                    tags: [#{executor}]

                  child_job_echo:
                    script:
                      - echo "run something $EXTRA_ARGS"
                      - echo "docker run $DOCKER_REMOTE_ARGS"
                      - echo "run --output=$EXTRACTED_CRT_FILE"
                      - echo "Will read private key from $MY_FILE_VAR"

                  child_job_cat:
                    script:
                      - cat "$MY_FILE_VAR"
                      - cat "$DOCKER_CA_CERT"
            YAML
          }
        ]
      end

      let(:downstream_project_file) do
        [
          {
            action: 'create',
            file_path: '.gitlab-ci.yml',
            content: <<~YAML
                  default:
                    tags: [#{executor}]

                  downstream_job_echo:
                    script:
                      - echo "run something $EXTRA_ARGS"
                      - echo "docker run $DOCKER_REMOTE_ARGS"
                      - echo "run --output=$EXTRACTED_CRT_FILE"
                      - echo "Will read private key from $MY_FILE_VAR"

                  downstream_job_cat:
                    script:
                      - cat "$MY_FILE_VAR"
                      - cat "$DOCKER_CA_CERT"
            YAML
          }
        ]
      end

      around do |example|
        Runtime::Feature.enable(:ci_prevent_file_var_expansion_downstream_pipeline, project: upstream_project)
        example.run
        Runtime::Feature.disable(:ci_prevent_file_var_expansion_downstream_pipeline, project: upstream_project)
      end

      before do
        add_file_variables_to_upstream_project
        add_ci_file(downstream_project, downstream_project_file)
        add_ci_file(upstream_project, upstream_project_files)
        Support::Waiter.wait_until(message: 'Wait for first pipeline creation') { upstream_project.pipelines.present? }

        wait_for_pipelines_to_finish
      end

      after do
        [upstream_project_runner, downstream_project_runner].each(&:remove_via_api!)
      end

      it(
        'creates variable with file path in downstream pipelines and can read file variable content',
        testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/416337'
      ) do
        child_echo_job = create(:job, project: upstream_project,
          id: upstream_project.job_by_name('child_job_echo')[:id])

        child_cat_job = create(:job, project: upstream_project, id: upstream_project.job_by_name('child_job_cat')[:id])

        downstream_project_echo_job = create(:job,
          project: downstream_project,
          id: downstream_project.job_by_name('downstream_job_echo')[:id])

        downstream_project_cat_job = create(:job,
          project: downstream_project,
          id: downstream_project.job_by_name('downstream_job_cat')[:id])

        aggregate_failures do
          trace = child_echo_job.trace
          expect(trace).to include('run something -f', "#{upstream_project.name}.tmp/TEST_PROJECT_FILE")
          expect(trace).to include('docker run --tlscacert=', "#{upstream_project.name}.tmp/DOCKER_CA_CERT")
          expect(trace).to include('run --output=', "#{upstream_project.name}.tmp/DOCKER_CA_CERT.crt")
          expect(trace).to include('Will read private key from', "#{upstream_project.name}.tmp/TEST_PROJECT_FILE")

          trace = child_cat_job.trace
          expect(trace).to have_content('hello, this is test')
          expect(trace).to have_content('This is secret')

          trace = downstream_project_echo_job.trace
          expect(trace).to include('run something -f', "#{downstream_project.name}.tmp/TEST_PROJECT_FILE")
          expect(trace).to include('docker run --tlscacert=', "#{downstream_project.name}.tmp/DOCKER_CA_CERT")
          expect(trace).to include('run --output=', "#{downstream_project.name}.tmp/DOCKER_CA_CERT.crt")
          expect(trace).to include('Will read private key from', "#{downstream_project.name}.tmp/TEST_PROJECT_FILE")

          trace = downstream_project_cat_job.trace
          expect(trace).to have_content('hello, this is test')
          expect(trace).to have_content('This is secret')
        end
      end

      private

      def add_file_variables_to_upstream_project
        {
          'TEST_PROJECT_FILE' => "hello, this is test\n",
          'DOCKER_CA_CERT' => "This is secret\n"
        }.each do |file_name, content|
          Resource::CiVariable.fabricate_via_api! do |ci_variable|
            ci_variable.project = upstream_project
            ci_variable.key = file_name
            ci_variable.value = content
            ci_variable.variable_type = 'file'
          end
        end
      end

      def add_ci_file(project, files)
        create(:commit, project: project, commit_message: 'Add CI files to project', actions: files)
      end

      def wait_for_pipelines_to_finish
        Support::Waiter.wait_until(max_duration: 300, sleep_interval: 10) do
          upstream_pipeline.status == 'success' &&
            child_pipeline.status == 'success' &&
            downstream_project_pipeline.status == 'success'
        end
      end

      # Fetch upstream project's parent pipeline
      def upstream_pipeline
        create(:pipeline, project: upstream_project, id: upstream_project.latest_pipeline[:id])
      end

      # Fetch upstream project's child pipeline
      def child_pipeline
        create(:pipeline,
          project: upstream_project,
          id: upstream_pipeline.downstream_pipeline_id(bridge_name: 'trigger_child'))
      end

      # Fetch downstream project's pipeline
      def downstream_project_pipeline
        create(:pipeline,
          project: downstream_project,
          id: upstream_pipeline.downstream_pipeline_id(bridge_name: 'trigger_downstream_project'))
      end
    end
  end
end