Mainflux.mainflux/things/redis/setup_test.go

57 lines
1000 B
Go
Raw Normal View History

MF-384 - Add test for Redis cache (#405) * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * MF-384 - Add test for Redis cache Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add copyright headers Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix copyright header Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix test setup Signed-off-by: Ivan Milošević <iva@blokovi.com> * handling errors and put test cases in maps instead of slices Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test case if thing already exists Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix TestThingSave to use require instead of assert Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test case for removing non-existing thing from cache Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use table of test cases for Remove and test Connect for every case Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use table of test cases for Save Signed-off-by: Ivan Milošević <iva@blokovi.com> * Test cases in slice instead of map for testing Remove Signed-off-by: Ivan Milošević <iva@blokovi.com> * Make test-cases independent, use asserts instead of requires Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use slice and map where appropriate Signed-off-by: Ivan Milošević <iva@blokovi.com>
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"
)
var cacheClient *redis.Client
MF-384 - Add test for Redis cache (#405) * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test Signed-off-by: Ivan Milošević <iva@blokovi.com> * MF-384 - Add test for Redis cache Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add copyright headers Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix copyright header Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix test setup Signed-off-by: Ivan Milošević <iva@blokovi.com> * handling errors and put test cases in maps instead of slices Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test case if thing already exists Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix TestThingSave to use require instead of assert Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add test case for removing non-existing thing from cache Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use table of test cases for Remove and test Connect for every case Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use table of test cases for Save Signed-off-by: Ivan Milošević <iva@blokovi.com> * Test cases in slice instead of map for testing Remove Signed-off-by: Ivan Milošević <iva@blokovi.com> * Make test-cases independent, use asserts instead of requires Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use slice and map where appropriate Signed-off-by: Ivan Milošević <iva@blokovi.com>
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)
}
container, err := pool.Run("redis", "4.0.9-alpine", nil)
if err != nil {
log.Fatalf("Could not start container: %s", err)
}
// When you're done, kill and remove the container
defer pool.Purge(container)
if err := pool.Retry(func() error {
cacheClient = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("localhost:%s", container.GetPort("6379/tcp")),
Password: "",
DB: 0,
})
return cacheClient.Ping().Err()
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
code := m.Run()
os.Exit(code)
}