| 1 |
/* |
| 2 |
Copyright 2023 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 internalversion |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
|
| 24 |
apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
"k8s.io/apimachinery/pkg/fields" |
| 27 |
"k8s.io/apimachinery/pkg/labels" |
| 28 |
) |
| 29 |
|
| 30 |
func TestSetListOptionsDefaults(t *testing.T) { |
| 31 |
boolPtrFn := func(b bool) *bool { |
| 32 |
return &b |
| 33 |
} |
| 34 |
|
| 35 |
scenarios := []struct { |
| 36 |
name string |
| 37 |
watchListFeatureEnabled bool |
| 38 |
targetObj ListOptions |
| 39 |
expectedObj ListOptions |
| 40 |
}{ |
| 41 |
{ |
| 42 |
name: "no-op, RV doesn't match", |
| 43 |
watchListFeatureEnabled: true, |
| 44 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersion: "1"}, |
| 45 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersion: "1"}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "no-op, SendInitialEvents set", |
| 49 |
watchListFeatureEnabled: true, |
| 50 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(true)}, |
| 51 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(true)}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "no-op, ResourceVersionMatch set", |
| 55 |
watchListFeatureEnabled: true, |
| 56 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersionMatch: "m"}, |
| 57 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersionMatch: "m"}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "no-op, Watch=false", |
| 61 |
watchListFeatureEnabled: true, |
| 62 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything()}, |
| 63 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything()}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "defaults applied, match on empty RV", |
| 67 |
watchListFeatureEnabled: true, |
| 68 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true}, |
| 69 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(true), ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "defaults applied, match on RV=0", |
| 73 |
watchListFeatureEnabled: true, |
| 74 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersion: "0"}, |
| 75 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersion: "0", SendInitialEvents: boolPtrFn(true), ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "no-op, match on empty RV but watch-list fg is off", |
| 79 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true}, |
| 80 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true}, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "no-op, match on empty RV but SendInitialEvents is on", |
| 84 |
watchListFeatureEnabled: true, |
| 85 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(true)}, |
| 86 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(true)}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "no-op, match on empty RV but SendInitialEvents is off", |
| 90 |
watchListFeatureEnabled: true, |
| 91 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(false)}, |
| 92 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, SendInitialEvents: boolPtrFn(false)}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "no-op, match on empty RV but ResourceVersionMatch set", |
| 96 |
watchListFeatureEnabled: true, |
| 97 |
targetObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersionMatch: "m"}, |
| 98 |
expectedObj: ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything(), Watch: true, ResourceVersionMatch: "m"}, |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
for _, scenario := range scenarios { |
| 103 |
t.Run(scenario.name, func(t *testing.T) { |
| 104 |
SetListOptionsDefaults(&scenario.targetObj, scenario.watchListFeatureEnabled) |
| 105 |
if !apiequality.Semantic.DeepEqual(&scenario.expectedObj, &scenario.targetObj) { |
| 106 |
t.Errorf("expected and defaulted objects are different:\n%s", cmp.Diff(&scenario.expectedObj, &scenario.targetObj)) |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
} |