knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/sorter_test.go (about)

     1  /*
     2  Copyright 2019 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  package testing
    17  
    18  import (
    19  	"testing"
    20  
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  )
    25  
    26  func TestObjectSorter(t *testing.T) {
    27  	sorter := NewObjectSorter(testScheme())
    28  
    29  	sorter.AddObjects(
    30  		newTestObject1("first"),
    31  		newTestObject2("second"),
    32  		newTestObject2("third"),
    33  		newTestObject2("fourth"),
    34  	)
    35  
    36  	objects := sorter.ObjectsForScheme(testSchemeSubset1())
    37  
    38  	if got, want := len(objects), 1; got != want {
    39  		t.Errorf("ObjectsForScheme did not return the correct number of elements - got %d, want %d",
    40  			got, want)
    41  	}
    42  
    43  	objects = sorter.ObjectsForSchemeFunc(testSchemeSubset2Func)
    44  
    45  	if got, want := len(objects), 3; got != want {
    46  		t.Errorf("ObjectsForSchemeFunc did not return the correct number of elements - got %d, want %d",
    47  			got, want)
    48  	}
    49  
    50  	indexer := sorter.IndexerForObjectType(&testObject2{})
    51  
    52  	if got, want := len(indexer.List()), 3; got != want {
    53  		t.Errorf("IndexerForObjectType did not return the correct number of elements - got %d, want %d",
    54  			got, want)
    55  	}
    56  }
    57  
    58  func TestObjectSorterAddUnrecognizedType(t *testing.T) {
    59  	defer func() {
    60  		if recover() == nil {
    61  			t.Error("AddObjects did not panic when receiving an unrecognized type ")
    62  		}
    63  	}()
    64  
    65  	sorter := NewObjectSorter(testSchemeSubset1())
    66  	sorter.AddObjects(
    67  		&testObject2{},
    68  	)
    69  }
    70  
    71  func TestObjectSorterIndexerUnrecognizedType(t *testing.T) {
    72  	defer func() {
    73  		if recover() == nil {
    74  			t.Error("IndexerForObjectType did not panic when receiving an unrecognized type ")
    75  		}
    76  	}()
    77  
    78  	sorter := NewObjectSorter(testSchemeSubset1())
    79  	sorter.IndexerForObjectType(&testObject2{})
    80  }
    81  
    82  func testScheme() *runtime.Scheme {
    83  	scheme := runtime.NewScheme()
    84  	scheme.AddKnownTypes(
    85  		schema.GroupVersion{
    86  			Group:   "test.group",
    87  			Version: "v1",
    88  		},
    89  		&testObject1{},
    90  		&testObject2{},
    91  	)
    92  	return scheme
    93  }
    94  
    95  func testSchemeSubset1() *runtime.Scheme {
    96  	scheme := runtime.NewScheme()
    97  	scheme.AddKnownTypes(
    98  		schema.GroupVersion{
    99  			Group:   "test.group",
   100  			Version: "v1",
   101  		},
   102  		&testObject1{},
   103  	)
   104  	return scheme
   105  }
   106  
   107  func testSchemeSubset2Func(scheme *runtime.Scheme) error {
   108  	scheme.AddKnownTypes(
   109  		schema.GroupVersion{
   110  			Group:   "test.group",
   111  			Version: "v1",
   112  		},
   113  		&testObject2{},
   114  	)
   115  	return nil
   116  }
   117  
   118  type testObject1 struct {
   119  	metav1.TypeMeta
   120  	metav1.ObjectMeta
   121  }
   122  
   123  type testObject2 struct {
   124  	metav1.TypeMeta
   125  	metav1.ObjectMeta
   126  }
   127  
   128  func (in *testObject1) DeepCopyObject() runtime.Object {
   129  	return in
   130  }
   131  
   132  func (in *testObject2) DeepCopyObject() runtime.Object {
   133  	return in
   134  }
   135  
   136  func newTestObject1(name string) *testObject1 {
   137  	return &testObject1{
   138  		ObjectMeta: metav1.ObjectMeta{
   139  			Name: name,
   140  		},
   141  	}
   142  }
   143  
   144  func newTestObject2(name string) *testObject2 {
   145  	return &testObject2{
   146  		ObjectMeta: metav1.ObjectMeta{
   147  			Name: name,
   148  		},
   149  	}
   150  }