summaryrefslogtreecommitdiffstats
path: root/tests/internal/utils/redis.go
blob: da3615dc6596583e7cb853ac29b7b3278a46db1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package utils

import (
	"context"
	"encoding/hex"
	"encoding/json"
	"github.com/go-redis/redis/v8"
	"github.com/icinga/icinga-testing/services"
	"github.com/stretchr/testify/require"
	"testing"
	"time"
)

func GetEnvironmentIdFromRedis(t *testing.T, r services.RedisServer) []byte {
	conn := r.Open()
	defer conn.Close()

	heartbeat, err := conn.XRead(context.Background(), &redis.XReadArgs{
		Streams: []string{"icinga:stats", "0"},
		Count:   1,
		Block:   10 * time.Second,
	}).Result()
	require.NoError(t, err, "reading from icinga:stats failed")
	require.NotEmpty(t, heartbeat, "response contains no streams")
	require.NotEmpty(t, heartbeat[0].Messages, "response contains no messages")
	require.Contains(t, heartbeat[0].Messages[0].Values, "icingadb_environment",
		"icinga:stats message misses icingadb_environment")

	var envIdHex string
	err = json.Unmarshal([]byte(heartbeat[0].Messages[0].Values["icingadb_environment"].(string)), &envIdHex)
	require.NoError(t, err, "cannot parse environment ID as a JSON string")

	envId, err := hex.DecodeString(envIdHex)
	require.NoError(t, err, "environment ID is not a hex string")

	return envId
}