mockstore_test.go 2.5 KB

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