summaryrefslogtreecommitdiffstats
path: root/tests/internal/utils/redis.go
blob: eaf2c2b34bf89631548bd936a1129209110370b0 (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/icinga/icinga-testing/services"
	"github.com/redis/go-redis/v9"
	"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
}