...

Source file src/k8s.io/client-go/tools/events/helper_test.go

Documentation: k8s.io/client-go/tools/events

     1  /*
     2  Copyright 2021 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 events
    18  
    19  import (
    20  	"testing"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	eventsv1 "k8s.io/api/events/v1"
    24  	eventsv1beta1 "k8s.io/api/events/v1beta1"
    25  	"k8s.io/apimachinery/pkg/fields"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/types"
    28  )
    29  
    30  func TestGetFieldSelector(t *testing.T) {
    31  	tests := []struct {
    32  		desc                      string
    33  		eventsGroupVersion        schema.GroupVersion
    34  		regardingName             string
    35  		regardingGroupVersionKind schema.GroupVersionKind
    36  		regardingUID              types.UID
    37  		expected                  fields.Set
    38  		expectedErr               bool
    39  	}{
    40  		{
    41  			desc:                      "events.k8s.io/v1beta1 event with empty parameters",
    42  			eventsGroupVersion:        eventsv1beta1.SchemeGroupVersion,
    43  			regardingName:             "",
    44  			regardingGroupVersionKind: schema.GroupVersionKind{},
    45  			regardingUID:              "",
    46  			expected:                  fields.Set{},
    47  			expectedErr:               false,
    48  		},
    49  		{
    50  			desc:               "events.k8s.io/v1beta1 event with non-empty parameters",
    51  			eventsGroupVersion: eventsv1beta1.SchemeGroupVersion,
    52  			regardingName:      "test-deployment",
    53  			regardingGroupVersionKind: schema.GroupVersionKind{
    54  				Kind:    "Deployment",
    55  				Group:   "apps",
    56  				Version: "v1",
    57  			},
    58  			regardingUID: "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
    59  			expected: fields.Set{
    60  				"regarding.name":       "test-deployment",
    61  				"regarding.kind":       "Deployment",
    62  				"regarding.apiVersion": "apps/v1",
    63  				"regarding.uid":        "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
    64  			},
    65  			expectedErr: false,
    66  		},
    67  		{
    68  			desc:                      "events.k8s.io/v1 event with empty parameters",
    69  			eventsGroupVersion:        eventsv1.SchemeGroupVersion,
    70  			regardingName:             "",
    71  			regardingGroupVersionKind: schema.GroupVersionKind{},
    72  			regardingUID:              "",
    73  			expected:                  fields.Set{},
    74  			expectedErr:               false,
    75  		},
    76  		{
    77  			desc:               "events.k8s.io/v1 event with non-empty parameters",
    78  			eventsGroupVersion: eventsv1.SchemeGroupVersion,
    79  			regardingName:      "test-deployment",
    80  			regardingGroupVersionKind: schema.GroupVersionKind{
    81  				Kind:    "Deployment",
    82  				Group:   "apps",
    83  				Version: "v1",
    84  			},
    85  			regardingUID: "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
    86  			expected: fields.Set{
    87  				"regarding.name":       "test-deployment",
    88  				"regarding.kind":       "Deployment",
    89  				"regarding.apiVersion": "apps/v1",
    90  				"regarding.uid":        "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
    91  			},
    92  			expectedErr: false,
    93  		},
    94  		{
    95  			desc:                      "v1 event with non-empty parameters",
    96  			eventsGroupVersion:        corev1.SchemeGroupVersion,
    97  			regardingName:             "",
    98  			regardingGroupVersionKind: schema.GroupVersionKind{},
    99  			regardingUID:              "",
   100  			expected:                  fields.Set{},
   101  			expectedErr:               false,
   102  		},
   103  		{
   104  			desc:               "v1 event with non-empty parameters",
   105  			eventsGroupVersion: corev1.SchemeGroupVersion,
   106  			regardingName:      "test-deployment",
   107  			regardingGroupVersionKind: schema.GroupVersionKind{
   108  				Kind:    "Deployment",
   109  				Group:   "apps",
   110  				Version: "v1",
   111  			},
   112  			regardingUID: "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
   113  			expected: fields.Set{
   114  				"involvedObject.name":       "test-deployment",
   115  				"involvedObject.kind":       "Deployment",
   116  				"involvedObject.apiVersion": "apps/v1",
   117  				"involvedObject.uid":        "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
   118  			},
   119  			expectedErr: false,
   120  		},
   121  		{
   122  			desc:               "unknown group version",
   123  			eventsGroupVersion: schema.GroupVersion{Group: corev1.GroupName, Version: "v1alpha1"},
   124  			regardingName:      "test-deployment",
   125  			regardingGroupVersionKind: schema.GroupVersionKind{
   126  				Kind:    "Deployment",
   127  				Group:   "apps",
   128  				Version: "v1",
   129  			},
   130  			regardingUID: "2c55cad7-ee4e-11e9-abe1-525400e7bc6b",
   131  			expected:     nil,
   132  			expectedErr:  true,
   133  		},
   134  	}
   135  
   136  	for _, test := range tests {
   137  		result, err := GetFieldSelector(test.eventsGroupVersion, test.regardingGroupVersionKind, test.regardingName, test.regardingUID)
   138  		if !test.expectedErr && err != nil {
   139  			t.Errorf("Unable to get field selector with %v", err)
   140  		}
   141  		if test.expectedErr && err == nil {
   142  			t.Errorf("Expect error but got nil")
   143  		}
   144  		if test.expected == nil && result != nil {
   145  			t.Errorf("Test %s expected <nil>, but got %v", test.desc, result)
   146  		}
   147  		if result != nil && !result.Matches(test.expected) {
   148  			t.Errorf("Test %s expected %v, but got %v", test.desc, test.expected.AsSelector(), result)
   149  		}
   150  	}
   151  }
   152  

View as plain text