bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/database/test/testSetup.go (about) 1 package dbtest 2 3 import ( 4 "flag" 5 "fmt" 6 "log" 7 "os" 8 "path/filepath" 9 "time" 10 11 "bosun.org/cmd/bosun/database" 12 ) 13 14 var flagReddisHost = flag.String("redis", "", "redis server to test against") 15 var flagFlushRedis = flag.Bool("flush", false, "flush database before tests. DANGER!") 16 17 func StartTestRedis(port int) (database.DataAccess, func()) { 18 flag.Parse() 19 // For redis tests we just point at an external server. 20 if *flagReddisHost != "" { 21 testData := database.NewDataAccess([]string{*flagReddisHost}, true, "", 0, "") 22 if *flagFlushRedis { 23 log.Println("FLUSHING REDIS") 24 c := testData.(database.RedisConnector).Get() 25 defer c.Close() 26 _, err := c.Do("FLUSHDB") 27 if err != nil { 28 log.Fatal(err) 29 } 30 } 31 return testData, func() {} 32 } 33 // To test ledis, start a local instance in a new tmp dir. We will attempt to delete it when we're done. 34 addr := fmt.Sprintf("127.0.0.1:%d", port) 35 testPath := filepath.Join(os.TempDir(), "bosun_ledis_test", fmt.Sprintf("%d-%d", time.Now().UnixNano(), port)) 36 log.Println("Test ledis at", testPath, addr) 37 stop, err := database.StartLedis(testPath, addr) 38 if err != nil { 39 log.Fatal(err) 40 } 41 testData := database.NewDataAccess([]string{addr}, false, "", 0, "") 42 return testData, func() { 43 stop() 44 os.RemoveAll(testPath) 45 } 46 }