...

Source file src/k8s.io/kubectl/pkg/explain/v2/generator_test.go

Documentation: k8s.io/kubectl/pkg/explain/v2

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v2
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/client-go/openapi/openapitest"
    27  )
    28  
    29  var appsv1Path = "apis/apps/v1"
    30  
    31  var appsDeploymentGVR = schema.GroupVersionResource{
    32  	Group:    "apps",
    33  	Version:  "v1",
    34  	Resource: "deployments",
    35  }
    36  
    37  // Shows generic throws error when attempting to `Render“ an invalid output name
    38  // And if it is then added as a template, no error is thrown upon `Render`
    39  func TestGeneratorMissingOutput(t *testing.T) {
    40  	var buf bytes.Buffer
    41  	var doc map[string]interface{}
    42  
    43  	appsv1Bytes := bytesForGV(t, appsv1Path)
    44  	err := json.Unmarshal(appsv1Bytes, &doc)
    45  	require.NoError(t, err)
    46  
    47  	gen := NewGenerator()
    48  	badTemplateName := "bad-template"
    49  	err = gen.Render(badTemplateName, doc, appsDeploymentGVR, nil, false, &buf)
    50  
    51  	require.ErrorContains(t, err, "unrecognized format: "+badTemplateName)
    52  	require.Zero(t, buf.Len())
    53  
    54  	err = gen.AddTemplate(badTemplateName, "ok")
    55  	require.NoError(t, err)
    56  
    57  	err = gen.Render(badTemplateName, doc, appsDeploymentGVR, nil, false, &buf)
    58  	require.NoError(t, err)
    59  	require.Equal(t, "ok", buf.String())
    60  }
    61  
    62  // Shows that correct context with the passed object is passed to the template
    63  func TestGeneratorContext(t *testing.T) {
    64  	var buf bytes.Buffer
    65  	var doc map[string]interface{}
    66  
    67  	appsv1Bytes := bytesForGV(t, appsv1Path)
    68  	err := json.Unmarshal(appsv1Bytes, &doc)
    69  	require.NoError(t, err)
    70  
    71  	gen := NewGenerator()
    72  	err = gen.AddTemplate("Context", "{{ toJson . }}")
    73  	require.NoError(t, err)
    74  
    75  	expectedContext := TemplateContext{
    76  		Document:  doc,
    77  		GVR:       appsDeploymentGVR,
    78  		Recursive: false,
    79  		FieldPath: nil,
    80  	}
    81  
    82  	err = gen.Render("Context",
    83  		expectedContext.Document,
    84  		expectedContext.GVR,
    85  		expectedContext.FieldPath,
    86  		expectedContext.Recursive,
    87  		&buf)
    88  	require.NoError(t, err)
    89  
    90  	var actualContext TemplateContext
    91  	err = json.Unmarshal(buf.Bytes(), &actualContext)
    92  	require.NoError(t, err)
    93  	require.Equal(t, expectedContext, actualContext)
    94  }
    95  
    96  // bytesForGV returns the OpenAPI V3 spec for the passed
    97  // group/version as a byte slice. Assumes bytes are in json
    98  // format. The passed path string looks like:
    99  //
   100  //	apis/apps/v1
   101  func bytesForGV(t *testing.T, gvPath string) []byte {
   102  	fakeClient := openapitest.NewEmbeddedFileClient()
   103  	paths, err := fakeClient.Paths()
   104  	require.NoError(t, err)
   105  	gv, found := paths[gvPath]
   106  	require.True(t, found)
   107  	gvBytes, err := gv.Schema("application/json")
   108  	require.NoError(t, err)
   109  	return gvBytes
   110  }
   111  

View as plain text