istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/echotest/setup.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 echotest 16 17 import ( 18 "istio.io/istio/pkg/test/framework" 19 "istio.io/istio/pkg/test/framework/components/echo" 20 "istio.io/istio/pkg/test/scopes" 21 ) 22 23 type ( 24 srcSetupFn func(t framework.TestContext, from echo.Callers) error 25 svcPairSetupFn func(t framework.TestContext, from echo.Callers, to echo.Services) error 26 dstSetupFn func(t framework.TestContext, to echo.Target) error 27 ) 28 29 // Setup runs the given function in the source deployment context. 30 // 31 // For example, given apps a, b, and c in 2 clusters, 32 // these tests would all run before the context is cleaned up: 33 // - a/to_b/from_cluster-1 34 // - a/to_b/from_cluster-2 35 // - a/to_c/from_cluster-1 36 // - a/to_c/from_cluster-2 37 // - cleanup... 38 // - b/to_a/from_cluster-1 39 // ... 40 func (t *T) Setup(setupFn srcSetupFn) *T { 41 t.sourceDeploymentSetup = append(t.sourceDeploymentSetup, setupFn) 42 return t 43 } 44 45 func (t *T) setup(ctx framework.TestContext, from echo.Callers) { 46 if !t.hasSourceSetup() { 47 ctx.SkipDumping() 48 scopes.Framework.Debugf("No echotest setup; skipping test dump at this scope.") 49 } 50 for _, setupFn := range t.sourceDeploymentSetup { 51 if err := setupFn(ctx, from); err != nil { 52 ctx.Fatal(err) 53 } 54 } 55 } 56 57 func (t *T) hasSourceSetup() bool { 58 return len(t.sourceDeploymentSetup) > 0 59 } 60 61 // SetupForPair runs the given function for every source instance in every cluster in combination with every 62 // destination service. 63 // 64 // Example of how long this setup lasts before the given context is cleaned up: 65 // - a/to_b/from_cluster-1 66 // - a/to_b/from_cluster-2 67 // - cleanup... 68 // - a/to_b/from_cluster-2 69 // - ... 70 func (t *T) SetupForPair(setupFn func(ctx framework.TestContext, from echo.Callers, dsts echo.Instances) error) *T { 71 return t.SetupForServicePair(func(ctx framework.TestContext, from echo.Callers, dsts echo.Services) error { 72 return setupFn(ctx, from, dsts.Instances()) 73 }) 74 } 75 76 // SetupForServicePair works similarly to SetupForPair, but the setup function accepts echo.Services, which 77 // contains instances for multiple services and should be used in combination with RunForN. 78 // The length of dsts services will always be N. 79 func (t *T) SetupForServicePair(setupFn svcPairSetupFn) *T { 80 t.deploymentPairSetup = append(t.deploymentPairSetup, setupFn) 81 return t 82 } 83 84 // SetupForDestination is run each time the destination Service (but not destination cluster) changes. 85 func (t *T) SetupForDestination(setupFn dstSetupFn) *T { 86 t.destinationDeploymentSetup = append(t.destinationDeploymentSetup, setupFn) 87 return t 88 } 89 90 func (t *T) hasDestinationSetup() bool { 91 return len(t.deploymentPairSetup)+len(t.destinationDeploymentSetup) > 0 92 } 93 94 func (t *T) setupPair(ctx framework.TestContext, from echo.Callers, dsts echo.Services) { 95 if !t.hasDestinationSetup() { 96 ctx.SkipDumping() 97 scopes.Framework.Debugf("No echotest setup; skipping test dump at this scope.") 98 } 99 for _, setupFn := range t.deploymentPairSetup { 100 if err := setupFn(ctx, from, dsts); err != nil { 101 ctx.Fatal(err) 102 } 103 } 104 for _, setupFn := range t.destinationDeploymentSetup { 105 if err := setupFn(ctx, dsts.Instances()); err != nil { 106 ctx.Fatal(err) 107 } 108 } 109 }