go.etcd.io/etcd@v3.3.27+incompatible/pkg/mock/mockstore/store_recorder.go (about) 1 // Copyright 2015 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mockstore 16 17 import ( 18 "time" 19 20 "github.com/coreos/etcd/pkg/testutil" 21 "github.com/coreos/etcd/store" 22 ) 23 24 // StoreRecorder provides a Store interface with a testutil.Recorder 25 type StoreRecorder struct { 26 store.Store 27 testutil.Recorder 28 } 29 30 // storeRecorder records all the methods it receives. 31 // storeRecorder DOES NOT work as a actual store. 32 // It always returns invalid empty response and no error. 33 type storeRecorder struct { 34 store.Store 35 testutil.Recorder 36 } 37 38 func NewNop() store.Store { return &storeRecorder{Recorder: &testutil.RecorderBuffered{}} } 39 func NewRecorder() *StoreRecorder { 40 sr := &storeRecorder{Recorder: &testutil.RecorderBuffered{}} 41 return &StoreRecorder{Store: sr, Recorder: sr.Recorder} 42 } 43 func NewRecorderStream() *StoreRecorder { 44 sr := &storeRecorder{Recorder: testutil.NewRecorderStream()} 45 return &StoreRecorder{Store: sr, Recorder: sr.Recorder} 46 } 47 48 func (s *storeRecorder) Version() int { return 0 } 49 func (s *storeRecorder) Index() uint64 { return 0 } 50 func (s *storeRecorder) Get(path string, recursive, sorted bool) (*store.Event, error) { 51 s.Record(testutil.Action{ 52 Name: "Get", 53 Params: []interface{}{path, recursive, sorted}, 54 }) 55 return &store.Event{}, nil 56 } 57 func (s *storeRecorder) Set(path string, dir bool, val string, expireOpts store.TTLOptionSet) (*store.Event, error) { 58 s.Record(testutil.Action{ 59 Name: "Set", 60 Params: []interface{}{path, dir, val, expireOpts}, 61 }) 62 return &store.Event{}, nil 63 } 64 func (s *storeRecorder) Update(path, val string, expireOpts store.TTLOptionSet) (*store.Event, error) { 65 s.Record(testutil.Action{ 66 Name: "Update", 67 Params: []interface{}{path, val, expireOpts}, 68 }) 69 return &store.Event{}, nil 70 } 71 func (s *storeRecorder) Create(path string, dir bool, val string, uniq bool, expireOpts store.TTLOptionSet) (*store.Event, error) { 72 s.Record(testutil.Action{ 73 Name: "Create", 74 Params: []interface{}{path, dir, val, uniq, expireOpts}, 75 }) 76 return &store.Event{}, nil 77 } 78 func (s *storeRecorder) CompareAndSwap(path, prevVal string, prevIdx uint64, val string, expireOpts store.TTLOptionSet) (*store.Event, error) { 79 s.Record(testutil.Action{ 80 Name: "CompareAndSwap", 81 Params: []interface{}{path, prevVal, prevIdx, val, expireOpts}, 82 }) 83 return &store.Event{}, nil 84 } 85 func (s *storeRecorder) Delete(path string, dir, recursive bool) (*store.Event, error) { 86 s.Record(testutil.Action{ 87 Name: "Delete", 88 Params: []interface{}{path, dir, recursive}, 89 }) 90 return &store.Event{}, nil 91 } 92 func (s *storeRecorder) CompareAndDelete(path, prevVal string, prevIdx uint64) (*store.Event, error) { 93 s.Record(testutil.Action{ 94 Name: "CompareAndDelete", 95 Params: []interface{}{path, prevVal, prevIdx}, 96 }) 97 return &store.Event{}, nil 98 } 99 func (s *storeRecorder) Watch(_ string, _, _ bool, _ uint64) (store.Watcher, error) { 100 s.Record(testutil.Action{Name: "Watch"}) 101 return store.NewNopWatcher(), nil 102 } 103 func (s *storeRecorder) Save() ([]byte, error) { 104 s.Record(testutil.Action{Name: "Save"}) 105 return nil, nil 106 } 107 func (s *storeRecorder) Recovery(b []byte) error { 108 s.Record(testutil.Action{Name: "Recovery"}) 109 return nil 110 } 111 112 func (s *storeRecorder) SaveNoCopy() ([]byte, error) { 113 s.Record(testutil.Action{Name: "SaveNoCopy"}) 114 return nil, nil 115 } 116 117 func (s *storeRecorder) Clone() store.Store { 118 s.Record(testutil.Action{Name: "Clone"}) 119 return s 120 } 121 122 func (s *storeRecorder) JsonStats() []byte { return nil } 123 func (s *storeRecorder) DeleteExpiredKeys(cutoff time.Time) { 124 s.Record(testutil.Action{ 125 Name: "DeleteExpiredKeys", 126 Params: []interface{}{cutoff}, 127 }) 128 } 129 130 func (s *storeRecorder) HasTTLKeys() bool { 131 s.Record(testutil.Action{ 132 Name: "HasTTLKeys", 133 }) 134 return true 135 } 136 137 // errStoreRecorder is a storeRecorder, but returns the given error on 138 // Get, Watch methods. 139 type errStoreRecorder struct { 140 storeRecorder 141 err error 142 } 143 144 func NewErrRecorder(err error) *StoreRecorder { 145 sr := &errStoreRecorder{err: err} 146 sr.Recorder = &testutil.RecorderBuffered{} 147 return &StoreRecorder{Store: sr, Recorder: sr.Recorder} 148 } 149 150 func (s *errStoreRecorder) Get(path string, recursive, sorted bool) (*store.Event, error) { 151 s.storeRecorder.Get(path, recursive, sorted) 152 return nil, s.err 153 } 154 func (s *errStoreRecorder) Watch(path string, recursive, sorted bool, index uint64) (store.Watcher, error) { 155 s.storeRecorder.Watch(path, recursive, sorted, index) 156 return nil, s.err 157 }