File: application_settings_analysis_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 (224 lines) | stat: -rw-r--r-- 7,884 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
219
220
221
222
223
224
# frozen_string_literal: true

require 'fast_spec_helper'
require 'gitlab/rspec/all'
require_relative '../../../scripts/cells/application-settings-analysis'

RSpec.describe ApplicationSettingsAnalysis, feature_category: :tooling do
  let(:stdout) { StringIO.new }

  subject(:analyzer) { described_class.new(stdout: stdout) }

  describe described_class::ApplicationSetting do
    let(:attr) { 'default_branch_name' }
    let(:definition_file_path) { File.expand_path("../../../config/application_setting_columns/#{attr}.yml", __dir__) }
    let(:definition_file_exist) { true }
    let(:definition) { { column: 'default_branch_name', db_type: 'fake type', clusterwide: true } }

    subject(:application_setting) do
      described_class.new(column: 'default_branch_name', db_type: 'text', clusterwide: true)
    end

    before do
      allow(File).to receive(:exist?).and_call_original
      allow(File).to receive(:exist?).with(definition_file_path).and_return(definition_file_exist)
      allow(YAML).to receive(:safe_load_file).with(definition_file_path).and_return(definition)
    end

    describe '#initialize' do
      it 'does not override codebase fields' do
        expect(application_setting.column).to eq('default_branch_name')
        expect(application_setting.db_type).to eq('text')
      end

      context 'when definition file does not' do
        let(:definition_file_exist) { false }

        it 'is not a problem' do
          expect { application_setting }.not_to raise_error
        end
      end
    end

    describe '#definition_file_path' do
      it 'returns the definition file path' do
        expect(application_setting.definition_file_path)
          .to eq(File.expand_path("../../../config/application_setting_columns/#{attr}.yml", __dir__))
      end
    end

    describe '#definition_file_exist?' do
      it 'returns true when the definition file exists' do
        expect(File).to receive(:exist?).with(definition_file_path).and_return(definition_file_exist)

        expect(application_setting.definition_file_exist?).to eq(definition_file_exist)
      end
    end
  end

  describe '#attributes' do
    subject(:attributes) { analyzer.attributes }

    describe 'return value' do
      it 'returns an array of described_class::ApplicationSetting' do
        expect(attributes).to all(be_a(described_class::ApplicationSetting))
      end
    end

    describe 'non-encrypted attribute' do
      it 'returns non-encrypted attributes from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'default_branch_name' }

        expect(setting).to be_present
      end
    end

    describe 'DB type' do
      it 'stores the column type from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'snippet_size_limit' }

        expect(setting.db_type).to eq('bigint')
      end
    end

    describe 'API type' do
      it 'fetches the API type from doc/api/settings.md' do
        setting = attributes.find { |attr| attr.column == 'snippet_size_limit' }

        expect(setting.api_type).to eq('integer')
      end
    end

    describe 'attr_encrypted columns' do
      it 'returns encrypted attribute columns from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'encrypted_external_auth_client_key' }
        encryption_iv_column = attributes
          .find { |attr| attr.column == 'encrypted_external_auth_client_key_iv' }

        expect(setting.attr).to eq('external_auth_client_key') # `encrypted_` prefix is removed
        expect(setting.encrypted).to eq(true)
        expect(encryption_iv_column).to be_nil # `*_iv` column aren't listed as it's an implementation detail
      end
    end

    describe 'TokenAuthenticatable columns' do
      it 'returns encrypted attribute columns from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'runners_registration_token_encrypted' }

        expect(setting.attr).to eq('runners_registration_token') # `_encrypted` suffix is removed
        expect(setting.encrypted).to eq(true)
      end
    end

    describe 'column `not null`' do
      it 'stores the column `not null` from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'snippet_size_limit' }

        expect(setting.not_null).to eq(true)
      end
    end

    describe 'column default' do
      it 'stores the column default from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'snippet_size_limit' }

        expect(setting.default).to eq('52428800')
      end
    end

    describe 'attributes different than default on GitLab.com' do
      it 'marks settings that have a different value than default set on GitLab.com' do
        setting = attributes.find { |attr| attr.column == 'zoekt_settings' }

        expect(setting.gitlab_com_different_than_default).to eq(true)
      end
    end

    describe 'attribute description' do
      it 'fetches attribute description from doc/api/settings.md' do
        setting = attributes.find { |attr| attr.column == 'commit_email_hostname' }

        expect(setting.description).to eq('Custom hostname (for private commit emails).')
      end
    end

    describe 'JiHu-specific columns' do
      it 'fetches JiHu-specific columns from db/structure.sql' do
        setting = attributes.find { |attr| attr.column == 'content_validation_endpoint_url' }

        expect(setting.jihu).to eq(true)
      end
    end

    describe 'HTML caching column' do
      it 'does not return _html-suffixed columns from db/structure.sql' do
        setting = attributes.find { |attr| attr.column.end_with?('_html') }

        expect(setting).to be_nil
      end
    end

    describe 'definition file' do
      it 'returns true when an attribute has an existing definition file' do
        setting = attributes.find { |attr| attr.column == 'commit_email_hostname' }

        expect(setting.definition_file_exist?).to eq(true)
      end
    end
  end

  describe '#execute' do
    before do
      allow(File).to receive(:write)
      allow(stdout).to receive(:puts)
    end

    it 'works without issues' do
      analyzer.execute
    end

    context 'when API type is not compatible with DB type' do
      let(:documentation_api_settings) do
        [described_class::ApplicationSettingApiDoc.new(attr: 'default_branch_name', api_type: 'integer')]
      end

      before do
        allow(analyzer).to receive(:documentation_api_settings).and_return(documentation_api_settings)
      end

      it 'raises an error' do
        expect { analyzer.execute }.to raise_error("`default_branch_name`: Documented type `integer` " \
          "isn't compatible with actual DB type `text`!")
      end
    end

    context 'when a definition file exists for an attribute that does not exist anymore' do
      let(:fake_attribute_definition_file_path) do
        File.expand_path("../../../config/application_setting_columns/fake_attribute.yml", __dir__)
      end

      before do
        allow(File).to receive(:unlink).and_call_original
      end

      it 'deletes the definition file' do
        # #definition_files is memoized so the stub it directly
        allow(described_class).to receive(:definition_files).and_return([fake_attribute_definition_file_path])
        expect(stdout).to receive(:puts).with(
          "Deleting #{fake_attribute_definition_file_path} since the fake_attribute attribute doesn't exist anymore."
        )
        expect(File).to receive(:unlink).with(fake_attribute_definition_file_path).and_return(1)

        analyzer.execute
      end
    end
  end

  describe '.definition_files' do
    it 'returns all definition files' do
      expect(described_class.definition_files).to eq(
        Dir.glob(File.expand_path("../../../config/application_setting_columns/*.yml", __dir__))
      )
    end
  end
end