github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/datastore/mock_store.go (about) 1 package datastore 2 3 import ( 4 "errors" 5 6 "github.com/docker/libkv/store" 7 "github.com/docker/libnetwork/types" 8 ) 9 10 var ( 11 // ErrNotImplemented exported 12 ErrNotImplemented = errors.New("Functionality not implemented") 13 ) 14 15 // MockData exported 16 type MockData struct { 17 Data []byte 18 Index uint64 19 } 20 21 // MockStore exported 22 type MockStore struct { 23 db map[string]*MockData 24 } 25 26 // NewMockStore creates a Map backed Datastore that is useful for mocking 27 func NewMockStore() *MockStore { 28 db := make(map[string]*MockData) 29 return &MockStore{db} 30 } 31 32 // Get the value at "key", returns the last modified index 33 // to use in conjunction to CAS calls 34 func (s *MockStore) Get(key string) (*store.KVPair, error) { 35 mData := s.db[key] 36 if mData == nil { 37 return nil, nil 38 } 39 return &store.KVPair{Value: mData.Data, LastIndex: mData.Index}, nil 40 41 } 42 43 // Put a value at "key" 44 func (s *MockStore) Put(key string, value []byte, options *store.WriteOptions) error { 45 mData := s.db[key] 46 if mData == nil { 47 mData = &MockData{value, 0} 48 } 49 mData.Index = mData.Index + 1 50 s.db[key] = mData 51 return nil 52 } 53 54 // Delete a value at "key" 55 func (s *MockStore) Delete(key string) error { 56 delete(s.db, key) 57 return nil 58 } 59 60 // Exists checks that the key exists inside the store 61 func (s *MockStore) Exists(key string) (bool, error) { 62 _, ok := s.db[key] 63 return ok, nil 64 } 65 66 // List gets a range of values at "directory" 67 func (s *MockStore) List(prefix string) ([]*store.KVPair, error) { 68 return nil, ErrNotImplemented 69 } 70 71 // DeleteTree deletes a range of values at "directory" 72 func (s *MockStore) DeleteTree(prefix string) error { 73 delete(s.db, prefix) 74 return nil 75 } 76 77 // Watch a single key for modifications 78 func (s *MockStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) { 79 return nil, ErrNotImplemented 80 } 81 82 // WatchTree triggers a watch on a range of values at "directory" 83 func (s *MockStore) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) { 84 return nil, ErrNotImplemented 85 } 86 87 // NewLock exposed 88 func (s *MockStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) { 89 return nil, ErrNotImplemented 90 } 91 92 // AtomicPut put a value at "key" if the key has not been 93 // modified in the meantime, throws an error if this is the case 94 func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) { 95 mData := s.db[key] 96 97 if previous == nil { 98 if mData != nil { 99 return false, nil, types.BadRequestErrorf("atomic put failed because key exists") 100 } // Else OK. 101 } else { 102 if mData == nil { 103 return false, nil, types.BadRequestErrorf("atomic put failed because key exists") 104 } 105 if mData != nil && mData.Index != previous.LastIndex { 106 return false, nil, types.BadRequestErrorf("atomic put failed due to mismatched Index") 107 } // Else OK. 108 } 109 err := s.Put(key, newValue, nil) 110 if err != nil { 111 return false, nil, err 112 } 113 return true, &store.KVPair{Key: key, Value: newValue, LastIndex: s.db[key].Index}, nil 114 } 115 116 // AtomicDelete deletes a value at "key" if the key has not 117 // been modified in the meantime, throws an error if this is the case 118 func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) { 119 mData := s.db[key] 120 if mData != nil && mData.Index != previous.LastIndex { 121 return false, types.BadRequestErrorf("atomic delete failed due to mismatched Index") 122 } 123 return true, s.Delete(key) 124 } 125 126 // Close closes the client connection 127 func (s *MockStore) Close() { 128 return 129 }