github.com/moby/docker@v26.1.3+incompatible/libnetwork/datastore/mockstore_test.go (about)

     1  package datastore
     2  
     3  import (
     4  	"strings"
     5  
     6  	store "github.com/docker/docker/libnetwork/internal/kvstore"
     7  	"github.com/docker/docker/libnetwork/types"
     8  )
     9  
    10  // MockData exported
    11  type MockData struct {
    12  	Data  []byte
    13  	Index uint64
    14  }
    15  
    16  // MockStore exported
    17  type MockStore struct {
    18  	db map[string]*MockData
    19  }
    20  
    21  // NewMockStore creates a Map backed Datastore that is useful for mocking
    22  func NewMockStore() *MockStore {
    23  	return &MockStore{db: make(map[string]*MockData)}
    24  }
    25  
    26  // Put a value at "key"
    27  func (s *MockStore) Put(key string, value []byte) error {
    28  	mData := s.db[key]
    29  	if mData == nil {
    30  		mData = &MockData{value, 0}
    31  	}
    32  	mData.Index = mData.Index + 1
    33  	s.db[key] = mData
    34  	return nil
    35  }
    36  
    37  // Exists checks that the key exists inside the store
    38  func (s *MockStore) Exists(key string) (bool, error) {
    39  	_, ok := s.db[key]
    40  	return ok, nil
    41  }
    42  
    43  // List gets a range of values at "directory"
    44  func (s *MockStore) List(prefix string) ([]*store.KVPair, error) {
    45  	var res []*store.KVPair
    46  	for k, v := range s.db {
    47  		if strings.HasPrefix(k, prefix) {
    48  			res = append(res, &store.KVPair{Key: k, Value: v.Data, LastIndex: v.Index})
    49  		}
    50  	}
    51  	if len(res) == 0 {
    52  		return nil, store.ErrKeyNotFound
    53  	}
    54  	return res, nil
    55  }
    56  
    57  // AtomicPut put a value at "key" if the key has not been
    58  // modified in the meantime, throws an error if this is the case
    59  func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPair) (*store.KVPair, error) {
    60  	mData := s.db[key]
    61  
    62  	if previous == nil {
    63  		if mData != nil {
    64  			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
    65  		} // Else OK.
    66  	} else {
    67  		if mData == nil {
    68  			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
    69  		}
    70  		if mData != nil && mData.Index != previous.LastIndex {
    71  			return nil, types.InvalidParameterErrorf("atomic put failed due to mismatched Index")
    72  		} // Else OK.
    73  	}
    74  	if err := s.Put(key, newValue); err != nil {
    75  		return nil, err
    76  	}
    77  	return &store.KVPair{Key: key, Value: newValue, LastIndex: s.db[key].Index}, nil
    78  }
    79  
    80  // AtomicDelete deletes a value at "key" if the key has not
    81  // been modified in the meantime, throws an error if this is the case
    82  func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) error {
    83  	mData := s.db[key]
    84  	if mData != nil && mData.Index != previous.LastIndex {
    85  		return types.InvalidParameterErrorf("atomic delete failed due to mismatched Index")
    86  	}
    87  	delete(s.db, key)
    88  	return nil
    89  }
    90  
    91  // Delete deletes a value at "key". Unlike AtomicDelete it doesn't check
    92  // whether the deleted key is at a specific version before deleting.
    93  func (s *MockStore) Delete(key string) error {
    94  	if _, ok := s.db[key]; !ok {
    95  		return store.ErrKeyNotFound
    96  	}
    97  	delete(s.db, key)
    98  	return nil
    99  }
   100  
   101  // Close closes the client connection
   102  func (s *MockStore) Close() {
   103  }