File: saas_feature_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (218 lines) | stat: -rw-r--r-- 8,119 bytes parent folder | download
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
215
216
217
218
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'

require_relative '../../bin/saas-feature'

RSpec.describe 'bin/saas-feature', feature_category: :feature_flags do
  using RSpec::Parameterized::TableSyntax

  let(:groups) { { geo: { label: 'group::geo' } } }

  before do
    allow(HTTParty)
      .to receive(:get)
        .with(SaasFeatureOptionParser::WWW_GITLAB_COM_GROUPS_JSON, format: :plain)
        .and_return(groups.to_json)
  end

  describe SaasFeatureCreator do
    let(:argv) { %w[saas-feature-name -g group::geo -m http://url -M 16.6] }
    let(:options) { SaasFeatureOptionParser.parse(argv) }
    let(:creator) { described_class.new(options) }
    let(:existing_saas_features) do
      { 'existing_saas_feature' => File.join('ee', 'config', 'saas_features', 'existing_saas_feature.yml') }
    end

    before do
      allow(creator).to receive(:all_saas_feature_names) { existing_saas_features }
      allow(creator).to receive(:branch_name).and_return('feature-branch')
      allow(creator).to receive(:editor).and_return(nil)

      # ignore writes
      allow(File).to receive(:write).and_return(true)

      # ignore stdin
      allow(Readline).to receive(:readline).and_raise('EOF')
    end

    subject(:execute) { creator.execute }

    it 'properly creates a SaaS feature' do
      expect(File).to receive(:write).with(
        File.join('ee', 'config', 'saas_features', 'saas_feature_name.yml'),
        anything)

      expect { execute }.to output(/name: saas_feature_name/).to_stdout
    end

    context 'when running on master' do
      it 'requires feature branch' do
        expect(creator).to receive(:branch_name).and_return('master')

        expect { execute }.to raise_error(SaasFeatureHelpers::Abort, /Create a branch first/)
      end
    end

    context 'with SaaS feature name validation' do
      where(:argv, :ex) do
        %w[.invalid.saas.feature] | /Provide a name for the SaaS feature that is/
        %w[existing-saas-feature] | /already exists!/
      end

      with_them do
        it do
          expect { execute }.to raise_error(ex)
        end
      end
    end
  end

  describe SaasFeatureOptionParser do
    describe '.parse' do
      where(:param, :argv, :result) do
        :name              | %w[foo]                                 | 'foo'
        :amend             | %w[foo --amend]                         | true
        :force             | %w[foo -f]                              | true
        :force             | %w[foo --force]                         | true
        :introduced_by_url | %w[foo -m https://url]                  | 'https://url'
        :introduced_by_url | %w[foo --introduced-by-url https://url] | 'https://url'
        :dry_run           | %w[foo -n]                              | true
        :dry_run           | %w[foo --dry-run]                       | true
        :group             | %w[foo -g group::geo]                   | 'group::geo'
        :group             | %w[foo --group group::geo]              | 'group::geo'
        :group             | %w[foo -g invalid]                      | nil
        :group             | %w[foo --group invalid]                 | nil
      end

      with_them do
        it do
          options = described_class.parse(Array(argv))

          expect(options.public_send(param)).to eq(result)
        end
      end

      it 'missing SaaS feature name' do
        expect do
          expect { described_class.parse(%w[--amend]) }.to output(/SaaS feature name is required/).to_stdout
        end.to raise_error(SaasFeatureHelpers::Abort)
      end

      it 'parses -h' do
        expect do
          expect { described_class.parse(%w[foo -h]) }.to output(/Usage:/).to_stdout
        end.to raise_error(SaasFeatureHelpers::Done)
      end
    end

    describe '.read_group' do
      before do
        allow(described_class).to receive(:fzf_available?).and_return(false)
      end

      context 'when valid group is given' do
        let(:group) { 'group::geo' }

        it 'reads group from stdin' do
          expect(Readline).to receive(:readline).and_return(group)
          expect do
            expect(described_class.read_group).to eq('group::geo')
          end.to output(/Specify the group label to which the SaaS feature belongs, from the following list/).to_stdout
        end
      end

      context 'when valid index is given' do
        it 'picks the group successfully' do
          expect(Readline).to receive(:readline).and_return('1')
          expect do
            expect(described_class.read_group).to eq('group::geo')
          end.to output(/Specify the group label to which the SaaS feature belongs, from the following list/).to_stdout
        end
      end

      context 'with invalid group given' do
        let(:type) { 'invalid' }

        it 'shows error message and retries' do
          expect(Readline).to receive(:readline).and_return(type)
          expect(Readline).to receive(:readline).and_raise('EOF')

          expect do
            expect { described_class.read_group }.to raise_error(/EOF/)
          end.to output(/Specify the group label to which the SaaS feature belongs, from the following list/).to_stdout
            .and output(/The group label isn't in the above labels list/).to_stderr
        end
      end

      context 'when invalid index is given' do
        it 'shows error message and retries' do
          expect(Readline).to receive(:readline).and_return('12')
          expect(Readline).to receive(:readline).and_raise('EOF')

          expect do
            expect { described_class.read_group }.to raise_error(/EOF/)
          end.to output(/Specify the group label to which the SaaS feature belongs, from the following list/).to_stdout
            .and output(/The group label isn't in the above labels list/).to_stderr
        end
      end
    end

    describe '.read_introduced_by_url' do
      context 'with valid URL given' do
        let(:url) { 'https://merge-request' }

        it 'reads URL from stdin' do
          expect(Readline).to receive(:readline).and_return(url)
          expect(HTTParty).to receive(:head).with(url).and_return(instance_double(HTTParty::Response, success?: true))

          expect do
            expect(described_class.read_introduced_by_url).to eq('https://merge-request')
          end.to output(/URL of the MR introducing the SaaS feature/).to_stdout
        end
      end

      context 'with invalid URL given' do
        let(:url) { 'https://invalid' }

        it 'shows error message and retries' do
          expect(Readline).to receive(:readline).and_return(url)
          expect(HTTParty).to receive(:head).with(url).and_return(instance_double(HTTParty::Response, success?: false))
          expect(Readline).to receive(:readline).and_raise('EOF')

          expect do
            expect { described_class.read_introduced_by_url }.to raise_error(/EOF/)
          end.to output(/URL of the MR introducing the SaaS feature/).to_stdout
                                                                     .and output(/URL '#{url}' isn't valid/).to_stderr
        end
      end

      context 'with empty URL given' do
        let(:url) { '' }

        it 'skips entry' do
          expect(Readline).to receive(:readline).and_return(url)

          expect do
            expect(described_class.read_introduced_by_url).to be_nil
          end.to output(/URL of the MR introducing the SaaS feature/).to_stdout
        end
      end

      context 'with a non-URL given' do
        let(:url) { 'malformed' }

        it 'shows error message and retries' do
          expect(Readline).to receive(:readline).and_return(url)
          expect(Readline).to receive(:readline).and_raise('EOF')

          expect do
            expect { described_class.read_introduced_by_url }.to raise_error(/EOF/)
          end.to output(/URL of the MR introducing the SaaS feature/).to_stdout
                                                                     .and output(/URL needs to start with/).to_stderr
        end
      end
    end
  end
end