File: mockstore_test.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (103 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package datastore

import (
	"strings"

	store "github.com/docker/docker/libnetwork/internal/kvstore"
	"github.com/docker/docker/libnetwork/types"
)

// MockData exported
type MockData struct {
	Data  []byte
	Index uint64
}

// MockStore exported
type MockStore struct {
	db map[string]*MockData
}

// NewMockStore creates a Map backed Datastore that is useful for mocking
func NewMockStore() *MockStore {
	return &MockStore{db: make(map[string]*MockData)}
}

// Put a value at "key"
func (s *MockStore) Put(key string, value []byte) error {
	mData := s.db[key]
	if mData == nil {
		mData = &MockData{value, 0}
	}
	mData.Index = mData.Index + 1
	s.db[key] = mData
	return nil
}

// Exists checks that the key exists inside the store
func (s *MockStore) Exists(key string) (bool, error) {
	_, ok := s.db[key]
	return ok, nil
}

// List gets a range of values at "directory"
func (s *MockStore) List(prefix string) ([]*store.KVPair, error) {
	var res []*store.KVPair
	for k, v := range s.db {
		if strings.HasPrefix(k, prefix) {
			res = append(res, &store.KVPair{Key: k, Value: v.Data, LastIndex: v.Index})
		}
	}
	if len(res) == 0 {
		return nil, store.ErrKeyNotFound
	}
	return res, nil
}

// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPair) (*store.KVPair, error) {
	mData := s.db[key]

	if previous == nil {
		if mData != nil {
			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
		} // Else OK.
	} else {
		if mData == nil {
			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
		}
		if mData != nil && mData.Index != previous.LastIndex {
			return nil, types.InvalidParameterErrorf("atomic put failed due to mismatched Index")
		} // Else OK.
	}
	if err := s.Put(key, newValue); err != nil {
		return nil, err
	}
	return &store.KVPair{Key: key, Value: newValue, LastIndex: s.db[key].Index}, nil
}

// AtomicDelete deletes a value at "key" if the key has not
// been modified in the meantime, throws an error if this is the case
func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) error {
	mData := s.db[key]
	if mData != nil && mData.Index != previous.LastIndex {
		return types.InvalidParameterErrorf("atomic delete failed due to mismatched Index")
	}
	delete(s.db, key)
	return nil
}

// Delete deletes a value at "key". Unlike AtomicDelete it doesn't check
// whether the deleted key is at a specific version before deleting.
func (s *MockStore) Delete(key string) error {
	if _, ok := s.db[key]; !ok {
		return store.ErrKeyNotFound
	}
	delete(s.db, key)
	return nil
}

// Close closes the client connection
func (s *MockStore) Close() {
}