mock_store.go 3.4 KB

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