knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kref/knative_reference_resolve_group_test.go (about)

     1  /*
     2  Copyright 2021 The Knative 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 kref
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/client-go/rest"
    27  
    28  	. "knative.dev/pkg/apis/duck/v1"
    29  	customresourcedefinitioninformer "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1/customresourcedefinition/fake"
    30  	"knative.dev/pkg/injection"
    31  )
    32  
    33  func TestResolveGroup(t *testing.T) {
    34  	const crdGroup = "messaging.knative.dev"
    35  	const crdName = "inmemorychannels." + crdGroup
    36  
    37  	ctx, _ := injection.Fake.SetupInformers(context.TODO(), &rest.Config{})
    38  
    39  	fakeCrdInformer := customresourcedefinitioninformer.Get(ctx)
    40  	fakeCrdInformer.Informer().GetIndexer().Add(
    41  		&apiextensionsv1.CustomResourceDefinition{
    42  			ObjectMeta: metav1.ObjectMeta{
    43  				Name: crdName,
    44  			},
    45  			Spec: apiextensionsv1.CustomResourceDefinitionSpec{
    46  				Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{
    47  					Name:    "v1beta1",
    48  					Storage: false,
    49  				}, {
    50  					Name:    "v1",
    51  					Storage: true,
    52  				}},
    53  			},
    54  		},
    55  	)
    56  
    57  	tests := map[string]struct {
    58  		input   *KReference
    59  		output  *KReference
    60  		wantErr bool
    61  	}{
    62  		"No group": {
    63  			input: &KReference{
    64  				Kind:       "Abc",
    65  				Name:       "123",
    66  				APIVersion: "something/v1",
    67  			},
    68  			output: &KReference{
    69  				Kind:       "Abc",
    70  				Name:       "123",
    71  				APIVersion: "something/v1",
    72  			},
    73  		},
    74  		"No group nor api version": {
    75  			input: &KReference{
    76  				Kind: "Abc",
    77  				Name: "123",
    78  			},
    79  			output: &KReference{
    80  				Kind: "Abc",
    81  				Name: "123",
    82  			},
    83  		},
    84  		"imc channel": {
    85  			input: &KReference{
    86  				Kind:  "InMemoryChannel",
    87  				Name:  "my-cool-channel",
    88  				Group: crdGroup,
    89  			},
    90  			output: &KReference{
    91  				Kind:       "InMemoryChannel",
    92  				Name:       "my-cool-channel",
    93  				Group:      crdGroup,
    94  				APIVersion: crdGroup + "/v1",
    95  			},
    96  		},
    97  		"unknown CRD": {
    98  			input: &KReference{
    99  				Kind:  "MyChannel",
   100  				Name:  "my-cool-channel",
   101  				Group: crdGroup,
   102  			},
   103  			wantErr: true,
   104  		},
   105  	}
   106  
   107  	for name, tc := range tests {
   108  		t.Run(name, func(t *testing.T) {
   109  			kr, err := NewKReferenceResolver(fakeCrdInformer.Lister()).ResolveGroup(tc.input)
   110  			if err != nil {
   111  				if !tc.wantErr {
   112  					t.Error("ResolveGroup() =", err)
   113  				}
   114  				return
   115  			} else if tc.wantErr {
   116  				t.Errorf("ResolveGroup() = %v, wanted error", err)
   117  				return
   118  			}
   119  
   120  			if tc.output != nil {
   121  				if !cmp.Equal(tc.output, kr) {
   122  					t.Errorf("ResolveGroup diff: (-want, +got) =\n%s", cmp.Diff(tc.input, tc.output))
   123  				}
   124  			}
   125  		})
   126  	}
   127  }