istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/resource/context.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 resource 16 17 import ( 18 "istio.io/istio/pkg/test/framework/components/cluster" 19 "istio.io/istio/pkg/test/framework/resource/config" 20 "istio.io/istio/pkg/test/framework/resource/config/cleanup" 21 "istio.io/istio/pkg/test/util/yml" 22 ) 23 24 // Context is the core context interface that is used by resources. 25 type Context interface { 26 yml.FileWriter 27 28 // TrackResource tracks a resource in this context. If the context is closed, then the resource will be 29 // cleaned up. 30 TrackResource(r Resource) ID 31 32 // GetResource accepts either a *T or *[]*T where T implements Resource. 33 // For a non-slice pointer, the value will be assigned to the first matching resource. 34 // For a slice pointer, the matching resources from this scope and its parent(s) will be appended. 35 // If ref is not a pointer, an error will be returned. 36 // If there is no match for a non-slice pointer, an error will be returned. 37 GetResource(ref any) error 38 39 // The Environment in which the tests run 40 Environment() Environment 41 42 // Clusters in this Environment. There will always be at least one. 43 Clusters() cluster.Clusters 44 45 // AllClusters in this Environment, including external control planes. 46 AllClusters() cluster.Clusters 47 48 // Settings returns common settings 49 Settings() *Settings 50 51 // Cleanup will trigger the provided cleanup function after the test context 52 // completes. This is identical to CleanupStrategy(Always). 53 // This function may not (safely) access the test context. 54 Cleanup(fn func()) 55 56 // CleanupConditionally will trigger a cleanup operation the test context 57 // completes, unless -istio.test.nocleanup is set. This is identical to 58 // CleanupStrategy(Conditionally). 59 // This function may not (safely) access the test context. 60 CleanupConditionally(fn func()) 61 62 // CleanupStrategy runs the given cleanup function after the test context completes, 63 // depending on the provided strategy. 64 // This function may not (safely) access the test context. 65 CleanupStrategy(strategy cleanup.Strategy, fn func()) 66 67 // CreateDirectory creates a new subdirectory within this context. 68 CreateDirectory(name string) (string, error) 69 70 // CreateTmpDirectory creates a new temporary directory within this context. 71 CreateTmpDirectory(prefix string) (string, error) 72 73 // ConfigKube returns a Context that writes config to the provided clusters. If 74 // no clusters are provided, writes to all clusters in the mesh. 75 ConfigKube(clusters ...cluster.Cluster) config.Factory 76 77 // ConfigIstio returns a Context that writes config to all Istio config clusters. 78 ConfigIstio() config.Factory 79 80 // RecordTraceEvent records an event. This is later saved to trace.yaml for analysis 81 RecordTraceEvent(key string, value any) 82 // Id returns the name of the context 83 ID() string 84 }