1919import time
2020import unittest
2121import uuid
22+ import six
2223
2324from kubernetes .client import api_client
2425from kubernetes .client .api import core_v1_api
2526from kubernetes .e2e_test import base
2627from kubernetes .stream import stream , portforward
2728from kubernetes .stream .ws_client import ERROR_CHANNEL
29+ from kubernetes .client .rest import ApiException
2830
2931import six .moves .urllib .request as urllib_request
3032
33+ if six .PY3 :
34+ from http import HTTPStatus
35+ else :
36+ import httplib
37+
3138def short_uuid ():
3239 id = str (uuid .uuid4 ())
3340 return id [- 12 :]
@@ -65,6 +72,27 @@ def test_pod_apis(self):
6572
6673 name = 'busybox-test-' + short_uuid ()
6774 pod_manifest = manifest_with_command (name , "while true;do date;sleep 5; done" )
75+
76+ # wait for the default service account to be created
77+ timeout = time .time () + 30
78+ while True :
79+ if time .time () > timeout :
80+ print ('timeout waiting for default service account creation' )
81+ break
82+ try :
83+ resp = api .read_namespaced_service_account (name = 'default' ,
84+ namespace = 'default' )
85+ except ApiException as e :
86+ if (six .PY3 and e .status != HTTPStatus .NOT_FOUND ) or (
87+ six .PY3 is False and e .status != httplib .NOT_FOUND ):
88+ print ('error: %s' % e )
89+ self .fail (msg = "unexpected error getting default service account" )
90+ print ('default service not found yet: %s' % e )
91+ time .sleep (1 )
92+ continue
93+ self .assertEqual ('default' , resp .metadata .name )
94+ break
95+
6896 resp = api .create_namespaced_pod (body = pod_manifest ,
6997 namespace = 'default' )
7098 self .assertEqual (name , resp .metadata .name )
@@ -130,6 +158,28 @@ def test_exit_code(self):
130158
131159 name = 'busybox-test-' + short_uuid ()
132160 pod_manifest = manifest_with_command (name , "while true;do date;sleep 5; done" )
161+
162+ # wait for the default service account to be created
163+ timeout = time .time () + 30
164+ while True :
165+ if time .time () > timeout :
166+ print ('timeout waiting for default service account creation' )
167+ break
168+
169+ try :
170+ resp = api .read_namespaced_service_account (name = 'default' ,
171+ namespace = 'default' )
172+ except ApiException as e :
173+ if (six .PY3 and e .status != HTTPStatus .NOT_FOUND ) or (
174+ six .PY3 is False and e .status != httplib .NOT_FOUND ):
175+ print ('error: %s' % e )
176+ self .fail (msg = "unexpected error getting default service account" )
177+ print ('default service not found yet: %s' % e )
178+ time .sleep (1 )
179+ continue
180+ self .assertEqual ('default' , resp .metadata .name )
181+ break
182+
133183 resp = api .create_namespaced_pod (body = pod_manifest ,
134184 namespace = 'default' )
135185 self .assertEqual (name , resp .metadata .name )
@@ -443,6 +493,7 @@ def test_configmap_apis(self):
443493 "apiVersion" : "v1" ,
444494 "metadata" : {
445495 "name" : name ,
496+ "labels" : {"e2e-tests" : "true" },
446497 },
447498 "data" : {
448499 "config.json" : "{\" command\" :\" /usr/bin/mysqld_safe\" }" ,
@@ -466,7 +517,7 @@ def test_configmap_apis(self):
466517 resp = api .delete_namespaced_config_map (
467518 name = name , body = {}, namespace = 'default' )
468519
469- resp = api .list_namespaced_config_map ('default' , pretty = True )
520+ resp = api .list_namespaced_config_map ('default' , pretty = True , label_selector = "e2e-tests=true" )
470521 self .assertEqual ([], resp .items )
471522
472523 def test_node_apis (self ):
0 commit comments