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

reset_tokens_spec.rb « doctor « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0cc947efdb4797f5fa5075507964865de9180450 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Doctor::ResetTokens, feature_category: :runner_fleet do
  let(:logger) { instance_double('Logger') }
  let(:model_names) { %w[Project Group] }
  let(:token_names) { %w[runners_token] }
  let(:dry_run) { false }
  let(:doctor) { described_class.new(logger, model_names: model_names, token_names: token_names, dry_run: dry_run) }

  let_it_be(:functional_project) { create(:project).tap(&:runners_token) }
  let_it_be(:functional_group) { create(:group).tap(&:runners_token) }

  let(:broken_project) { create(:project).tap { |project| project.update_columns(runners_token_encrypted: 'aaa') } }
  let(:project_with_cipher_error) do
    create(:project).tap do |project|
      project.update_columns(
        runners_token_encrypted: '|rXs75DSHXPE9MGAIgyxcut8pZc72gaa/2ojU0GS1+R+cXNqkbUB13Vb5BaMwf47d98980fc1')
    end
  end

  let(:broken_group) { create(:group, runners_token_encrypted: 'aaa') }

  subject(:run!) do
    expect(logger).to receive(:info).with(
      "Resetting #{token_names.join(', ')} on #{model_names.join(', ')} if they can not be read"
    )
    expect(logger).to receive(:info).with('Done!')
    doctor.run!
  end

  before do
    allow(logger).to receive(:info).with(%r{Checked \d/\d Projects})
    allow(logger).to receive(:info).with(%r{Checked \d Projects})
    allow(logger).to receive(:info).with(%r{Checked \d/\d Groups})
    allow(logger).to receive(:info).with(%r{Checked \d Groups})
  end

  it 'fixes broken project and not the functional project' do
    expect(logger).to receive(:debug).with("> Fix Project[#{broken_project.id}].runners_token")

    expect { run! }.to change { broken_project.reload.runners_token_encrypted }.from('aaa')
      .and not_change { functional_project.reload.runners_token_encrypted }
    expect { broken_project.runners_token }.not_to raise_error
  end

  it 'fixes project with cipher error' do
    expect { project_with_cipher_error.runners_token }.to raise_error(OpenSSL::Cipher::CipherError)
    expect(logger).to receive(:debug).with("> Fix Project[#{project_with_cipher_error.id}].runners_token")

    expect { run! }.to change { project_with_cipher_error.reload.runners_token_encrypted }
    expect { project_with_cipher_error.runners_token }.not_to raise_error
  end

  it 'fixes broken group and not the functional group' do
    expect(logger).to receive(:debug).with("> Fix Group[#{broken_group.id}].runners_token")

    expect { run! }.to change { broken_group.reload.runners_token_encrypted }.from('aaa')
      .and not_change { functional_group.reload.runners_token_encrypted }

    expect { broken_group.runners_token }.not_to raise_error
  end

  context 'when one model specified' do
    let(:model_names) { %w[Project] }

    it 'fixes broken project' do
      expect(logger).to receive(:debug).with("> Fix Project[#{broken_project.id}].runners_token")

      expect { run! }.to change { broken_project.reload.runners_token_encrypted }.from('aaa')
      expect { broken_project.runners_token }.not_to raise_error
    end

    it 'does not fix other models' do
      expect { run! }.not_to change { broken_group.reload.runners_token_encrypted }.from('aaa')
    end
  end

  context 'when non-existing token field is given' do
    let(:token_names) { %w[nonexisting_token] }

    it 'does not fix anything' do
      expect { run! }.not_to change { broken_project.reload.runners_token_encrypted }.from('aaa')
    end
  end

  context 'when executing in a dry-run mode' do
    let(:dry_run) { true }

    it 'prints info about fixed project, but does not actually do anything' do
      expect(logger).to receive(:info).with('Executing in DRY RUN mode, no records will actually be updated')
      expect(logger).to receive(:debug).with("> Fix Project[#{broken_project.id}].runners_token")

      expect { run! }.not_to change { broken_project.reload.runners_token_encrypted }.from('aaa')
      expect { broken_project.runners_token }.to raise_error(TypeError)
    end
  end

  it 'prints progress along the way' do
    stub_const('Gitlab::Doctor::ResetTokens::PRINT_PROGRESS_EVERY', 1)

    broken_project
    project_with_cipher_error

    expect(logger).to receive(:info).with(
      "Resetting #{token_names.join(', ')} on #{model_names.join(', ')} if they can not be read"
    )
    expect(logger).to receive(:info).with('Checked 1/3 Projects')
    expect(logger).to receive(:debug).with("> Fix Project[#{broken_project.id}].runners_token")
    expect(logger).to receive(:info).with('Checked 2/3 Projects')
    expect(logger).to receive(:debug).with("> Fix Project[#{project_with_cipher_error.id}].runners_token")
    expect(logger).to receive(:info).with('Checked 3/3 Projects')
    expect(logger).to receive(:info).with('Done!')

    doctor.run!
  end

  it "prints 'Something went wrong' error when encounters unexpected exception, but continues" do
    broken_project
    project_with_cipher_error

    expect(logger).to receive(:debug).with(
      "> Something went wrong for Project[#{broken_project.id}].runners_token: Error message")
    expect(logger).to receive(:debug).with("> Fix Project[#{project_with_cipher_error.id}].runners_token")

    expect(broken_project).to receive(:runners_token).and_raise("Error message")
    expect(Project).to receive(:find_each).and_return([broken_project, project_with_cipher_error].each)

    expect { run! }.to not_change { broken_project.reload.runners_token_encrypted }.from('aaa')
      .and change { project_with_cipher_error.reload.runners_token_encrypted }
  end
end