@@ -16,7 +16,9 @@ package krttest
1616
1717import (
1818 "fmt"
19+ "strings"
1920
21+ kubelib "istio.io/istio/pkg/kube"
2022 "istio.io/istio/pkg/kube/krt"
2123 "istio.io/istio/pkg/slices"
2224 "istio.io/istio/pkg/test"
@@ -35,7 +37,19 @@ type MockCollection struct {
3537// pods := krttest.GetMockCollection[Pod](mock) // makes a collection of all Pod types from inputs
3638func NewMock (t test.Failer , inputs []any ) * MockCollection {
3739 t .Helper ()
38- mc := & MockCollection {t : t , inputs : inputs }
40+ finalInputs := make ([]any , 0 , len (inputs ))
41+ for _ , input := range inputs {
42+ if s , ok := input .(string ); ok {
43+ objs , err := kubernetesObjectsFromString (s )
44+ if err != nil {
45+ t .Fatalf ("invalid yaml: %v" , err )
46+ }
47+ finalInputs = append (finalInputs , objs ... )
48+ } else {
49+ finalInputs = append (finalInputs , input )
50+ }
51+ }
52+ mc := & MockCollection {t : t , inputs : finalInputs }
3953 t .Cleanup (func () {
4054 t .Helper ()
4155 types := slices .Map (mc .inputs , func (e any ) string {
@@ -83,3 +97,20 @@ func extractType[T any](items *[]any) []T {
8397func Options (t test.Failer ) krt.OptionsBuilder {
8498 return krt .NewOptionsBuilder (test .NewStop (t ), "test" , krt .GlobalDebugHandler )
8599}
100+
101+ func kubernetesObjectsFromString (s string ) ([]any , error ) {
102+ var objects []any
103+ decode := kubelib .IstioCodec .UniversalDeserializer ().Decode
104+ objectStrs := strings .Split (s , "---" )
105+ for _ , s := range objectStrs {
106+ if len (strings .TrimSpace (s )) == 0 {
107+ continue
108+ }
109+ o , _ , err := decode ([]byte (s ), nil , nil )
110+ if err != nil {
111+ return nil , fmt .Errorf ("failed deserializing kubernetes object: %v (%v)" , err , s )
112+ }
113+ objects = append (objects , o )
114+ }
115+ return objects , nil
116+ }
0 commit comments