...

Source file src/k8s.io/cli-runtime/pkg/resource/crd_finder_test.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2018 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 resource
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  )
    25  
    26  func TestCacheCRDFinder(t *testing.T) {
    27  	called := 0
    28  	getter := func() ([]schema.GroupKind, error) {
    29  		called += 1
    30  		return nil, nil
    31  	}
    32  	finder := NewCRDFinder(getter)
    33  	if called != 0 {
    34  		t.Fatalf("Creating the finder shouldn't call the getter, has called = %v", called)
    35  	}
    36  	_, err := finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
    37  	if err != nil {
    38  		t.Fatalf("Failed to call HasCRD: %v", err)
    39  	}
    40  	if called != 1 {
    41  		t.Fatalf("First call should call the getter, has called = %v", called)
    42  	}
    43  
    44  	_, err = finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
    45  	if err != nil {
    46  		t.Fatalf("Failed to call HasCRD: %v", err)
    47  	}
    48  	if called != 1 {
    49  		t.Fatalf("Second call should NOT call the getter, has called = %v", called)
    50  	}
    51  }
    52  
    53  func TestCRDFinderErrors(t *testing.T) {
    54  	getter := func() ([]schema.GroupKind, error) {
    55  		return nil, errors.New("not working")
    56  	}
    57  	finder := NewCRDFinder(getter)
    58  	found, err := finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
    59  	if found {
    60  		t.Fatalf("Found the CRD with non-working getter function")
    61  	}
    62  	if err == nil {
    63  		t.Fatalf("Error in getter should be reported")
    64  	}
    65  }
    66  
    67  func TestCRDFinder(t *testing.T) {
    68  	getter := func() ([]schema.GroupKind, error) {
    69  		return []schema.GroupKind{
    70  			{
    71  				Group: "crd.com",
    72  				Kind:  "MyCRD",
    73  			},
    74  			{
    75  				Group: "crd.com",
    76  				Kind:  "MyNewCRD",
    77  			},
    78  		}, nil
    79  	}
    80  	finder := NewCRDFinder(getter)
    81  
    82  	if found, _ := finder.HasCRD(schema.GroupKind{Group: "crd.com", Kind: "MyCRD"}); !found {
    83  		t.Fatalf("Failed to find CRD MyCRD")
    84  	}
    85  	if found, _ := finder.HasCRD(schema.GroupKind{Group: "crd.com", Kind: "Random"}); found {
    86  		t.Fatalf("Found crd Random that doesn't exist")
    87  	}
    88  }
    89  

View as plain text