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

redis_cluster_validator_spec.rb « instrumentation « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58c75bff9dd90f0d2ea8ca8bd9e11c466d8020f3 (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
# 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
      :mget | [] | false # This is invalid, but not because it's a cross-slot 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

    where(:arguments, :should_raise) do
      [[:get, "foo"], [:get, "bar"]] | true
      [[:get, "foo"], [:mget, "foo", "bar"]] | true # mix of single-key and multi-key cmds
      [[:get, "{foo}:name"], [:get, "{foo}:profile"]] | false
      [[:del, "foo"], [:del, "bar"]] | true
      [] | false # pipeline or transaction opened and closed without ops
    end

    with_them do
      it do
        if should_raise
          expect { described_class.validate!(arguments) }
            .to raise_error(described_class::CrossSlotError)
        else
          expect { described_class.validate!(arguments) }.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