mock_store.go 2.6 KB

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