tests: add "test/util" subpackage
Signed-off-by: Paulo Pires <pjpires@gmail.com>
This commit is contained in:
@@ -3,22 +3,17 @@ package vkubelet
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"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"
|
||||
"k8s.io/client-go/tools/record"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/manager"
|
||||
testutil "github.com/virtual-kubelet/virtual-kubelet/test/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultEventRecorderBufferSize is the default buffer size to use when creating fake event recorders.
|
||||
defaultEventRecorderBufferSize = 5
|
||||
// invalidKey1 is a key that cannot be used as the name of an environment variable (since it starts with a digit).
|
||||
invalidKey1 = "1INVALID"
|
||||
// invalidKey2 is a key that cannot be used as the name of an environment variable (since it starts with a digit).
|
||||
@@ -51,34 +46,34 @@ var (
|
||||
// Used so we can take its address when a pointer to a bool is required.
|
||||
bTrue = true
|
||||
// configMap1 is a configmap containing a single key, valid as the name of an environment variable.
|
||||
configMap1 = mockConfigMap(namespace, "configmap-1", map[string]string{
|
||||
configMap1 = testutil.FakeConfigMap(namespace, "configmap-1", map[string]string{
|
||||
keyFoo: "__foo__",
|
||||
})
|
||||
// configMap2 is a configmap containing a single key, valid as the name of an environment variable.
|
||||
configMap2 = mockConfigMap(namespace, "configmap-2", map[string]string{
|
||||
configMap2 = testutil.FakeConfigMap(namespace, "configmap-2", map[string]string{
|
||||
keyBar: "__bar__",
|
||||
})
|
||||
// configMap3 is a configmap containing a single key, valid as the name of an environment variable.
|
||||
configMap3 = mockConfigMap(namespace, "configmap-2", map[string]string{
|
||||
configMap3 = testutil.FakeConfigMap(namespace, "configmap-2", map[string]string{
|
||||
keyFoo: "__foo__",
|
||||
keyBar: "__bar__",
|
||||
})
|
||||
// invalidConfigMap1 is a configmap containing two keys, one of which is invalid as the name of an environment variable.
|
||||
invalidConfigMap1 = mockConfigMap(namespace, "invalid-configmap-1", map[string]string{
|
||||
invalidConfigMap1 = testutil.FakeConfigMap(namespace, "invalid-configmap-1", map[string]string{
|
||||
keyFoo: "__foo__",
|
||||
invalidKey1: "will-be-skipped",
|
||||
invalidKey2: "will-be-skipped",
|
||||
})
|
||||
// secret1 is a secret containing a single key, valid as the name of an environment variable.
|
||||
secret1 = mockSecret(namespace, "secret-1", map[string]string{
|
||||
secret1 = testutil.FakeSecret(namespace, "secret-1", map[string]string{
|
||||
keyBaz: "__baz__",
|
||||
})
|
||||
// secret2 is a secret containing a single key, valid as the name of an environment variable.
|
||||
secret2 = mockSecret(namespace, "secret-2", map[string]string{
|
||||
secret2 = testutil.FakeSecret(namespace, "secret-2", map[string]string{
|
||||
keyFoo: "__foo__",
|
||||
})
|
||||
// invalidSecret1 is a secret containing two keys, one of which is invalid as the name for an environment variable.
|
||||
invalidSecret1 = mockSecret(namespace, "invalid-secret-1", map[string]string{
|
||||
invalidSecret1 = testutil.FakeSecret(namespace, "invalid-secret-1", map[string]string{
|
||||
invalidKey3: "will-be-skipped",
|
||||
keyBaz: "__baz__",
|
||||
})
|
||||
@@ -87,8 +82,8 @@ var (
|
||||
// TestPopulatePodWithInitContainers populates the environment of a pod with four containers (two init containers, two containers).
|
||||
// Then, it checks that the resulting environment for each container contains the expected environment variables.
|
||||
func TestPopulatePodWithInitContainers(t *testing.T) {
|
||||
rm := fakeResourceManager(configMap1, configMap2, secret1, secret2)
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager(configMap1, configMap2, secret1, secret2)
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
// Create a pod object having two init containers and two containers.
|
||||
// The containers' environment is to be populated from two configmaps and two secrets.
|
||||
@@ -187,8 +182,8 @@ func TestPopulatePodWithInitContainers(t *testing.T) {
|
||||
// TestEnvFromTwoConfigMapsAndOneSecret populates the environment of a container from two configmaps and one secret.
|
||||
// Then, it checks that the resulting environment contains all the expected environment variables and values.
|
||||
func TestEnvFromTwoConfigMapsAndOneSecret(t *testing.T) {
|
||||
rm := fakeResourceManager(configMap1, configMap2, secret1)
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager(configMap1, configMap2, secret1)
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
// Create a pod object having a single container.
|
||||
// The container's environment is to be populated from two configmaps and one secret.
|
||||
@@ -258,8 +253,8 @@ func TestEnvFromTwoConfigMapsAndOneSecret(t *testing.T) {
|
||||
// TestEnvFromConfigMapAndSecretWithInvalidKeys populates the environment of a container from a configmap and a secret containing invalid keys.
|
||||
// Then, it checks that the resulting environment contains all the expected environment variables and values, and that the invalid keys have been skipped.
|
||||
func TestEnvFromConfigMapAndSecretWithInvalidKeys(t *testing.T) {
|
||||
rm := fakeResourceManager(invalidConfigMap1, invalidSecret1)
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager(invalidConfigMap1, invalidSecret1)
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
// Create a pod object having a single container.
|
||||
// The container's environment is to be populated from a configmap and a secret, both of which have some invalid keys.
|
||||
@@ -326,8 +321,8 @@ func TestEnvFromConfigMapAndSecretWithInvalidKeys(t *testing.T) {
|
||||
// TestEnvOverridesEnvFrom populates the environment of a container from a configmap, and from another configmap's key with a "conflicting" key.
|
||||
// Then, it checks that the value of the "conflicting" key has been correctly overriden.
|
||||
func TestEnvOverridesEnvFrom(t *testing.T) {
|
||||
rm := fakeResourceManager(configMap3)
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager(configMap3)
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
// override will override the value of "keyFoo" from "configMap3".
|
||||
override := "__override__"
|
||||
@@ -385,8 +380,8 @@ func TestEnvOverridesEnvFrom(t *testing.T) {
|
||||
// TestEnvFromInexistentConfigMaps populates the environment of a container from two configmaps (one of them optional) that do not exist.
|
||||
// Then, it checks that the expected events have been recorded.
|
||||
func TestEnvFromInexistentConfigMaps(t *testing.T) {
|
||||
rm := fakeResourceManager()
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager()
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
missingConfigMap1Name := "missing-config-map-1"
|
||||
missingConfigMap2Name := "missing-config-map-2"
|
||||
@@ -441,8 +436,8 @@ func TestEnvFromInexistentConfigMaps(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnvFromInexistentSecrets(t *testing.T) {
|
||||
rm := fakeResourceManager()
|
||||
er := fakeEventRecorder()
|
||||
rm := testutil.FakeResourceManager()
|
||||
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
|
||||
|
||||
missingSecret1Name := "missing-secret-1"
|
||||
missingSecret2Name := "missing-secret-2"
|
||||
@@ -495,60 +490,3 @@ func TestEnvFromInexistentSecrets(t *testing.T) {
|
||||
assert.Contains(t, event2, ReasonMandatorySecretNotFound)
|
||||
assert.Contains(t, event2, missingSecret2Name)
|
||||
}
|
||||
|
||||
// mockConfigMap returns a configmap with the specified namespace, name and data.
|
||||
func mockConfigMap(namespace, name string, data map[string]string) *corev1.ConfigMap {
|
||||
return &corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: namespace,
|
||||
Name: name,
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// fakeEventRecorder returns an event recorder that can be used to capture events.
|
||||
func fakeEventRecorder() *record.FakeRecorder {
|
||||
return record.NewFakeRecorder(10)
|
||||
}
|
||||
|
||||
// fakeResourceManager returns an instance of the resource manager that will return the specified objects when its "GetX" functions 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 and start informers for pods, configmaps and secrets.
|
||||
pInformer := kubeInformerFactory.Core().V1().Pods()
|
||||
mInformer := kubeInformerFactory.Core().V1().ConfigMaps()
|
||||
sInformer := kubeInformerFactory.Core().V1().Secrets()
|
||||
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
|
||||
}
|
||||
|
||||
// mockSecret returns a secret with the specified namespace, name and data.
|
||||
func mockSecret(namespace, name string, data map[string]string) *corev1.Secret {
|
||||
res := &corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: namespace,
|
||||
Name: name,
|
||||
},
|
||||
Data: make(map[string][]byte),
|
||||
}
|
||||
for key, val := range data {
|
||||
res.Data[key] = []byte(val)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user