summaryrefslogtreecommitdiffstats
path: root/tests/internal/utils
diff options
context:
space:
mode:
Diffstat (limited to 'tests/internal/utils')
-rw-r--r--tests/internal/utils/database.go28
-rw-r--r--tests/internal/utils/redis.go37
-rw-r--r--tests/internal/utils/slice.go37
-rw-r--r--tests/internal/utils/slice_test.go31
4 files changed, 133 insertions, 0 deletions
diff --git a/tests/internal/utils/database.go b/tests/internal/utils/database.go
new file mode 100644
index 0000000..c32566b
--- /dev/null
+++ b/tests/internal/utils/database.go
@@ -0,0 +1,28 @@
+package utils
+
+import (
+ "fmt"
+ "github.com/icinga/icinga-testing"
+ "github.com/icinga/icinga-testing/services"
+ "os"
+ "strings"
+ "testing"
+)
+
+func GetDatabase(it *icingatesting.IT, t testing.TB) services.RelationalDatabase {
+ k := "ICINGADB_TESTS_DATABASE_TYPE"
+ v := strings.ToLower(os.Getenv(k))
+
+ var rdb services.RelationalDatabase
+
+ switch v {
+ case "mysql":
+ rdb = it.MysqlDatabaseT(t)
+ case "pgsql":
+ rdb = it.PostgresqlDatabaseT(t)
+ default:
+ panic(fmt.Sprintf(`unknown database in %s environment variable: %q (must be "mysql" or "pgsql")`, k, v))
+ }
+
+ return rdb
+}
diff --git a/tests/internal/utils/redis.go b/tests/internal/utils/redis.go
new file mode 100644
index 0000000..da3615d
--- /dev/null
+++ b/tests/internal/utils/redis.go
@@ -0,0 +1,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
+}
diff --git a/tests/internal/utils/slice.go b/tests/internal/utils/slice.go
new file mode 100644
index 0000000..85821f5
--- /dev/null
+++ b/tests/internal/utils/slice.go
@@ -0,0 +1,37 @@
+package utils
+
+import (
+ "fmt"
+ "reflect"
+)
+
+// AnySliceToInterfaceSlice takes a slice of type []T for any T and returns a slice of type []interface{} containing
+// the same elements, somewhat like casting []T to []interface{}.
+func AnySliceToInterfaceSlice(in interface{}) []interface{} {
+ v := reflect.ValueOf(in)
+ if v.Kind() != reflect.Slice {
+ panic(fmt.Errorf("AnySliceToInterfaceSlice() called on %T instead of a slice type", in))
+ }
+
+ out := make([]interface{}, v.Len())
+ for i := 0; i < v.Len(); i++ {
+ out[i] = v.Index(i).Interface()
+ }
+ return out
+}
+
+func SliceSubsets(in ...string) [][]string {
+ result := make([][]string, 0, 1<<len(in))
+
+ for bitset := 0; bitset < (1 << len(in)); bitset++ {
+ var subset []string
+ for i := 0; i < len(in); i++ {
+ if bitset&(1<<i) != 0 {
+ subset = append(subset, in[i])
+ }
+ }
+ result = append(result, subset)
+ }
+
+ return result
+}
diff --git a/tests/internal/utils/slice_test.go b/tests/internal/utils/slice_test.go
new file mode 100644
index 0000000..c878226
--- /dev/null
+++ b/tests/internal/utils/slice_test.go
@@ -0,0 +1,31 @@
+package utils
+
+import (
+ "github.com/stretchr/testify/require"
+ "testing"
+)
+
+func TestSliceSubsets(t *testing.T) {
+ data := []string{
+ "bla",
+ "blub",
+ "derp",
+ }
+
+ result := SliceSubsets(data...)
+
+ expected := [][]string{
+ nil,
+ {"bla"},
+ {"blub"},
+ {"bla", "blub"},
+ {"derp"},
+ {"bla", "derp"},
+ {"blub", "derp"},
+ {"bla", "blub", "derp"},
+ }
+
+ require.Equal(t, expected, result)
+
+ t.Logf("%#v", result)
+}