Mainflux.mainflux/things/redis/setup_test.go

58 lines
1018 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 redisClient *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", "5.0-alpine", nil)
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
if err != nil {
log.Fatalf("Could not start container: %s", err)
}
if err := pool.Retry(func() error {
redisClient = redis.NewClient(&redis.Options{
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
Addr: fmt.Sprintf("localhost:%s", container.GetPort("6379/tcp")),
Password: "",
DB: 0,
})
return redisClient.Ping().Err()
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
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
code := m.Run()
if err := pool.Purge(container); err != nil {
log.Fatalf("Could not purge container: %s", err)
}
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
os.Exit(code)
}