diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:40:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:40:59 +0000 |
commit | bc4e624732bd51c0dd1e9529cf228e8c23127732 (patch) | |
tree | d95dab8960e9d02d3b95f8653074ad2e54ca207c /tests/internal | |
parent | Initial commit. (diff) | |
download | icingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.tar.xz icingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.zip |
Adding upstream version 1.1.1.upstream/1.1.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | tests/internal/utils/database.go | 28 | ||||
-rw-r--r-- | tests/internal/utils/redis.go | 37 | ||||
-rw-r--r-- | tests/internal/utils/slice.go | 37 | ||||
-rw-r--r-- | tests/internal/utils/slice_test.go | 31 | ||||
-rw-r--r-- | tests/internal/value/notification_states.go | 37 | ||||
-rw-r--r-- | tests/internal/value/notification_types.go | 40 | ||||
-rw-r--r-- | tests/internal/value/value.go | 103 |
7 files changed, 313 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) +} diff --git a/tests/internal/value/notification_states.go b/tests/internal/value/notification_states.go new file mode 100644 index 0000000..3c5479e --- /dev/null +++ b/tests/internal/value/notification_states.go @@ -0,0 +1,37 @@ +package value + +import "fmt" + +type NotificationStates []string + +func (s NotificationStates) IcingaDbValue() interface{} { + v := uint(0) + + for _, s := range s { + if bit, ok := notificationStateMap[s]; ok { + v |= bit + } else { + panic(fmt.Errorf("unknown notification state %q", s)) + } + } + + return v +} + +func (s NotificationStates) Icinga2ConfigValue() string { + return ToIcinga2Config([]string(s)) +} + +func (s NotificationStates) Icinga2ApiValue() interface{} { + return ToIcinga2Api([]string(s)) +} + +// https://github.com/Icinga/icinga2/blob/a8f98cf72115d50152137bc924277b426f483a3f/lib/icinga/notification.hpp#L20-L32 +var notificationStateMap = map[string]uint{ + "OK": 1, + "Warning": 2, + "Critical": 4, + "Unknown": 8, + "Up": 16, + "Down": 32, +} diff --git a/tests/internal/value/notification_types.go b/tests/internal/value/notification_types.go new file mode 100644 index 0000000..f368ed5 --- /dev/null +++ b/tests/internal/value/notification_types.go @@ -0,0 +1,40 @@ +package value + +import "fmt" + +type NotificationTypes []string + +func (t NotificationTypes) IcingaDbValue() interface{} { + v := uint(0) + + for _, s := range t { + if bit, ok := notificationTypeMap[s]; ok { + v |= bit + } else { + panic(fmt.Errorf("unknown notification type %q", s)) + } + } + + return v +} + +func (t NotificationTypes) Icinga2ConfigValue() string { + return ToIcinga2Config([]string(t)) +} + +func (t NotificationTypes) Icinga2ApiValue() interface{} { + return ToIcinga2Api([]string(t)) +} + +// https://github.com/Icinga/icinga2/blob/a8f98cf72115d50152137bc924277b426f483a3f/lib/icinga/notification.hpp#L34-L50 +var notificationTypeMap = map[string]uint{ + "DowntimeStart": 1, + "DowntimeEnd": 2, + "DowntimeRemoved": 4, + "Custom": 8, + "Acknowledgement": 16, + "Problem": 32, + "Recovery": 64, + "FlappingStart": 128, + "FlappingEnd": 256, +} diff --git a/tests/internal/value/value.go b/tests/internal/value/value.go new file mode 100644 index 0000000..87027dd --- /dev/null +++ b/tests/internal/value/value.go @@ -0,0 +1,103 @@ +package value + +import ( + "fmt" + "reflect" +) + +func ToIcinga2Config(value interface{}) string { + if value == nil { + return "null" + } + + refVal := reflect.ValueOf(value) + switch refVal.Kind() { + case reflect.Slice: + vs := "" + for i := 0; i < refVal.Len(); i++ { + vs += ToIcinga2Config(refVal.Index(i).Interface()) + "," + } + return "[" + vs + "]" + case reflect.Map: + kvs := "" + iter := refVal.MapRange() + for iter.Next() { + kvs += ToIcinga2Config(iter.Key().Interface()) + "=" + ToIcinga2Config(iter.Value().Interface()) + "," + } + return "{" + kvs + "}" + } + + switch v := value.(type) { + case interface{ Icinga2ConfigValue() string }: + return v.Icinga2ConfigValue() + case string: + // TODO(jb): probably not perfect quoting, but good enough for now + return fmt.Sprintf("%q", v) + case *string: + if v != nil { + return ToIcinga2Config(*v) + } else { + return "null" + } + case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + return fmt.Sprintf("%v", v) + default: + panic(fmt.Errorf("ToIcinga2Config called on unknown type %T", value)) + } +} + +func ToIcinga2Api(value interface{}) interface{} { + if value == nil { + return nil + } + + refVal := reflect.ValueOf(value) + switch refVal.Kind() { + case reflect.Slice: + r := make([]interface{}, refVal.Len()) + for i := range r { + r[i] = ToIcinga2Api(refVal.Index(i).Interface()) + } + return r + case reflect.Map: + r := make(map[string]interface{}) + iter := refVal.MapRange() + for iter.Next() { + // TODO: perform a better check than the type assertion + r[ToIcinga2Api(iter.Key().Interface()).(string)] = ToIcinga2Api(iter.Value().Interface()) + } + return r + } + + switch v := value.(type) { + case interface{ Icinga2ApiValue() interface{} }: + return v.Icinga2ApiValue() + case string, []string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + return v + case *string: + if v != nil { + return ToIcinga2Api(*v) + } else { + return nil + } + default: + panic(fmt.Errorf("ToIcinga2Api called on unknown type %T", value)) + } +} + +func ToIcingaDb(value interface{}) interface{} { + switch v := value.(type) { + case interface{ IcingaDbValue() interface{} }: + return v.IcingaDbValue() + case bool: + if v { + return "y" + } else { + return "n" + } + case string, *string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + return v + default: + panic(fmt.Errorf("ToIcinga2Api called on unknown type %T", value)) + } +} |