14
14
15
15
import os
16
16
import pytest
17
- import random
17
+ import uuid
18
+
19
+ from googleapiclient import errors
20
+ from retrying import retry
18
21
19
22
import access
20
23
import service_accounts
26
29
GCP_ROLE = "roles/owner"
27
30
28
31
32
+ def retry_if_conflict (exception ):
33
+ return (isinstance (exception , errors .HttpError )
34
+ and 'There were concurrent policy changes' in str (exception ))
35
+
36
+
29
37
@pytest .fixture (scope = "module" )
30
38
def test_member ():
31
39
# section to create service account to test policy updates.
32
- rand = str ( random . randint ( 0 , 1000 ))
33
- name = "python-test-" + rand
40
+ # we use the first portion of uuid4 because full version is too long.
41
+ name = "python-test-" + str ( uuid . uuid4 ()). split ( '-' )[ 0 ]
34
42
email = name + "@" + GCLOUD_PROJECT + ".iam.gserviceaccount.com"
35
43
member = "serviceAccount:" + email
36
44
service_accounts .create_service_account (
@@ -50,24 +58,36 @@ def test_get_policy(capsys):
50
58
51
59
52
60
def test_modify_policy_add_role (test_member , capsys ):
53
- policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
54
- access .modify_policy_add_role (policy , GCLOUD_PROJECT , test_member )
55
- out , _ = capsys .readouterr ()
56
- assert u"etag" in out
61
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
62
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_conflict )
63
+ def test_call ():
64
+ policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
65
+ access .modify_policy_add_role (policy , GCLOUD_PROJECT , test_member )
66
+ out , _ = capsys .readouterr ()
67
+ assert u"etag" in out
68
+ test_call ()
57
69
58
70
59
71
def test_modify_policy_remove_member (test_member , capsys ):
60
- policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
61
- access .modify_policy_remove_member (policy , GCP_ROLE , test_member )
62
- out , _ = capsys .readouterr ()
63
- assert "iam.gserviceaccount.com" in out
72
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
73
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_conflict )
74
+ def test_call ():
75
+ policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
76
+ access .modify_policy_remove_member (policy , GCP_ROLE , test_member )
77
+ out , _ = capsys .readouterr ()
78
+ assert "iam.gserviceaccount.com" in out
79
+ test_call ()
64
80
65
81
66
82
def test_set_policy (capsys ):
67
- policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
68
- access .set_policy (GCLOUD_PROJECT , policy )
69
- out , _ = capsys .readouterr ()
70
- assert u"etag" in out
83
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
84
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_conflict )
85
+ def test_call ():
86
+ policy = access .get_policy (GCLOUD_PROJECT , version = 3 )
87
+ access .set_policy (GCLOUD_PROJECT , policy )
88
+ out , _ = capsys .readouterr ()
89
+ assert u"etag" in out
90
+ test_call ()
71
91
72
92
73
93
def test_permissions (capsys ):
0 commit comments