github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/volume/service/db_test.go (about)

     1  package service // import "github.com/Prakhar-Agarwal-byte/moby/volume/service"
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  	"time"
     8  
     9  	bolt "go.etcd.io/bbolt"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestSetGetMeta(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	dir, err := os.MkdirTemp("", "test-set-get")
    18  	assert.NilError(t, err)
    19  	defer os.RemoveAll(dir)
    20  
    21  	db, err := bolt.Open(filepath.Join(dir, "db"), 0o600, &bolt.Options{Timeout: 1 * time.Second})
    22  	assert.NilError(t, err)
    23  
    24  	store := &VolumeStore{db: db}
    25  	defer store.Shutdown()
    26  
    27  	_, err = store.getMeta("test")
    28  	assert.Assert(t, is.ErrorContains(err, ""))
    29  
    30  	err = db.Update(func(tx *bolt.Tx) error {
    31  		_, err := tx.CreateBucket(volumeBucketName)
    32  		return err
    33  	})
    34  	assert.NilError(t, err)
    35  
    36  	meta, err := store.getMeta("test")
    37  	assert.NilError(t, err)
    38  	assert.DeepEqual(t, volumeMetadata{}, meta)
    39  
    40  	testMeta := volumeMetadata{
    41  		Name:    "test",
    42  		Driver:  "fake",
    43  		Labels:  map[string]string{"a": "1", "b": "2"},
    44  		Options: map[string]string{"foo": "bar"},
    45  	}
    46  	err = store.setMeta("test", testMeta)
    47  	assert.NilError(t, err)
    48  
    49  	meta, err = store.getMeta("test")
    50  	assert.NilError(t, err)
    51  	assert.DeepEqual(t, testMeta, meta)
    52  }