github.com/hairyhenderson/templater@v3.5.0+incompatible/tests/integration/datasources_boltdb_test.go (about)

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	. "gopkg.in/check.v1"
     7  
     8  	"github.com/boltdb/bolt"
     9  	"github.com/gotestyourself/gotestyourself/fs"
    10  	"github.com/gotestyourself/gotestyourself/icmd"
    11  )
    12  
    13  type BoltDBDatasourcesSuite struct {
    14  	tmpDir *fs.Dir
    15  }
    16  
    17  var _ = Suite(&BoltDBDatasourcesSuite{})
    18  
    19  func (s *BoltDBDatasourcesSuite) SetUpSuite(c *C) {
    20  	s.tmpDir = fs.NewDir(c, "gomplate-inttests")
    21  	db, err := bolt.Open(s.tmpDir.Join("config.db"), 0600, nil)
    22  	handle(c, err)
    23  	defer db.Close()
    24  
    25  	err = db.Update(func(tx *bolt.Tx) error {
    26  		var b *bolt.Bucket
    27  		b, err = tx.CreateBucket([]byte("Bucket1"))
    28  		if err != nil {
    29  			return err
    30  		}
    31  		// the first 8 bytes are ignored when read by libkv, so we prefix with gibberish
    32  		err = b.Put([]byte("foo"), []byte("00000000bar"))
    33  		if err != nil {
    34  			return err
    35  		}
    36  
    37  		b, err = tx.CreateBucket([]byte("Bucket2"))
    38  		if err != nil {
    39  			return err
    40  		}
    41  		err = b.Put([]byte("foobar"), []byte("00000000baz"))
    42  		return err
    43  	})
    44  	handle(c, err)
    45  }
    46  
    47  func (s *BoltDBDatasourcesSuite) TearDownSuite(c *C) {
    48  	s.tmpDir.Remove()
    49  }
    50  
    51  func (s *BoltDBDatasourcesSuite) TestBoltDBDatasource(c *C) {
    52  	result := icmd.RunCommand(GomplateBin,
    53  		"-d", "config=boltdb://"+s.tmpDir.Join("config.db#Bucket1"),
    54  		"-i", `{{(ds "config" "foo")}}`,
    55  	)
    56  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"})
    57  
    58  	result = icmd.RunCommand(GomplateBin,
    59  		"-d", "config=boltdb://"+s.tmpDir.Join("config.db#Bucket1"),
    60  		"-d", "config2=boltdb://"+s.tmpDir.Join("config.db#Bucket2"),
    61  		"-i", `{{(ds "config" "foo")}}-{{(ds "config2" "foobar")}}`,
    62  	)
    63  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar-baz"})
    64  }