knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmeta/labels_test.go (about)

     1  /*
     2  Copyright 2018 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 kmeta
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  )
    24  
    25  func TestMakeVersionLabels(t *testing.T) {
    26  	tests := []struct {
    27  		name string
    28  		om   metav1.ObjectMeta
    29  		s    string
    30  	}{{
    31  		name: "simple translation",
    32  		om: metav1.ObjectMeta{
    33  			UID:             "1234",
    34  			ResourceVersion: "abcd",
    35  		},
    36  		s: "controller=1234,version=abcd",
    37  	}, {
    38  		name: "another simple translation",
    39  		om: metav1.ObjectMeta{
    40  			UID:             "abcd",
    41  			ResourceVersion: "1234",
    42  		},
    43  		s: "controller=abcd,version=1234",
    44  	}}
    45  
    46  	for _, test := range tests {
    47  		t.Run(test.name, func(t *testing.T) {
    48  			ls := MakeVersionLabels(&test.om)
    49  			if want, got := test.s, ls.String(); got != want {
    50  				t.Errorf("MakeVersionLabels() = %v, wanted %v", got, want)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestMakeVersionLabelSelector(t *testing.T) {
    57  	tests := []struct {
    58  		name string
    59  		om   metav1.ObjectMeta
    60  		s    string
    61  	}{{
    62  		name: "simple translation",
    63  		om: metav1.ObjectMeta{
    64  			UID:             "1234",
    65  			ResourceVersion: "abcd",
    66  		},
    67  		s: "controller=1234,version=abcd",
    68  	}, {
    69  		name: "another simple translation",
    70  		om: metav1.ObjectMeta{
    71  			UID:             "abcd",
    72  			ResourceVersion: "1234",
    73  		},
    74  		s: "controller=abcd,version=1234",
    75  	}}
    76  
    77  	for _, test := range tests {
    78  		t.Run(test.name, func(t *testing.T) {
    79  			ls := MakeVersionLabelSelector(&test.om)
    80  			if want, got := test.s, ls.String(); got != want {
    81  				t.Errorf("MakeVersionLabelSelector() = %v, wanted %v", got, want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestMakeOldVersionLabelSelector(t *testing.T) {
    88  	tests := []struct {
    89  		name string
    90  		om   metav1.ObjectMeta
    91  		s    string
    92  	}{{
    93  		name: "simple translation",
    94  		om: metav1.ObjectMeta{
    95  			UID:             "1234",
    96  			ResourceVersion: "abcd",
    97  		},
    98  		s: "controller=1234,version!=abcd",
    99  	}, {
   100  		name: "another simple translation",
   101  		om: metav1.ObjectMeta{
   102  			UID:             "abcd",
   103  			ResourceVersion: "1234",
   104  		},
   105  		s: "controller=abcd,version!=1234",
   106  	}}
   107  
   108  	for _, test := range tests {
   109  		t.Run(test.name, func(t *testing.T) {
   110  			ls := MakeOldVersionLabelSelector(&test.om)
   111  			if want, got := test.s, ls.String(); got != want {
   112  				t.Errorf("MakeOldVersionLabelSelector() = %v, wanted %v", got, want)
   113  			}
   114  		})
   115  	}
   116  }
   117  
   118  func TestMakeGenerationLabels(t *testing.T) {
   119  	tests := []struct {
   120  		name string
   121  		om   metav1.ObjectMeta
   122  		s    string
   123  	}{{
   124  		name: "simple translation",
   125  		om: metav1.ObjectMeta{
   126  			UID:        "1234",
   127  			Generation: 5,
   128  		},
   129  		s: "controller=1234,generation=00005",
   130  	}, {
   131  		name: "another simple translation",
   132  		om: metav1.ObjectMeta{
   133  			UID:        "abcd",
   134  			Generation: 5432,
   135  		},
   136  		s: "controller=abcd,generation=05432",
   137  	}}
   138  
   139  	for _, test := range tests {
   140  		t.Run(test.name, func(t *testing.T) {
   141  			ls := MakeGenerationLabels(&test.om)
   142  			if want, got := test.s, ls.String(); got != want {
   143  				t.Errorf("MakeGenerationLabels() = %v, wanted %v", got, want)
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func TestMakeGenerationLabelSelector(t *testing.T) {
   150  	tests := []struct {
   151  		name string
   152  		om   metav1.ObjectMeta
   153  		s    string
   154  	}{{
   155  		name: "simple translation",
   156  		om: metav1.ObjectMeta{
   157  			UID:        "1234",
   158  			Generation: 5,
   159  		},
   160  		s: "controller=1234,generation=00005",
   161  	}, {
   162  		name: "another simple translation",
   163  		om: metav1.ObjectMeta{
   164  			UID:        "abcd",
   165  			Generation: 5432,
   166  		},
   167  		s: "controller=abcd,generation=05432",
   168  	}}
   169  
   170  	for _, test := range tests {
   171  		t.Run(test.name, func(t *testing.T) {
   172  			ls := MakeGenerationLabelSelector(&test.om)
   173  			if want, got := test.s, ls.String(); got != want {
   174  				t.Errorf("MakeGenerationLabelSelector() = %v, wanted %v", got, want)
   175  			}
   176  		})
   177  	}
   178  }
   179  
   180  func TestMakeOldGenerationLabelSelector(t *testing.T) {
   181  	tests := []struct {
   182  		name string
   183  		om   metav1.ObjectMeta
   184  		s    string
   185  	}{{
   186  		name: "simple translation",
   187  		om: metav1.ObjectMeta{
   188  			UID:        "1234",
   189  			Generation: 5,
   190  		},
   191  		s: "controller=1234,generation!=00005",
   192  	}, {
   193  		name: "another simple translation",
   194  		om: metav1.ObjectMeta{
   195  			UID:        "abcd",
   196  			Generation: 5432,
   197  		},
   198  		s: "controller=abcd,generation!=05432",
   199  	}}
   200  
   201  	for _, test := range tests {
   202  		t.Run(test.name, func(t *testing.T) {
   203  			ls := MakeOldGenerationLabelSelector(&test.om)
   204  			if want, got := test.s, ls.String(); got != want {
   205  				t.Errorf("MakeOldGenerationLabelSelector() = %v, wanted %v", got, want)
   206  			}
   207  		})
   208  	}
   209  }