github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/storagetest/utils.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package storagetest
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/insolar/insolar/component"
    26  	"github.com/insolar/insolar/configuration"
    27  	"github.com/insolar/insolar/ledger/storage"
    28  	"github.com/insolar/insolar/testutils"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  type tmpDBOptions struct {
    34  	dir         string
    35  	nobootstrap bool
    36  }
    37  
    38  // Option provides functional option for TmpDB.
    39  type Option func(*tmpDBOptions)
    40  
    41  // Dir defines temporary directory for database.
    42  func Dir(dir string) Option {
    43  	return func(opts *tmpDBOptions) {
    44  		opts.dir = dir
    45  	}
    46  }
    47  
    48  // DisableBootstrap skip bootstrap records creation.
    49  func DisableBootstrap() Option {
    50  	return func(opts *tmpDBOptions) {
    51  		opts.nobootstrap = true
    52  	}
    53  }
    54  
    55  // TmpDB returns BadgerDB's storage implementation and cleanup function.
    56  //
    57  // Creates BadgerDB in temporary directory and uses t for errors reporting.
    58  func TmpDB(ctx context.Context, t testing.TB, options ...Option) (storage.DBContext, func()) {
    59  	opts := &tmpDBOptions{}
    60  	for _, o := range options {
    61  		o(opts)
    62  	}
    63  	tmpdir, err := ioutil.TempDir(opts.dir, "bdb-test-")
    64  	assert.NoError(t, err)
    65  
    66  	db, err := storage.NewDB(configuration.Ledger{
    67  		JetSizesHistoryDepth: 10,
    68  		Storage: configuration.Storage{
    69  			DataDirectory: tmpdir,
    70  		},
    71  	}, nil)
    72  	require.NoError(t, err)
    73  
    74  	cm := &component.Manager{}
    75  
    76  	cm.Inject(
    77  		testutils.NewPlatformCryptographyScheme(),
    78  		db,
    79  		storage.NewJetStorage(),
    80  		storage.NewObjectStorage(),
    81  		storage.NewDropStorage(10),
    82  		storage.NewPulseTracker(),
    83  	)
    84  
    85  	if !opts.nobootstrap {
    86  		gi := storage.NewGenesisInitializer()
    87  		cm.Inject(gi)
    88  	}
    89  
    90  	err = cm.Init(ctx)
    91  	if err != nil {
    92  		t.Error("ComponentManager init failed", err)
    93  	}
    94  	err = cm.Start(ctx)
    95  	if err != nil {
    96  		t.Error("ComponentManager start failed", err)
    97  	}
    98  
    99  	return db, func() {
   100  		rmErr := os.RemoveAll(tmpdir)
   101  		if rmErr != nil {
   102  			t.Fatal("temporary db dir cleanup failed", rmErr)
   103  		}
   104  	}
   105  }