k8s.io/apiserver@v0.29.3/pkg/storage/etcd3/event_test.go (about)

     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  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"go.etcd.io/etcd/api/v3/mvccpb"
    23  	clientv3 "go.etcd.io/etcd/client/v3"
    24  	"testing"
    25  )
    26  
    27  func TestParseEvent(t *testing.T) {
    28  	for _, tc := range []struct {
    29  		name          string
    30  		etcdEvent     *clientv3.Event
    31  		expectedEvent *event
    32  		expectedErr   string
    33  	}{
    34  		{
    35  			name: "successful create",
    36  			etcdEvent: &clientv3.Event{
    37  				Type:   clientv3.EventTypePut,
    38  				PrevKv: nil,
    39  				Kv: &mvccpb.KeyValue{
    40  					// key is the key in bytes. An empty key is not allowed.
    41  					Key:            []byte("key"),
    42  					ModRevision:    1,
    43  					CreateRevision: 1,
    44  					Value:          []byte("value"),
    45  				},
    46  			},
    47  			expectedEvent: &event{
    48  				key:       "key",
    49  				value:     []byte("value"),
    50  				prevValue: nil,
    51  				rev:       1,
    52  				isDeleted: false,
    53  				isCreated: true,
    54  			},
    55  			expectedErr: "",
    56  		},
    57  		{
    58  			name: "unsuccessful delete",
    59  			etcdEvent: &clientv3.Event{
    60  				Type:   mvccpb.DELETE,
    61  				PrevKv: nil,
    62  				Kv: &mvccpb.KeyValue{
    63  					Key:            []byte("key"),
    64  					CreateRevision: 1,
    65  					ModRevision:    2,
    66  					Value:          nil,
    67  				},
    68  			},
    69  			expectedErr: "etcd event received with PrevKv=nil",
    70  		},
    71  		{
    72  			name: "successful delete",
    73  			etcdEvent: &clientv3.Event{
    74  				Type: mvccpb.DELETE,
    75  				PrevKv: &mvccpb.KeyValue{
    76  					Key:            []byte("key"),
    77  					CreateRevision: 1,
    78  					ModRevision:    1,
    79  					Value:          []byte("value"),
    80  				},
    81  				Kv: &mvccpb.KeyValue{
    82  					Key:            []byte("key"),
    83  					CreateRevision: 1,
    84  					ModRevision:    2,
    85  					Value:          nil,
    86  				},
    87  			},
    88  			expectedEvent: &event{
    89  				key:       "key",
    90  				value:     nil,
    91  				prevValue: []byte("value"),
    92  				rev:       2,
    93  				isDeleted: true,
    94  				isCreated: false,
    95  			},
    96  			expectedErr: "",
    97  		},
    98  	} {
    99  		t.Run(tc.name, func(t *testing.T) {
   100  			actualEvent, err := parseEvent(tc.etcdEvent)
   101  			if tc.expectedErr != "" {
   102  				require.Error(t, err)
   103  				assert.Contains(t, err.Error(), tc.expectedErr)
   104  			} else {
   105  				require.NoError(t, err)
   106  				assert.Equal(t, tc.expectedEvent, actualEvent)
   107  			}
   108  		})
   109  	}
   110  }