github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/search/indexed_tlf_db_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 search
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/keybase/client/go/kbfs/kbfsmd"
    13  	"github.com/keybase/client/go/kbfs/libkbfs"
    14  	"github.com/keybase/client/go/kbfs/tlf"
    15  	"github.com/stretchr/testify/require"
    16  	"github.com/syndtr/goleveldb/leveldb/storage"
    17  	"golang.org/x/net/context"
    18  )
    19  
    20  func newIndexedTlfDbForTestWithStorage(
    21  	t *testing.T, tlfS storage.Storage) *IndexedTlfDb {
    22  	config := libkbfs.MakeTestConfigOrBust(t, "user1")
    23  	db, err := newIndexedTlfDbFromStorage(config, tlfS)
    24  	require.NoError(t, err)
    25  	return db
    26  }
    27  
    28  func newIndexedTlfDbForTest(t *testing.T) (
    29  	*IndexedTlfDb, string) {
    30  	// Use a disk-based level, instead of memory storage, because we
    31  	// want to simulate a restart and memory storages can't be reused.
    32  	tempdir, err := os.MkdirTemp(os.TempDir(), "indexed_tlfs_db")
    33  	require.NoError(t, err)
    34  	tlfS, err := storage.OpenFile(filepath.Join(tempdir, "tlfs"), false)
    35  	require.NoError(t, err)
    36  
    37  	db := newIndexedTlfDbForTestWithStorage(t, tlfS)
    38  	return db, tempdir
    39  }
    40  
    41  func shutdownIndexedTlfDbTest(db *IndexedTlfDb, tempdir string) {
    42  	db.Shutdown(context.Background())
    43  	os.RemoveAll(tempdir)
    44  }
    45  
    46  func TestIndexedTlfDbCreate(t *testing.T) {
    47  	config := libkbfs.MakeTestConfigOrBust(t, "user1")
    48  	defer func() {
    49  		err := config.Shutdown(context.Background())
    50  		require.NoError(t, err)
    51  	}()
    52  
    53  	tempdir, err := os.MkdirTemp(os.TempDir(), "indexed_tlfs_db")
    54  	require.NoError(t, err)
    55  	db, err := newIndexedTlfDb(config, tempdir)
    56  	require.NoError(t, err)
    57  	shutdownIndexedTlfDbTest(db, tempdir)
    58  }
    59  
    60  func TestIndexedTlfDb(t *testing.T) {
    61  	t.Skip() // TRIAGE-1674
    62  	t.Parallel()
    63  	t.Log("Test that indexed TLF db Put and Get operations work.")
    64  	db, tempdir := newIndexedTlfDbForTest(t)
    65  	defer func() {
    66  		shutdownIndexedTlfDbTest(db, tempdir)
    67  	}()
    68  
    69  	ctx := context.Background()
    70  	tlfID1 := tlf.FakeID(1, tlf.Private)
    71  	ir1 := kbfsmd.Revision(50)
    72  	sr1 := kbfsmd.RevisionUninitialized
    73  
    74  	t.Log("Put TLF MD into the db.")
    75  	_, _, err := db.Get(ctx, tlfID1)
    76  	require.Error(t, err) // not in db yet
    77  	err = db.Put(ctx, tlfID1, ir1, sr1)
    78  	require.NoError(t, err)
    79  
    80  	t.Log("Get TLF MD from the db.")
    81  	getIR1, getSR1, err := db.Get(ctx, tlfID1)
    82  	require.NoError(t, err)
    83  	checkWrite := func(
    84  		expectedIR, ir, expectedSR, sr kbfsmd.Revision) {
    85  		require.Equal(t, expectedIR, ir)
    86  		require.Equal(t, expectedSR, sr)
    87  	}
    88  	checkWrite(ir1, getIR1, sr1, getSR1)
    89  
    90  	t.Log("A second entry.")
    91  	tlfID2 := tlf.FakeID(2, tlf.Private)
    92  	ir2 := kbfsmd.Revision(500)
    93  	sr2 := kbfsmd.Revision(600)
    94  
    95  	err = db.Put(ctx, tlfID2, ir2, sr2)
    96  	require.NoError(t, err)
    97  	getIR2, getSR2, err := db.Get(ctx, tlfID2)
    98  	require.NoError(t, err)
    99  	checkWrite(ir2, getIR2, sr2, getSR2)
   100  
   101  	t.Log("Override the first block with new start revision")
   102  	sr1 = kbfsmd.Revision(60)
   103  	err = db.Put(ctx, tlfID1, ir1, sr1)
   104  	require.NoError(t, err)
   105  	getIR1, getSR1, err = db.Get(ctx, tlfID1)
   106  	require.NoError(t, err)
   107  	checkWrite(ir1, getIR1, sr1, getSR1)
   108  
   109  	t.Log("Restart the db and check the MD")
   110  	db.Shutdown(ctx)
   111  	tlfS, err := storage.OpenFile(filepath.Join(tempdir, "tlfs"), false)
   112  	require.NoError(t, err)
   113  	db = newIndexedTlfDbForTestWithStorage(t, tlfS)
   114  	getIR1, getSR1, err = db.Get(ctx, tlfID1)
   115  	require.NoError(t, err)
   116  	checkWrite(ir1, getIR1, sr1, getSR1)
   117  	getIR2, getSR2, err = db.Get(ctx, tlfID2)
   118  	require.NoError(t, err)
   119  	checkWrite(ir2, getIR2, sr2, getSR2)
   120  }