agones.dev/agones@v1.54.0/pkg/util/crd/crd_test.go (about) 1 // Copyright 2018 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package crd 16 17 import ( 18 "context" 19 "sync" 20 "testing" 21 "time" 22 23 "github.com/sirupsen/logrus" 24 "github.com/stretchr/testify/assert" 25 apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 26 extfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" 27 "k8s.io/apimachinery/pkg/runtime" 28 k8stesting "k8s.io/client-go/testing" 29 ) 30 31 func TestWaitForEstablishedCRD(t *testing.T) { 32 t.Parallel() 33 crd := &apiextv1.CustomResourceDefinition{ 34 Status: apiextv1.CustomResourceDefinitionStatus{ 35 Conditions: []apiextv1.CustomResourceDefinitionCondition{{ 36 Type: apiextv1.Established, 37 Status: apiextv1.ConditionTrue, 38 }}, 39 }, 40 } 41 42 t.Run("CRD already established", func(t *testing.T) { 43 extClient := &extfake.Clientset{} 44 extClient.AddReactor("get", "customresourcedefinitions", func(_ k8stesting.Action) (bool, runtime.Object, error) { 45 return true, crd, nil 46 }) 47 48 err := WaitForEstablishedCRD(context.Background(), extClient.ApiextensionsV1().CustomResourceDefinitions(), "test", logrus.WithField("test", "already-established")) 49 assert.Nil(t, err) 50 }) 51 52 t.Run("CRD takes a second to become established", func(t *testing.T) { 53 extClient := &extfake.Clientset{} 54 m := sync.RWMutex{} 55 established := false 56 57 extClient.AddReactor("get", "customresourcedefinitions", func(_ k8stesting.Action) (bool, runtime.Object, error) { 58 m.RLock() 59 defer m.RUnlock() 60 if established { 61 return true, crd, nil 62 } 63 return false, nil, nil 64 }) 65 66 go func() { 67 time.Sleep(3 * time.Second) 68 m.Lock() 69 defer m.Unlock() 70 established = true 71 }() 72 73 err := WaitForEstablishedCRD(context.Background(), extClient.ApiextensionsV1().CustomResourceDefinitions(), "test", logrus.WithField("test", "already-established")) 74 assert.Nil(t, err) 75 }) 76 }