github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libfs/leveldb_storage_test.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libfs
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/libkbfs"
    12  	"github.com/keybase/client/go/kbfs/tlf"
    13  	"github.com/keybase/client/go/kbfs/tlfhandle"
    14  	"github.com/keybase/client/go/protocol/keybase1"
    15  	"github.com/stretchr/testify/require"
    16  	"github.com/syndtr/goleveldb/leveldb"
    17  )
    18  
    19  func TestLevelDBWithFS(t *testing.T) {
    20  	ctx, _, fs := makeFS(t, "")
    21  	defer libkbfs.CheckConfigAndShutdown(ctx, t, fs.config)
    22  
    23  	t.Log("Open a leveldb using a KBFS billy filesystem")
    24  	s, err := OpenLevelDBStorage(fs, false)
    25  	require.NoError(t, err)
    26  	sNeedsClose := true
    27  	defer func() {
    28  		if sNeedsClose {
    29  			err := s.Close()
    30  			require.NoError(t, err)
    31  		}
    32  	}()
    33  	db, err := leveldb.Open(s, nil)
    34  	require.NoError(t, err)
    35  	dbNeedsClose := true
    36  	defer func() {
    37  		if dbNeedsClose {
    38  			err := db.Close()
    39  			require.NoError(t, err)
    40  		}
    41  	}()
    42  
    43  	t.Log("Put some stuff into the db")
    44  	key1 := []byte("key1")
    45  	val1 := []byte("val1")
    46  	key2 := []byte("key2")
    47  	val2 := []byte("val2")
    48  	err = db.Put(key1, val1, nil)
    49  	require.NoError(t, err)
    50  	err = db.Put(key2, val2, nil)
    51  	require.NoError(t, err)
    52  
    53  	t.Log("Close the db to release the lock")
    54  	err = db.Close()
    55  	require.NoError(t, err)
    56  	dbNeedsClose = false
    57  	err = s.Close()
    58  	require.NoError(t, err)
    59  	sNeedsClose = false
    60  
    61  	t.Log("Read from another device")
    62  	config2 := libkbfs.ConfigAsUser(fs.config.(*libkbfs.ConfigLocal), "user1")
    63  	defer libkbfs.CheckConfigAndShutdown(ctx, t, config2)
    64  	h, err := tlfhandle.ParseHandle(
    65  		ctx, config2.KBPKI(), config2.MDOps(), nil, "user1", tlf.Private)
    66  	require.NoError(t, err)
    67  	fs2, err := NewFS(
    68  		ctx, config2, h, data.MasterBranch, "", "", keybase1.MDPriorityNormal)
    69  	require.NoError(t, err)
    70  	s2, err := OpenLevelDBStorage(fs2, false)
    71  	require.NoError(t, err)
    72  	defer func() {
    73  		err := s2.Close()
    74  		require.NoError(t, err)
    75  	}()
    76  	db2, err := leveldb.Open(s2, nil)
    77  	require.NoError(t, err)
    78  	defer func() {
    79  		err := db2.Close()
    80  		require.NoError(t, err)
    81  	}()
    82  
    83  	gotVal1, err := db2.Get(key1, nil)
    84  	require.NoError(t, err)
    85  	require.Equal(t, val1, gotVal1)
    86  	gotVal2, err := db2.Get(key2, nil)
    87  	require.NoError(t, err)
    88  	require.Equal(t, val2, gotVal2)
    89  }