summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/shared_contexts/variable_inheritance_shared_context.rb
blob: 45caeced35ce540be256d9ba982a164149a2ef2d (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
# frozen_string_literal: true

module QA
  RSpec.shared_context 'variable inheritance test prep' do
    let(:key) { 'TEST_VAR' }
    let(:value) { 'This is great!' }
    let(:random_string) { Faker::Alphanumeric.alphanumeric(number: 8) }

    let(:group) do
      Resource::Group.fabricate_via_api! do |group|
        group.path = "group-for-variable-inheritance-#{random_string}"
      end
    end

    let(:upstream_project) do
      Resource::Project.fabricate_via_api! do |project|
        project.group = group
        project.name = 'upstream-variable-inheritance'
        project.description = 'Project for pipeline with variable defined via UI - Upstream'
      end
    end

    let(:downstream1_project) do
      Resource::Project.fabricate_via_api! do |project|
        project.group = group
        project.name = 'downstream1-variable-inheritance'
        project.description = 'Project for pipeline with variable defined via UI - Downstream'
      end
    end

    let(:downstream2_project) do
      Resource::Project.fabricate_via_api! do |project|
        project.group = group
        project.name = 'downstream2-variable-inheritance'
        project.description = 'Project for pipeline with variable defined via UI - Downstream'
      end
    end

    let!(:runner) do
      Resource::Runner.fabricate! do |runner|
        runner.token = group.reload!.runners_token
        runner.name = random_string
        runner.tags = [random_string]
      end
    end

    before do
      Flow::Login.sign_in
    end

    after do
      runner.remove_via_api!
    end

    def start_pipeline_with_variable
      upstream_project.visit!
      Flow::Pipeline.wait_for_latest_pipeline
      Page::Project::Pipeline::Index.perform(&:click_run_pipeline_button)
      Page::Project::Pipeline::New.perform do |new|
        new.configure_variable(key: key, value: value)
        new.click_run_pipeline_button
      end
    end

    def wait_for_pipelines
      Support::Waiter.wait_until(max_duration: 300, sleep_interval: 10) do
        upstream_pipeline.status == 'success' &&
          downstream_pipeline(downstream1_project, 'downstream1_trigger').status == 'success'
      end
    end

    def add_ci_file(project, files)
      Resource::Repository::Commit.fabricate_via_api! do |commit|
        commit.project = project
        commit.commit_message = 'Add CI config file'
        commit.add_files(files)
      end
    end

    def visit_job_page(pipeline_title, job_name)
      Page::Project::Pipeline::Show.perform do |show|
        show.expand_child_pipeline(title: pipeline_title)
        show.click_job(job_name)
      end
    end

    def verify_job_log_shows_variable_value
      Page::Project::Job::Show.perform do |show|
        show.wait_until { show.successful? }
        expect(show.output).to have_content(value)
      end
    end

    def verify_job_log_does_not_show_variable_value
      Page::Project::Job::Show.perform do |show|
        show.wait_until { show.successful? }
        expect(show.output).to have_no_content(value)
      end
    end

    def upstream_pipeline
      Resource::Pipeline.fabricate_via_api! do |pipeline|
        pipeline.project = upstream_project
        pipeline.id = upstream_project.pipelines.first[:id]
      end
    end

    def downstream_pipeline(project, bridge_name)
      Resource::Pipeline.fabricate_via_api! do |pipeline|
        pipeline.project = project
        pipeline.id = upstream_pipeline.downstream_pipeline_id(bridge_name: bridge_name)
      end
    end

    def upstream_child1_ci_file
      {
        file_path: '.child1-ci.yml',
        content: <<~YAML
            child1_job:
              stage: test
              tags: ["#{random_string}"]
              script:
                - echo $TEST_VAR
                - echo Done!
        YAML
      }
    end

    def upstream_child2_ci_file
      {
        file_path: '.child2-ci.yml',
        content: <<~YAML
            child2_job:
              stage: test
              tags: ["#{random_string}"]
              script:
                - echo $TEST_VAR
                - echo Done!
        YAML
      }
    end

    def downstream1_ci_file
      {
        file_path: '.gitlab-ci.yml',
        content: <<~YAML
            downstream1_job:
              stage: deploy
              tags: ["#{random_string}"]
              script:
                - echo $TEST_VAR
                - echo Done!
        YAML
      }
    end

    def downstream2_ci_file
      {
        file_path: '.gitlab-ci.yml',
        content: <<~YAML
            downstream2_job:
              stage: deploy
              tags: ["#{random_string}"]
              script:
                - echo $TEST_VAR
                - echo Done!
        YAML
      }
    end
  end
end