2018-09-30 08:03:00 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
|
|
|
dockertest "gopkg.in/ory-am/dockertest.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
wrongID = 0
|
|
|
|
wrongValue = "wrong-value"
|
|
|
|
)
|
|
|
|
|
2018-11-21 02:46:33 +08:00
|
|
|
var redisClient *redis.Client
|
2018-09-30 08:03:00 +08:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:46:33 +08:00
|
|
|
container, err := pool.Run("redis", "5.0-alpine", nil)
|
2018-09-30 08:03:00 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not start container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pool.Retry(func() error {
|
2018-11-21 02:46:33 +08:00
|
|
|
redisClient = redis.NewClient(&redis.Options{
|
2018-09-30 08:03:00 +08:00
|
|
|
Addr: fmt.Sprintf("localhost:%s", container.GetPort("6379/tcp")),
|
|
|
|
Password: "",
|
|
|
|
DB: 0,
|
|
|
|
})
|
|
|
|
|
2018-11-21 02:46:33 +08:00
|
|
|
return redisClient.Ping().Err()
|
2018-09-30 08:03:00 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
2018-11-21 02:46:33 +08:00
|
|
|
if err := pool.Purge(container); err != nil {
|
|
|
|
log.Fatalf("Could not purge container: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 08:03:00 +08:00
|
|
|
os.Exit(code)
|
|
|
|
}
|