github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/libnetwork/datastore/mock_store.go (about) 1 package datastore 2 3 import ( 4 "errors" 5 6 "github.com/docker/docker/libnetwork/types" 7 "github.com/docker/libkv/store" 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 // Put a value at "key" 43 func (s *MockStore) Put(key string, value []byte, options *store.WriteOptions) error { 44 mData := s.db[key] 45 if mData == nil { 46 mData = &MockData{value, 0} 47 } 48 mData.Index = mData.Index + 1 49 s.db[key] = mData 50 return nil 51 } 52 53 // Delete a value at "key" 54 func (s *MockStore) Delete(key string) error { 55 delete(s.db, key) 56 return nil 57 } 58 59 // Exists checks that the key exists inside the store 60 func (s *MockStore) Exists(key string) (bool, error) { 61 _, ok := s.db[key] 62 return ok, nil 63 } 64 65 // List gets a range of values at "directory" 66 func (s *MockStore) List(prefix string) ([]*store.KVPair, error) { 67 return nil, ErrNotImplemented 68 } 69 70 // DeleteTree deletes a range of values at "directory" 71 func (s *MockStore) DeleteTree(prefix string) error { 72 delete(s.db, prefix) 73 return nil 74 } 75 76 // Watch a single key for modifications 77 func (s *MockStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) { 78 return nil, ErrNotImplemented 79 } 80 81 // WatchTree triggers a watch on a range of values at "directory" 82 func (s *MockStore) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) { 83 return nil, ErrNotImplemented 84 } 85 86 // NewLock exposed 87 func (s *MockStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) { 88 return nil, ErrNotImplemented 89 } 90 91 // AtomicPut put a value at "key" if the key has not been 92 // modified in the meantime, throws an error if this is the case 93 func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) { 94 mData := s.db[key] 95 96 if previous == nil { 97 if mData != nil { 98 return false, nil, types.BadRequestErrorf("atomic put failed because key exists") 99 } // Else OK. 100 } else { 101 if mData == nil { 102 return false, nil, types.BadRequestErrorf("atomic put failed because key exists") 103 } 104 if mData != nil && mData.Index != previous.LastIndex { 105 return false, nil, types.BadRequestErrorf("atomic put failed due to mismatched Index") 106 } // Else OK. 107 } 108 err := s.Put(key, newValue, nil) 109 if err != nil { 110 return false, nil, err 111 } 112 return true, &store.KVPair{Key: key, Value: newValue, LastIndex: s.db[key].Index}, nil 113 } 114 115 // AtomicDelete deletes a value at "key" if the key has not 116 // been modified in the meantime, throws an error if this is the case 117 func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) { 118 mData := s.db[key] 119 if mData != nil && mData.Index != previous.LastIndex { 120 return false, types.BadRequestErrorf("atomic delete failed due to mismatched Index") 121 } 122 return true, s.Delete(key) 123 } 124 125 // Close closes the client connection 126 func (s *MockStore) Close() { 127 }