github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/qdrmanagement/qdmanage.go (about) 1 package qdrmanagement 2 3 import ( 4 "encoding/json" 5 "github.com/interconnectedcloud/qdr-operator/test/e2e/framework" 6 "github.com/interconnectedcloud/qdr-operator/test/e2e/framework/qdrmanagement/entities" 7 "reflect" 8 "time" 9 ) 10 11 const ( 12 timeout time.Duration = 60 * time.Second 13 ) 14 15 var ( 16 queryCommand = []string{"qdmanage", "query", "--type"} 17 ) 18 19 // QdmanageQuery executes a "qdmanager query" command on the provided pod, returning 20 // a slice of entities of the provided "entity" type. 21 func QdmanageQuery(f *framework.Framework, pod string, entity entities.Entity, fn func(entities.Entity) bool) ([]entities.Entity, error) { 22 // Preparing command to execute 23 command := append(queryCommand, entity.GetEntityId()) 24 kubeExec := framework.NewKubectlExecCommand(f, pod, timeout, command...) 25 jsonString, err := kubeExec.Exec() 26 if err != nil { 27 return nil, err 28 } 29 30 // Using reflection to get a slice instance of the concrete type 31 vo := reflect.TypeOf(entity) 32 v := reflect.SliceOf(vo) 33 nv := reflect.New(v) 34 //fmt.Printf("v - %T - %v\n", v, v) 35 //fmt.Printf("nv - %T - %v\n", nv, nv) 36 37 // Unmarshalling to a slice of the concrete Entity type provided via "entity" instance 38 err = json.Unmarshal([]byte(jsonString), nv.Interface()) 39 if err != nil { 40 //fmt.Printf("ERROR: %v\n", err) 41 return nil, err 42 } 43 44 // Adding each parsed concrete Entity to the parsedEntities 45 parsedEntities := []entities.Entity{} 46 for i := 0; i < nv.Elem().Len(); i++ { 47 candidate := nv.Elem().Index(i).Interface().(entities.Entity) 48 49 // If no filter function provided, just add 50 if fn == nil { 51 parsedEntities = append(parsedEntities, candidate) 52 continue 53 } 54 55 // Otherwhise invoke to determine whether to include 56 if fn(candidate) { 57 parsedEntities = append(parsedEntities, candidate) 58 } 59 } 60 61 return parsedEntities, err 62 }