bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/database/test/database_test.go (about)

     1  package dbtest
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  
    12  	"bosun.org/cmd/bosun/database"
    13  )
    14  
    15  // data access object to use for all unit tests. Pointed at ephemeral ledis, or redis server passed in with --redis=addr
    16  var testData database.DataAccess
    17  
    18  func TestMain(m *testing.M) {
    19  	rand.Seed(time.Now().UnixNano())
    20  	var closeF func()
    21  	testData, closeF = StartTestRedis(9993)
    22  	status := m.Run()
    23  	closeF()
    24  	os.Exit(status)
    25  }
    26  
    27  var cleanups = []func(){}
    28  
    29  // use random keys in tests to avoid conflicting test data.
    30  func randString(l int) string {
    31  	s := ""
    32  	for len(s) < l {
    33  		s += string("abcdefghijklmnopqrstuvwxyz"[rand.Intn(26)])
    34  	}
    35  	return s
    36  }
    37  
    38  func check(t *testing.T, err error) {
    39  	if err != nil {
    40  		s := err.Error()
    41  		if _, filename, line, ok := runtime.Caller(1); ok {
    42  			s = fmt.Sprintf("%s:%d: %v", filepath.Base(filename), line, s)
    43  		}
    44  		t.Fatal(s)
    45  	}
    46  }