...

Source file src/k8s.io/kubectl/pkg/util/openapi/testing/openapi.go

Documentation: k8s.io/kubectl/pkg/util/openapi/testing

     1  /*
     2  Copyright 2017 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 testing
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/runtime/schema"
    21  	"k8s.io/kube-openapi/pkg/util/proto"
    22  	"k8s.io/kube-openapi/pkg/util/proto/testing"
    23  	"k8s.io/kubectl/pkg/util/openapi"
    24  )
    25  
    26  // FakeResources is a wrapper to directly load the openapi schema from a
    27  // file, and get the schema for given GVK. This is only for test since
    28  // it's assuming that the file is there and everything will go fine.
    29  type FakeResources struct {
    30  	fake testing.Fake
    31  }
    32  
    33  var _ openapi.Resources = &FakeResources{}
    34  
    35  // NewFakeResources creates a new FakeResources.
    36  func NewFakeResources(path string) *FakeResources {
    37  	return &FakeResources{
    38  		fake: testing.Fake{Path: path},
    39  	}
    40  }
    41  
    42  // LookupResource will read the schema, parse it and return the
    43  // resources. It doesn't return errors and will panic instead.
    44  func (f *FakeResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
    45  	s, err := f.fake.OpenAPISchema()
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	resources, err := openapi.NewOpenAPIData(s)
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  	return resources.LookupResource(gvk)
    54  }
    55  
    56  func (f *FakeResources) GetConsumes(gvk schema.GroupVersionKind, operation string) []string {
    57  	s, err := f.fake.OpenAPISchema()
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  	resources, err := openapi.NewOpenAPIData(s)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	return resources.GetConsumes(gvk, operation)
    67  }
    68  
    69  // EmptyResources implement a Resources that just doesn't have any resources.
    70  type EmptyResources struct{}
    71  
    72  var _ openapi.Resources = EmptyResources{}
    73  
    74  // LookupResource will always return nil. It doesn't have any resources.
    75  func (f EmptyResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
    76  	return nil
    77  }
    78  
    79  func (f EmptyResources) GetConsumes(gvk schema.GroupVersionKind, operation string) []string {
    80  	return nil
    81  }
    82  
    83  // CreateOpenAPISchemaFunc returns a function useful for the TestFactory.
    84  func CreateOpenAPISchemaFunc(path string) func() (openapi.Resources, error) {
    85  	return func() (openapi.Resources, error) {
    86  		return NewFakeResources(path), nil
    87  	}
    88  }
    89  

View as plain text