mockstore_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package datastore
  2. import (
  3. "strings"
  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. // Put a value at "key"
  21. func (s *MockStore) Put(key string, value []byte) error {
  22. mData := s.db[key]
  23. if mData == nil {
  24. mData = &MockData{value, 0}
  25. }
  26. mData.Index = mData.Index + 1
  27. s.db[key] = mData
  28. return nil
  29. }
  30. // Exists checks that the key exists inside the store
  31. func (s *MockStore) Exists(key string) (bool, error) {
  32. _, ok := s.db[key]
  33. return ok, nil
  34. }
  35. // List gets a range of values at "directory"
  36. func (s *MockStore) List(prefix string) ([]*store.KVPair, error) {
  37. var res []*store.KVPair
  38. for k, v := range s.db {
  39. if strings.HasPrefix(k, prefix) {
  40. res = append(res, &store.KVPair{Key: k, Value: v.Data, LastIndex: v.Index})
  41. }
  42. }
  43. if len(res) == 0 {
  44. return nil, store.ErrKeyNotFound
  45. }
  46. return res, nil
  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.InvalidParameterErrorf("atomic put failed because key exists")
  55. } // Else OK.
  56. } else {
  57. if mData == nil {
  58. return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
  59. }
  60. if mData != nil && mData.Index != previous.LastIndex {
  61. return nil, types.InvalidParameterErrorf("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.InvalidParameterErrorf("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. }