|
| 1 | +package raft |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/raft" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSplitPeerAddr(t *testing.T) { |
| 13 | + specs := map[string]struct { |
| 14 | + in string |
| 15 | + exp raft.Server |
| 16 | + expErr error |
| 17 | + }{ |
| 18 | + "valid": { |
| 19 | + in: "node1@127.0.0.1:1234", |
| 20 | + exp: raft.Server{ID: raft.ServerID("node1"), Address: raft.ServerAddress("127.0.0.1:1234")}, |
| 21 | + }, |
| 22 | + "trims whitespace": { |
| 23 | + in: " node2 @ 10.0.0.2:9000 ", |
| 24 | + exp: raft.Server{ID: raft.ServerID("node2"), Address: raft.ServerAddress("10.0.0.2:9000")}, |
| 25 | + }, |
| 26 | + "missing at": { |
| 27 | + in: "node1", |
| 28 | + expErr: errors.New("expecting nodeID@address for peer"), |
| 29 | + }, |
| 30 | + "empty node id": { |
| 31 | + in: "@127.0.0.1:1234", |
| 32 | + expErr: errors.New("nodeID cannot be empty"), |
| 33 | + }, |
| 34 | + "empty address": { |
| 35 | + in: "node1@", |
| 36 | + expErr: errors.New("address cannot be empty"), |
| 37 | + }, |
| 38 | + "multiple ats": { |
| 39 | + in: "a@b@c", |
| 40 | + expErr: errors.New("expecting nodeID@address for peer"), |
| 41 | + }, |
| 42 | + "only spaces": { |
| 43 | + in: " @ ", |
| 44 | + expErr: errors.New("nodeID cannot be empty"), |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for name, spec := range specs { |
| 49 | + t.Run(name, func(t *testing.T) { |
| 50 | + ctx := t.Context() |
| 51 | + _ = ctx // keep to follow guideline to prefer t.Context; function under test doesn't use context |
| 52 | + |
| 53 | + got, err := splitPeerAddr(spec.in) |
| 54 | + if spec.expErr != nil { |
| 55 | + require.Error(t, err) |
| 56 | + assert.Equal(t, spec.expErr.Error(), err.Error()) |
| 57 | + return |
| 58 | + } |
| 59 | + require.NoError(t, err) |
| 60 | + assert.Equal(t, spec.exp, got) |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestDeduplicateServers(t *testing.T) { |
| 66 | + |
| 67 | + specs := map[string]struct { |
| 68 | + in []raft.Server |
| 69 | + exp []raft.Server |
| 70 | + }{ |
| 71 | + "empty": { |
| 72 | + in: nil, |
| 73 | + exp: []raft.Server{}, |
| 74 | + }, |
| 75 | + "no duplicates": { |
| 76 | + in: []raft.Server{ |
| 77 | + {ID: raft.ServerID("n1"), Address: raft.ServerAddress("a1")}, |
| 78 | + {ID: raft.ServerID("n2"), Address: raft.ServerAddress("a2")}, |
| 79 | + }, |
| 80 | + exp: []raft.Server{ |
| 81 | + {ID: raft.ServerID("n1"), Address: raft.ServerAddress("a1")}, |
| 82 | + {ID: raft.ServerID("n2"), Address: raft.ServerAddress("a2")}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "duplicates keep first": { |
| 86 | + in: []raft.Server{ |
| 87 | + {ID: raft.ServerID("n1"), Address: raft.ServerAddress("a1")}, |
| 88 | + {ID: raft.ServerID("n2"), Address: raft.ServerAddress("a2")}, |
| 89 | + {ID: raft.ServerID("n1"), Address: raft.ServerAddress("a3")}, |
| 90 | + {ID: raft.ServerID("n3"), Address: raft.ServerAddress("a4")}, |
| 91 | + {ID: raft.ServerID("n2"), Address: raft.ServerAddress("a5")}, |
| 92 | + }, |
| 93 | + exp: []raft.Server{ |
| 94 | + {ID: raft.ServerID("n1"), Address: raft.ServerAddress("a1")}, |
| 95 | + {ID: raft.ServerID("n2"), Address: raft.ServerAddress("a2")}, |
| 96 | + {ID: raft.ServerID("n3"), Address: raft.ServerAddress("a4")}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for name, spec := range specs { |
| 102 | + t.Run(name, func(t *testing.T) { |
| 103 | + ctx := t.Context() |
| 104 | + _ = ctx |
| 105 | + |
| 106 | + got := deduplicateServers(spec.in) |
| 107 | + assert.Equal(t, spec.exp, got) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments