44package featuregates
55
66import (
7+ "errors"
78 "testing"
89
910 . "github.com/onsi/ginkgo/v2"
@@ -13,10 +14,42 @@ import (
1314 resourcev1beta1 "k8s.io/api/resource/v1beta1"
1415 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516 version "k8s.io/apimachinery/pkg/version"
17+ "k8s.io/client-go/discovery"
1618 fakediscovery "k8s.io/client-go/discovery/fake"
1719 "k8s.io/client-go/kubernetes/fake"
1820)
1921
22+ // erroringDiscovery fails the requested discovery call, standing in for an API
23+ // server that is briefly unreachable while a component starts up.
24+ type erroringDiscovery struct {
25+ discovery.DiscoveryInterface
26+ versionErr error
27+ groupsErr error
28+ }
29+
30+ func (e * erroringDiscovery ) ServerVersion () (* version.Info , error ) {
31+ if e .versionErr != nil {
32+ return nil , e .versionErr
33+ }
34+ return e .DiscoveryInterface .ServerVersion ()
35+ }
36+
37+ func (e * erroringDiscovery ) ServerGroups () (* metav1.APIGroupList , error ) {
38+ if e .groupsErr != nil {
39+ return nil , e .groupsErr
40+ }
41+ return e .DiscoveryInterface .ServerGroups ()
42+ }
43+
44+ func discoveryForVersion (major , minor string ) discovery.DiscoveryInterface {
45+ fakeClient := fake .NewClientset ()
46+ fakeClient .Discovery ().(* fakediscovery.FakeDiscovery ).FakedServerVersion = & version.Info {
47+ Major : major ,
48+ Minor : minor ,
49+ }
50+ return fakeClient .Discovery ()
51+ }
52+
2053func TestCache (t * testing.T ) {
2154 RegisterFailHandler (Fail )
2255 RunSpecs (t , "Test cache" )
@@ -46,5 +79,75 @@ var _ = Describe("New", func() {
4679 Entry ("edge case version (1.31) with resource API should not enable DRA" , "1" , "31" , []string {resourcev1alhpa3 .SchemeGroupVersion .String ()}, false ),
4780 Entry ("higher compatible version (1.35) with resource API should enable DRA" , "1" , "34" , []string {resourcev1 .SchemeGroupVersion .String ()}, true ),
4881 )
82+
83+ It ("should return an error when the server version cannot be retrieved" , func () {
84+ discoveryClient := & erroringDiscovery {
85+ DiscoveryInterface : discoveryForVersion ("1" , "34" ),
86+ versionErr : errors .New ("connection refused" ),
87+ }
88+
89+ _ , err := IsDynamicResourcesEnabled (discoveryClient )
90+ Expect (err ).To (HaveOccurred ())
91+ })
92+
93+ It ("should return an error when the server groups cannot be retrieved" , func () {
94+ discoveryClient := & erroringDiscovery {
95+ DiscoveryInterface : discoveryForVersion ("1" , "34" ),
96+ groupsErr : errors .New ("connection refused" ),
97+ }
98+
99+ _ , err := IsDynamicResourcesEnabled (discoveryClient )
100+ Expect (err ).To (HaveOccurred ())
101+ })
102+
103+ It ("should leave the gate untouched when discovery fails" , func () {
104+ SetDynamicResourcesEnabledForTest (true )
105+ DeferCleanup (SetDynamicResourcesEnabledForTest , false )
106+
107+ discoveryClient := & erroringDiscovery {
108+ DiscoveryInterface : discoveryForVersion ("1" , "34" ),
109+ versionErr : errors .New ("connection refused" ),
110+ }
111+
112+ Expect (SetDRAFeatureGate (discoveryClient )).NotTo (Succeed ())
113+ Expect (DynamicResourcesEnabled ()).To (BeTrue ())
114+ })
115+ })
116+
117+ Context ("NodeResourceTopology Feature Gate" , func () {
118+ It ("should report availability when the API group is served" , func () {
119+ fakeClient := fake .NewClientset ()
120+ fakeClient .Resources = append (fakeClient .Resources ,
121+ & metav1.APIResourceList {GroupVersion : nodeResourceTopologyGroup + "/v1alpha2" })
122+
123+ Expect (IsNodeResourceTopologyEnabled (fakeClient .Discovery ())).To (BeTrue ())
124+ })
125+
126+ It ("should report unavailability when the API group is absent" , func () {
127+ Expect (IsNodeResourceTopologyEnabled (fake .NewClientset ().Discovery ())).To (BeFalse ())
128+ })
129+
130+ It ("should return an error when the server groups cannot be retrieved" , func () {
131+ discoveryClient := & erroringDiscovery {
132+ DiscoveryInterface : fake .NewClientset ().Discovery (),
133+ groupsErr : errors .New ("connection refused" ),
134+ }
135+
136+ _ , err := IsNodeResourceTopologyEnabled (discoveryClient )
137+ Expect (err ).To (HaveOccurred ())
138+ })
139+
140+ It ("should leave the gate untouched when discovery fails" , func () {
141+ SetNodeResourceTopologyEnabledForTest (true )
142+ DeferCleanup (SetNodeResourceTopologyEnabledForTest , false )
143+
144+ discoveryClient := & erroringDiscovery {
145+ DiscoveryInterface : fake .NewClientset ().Discovery (),
146+ groupsErr : errors .New ("connection refused" ),
147+ }
148+
149+ Expect (SetNodeResourceTopologyFeatureGate (discoveryClient )).NotTo (Succeed ())
150+ Expect (NodeResourceTopologyEnabled ()).To (BeTrue ())
151+ })
49152 })
50153})
0 commit comments