From 64907ecc42ea17acbd68ccdd4b32d560e0c673e0 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 7 Nov 2024 16:01:03 +0100 Subject: pkg/manager: add test for all HTTP templates It's easy to make mistakes in templates that lead to runtime panics. Catch them during testing. This required to convert some pointers to values for things that must not be nil. Otherwise the randomized test can pass nil for the objects and templates fail. --- pkg/testutil/testutil.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'pkg/testutil') diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go index 67dee7ddf..153584dd5 100644 --- a/pkg/testutil/testutil.go +++ b/pkg/testutil/testutil.go @@ -6,8 +6,10 @@ package testutil import ( "math/rand" "os" + "reflect" "strconv" "testing" + "testing/quick" "time" ) @@ -41,3 +43,50 @@ func RandMountImage(r *rand.Rand) []byte { r.Read(slice) return slice } + +// RandValue creates a random value of the same type as the argument typ. +// It recursively fills structs/slices/maps similar to testing/quick.Value, +// but it handles time.Time as well w/o panicing (unfortunately testing/quick panics on time.Time). +func RandValue(t *testing.T, typ any) any { + return randValue(t, rand.New(RandSource(t)), reflect.TypeOf(typ)).Interface() +} + +func randValue(t *testing.T, rnd *rand.Rand, typ reflect.Type) reflect.Value { + v := reflect.New(typ).Elem() + switch typ.Kind() { + default: + ok := false + v, ok = quick.Value(typ, rnd) + if !ok { + t.Fatalf("failed to generate random value of type %v", typ) + } + case reflect.Slice: + size := rand.Intn(4) + v.Set(reflect.MakeSlice(typ, size, size)) + fallthrough + case reflect.Array: + for i := 0; i < v.Len(); i++ { + v.Index(i).Set(randValue(t, rnd, typ.Elem())) + } + case reflect.Struct: + if typ.String() == "time.Time" { + v = reflect.ValueOf(time.UnixMilli(rnd.Int63())) + } else { + for i := 0; i < v.NumField(); i++ { + v.Field(i).Set(randValue(t, rnd, typ.Field(i).Type)) + } + } + case reflect.Pointer: + v.SetZero() + if rand.Intn(2) == 0 { + v.Set(reflect.New(typ.Elem())) + v.Elem().Set(randValue(t, rnd, typ.Elem())) + } + case reflect.Map: + v.Set(reflect.MakeMap(typ)) + for i := rand.Intn(4); i > 0; i-- { + v.SetMapIndex(randValue(t, rnd, typ.Key()), randValue(t, rnd, typ.Elem())) + } + } + return v +} -- cgit mrf-deployment