Browse code

integration tests: wait for admission control cache to allow pod creation

Cesar Wong authored on 2016/04/29 02:14:50
Showing 3 changed files
... ...
@@ -80,7 +80,7 @@ func setupClusterAdminPodNodeConstraintsTest(t *testing.T, pluginConfig *plugina
80 80
 	if err != nil {
81 81
 		t.Fatalf("error creating namespace: %v", err)
82 82
 	}
83
-	if err := testserver.WaitForServiceAccounts(kubeClient, testutil.Namespace(), []string{bootstrappolicy.DefaultServiceAccountName}); err != nil {
83
+	if err := testserver.WaitForPodCreationServiceAccounts(kubeClient, testutil.Namespace()); err != nil {
84 84
 		t.Fatalf("unexpected error: %v", err)
85 85
 	}
86 86
 	return openShiftClient, kubeClient
... ...
@@ -9,7 +9,6 @@ import (
9 9
 	kclient "k8s.io/kubernetes/pkg/client/unversioned"
10 10
 
11 11
 	configapi "github.com/openshift/origin/pkg/cmd/server/api"
12
-	"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
13 12
 	pluginapi "github.com/openshift/origin/pkg/quota/admission/runonceduration/api"
14 13
 	testutil "github.com/openshift/origin/test/util"
15 14
 	testserver "github.com/openshift/origin/test/util/server"
... ...
@@ -98,7 +97,7 @@ func setupRunOnceDurationTest(t *testing.T, pluginConfig *pluginapi.RunOnceDurat
98 98
 	if err != nil {
99 99
 		t.Fatalf("error creating namespace: %v", err)
100 100
 	}
101
-	if err := testserver.WaitForServiceAccounts(kubeClient, testutil.Namespace(), []string{bootstrappolicy.DefaultServiceAccountName}); err != nil {
101
+	if err := testserver.WaitForPodCreationServiceAccounts(kubeClient, testutil.Namespace()); err != nil {
102 102
 		t.Errorf("unexpected error: %v", err)
103 103
 	}
104 104
 	return kubeClient
... ...
@@ -37,6 +37,10 @@ import (
37 37
 // controllers to start up, and populate the service accounts in the test namespace
38 38
 const ServiceAccountWaitTimeout = 30 * time.Second
39 39
 
40
+// PodCreationWaitTimeout is used to determine how long to wait after the service account token
41
+// is available for the admission control cache to catch up and allow pod creation
42
+const PodCreationWaitTimeout = 10 * time.Second
43
+
40 44
 // RequireServer verifies if the etcd and the OpenShift server are
41 45
 // available and you can successfully connect to them.
42 46
 func RequireServer(t *testing.T) {
... ...
@@ -395,6 +399,37 @@ func serviceAccountSecretsExist(client *kclient.Client, namespace string, sa *ka
395 395
 	return foundTokenSecret && foundDockercfgSecret
396 396
 }
397 397
 
398
+// WaitForPodCreationServiceAccounts ensures that the service account needed for pod creation exists
399
+// and that the cache for the admission control that checks for pod tokens has caught up to allow
400
+// pod creation.
401
+func WaitForPodCreationServiceAccounts(client *kclient.Client, namespace string) error {
402
+	if err := WaitForServiceAccounts(client, namespace, []string{bootstrappolicy.DefaultServiceAccountName}); err != nil {
403
+		return err
404
+	}
405
+
406
+	testPod := &kapi.Pod{}
407
+	testPod.GenerateName = "test"
408
+	testPod.Spec.Containers = []kapi.Container{
409
+		{
410
+			Name:  "container",
411
+			Image: "openshift/origin-pod:latest",
412
+		},
413
+	}
414
+
415
+	return wait.PollImmediate(time.Second, PodCreationWaitTimeout, func() (bool, error) {
416
+		pod, err := client.Pods(namespace).Create(testPod)
417
+		if err != nil {
418
+			glog.Warningf("Error attempting to create test pod: %v", err)
419
+			return false, nil
420
+		}
421
+		err = client.Pods(namespace).Delete(pod.Name, kapi.NewDeleteOptions(0))
422
+		if err != nil {
423
+			return false, err
424
+		}
425
+		return true, nil
426
+	})
427
+}
428
+
398 429
 // WaitForServiceAccounts ensures the service accounts needed by build pods exist in the namespace
399 430
 // The extra controllers tend to starve the service account controller
400 431
 func WaitForServiceAccounts(client *kclient.Client, namespace string, accounts []string) error {