...

Source file src/k8s.io/client-go/openapi/openapitest/fakeclient.go

Documentation: k8s.io/client-go/openapi/openapitest

     1  /*
     2  Copyright 2023 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 openapitest
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/client-go/openapi"
    24  )
    25  
    26  // FakeClient implements openapi.Client interface, with hard-coded
    27  // return values, including the possibility to force errors.
    28  type FakeClient struct {
    29  	// Hard-coded paths to return from Paths() function.
    30  	PathsMap map[string]openapi.GroupVersion
    31  	// Hard-coded returned error.
    32  	ForcedErr error
    33  }
    34  
    35  // Validate FakeClient implements openapi.Client interface.
    36  var _ openapi.Client = &FakeClient{}
    37  
    38  // NewFakeClient returns a fake openapi client with an empty PathsMap.
    39  func NewFakeClient() *FakeClient {
    40  	return &FakeClient{PathsMap: make(map[string]openapi.GroupVersion)}
    41  }
    42  
    43  // Paths returns stored PathsMap field, creating an empty one if
    44  // it does not already exist. If ForcedErr is set, this function
    45  // returns the error instead.
    46  func (f FakeClient) Paths() (map[string]openapi.GroupVersion, error) {
    47  	if f.ForcedErr != nil {
    48  		return nil, f.ForcedErr
    49  	}
    50  	return f.PathsMap, nil
    51  }
    52  
    53  // FakeGroupVersion implements openapi.GroupVersion with hard-coded
    54  // return GroupVersion specification bytes. If ForcedErr is set, then
    55  // "Schema()" function returns the error instead of the GVSpec.
    56  type FakeGroupVersion struct {
    57  	// Hard-coded GroupVersion specification
    58  	GVSpec []byte
    59  	// Hard-coded returned error.
    60  	ForcedErr error
    61  }
    62  
    63  // FileOpenAPIGroupVersion implements the openapi.GroupVersion interface.
    64  var _ openapi.GroupVersion = &FakeGroupVersion{}
    65  
    66  // Schema returns the hard-coded byte slice, including creating an
    67  // empty slice if it has not been set yet. If the ForcedErr is set,
    68  // this function returns the error instead of the GVSpec field. If
    69  // content type other than application/json is passed, and error is
    70  // returned.
    71  func (f FakeGroupVersion) Schema(contentType string) ([]byte, error) {
    72  	if contentType != runtime.ContentTypeJSON {
    73  		return nil, fmt.Errorf("application/json is only content type supported: %s", contentType)
    74  	}
    75  	if f.ForcedErr != nil {
    76  		return nil, f.ForcedErr
    77  	}
    78  	return f.GVSpec, nil
    79  }
    80  

View as plain text