summaryrefslogtreecommitdiff
path: root/volume/service/db_test.go
blob: 8b6295adfd5c5bcd5f716b3383846f2805c42392 (plain)
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
package service // import "github.com/docker/docker/volume/service"

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"

	bolt "go.etcd.io/bbolt"
	"gotest.tools/assert"
	is "gotest.tools/assert/cmp"
)

func TestSetGetMeta(t *testing.T) {
	t.Parallel()

	dir, err := ioutil.TempDir("", "test-set-get")
	assert.NilError(t, err)
	defer os.RemoveAll(dir)

	db, err := bolt.Open(filepath.Join(dir, "db"), 0600, &bolt.Options{Timeout: 1 * time.Second})
	assert.NilError(t, err)

	store := &VolumeStore{db: db}

	_, err = store.getMeta("test")
	assert.Assert(t, is.ErrorContains(err, ""))

	err = db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucket(volumeBucketName)
		return err
	})
	assert.NilError(t, err)

	meta, err := store.getMeta("test")
	assert.NilError(t, err)
	assert.DeepEqual(t, volumeMetadata{}, meta)

	testMeta := volumeMetadata{
		Name:    "test",
		Driver:  "fake",
		Labels:  map[string]string{"a": "1", "b": "2"},
		Options: map[string]string{"foo": "bar"},
	}
	err = store.setMeta("test", testMeta)
	assert.NilError(t, err)

	meta, err = store.getMeta("test")
	assert.NilError(t, err)
	assert.DeepEqual(t, testMeta, meta)
}