File: commands_on_cluster_test.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (159 lines) | stat: -rw-r--r-- 5,293 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
# frozen_string_literal: true

require "helper"

# ruby -w -Itest test/cluster_commands_on_cluster_test.rb
# @see https://redis.io/commands#cluster
class TestClusterCommandsOnCluster < Minitest::Test
  include Helper::Cluster

  def test_cluster_addslots
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER ADDSLOTS command should be...') do
      redis.cluster(:addslots, 0, 1, 2)
    end
  end

  def test_cluster_count_failure_reports
    assert_raises(Redis::CommandError, 'ERR Unknown node unknown-node-id') do
      redis.cluster('count-failure-reports', 'unknown-node-id')
    end

    node_id = redis.cluster(:nodes).first.fetch('node_id')
    assert_equal true, (redis.cluster('count-failure-reports', node_id) >= 0)
  end

  def test_cluster_countkeysinslot
    assert_equal true, (redis.cluster(:countkeysinslot, 0) >= 0)
    assert_equal true, (redis.cluster(:countkeysinslot, 16_383) >= 0)

    assert_raises(Redis::CommandError, 'ERR Invalid slot') do
      redis.cluster(:countkeysinslot, -1)
    end

    assert_raises(Redis::CommandError, 'ERR Invalid slot') do
      redis.cluster(:countkeysinslot, 16_384)
    end
  end

  def test_cluster_delslots
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER DELSLOTS command should be...') do
      redis.cluster(:delslots, 0, 1, 2)
    end
  end

  def test_cluster_failover
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER FAILOVER command should be...') do
      redis.cluster(:failover, 'FORCE')
    end
  end

  def test_cluster_forget
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER FORGET command should be...') do
      redis.cluster(:forget, 'unknown-node-id')
    end
  end

  def test_cluster_getkeysinslot
    assert_instance_of Array, redis.cluster(:getkeysinslot, 0, 3)
  end

  def test_cluster_info
    info = redis.cluster(:info)

    assert_equal '3', info.fetch('cluster_size')
  end

  def test_cluster_meet
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER MEET command should be...') do
      redis.cluster(:meet, '127.0.0.1', 11_211)
    end
  end

  def test_cluster_nodes
    cluster_nodes = redis.cluster(:nodes)
    sample_node = cluster_nodes.first

    assert_equal 6, cluster_nodes.length
    assert_equal true, sample_node.key?('node_id')
    assert_equal true, sample_node.key?('ip_port')
    assert_equal true, sample_node.key?('flags')
    assert_equal true, sample_node.key?('master_node_id')
    assert_equal true, sample_node.key?('ping_sent')
    assert_equal true, sample_node.key?('pong_recv')
    assert_equal true, sample_node.key?('config_epoch')
    assert_equal true, sample_node.key?('link_state')
    assert_equal true, sample_node.key?('slots')
  end

  def test_cluster_replicate
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER REPLICATE command should be...') do
      redis.cluster(:replicate)
    end
  end

  def test_cluster_reset
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER RESET command should be...') do
      redis.cluster(:reset)
    end
  end

  def test_cluster_saveconfig
    assert_equal 'OK', redis.cluster(:saveconfig)
  end

  def test_cluster_set_config_epoch
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER SET-CONFIG-EPOCH command should be...') do
      redis.cluster('set-config-epoch')
    end
  end

  def test_cluster_setslot
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER SETSLOT command should be...') do
      redis.cluster(:setslot)
    end
  end

  def test_cluster_slaves
    cluster_nodes = redis.cluster(:nodes)

    sample_master_node_id = cluster_nodes.find { |n| n.fetch('master_node_id') == '-' }.fetch('node_id')
    sample_slave_node_id = cluster_nodes.find { |n| n.fetch('master_node_id') != '-' }.fetch('node_id')

    assert_equal 'slave', redis.cluster(:slaves, sample_master_node_id).first.fetch('flags').first
    assert_raises(Redis::CommandError, 'ERR The specified node is not a master') do
      redis.cluster(:slaves, sample_slave_node_id)
    end
  end

  def test_cluster_slots
    slots = redis.cluster(:slots)
    sample_slot = slots.first

    assert_equal 3, slots.length
    assert_equal true, sample_slot.key?('start_slot')
    assert_equal true, sample_slot.key?('end_slot')
    assert_equal true, sample_slot.key?('master')
    assert_equal true, sample_slot.fetch('master').key?('ip')
    assert_equal true, sample_slot.fetch('master').key?('port')
    assert_equal true, sample_slot.fetch('master').key?('node_id')
    assert_equal true, sample_slot.key?('replicas')
    assert_equal true, sample_slot.fetch('replicas').is_a?(Array)
    sample_slot.fetch('replicas').each do |replica|
      assert_equal true, replica.key?('ip')
      assert_equal true, replica.key?('port')
      assert_equal true, replica.key?('node_id')
    end
  end

  def test_readonly
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'READONLY command should be...') do
      redis.readonly
    end
  end

  def test_readwrite
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'READWRITE command should be...') do
      redis.readwrite
    end
  end
end