Debian Codegraph @

pkg/storage/etcd3/event_test.go

raw
1 /*
2 Copyright 2019 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package etcd3
18
19 import (
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23 "github.com/stretchr/testify/require"
24 "go.etcd.io/etcd/api/v3/mvccpb"
25 clientv3 "go.etcd.io/etcd/client/v3"
26 )
27
28 func TestParseEvent(t *testing.T) {
29 for _, tc := range []struct {
30 name string
31 etcdEvent *clientv3.Event
32 expectedEvent *event
33 expectedErr string
34 }{
35 {
36 name: "successful create",
37 etcdEvent: &clientv3.Event{
38 Type: clientv3.EventTypePut,
39 PrevKv: nil,
40 Kv: &mvccpb.KeyValue{
41 // key is the key in bytes. An empty key is not allowed.
42 Key: []byte("key"),
43 ModRevision: 1,
44 CreateRevision: 1,
45 Value: []byte("value"),
46 },
47 },
48 expectedEvent: &event{
49 key: "key",
50 value: []byte("value"),
51 prevValue: nil,
52 rev: 1,
53 isDeleted: false,
54 isCreated: true,
55 },
56 expectedErr: "",
57 },
58 {
59 name: "unsuccessful delete",
60 etcdEvent: &clientv3.Event{
61 Type: mvccpb.DELETE,
62 PrevKv: nil,
63 Kv: &mvccpb.KeyValue{
64 Key: []byte("key"),
65 CreateRevision: 1,
66 ModRevision: 2,
67 Value: nil,
68 },
69 },
70 expectedErr: "etcd event received with PrevKv=nil",
71 },
72 {
73 name: "successful delete",
74 etcdEvent: &clientv3.Event{
75 Type: mvccpb.DELETE,
76 PrevKv: &mvccpb.KeyValue{
77 Key: []byte("key"),
78 CreateRevision: 1,
79 ModRevision: 1,
80 Value: []byte("value"),
81 },
82 Kv: &mvccpb.KeyValue{
83 Key: []byte("key"),
84 CreateRevision: 1,
85 ModRevision: 2,
86 Value: nil,
87 },
88 },
89 expectedEvent: &event{
90 key: "key",
91 value: nil,
92 prevValue: []byte("value"),
93 rev: 2,
94 isDeleted: true,
95 isCreated: false,
96 },
97 expectedErr: "",
98 },
99 } {
100 t.Run(tc.name, func(t *testing.T) {
101 actualEvent, err := parseEvent(tc.etcdEvent)
102 if tc.expectedErr != "" {
103 require.Error(t, err)
104 assert.ErrorContains(t, err, tc.expectedErr)
105 } else {
106 require.NoError(t, err)
107 assert.Equal(t, tc.expectedEvent, actualEvent)
108 }
109 })
110 }
111 }