...

Source file src/k8s.io/kubectl/pkg/explain/v2/explain_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  	"fmt"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/client-go/openapi/openapitest"
    28  )
    29  
    30  var apiGroupsPath = "apis/discovery.k8s.io/v1"
    31  
    32  var apiGroupsGVR = schema.GroupVersionResource{
    33  	Group:    "discovery.k8s.io",
    34  	Version:  "v1",
    35  	Resource: "apigroups",
    36  }
    37  
    38  func TestExplainErrors(t *testing.T) {
    39  	var buf bytes.Buffer
    40  
    41  	// Validate error when GVR is not found in returned paths map.
    42  	fakeClient := openapitest.NewFakeClient()
    43  	err := PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{
    44  		Group:    "test0.example.com",
    45  		Version:  "v1",
    46  		Resource: "doesntmatter",
    47  	}, false, "unknown-format")
    48  	require.ErrorContains(t, err, "couldn't find resource for \"test0.example.com/v1, Resource=doesntmatter\"")
    49  
    50  	// Validate error when openapi client returns error.
    51  	fakeClient.ForcedErr = fmt.Errorf("Always fails")
    52  	err = PrintModelDescription(nil, &buf, fakeClient, apiGroupsGVR, false, "unknown-format")
    53  	require.ErrorContains(t, err, "failed to fetch list of groupVersions")
    54  
    55  	// Validate error when GroupVersion "Schema()" call returns error.
    56  	fakeClient = openapitest.NewFakeClient()
    57  	forceErrorGV := openapitest.FakeGroupVersion{ForcedErr: fmt.Errorf("Always fails")}
    58  	fakeClient.PathsMap["apis/test1.example.com/v1"] = &forceErrorGV
    59  	err = PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{
    60  		Group:    "test1.example.com",
    61  		Version:  "v1",
    62  		Resource: "doesntmatter",
    63  	}, false, "unknown-format")
    64  	require.ErrorContains(t, err, "failed to fetch openapi schema ")
    65  
    66  	// Validate error when returned bytes from GroupVersion "Schema" are invalid.
    67  	parseErrorGV := openapitest.FakeGroupVersion{GVSpec: []byte(`<some invalid json!>`)}
    68  	fakeClient.PathsMap["apis/test2.example.com/v1"] = &parseErrorGV
    69  	err = PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{
    70  		Group:    "test2.example.com",
    71  		Version:  "v1",
    72  		Resource: "doesntmatter",
    73  	}, false, "unknown-format")
    74  	require.ErrorContains(t, err, "failed to parse openapi schema")
    75  
    76  	// Validate error when render template is not recognized.
    77  	client := openapitest.NewEmbeddedFileClient()
    78  	err = PrintModelDescription(nil, &buf, client, apiGroupsGVR, false, "unknown-format")
    79  	require.ErrorContains(t, err, "unrecognized format: unknown-format")
    80  }
    81  
    82  // Shows that the correct GVR is fetched from the open api client when
    83  // given to explain
    84  func TestExplainOpenAPIClient(t *testing.T) {
    85  	var buf bytes.Buffer
    86  
    87  	fileClient := openapitest.NewEmbeddedFileClient()
    88  	paths, err := fileClient.Paths()
    89  	require.NoError(t, err)
    90  	gv, found := paths[apiGroupsPath]
    91  	require.True(t, found)
    92  	discoveryBytes, err := gv.Schema("application/json")
    93  	require.NoError(t, err)
    94  
    95  	var doc map[string]interface{}
    96  	err = json.Unmarshal(discoveryBytes, &doc)
    97  	require.NoError(t, err)
    98  
    99  	gen := NewGenerator()
   100  	err = gen.AddTemplate("Context", "{{ toJson . }}")
   101  	require.NoError(t, err)
   102  
   103  	expectedContext := TemplateContext{
   104  		Document:  doc,
   105  		GVR:       apiGroupsGVR,
   106  		Recursive: false,
   107  		FieldPath: nil,
   108  	}
   109  
   110  	err = printModelDescriptionWithGenerator(gen, nil, &buf, fileClient, apiGroupsGVR, false, "Context")
   111  	require.NoError(t, err)
   112  
   113  	var actualContext TemplateContext
   114  	err = json.Unmarshal(buf.Bytes(), &actualContext)
   115  	require.NoError(t, err)
   116  	require.Equal(t, expectedContext, actualContext)
   117  }
   118  

View as plain text