2022-05-08 09:00:22 +00:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-08-07 10:22:21 +00:00
|
|
|
"time"
|
2022-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/maxatome/go-testdeep/td"
|
2023-03-01 17:22:31 +00:00
|
|
|
redisCli "github.com/redis/go-redis/v9"
|
2022-05-08 09:00:22 +00:00
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/check"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/config"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/grammar"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/protocols/redis"
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChecks_Execute(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
redisModule := redis.Module()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
check string
|
|
|
|
setup func(tb testing.TB, cli redisCli.UniversalClient)
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Get value",
|
|
|
|
check: `redis.GET("%s", "some_key")`,
|
|
|
|
setup: func(tb testing.TB, cli redisCli.UniversalClient) {
|
|
|
|
tb.Helper()
|
|
|
|
td.CmpNoError(tb, cli.Set(context.Background(), "some_key", "some_value", 0).Err())
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get value - validate value",
|
2022-06-09 15:20:06 +00:00
|
|
|
check: `redis.GET("%s", "some_key") => Equals("some_value")`,
|
2022-05-08 09:00:22 +00:00
|
|
|
setup: func(tb testing.TB, cli redisCli.UniversalClient) {
|
|
|
|
tb.Helper()
|
|
|
|
td.CmpNoError(tb, cli.Set(context.Background(), "some_key", "some_value", 0).Err())
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PING check",
|
|
|
|
check: `redis.PING("%s")`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PING check - with custom message",
|
|
|
|
check: `redis.PING("%s", "Hello, Redis!")`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-12-04 15:59:10 +00:00
|
|
|
{
|
|
|
|
name: "SET check",
|
|
|
|
check: `redis.SET("%s", "Hello", "World!")`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
srv := PrepareRedisContainer(t)
|
|
|
|
serverName := uuid.NewString()
|
|
|
|
|
|
|
|
register := config.NewServerRegister()
|
|
|
|
|
|
|
|
if err := register.Register(serverName, *srv); err != nil {
|
|
|
|
t.Fatalf("DefaultLookup.Register() err = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := redis.ClientForServer(srv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("redis.ClientForServer() err = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.setup != nil {
|
|
|
|
tt.setup(t, cli)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(tt.check, "%s") {
|
|
|
|
tt.check = fmt.Sprintf(tt.check, serverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser, err := grammar.NewParser[grammar.Check]()
|
|
|
|
td.CmpNoError(t, err, "grammar.NewParser()")
|
|
|
|
parsedCheck, err := parser.Parse(tt.check)
|
|
|
|
td.CmpNoError(t, err, "parser.Parse()")
|
|
|
|
|
|
|
|
chk, err := redisModule.Lookup(*parsedCheck, register)
|
|
|
|
td.CmpNoError(t, err, "redis.LookupCheck()")
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
ctx, cancel := check.AttemptsContext(context.Background(), 100, 500*time.Millisecond)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
if tt.wantErr {
|
2022-08-07 10:22:21 +00:00
|
|
|
td.CmpError(t, chk.Execute(ctx))
|
2022-05-08 09:00:22 +00:00
|
|
|
} else {
|
2022-08-07 10:22:21 +00:00
|
|
|
td.CmpNoError(t, chk.Execute(ctx))
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|