Tests to make sure the mock Etcd client works which we need for kv.Client tests. Quis custodiet ipsos custodes?
(t *testing.T)
| 15 | // Quis custodiet ipsos custodes? |
| 16 | |
| 17 | func TestMockKv_Get(t *testing.T) { |
| 18 | t.Run("exact match", func(t *testing.T) { |
| 19 | pair := mvccpb.KeyValue{ |
| 20 | Key: []byte("/foo"), |
| 21 | Value: []byte("1"), |
| 22 | } |
| 23 | |
| 24 | kv := newMockKV() |
| 25 | kv.values = map[string]mvccpb.KeyValue{string(pair.Key): pair} |
| 26 | res, err := kv.Get(context.Background(), "/foo") |
| 27 | |
| 28 | require.NoError(t, err) |
| 29 | require.Len(t, res.Kvs, 1) |
| 30 | assert.Equal(t, []byte("/foo"), res.Kvs[0].Key) |
| 31 | }) |
| 32 | |
| 33 | t.Run("not exact match", func(t *testing.T) { |
| 34 | pair := mvccpb.KeyValue{ |
| 35 | Key: []byte("/foo"), |
| 36 | Value: []byte("1"), |
| 37 | } |
| 38 | |
| 39 | kv := newMockKV() |
| 40 | kv.values = map[string]mvccpb.KeyValue{string(pair.Key): pair} |
| 41 | res, err := kv.Get(context.Background(), "/bar") |
| 42 | |
| 43 | require.NoError(t, err) |
| 44 | assert.Empty(t, res.Kvs) |
| 45 | }) |
| 46 | |
| 47 | t.Run("prefix match", func(t *testing.T) { |
| 48 | fooPair := mvccpb.KeyValue{ |
| 49 | Key: []byte("/foo"), |
| 50 | Value: []byte("1"), |
| 51 | } |
| 52 | bazPair := mvccpb.KeyValue{ |
| 53 | Key: []byte("/baz"), |
| 54 | Value: []byte("2"), |
| 55 | } |
| 56 | firstPair := mvccpb.KeyValue{ |
| 57 | Key: []byte("/first"), |
| 58 | Value: []byte("3"), |
| 59 | } |
| 60 | |
| 61 | kv := newMockKV() |
| 62 | kv.values = map[string]mvccpb.KeyValue{ |
| 63 | string(fooPair.Key): fooPair, |
| 64 | string(bazPair.Key): bazPair, |
| 65 | string(firstPair.Key): firstPair, |
| 66 | } |
| 67 | res, err := kv.Get(context.Background(), "/f", clientv3.WithPrefix()) |
| 68 | |
| 69 | require.NoError(t, err) |
| 70 | assert.ElementsMatch(t, []*mvccpb.KeyValue{&fooPair, &firstPair}, res.Kvs) |
| 71 | }) |
| 72 | |
| 73 | t.Run("empty prefix", func(t *testing.T) { |
| 74 | fooPair := mvccpb.KeyValue{ |