tests: add "test/util" subpackage
Signed-off-by: Paulo Pires <pjpires@gmail.com>
This commit is contained in:
40
test/util/resource_manager.go
Normal file
40
test/util/resource_manager.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/manager"
|
||||
)
|
||||
|
||||
// FakeResourceManager returns an instance of the resource manager that will return the specified objects when its "GetX" methods are called.
|
||||
// Objects can be any valid Kubernetes object (corev1.Pod, corev1.ConfigMap, corev1.Secret, ...).
|
||||
func FakeResourceManager(objects ...runtime.Object) *manager.ResourceManager {
|
||||
// Create a fake Kubernetes client that will list the specified objects.
|
||||
kubeClient := fake.NewSimpleClientset(objects...)
|
||||
// Create a shared informer factory from where we can grab informers and listers for pods, configmaps and secrets.
|
||||
kubeInformerFactory := informers.NewSharedInformerFactory(kubeClient, 30*time.Second)
|
||||
// Grab informers for pods, configmaps and secrets.
|
||||
pInformer := kubeInformerFactory.Core().V1().Pods()
|
||||
mInformer := kubeInformerFactory.Core().V1().ConfigMaps()
|
||||
sInformer := kubeInformerFactory.Core().V1().Secrets()
|
||||
// Start all the required informers.
|
||||
go pInformer.Informer().Run(wait.NeverStop)
|
||||
go mInformer.Informer().Run(wait.NeverStop)
|
||||
go sInformer.Informer().Run(wait.NeverStop)
|
||||
// Wait for the caches to be synced.
|
||||
if !cache.WaitForCacheSync(wait.NeverStop, pInformer.Informer().HasSynced, mInformer.Informer().HasSynced, sInformer.Informer().HasSynced) {
|
||||
panic("failed to wait for caches to be synced")
|
||||
}
|
||||
// Create a new instance of the resource manager using the listers for pods, configmaps and secrets.
|
||||
r, err := manager.NewResourceManager(pInformer.Lister(), sInformer.Lister(), mInformer.Lister())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user