istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/check/visitor.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package check 16 17 import ( 18 "fmt" 19 20 "github.com/hashicorp/go-multierror" 21 22 echoClient "istio.io/istio/pkg/test/echo" 23 "istio.io/istio/pkg/test/framework/components/echo" 24 "istio.io/istio/pkg/util/istiomultierror" 25 ) 26 27 // Visitor is performs a partial check operation on a single message. 28 type Visitor func(echoClient.Response) error 29 30 // Visit is a utility method that just invokes this Visitor function on the given response. 31 func (v Visitor) Visit(r echoClient.Response) error { 32 return v(r) 33 } 34 35 // And returns a Visitor that performs a logical AND of this Visitor and the one provided. 36 func (v Visitor) And(o Visitor) Visitor { 37 return func(r echoClient.Response) error { 38 if err := v(r); err != nil { 39 return err 40 } 41 return o(r) 42 } 43 } 44 45 // Or returns a Visitor that performs a logical OR of this Visitor and the one provided. 46 func (v Visitor) Or(o Visitor) Visitor { 47 return func(r echoClient.Response) error { 48 if err := v(r); err != nil { 49 return err 50 } 51 return o(r) 52 } 53 } 54 55 // Checker returns an echo.Checker based on this Visitor. 56 func (v Visitor) Checker() echo.Checker { 57 return func(result echo.CallResult, _ error) error { 58 rs := result.Responses 59 if rs.IsEmpty() { 60 return fmt.Errorf("no responses received") 61 } 62 outErr := istiomultierror.New() 63 for i, r := range rs { 64 if err := v.Visit(r); err != nil { 65 outErr = multierror.Append(outErr, fmt.Errorf("response[%d]: %v", i, err)) 66 } 67 } 68 return outErr.ErrorOrNil() 69 } 70 }