agones.dev/agones@v1.54.0/pkg/testing/controller.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 testing
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	gotesting "testing"
    21  	"time"
    22  
    23  	agonesfake "agones.dev/agones/pkg/client/clientset/versioned/fake"
    24  	"agones.dev/agones/pkg/client/informers/externalversions"
    25  	"github.com/sirupsen/logrus"
    26  	"github.com/stretchr/testify/assert"
    27  	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    28  	extfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
    29  	"k8s.io/client-go/informers"
    30  	kubefake "k8s.io/client-go/kubernetes/fake"
    31  	"k8s.io/client-go/tools/cache"
    32  	"k8s.io/client-go/tools/record"
    33  )
    34  
    35  // Handy tools for testing controllers
    36  
    37  // Mocks is a holder for all my fakes and Mocks
    38  type Mocks struct {
    39  	KubeClient            *kubefake.Clientset
    40  	KubeInformerFactory   informers.SharedInformerFactory
    41  	ExtClient             *extfake.Clientset
    42  	AgonesClient          *agonesfake.Clientset
    43  	AgonesInformerFactory externalversions.SharedInformerFactory
    44  	FakeRecorder          *record.FakeRecorder
    45  	Mux                   *http.ServeMux
    46  }
    47  
    48  // NewMocks creates a new set of fakes and mocks.
    49  func NewMocks() Mocks {
    50  	kubeClient := &kubefake.Clientset{}
    51  	agonesClient := &agonesfake.Clientset{}
    52  
    53  	m := Mocks{
    54  		KubeClient:            kubeClient,
    55  		KubeInformerFactory:   informers.NewSharedInformerFactory(kubeClient, 30*time.Second),
    56  		ExtClient:             &extfake.Clientset{},
    57  		AgonesClient:          agonesClient,
    58  		AgonesInformerFactory: externalversions.NewSharedInformerFactory(agonesClient, 30*time.Second),
    59  		FakeRecorder:          record.NewFakeRecorder(100),
    60  	}
    61  	return m
    62  }
    63  
    64  // StartInformers starts new fake informers
    65  func StartInformers(mocks Mocks, sync ...cache.InformerSynced) (context.Context, context.CancelFunc) {
    66  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    67  
    68  	mocks.KubeInformerFactory.Start(ctx.Done())
    69  	mocks.AgonesInformerFactory.Start(ctx.Done())
    70  
    71  	logrus.Info("Wait for cache sync")
    72  	if !cache.WaitForCacheSync(ctx.Done(), sync...) {
    73  		panic("Cache never synced")
    74  	}
    75  
    76  	return ctx, cancel
    77  }
    78  
    79  // NewEstablishedCRD fakes CRD installation success.
    80  func NewEstablishedCRD() *apiextv1.CustomResourceDefinition {
    81  	return &apiextv1.CustomResourceDefinition{
    82  		Status: apiextv1.CustomResourceDefinitionStatus{
    83  			Conditions: []apiextv1.CustomResourceDefinitionCondition{{
    84  				Type:   apiextv1.Established,
    85  				Status: apiextv1.ConditionTrue,
    86  			}},
    87  		},
    88  	}
    89  }
    90  
    91  // AssertEventContains asserts that a k8s event stream contains a
    92  // value, and assert.FailNow() if it does not
    93  func AssertEventContains(t *gotesting.T, events <-chan string, contains string) {
    94  	select {
    95  	case e := <-events:
    96  		assert.Contains(t, e, contains)
    97  	case <-time.After(3 * time.Second):
    98  		assert.FailNow(t, "Did not receive "+contains+" event")
    99  	}
   100  }
   101  
   102  // AssertNoEvent asserts that the event stream does not
   103  // have a value in it (at least in the next second)
   104  func AssertNoEvent(t *gotesting.T, events <-chan string) {
   105  	select {
   106  	case e := <-events:
   107  		assert.Fail(t, "should not have an event", e)
   108  	case <-time.After(1 * time.Second):
   109  	}
   110  }