github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/nbs/factory_test.go (about)

     1  // Copyright 2017 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package nbs
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/attic-labs/noms/go/chunks"
    14  	"github.com/attic-labs/noms/go/constants"
    15  	"github.com/attic-labs/noms/go/hash"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestLocalStoreFactory(t *testing.T) {
    20  	assert := assert.New(t)
    21  	dir := makeTempDir(t)
    22  	defer os.RemoveAll(dir)
    23  
    24  	f := NewLocalStoreFactory(dir, 0, 8)
    25  	stats := &Stats{}
    26  
    27  	dbName := "db"
    28  	store := f.CreateStore(dbName)
    29  
    30  	c := chunks.NewChunk([]byte{0xff})
    31  	store.Put(c)
    32  	assert.True(store.Commit(c.Hash(), hash.Hash{}))
    33  
    34  	dbDir := filepath.Join(dir, dbName)
    35  	exists, contents := fileManifest{dbDir}.ParseIfExists(stats, nil)
    36  	assert.True(exists)
    37  	assert.Len(contents.specs, 1)
    38  
    39  	_, err := os.Stat(filepath.Join(dbDir, contents.specs[0].name.String()))
    40  	assert.NoError(err)
    41  
    42  	// Simulate another process writing a manifest.
    43  	lock := computeAddr([]byte("locker"))
    44  	newRoot := hash.Of([]byte("new root"))
    45  	err = clobberManifest(dbDir, strings.Join([]string{StorageVersion, constants.NomsVersion, lock.String(), newRoot.String(), contents.specs[0].name.String(), "1"}, ":"))
    46  	assert.NoError(err)
    47  
    48  	cached := f.CreateStoreFromCache(dbName)
    49  	assert.Equal(c.Hash(), cached.Root())
    50  }