summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/instrumentation/redis_cluster_validator_spec.rb
blob: 2ca7465e775aff3efd42b959557dd580e61132d4 (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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'support/helpers/rails_helpers'
require 'rspec-parameterized'

RSpec.describe Gitlab::Instrumentation::RedisClusterValidator do
  include RailsHelpers

  describe '.validate!' do
    using RSpec::Parameterized::TableSyntax

    context 'Rails environments' do
      where(:env, :should_raise) do
        'production' | false
        'staging' | false
        'development' | true
        'test' | true
      end

      with_them do
        it do
          stub_rails_env(env)

          args = [:mget, 'foo', 'bar']

          if should_raise
            expect { described_class.validate!(args) }
              .to raise_error(described_class::CrossSlotError)
          else
            expect { described_class.validate!(args) }.not_to raise_error
          end
        end
      end
    end

    where(:command, :arguments, :should_raise) do
      :rename | %w(foo bar) | true
      :RENAME | %w(foo bar) | true
      'rename' | %w(foo bar) | true
      'RENAME' | %w(foo bar) | true
      :rename | %w(iaa ahy) | false # 'iaa' and 'ahy' hash to the same slot
      :rename | %w({foo}:1 {foo}:2) | false
      :rename | %w(foo foo bar) | false # This is not a valid command but should not raise here
      :mget | %w(foo bar) | true
      :mget | %w(foo foo bar) | true
      :mget | %w(foo foo) | false
      :blpop | %w(foo bar 1) | true
      :blpop | %w(foo foo 1) | false
      :mset | %w(foo a bar a) | true
      :mset | %w(foo a foo a) | false
      :del | %w(foo bar) | true
      :del | [%w(foo bar)] | true # Arguments can be a nested array
      :del | %w(foo foo) | false
      :hset | %w(foo bar) | false # Not a multi-key command
    end

    with_them do
      it do
        args = [command] + arguments

        if should_raise
          expect { described_class.validate!(args) }
            .to raise_error(described_class::CrossSlotError)
        else
          expect { described_class.validate!(args) }.not_to raise_error
        end
      end
    end
  end

  describe '.allow_cross_slot_commands' do
    it 'does not raise for invalid arguments' do
      expect do
        described_class.allow_cross_slot_commands do
          described_class.validate!([:mget, 'foo', 'bar'])
        end
      end.not_to raise_error
    end

    it 'allows nested invocation' do
      expect do
        described_class.allow_cross_slot_commands do
          described_class.allow_cross_slot_commands do
            described_class.validate!([:mget, 'foo', 'bar'])
          end

          described_class.validate!([:mget, 'foo', 'bar'])
        end
      end.not_to raise_error
    end
  end
end