@@ -23,9 +23,13 @@ import (
2323 "github.com/stretchr/testify/require"
2424 "helm.sh/helm/v3/pkg/chartutil"
2525 "helm.sh/helm/v3/pkg/engine"
26+ "k8s.io/apimachinery/pkg/api/meta"
2627 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728 "k8s.io/apimachinery/pkg/runtime"
29+ "k8s.io/apimachinery/pkg/runtime/schema"
2830 "k8s.io/apimachinery/pkg/util/yaml"
31+ "k8s.io/client-go/dynamic"
32+ "k8s.io/client-go/dynamic/fake"
2933 "k8s.io/kubectl/pkg/util/openapi"
3034 "k8s.io/kubectl/pkg/validation"
3135 k8sYaml "sigs.k8s.io/yaml"
@@ -112,7 +116,18 @@ func (r *runner) readAndValidateYAML(fileName, fileContents string, resources op
112116 return objs
113117}
114118
115- func (r * runner ) instantiateWorld (renderVals chartutil.Values , resources openapi.Resources ) map [string ]interface {} {
119+ type clientProviderFromDynamicClient struct {
120+ dynIface dynamic.Interface
121+ }
122+
123+ func (c clientProviderFromDynamicClient ) GetClientFor (apiVersion , kind string ) (dynamic.NamespaceableResourceInterface , bool , error ) {
124+ // This is suboptimal, but the best kind->resource translation we can do without a discovery client.
125+ gvr , _ := meta .UnsafeGuessKindToResource (schema .FromAPIVersionAndKind (apiVersion , kind ))
126+ namespaced := true // we infer this from the namespace argument to the lookup function
127+ return c .dynIface .Resource (gvr ), namespaced , nil
128+ }
129+
130+ func (r * runner ) instantiateWorld (renderVals chartutil.Values , resources openapi.Resources , objects []runtime.Object ) map [string ]interface {} {
116131 world := make (map [string ]interface {})
117132
118133 renderValsBytes , err := json .Marshal (renderVals )
@@ -123,9 +138,12 @@ func (r *runner) instantiateWorld(renderVals chartutil.Values, resources openapi
123138 if err := json .Unmarshal (renderValsBytes , & helmRenderVals ); err != nil {
124139 panic (errors .Wrap (err , "unmarshaling Helm render values" ))
125140 }
141+
142+ client := fake .NewSimpleDynamicClient (runtime .NewScheme (), objects ... )
143+ clientProvider := clientProviderFromDynamicClient {dynIface : client }
126144 world ["helm" ] = helmRenderVals
127145
128- renderedTemplates , err := ( & engine.Engine {}). Render (r .tgt .Chart , renderVals )
146+ renderedTemplates , err := engine .RenderWithClientProvider (r .tgt .Chart , renderVals , clientProvider )
129147
130148 if * r .test .ExpectError {
131149 r .Require ().Error (err , "expected rendering to fail" )
@@ -177,7 +195,7 @@ func (r *runner) instantiateWorld(renderVals chartutil.Values, resources openapi
177195 return world
178196}
179197
180- func (r * runner ) loadSchemas () (visible , available schemas.Schemas ) {
198+ func (r * runner ) loadServerSettings () (visible , available schemas.Schemas , objects []runtime. Object ) {
181199 var visibleSchemaNames , availableSchemaNames []string
182200 r .test .forEachScopeTopDown (func (t * Test ) {
183201 server := t .Server
@@ -198,6 +216,10 @@ func (r *runner) loadSchemas() (visible, available schemas.Schemas) {
198216 // Every visible schema is also available (but not vice versa)
199217 availableSchemaNames = append (availableSchemaNames , schemaName )
200218 }
219+ for _ , o := range server .Objects {
220+ obj := & unstructured.Unstructured {Object : o }
221+ objects = append (objects , obj .DeepCopyObject ())
222+ }
201223 })
202224
203225 availableSchemaNames = sliceutils .StringUnique (availableSchemaNames )
@@ -219,7 +241,7 @@ func (r *runner) loadSchemas() (visible, available schemas.Schemas) {
219241 visible = append (visible , schema )
220242 }
221243
222- return visible , available
244+ return visible , available , objects
223245}
224246
225247func (r * runner ) Run () {
@@ -241,7 +263,7 @@ func (r *runner) Run() {
241263 rel .apply (& releaseOpts )
242264 })
243265
244- visibleSchemas , availableSchemas := r .loadSchemas ()
266+ visibleSchemas , availableSchemas , availableObjects := r .loadServerSettings ()
245267
246268 caps := r .tgt .Capabilities
247269 if caps == nil {
@@ -265,8 +287,7 @@ func (r *runner) Run() {
265287
266288 renderVals , err := chartutil .ToRenderValues (r .tgt .Chart , values , releaseOpts , caps )
267289 r .Require ().NoError (err , "failed to obtain render values" )
268-
269- world := r .instantiateWorld (renderVals , availableSchemas )
290+ world := r .instantiateWorld (renderVals , availableSchemas , availableObjects )
270291 r .evaluatePredicates (world )
271292}
272293
0 commit comments