File: datastore_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 (231 lines) | stat: -rw-r--r-- 5,350 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package datastore

import (
	"encoding/json"
	"testing"

	"github.com/docker/docker/libnetwork/options"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

const dummyKey = "dummy"

// NewTestDataStore can be used by other Tests in order to use custom datastore
func NewTestDataStore() *Store {
	s := NewMockStore()
	return &Store{store: s, cache: newCache(s)}
}

func TestKey(t *testing.T) {
	sKey := Key("hello", "world")
	const expected = "docker/network/v1.0/hello/world/"
	assert.Check(t, is.Equal(sKey, expected))
}

func TestInvalidDataStore(t *testing.T) {
	_, err := New(ScopeCfg{
		Client: ScopeClientCfg{
			Provider: "invalid",
			Address:  "localhost:8500",
		},
	})
	assert.Check(t, is.Error(err, "unsupported KV store"))
}

func TestKVObjectFlatKey(t *testing.T) {
	store := NewTestDataStore()
	expected := dummyKVObject("1000", true)
	err := store.PutObjectAtomic(expected)
	assert.Check(t, err)

	n := dummyObject{ID: "1000"} // GetObject uses KVObject.Key() for cache lookup.
	err = store.GetObject(&n)
	assert.Check(t, err)
	assert.Check(t, is.Equal(n.Name, expected.Name))
}

func TestAtomicKVObjectFlatKey(t *testing.T) {
	store := NewTestDataStore()
	expected := dummyKVObject("1111", true)
	assert.Check(t, !expected.Exists())
	err := store.PutObjectAtomic(expected)
	assert.Check(t, err)
	assert.Check(t, expected.Exists())

	// PutObjectAtomic automatically sets the Index again. Hence the following must pass.

	err = store.PutObjectAtomic(expected)
	assert.Check(t, err, "Atomic update should succeed.")

	// Get the latest index and try PutObjectAtomic again for the same Key
	// This must succeed as well
	n := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
	err = store.GetObject(&n)
	assert.Check(t, err)
	n.ReturnValue = true
	err = store.PutObjectAtomic(&n)
	assert.Check(t, err)

	// Get the Object using GetObject, then set again.
	newObj := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
	err = store.GetObject(&newObj)
	assert.Check(t, err)
	assert.Check(t, newObj.Exists())
	err = store.PutObjectAtomic(&n)
	assert.Check(t, err)
}

// dummy data used to test the datastore
type dummyObject struct {
	Name        string                `kv:"leaf"`
	NetworkType string                `kv:"leaf"`
	EnableIPv6  bool                  `kv:"leaf"`
	Rec         *recStruct            `kv:"recursive"`
	Dict        map[string]*recStruct `kv:"iterative"`
	Generic     options.Generic       `kv:"iterative"`
	ID          string
	DBIndex     uint64
	DBExists    bool
	SkipSave    bool
	ReturnValue bool
}

func (n *dummyObject) Key() []string {
	return []string{dummyKey, n.ID}
}

func (n *dummyObject) KeyPrefix() []string {
	return []string{dummyKey}
}

func (n *dummyObject) Value() []byte {
	if !n.ReturnValue {
		return nil
	}

	b, err := json.Marshal(n)
	if err != nil {
		return nil
	}
	return b
}

func (n *dummyObject) SetValue(value []byte) error {
	return json.Unmarshal(value, n)
}

func (n *dummyObject) Index() uint64 {
	return n.DBIndex
}

func (n *dummyObject) SetIndex(index uint64) {
	n.DBIndex = index
	n.DBExists = true
}

func (n *dummyObject) Exists() bool {
	return n.DBExists
}

func (n *dummyObject) Skip() bool {
	return n.SkipSave
}

func (n *dummyObject) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"name":        n.Name,
		"networkType": n.NetworkType,
		"enableIPv6":  n.EnableIPv6,
		"generic":     n.Generic,
	})
}

func (n *dummyObject) UnmarshalJSON(b []byte) error {
	var netMap map[string]interface{}
	if err := json.Unmarshal(b, &netMap); err != nil {
		return err
	}
	n.Name = netMap["name"].(string)
	n.NetworkType = netMap["networkType"].(string)
	n.EnableIPv6 = netMap["enableIPv6"].(bool)
	n.Generic = netMap["generic"].(map[string]interface{})
	return nil
}

func (n *dummyObject) New() KVObject {
	return &dummyObject{}
}

func (n *dummyObject) CopyTo(o KVObject) error {
	if err := o.SetValue(n.Value()); err != nil {
		return err
	}
	o.SetIndex(n.Index())
	return nil
}

// dummy structure to test "recursive" cases
type recStruct struct {
	Name     string            `kv:"leaf"`
	Field1   int               `kv:"leaf"`
	Dict     map[string]string `kv:"iterative"`
	DBIndex  uint64
	DBExists bool
	SkipSave bool
}

func (r *recStruct) Key() []string {
	return []string{"recStruct"}
}

func (r *recStruct) Value() []byte {
	b, err := json.Marshal(r)
	if err != nil {
		return nil
	}
	return b
}

func (r *recStruct) SetValue(value []byte) error {
	return json.Unmarshal(value, r)
}

func (r *recStruct) Index() uint64 {
	return r.DBIndex
}

func (r *recStruct) SetIndex(index uint64) {
	r.DBIndex = index
	r.DBExists = true
}

func (r *recStruct) Exists() bool {
	return r.DBExists
}

func (r *recStruct) Skip() bool {
	return r.SkipSave
}

func dummyKVObject(id string, retValue bool) *dummyObject {
	cDict := map[string]string{
		"foo":   "bar",
		"hello": "world",
	}
	return &dummyObject{
		Name:        "testNw",
		NetworkType: "bridge",
		EnableIPv6:  true,
		Rec:         &recStruct{Name: "gen", Field1: 5, Dict: cDict},
		ID:          id,
		DBIndex:     0,
		ReturnValue: retValue,
		DBExists:    false,
		SkipSave:    false,
		Generic: map[string]interface{}{
			"label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
			"label2": "subnet=10.1.1.0/16",
		},
	}
}