Skip to content

Commit 2e2ca5c

Browse files
authored
fix(privateca.tests): Adding a random amount of seconds to backoff timer (GoogleCloudPlatform#9293)
* fix(privateca.tests): Adding a random amount of seconds to backoff timer * Fixing the None value case * Pick a random region for this tests, to avoid Quota * Making the random region actually work * linting * Changing number of tries * Added missing continue
1 parent 9d89ee0 commit 2e2ca5c

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

privateca/snippets/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import random
1516
import uuid
1617

1718
import backoff
@@ -29,7 +30,7 @@
2930
from delete_certificate_template import delete_certificate_template
3031

3132
PROJECT = google.auth.default()[1]
32-
LOCATION = "us-central1"
33+
LOCATION = random.choice(("us-central1", "europe-north1", "europe-central2", "europe-west2", "us-east4"))
3334
COMMON_NAME = "COMMON_NAME"
3435
ORGANIZATION = "ORGANIZATION"
3536
CA_DURATION = 1000000

privateca/snippets/test_ca_pools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
import google.auth
2020

21-
from conftest import delete_stale_resources
21+
from conftest import delete_stale_resources, LOCATION
2222

2323
from create_ca_pool import create_ca_pool
2424
from delete_ca_pool import delete_ca_pool
2525
from list_ca_pools import list_ca_pools
2626
from update_ca_pool_issuance_policy import update_ca_pool_issuance_policy
2727

2828
PROJECT = google.auth.default()[1]
29-
LOCATION = "us-central1"
3029

3130
delete_stale_resources()
3231

privateca/snippets/test_certificate_authorities.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import random
1515
import re
1616
import typing
1717
import uuid
1818

1919
import backoff
2020
import google.auth
2121

22+
from conftest import LOCATION
2223
from create_ca_pool import create_ca_pool
2324
from create_certificate_authority import create_certificate_authority
2425
from delete_ca_pool import delete_ca_pool
@@ -30,7 +31,6 @@
3031
from update_certificate_authority import update_ca_label
3132

3233
PROJECT = google.auth.default()[1]
33-
LOCATION = "us-central1"
3434
COMMON_NAME = "COMMON_NAME"
3535
ORGANIZATION = "ORGANIZATION"
3636
CA_DURATION = 1000000
@@ -43,8 +43,15 @@ def generate_name() -> str:
4343
# We are hitting 5 CAs per minute limit which can't be changed
4444
# We set the backoff function to use 4 as base - this way the 3rd try
4545
# should wait for 64 seconds and avoid per minute quota
46+
# Adding some random amount of time to the backoff timer, so that we
47+
# don't try to call the API at the same time in different tests running
48+
# simultaneously.
4649
def backoff_expo_wrapper():
47-
return backoff.expo(base=4)
50+
for exp in backoff.expo(base=4):
51+
if exp is None:
52+
yield None
53+
continue
54+
yield exp * (1 + random.random())
4855

4956

5057
@backoff.on_exception(backoff_expo_wrapper, Exception, max_tries=3)

privateca/snippets/test_certificates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
2424
import google.auth
2525

26+
from conftest import LOCATION
2627
from create_certificate import create_certificate
2728
from disable_certificate_authority import disable_certificate_authority
2829
from enable_certificate_authority import enable_certificate_authority
2930
from filter_certificates import filter_certificates
3031
from revoke_certificate import revoke_certificate
3132

3233
PROJECT = google.auth.default()[1]
33-
LOCATION = "us-central1"
3434
COMMON_NAME = "COMMON_NAME"
3535
ORGANIZATION = "ORGANIZATION"
3636
CERTIFICATE_LIFETIME = 1000000

privateca/snippets/test_crud_certificate_templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import google.auth
2020

21+
from conftest import LOCATION
2122
from create_certificate_template import create_certificate_template
2223
from delete_certificate_template import delete_certificate_template
2324
from list_certificate_templates import list_certificate_templates
2425
from update_certificate_template import update_certificate_template
2526

2627
PROJECT = google.auth.default()[1]
27-
LOCATION = "us-central1"
2828
COMMON_NAME = "COMMON_NAME"
2929
ORGANIZATION = "ORGANIZATION"
3030
CA_DURATION = 1000000

privateca/snippets/test_subordinate_ca.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
15+
import random
1616
import re
1717
import typing
1818
import uuid
@@ -22,12 +22,12 @@
2222
import google.cloud.security.privateca_v1 as privateca_v1
2323

2424
from activate_subordinate_ca import activate_subordinate_ca
25+
from conftest import LOCATION
2526
from create_certificate_csr import create_certificate_csr
2627
from create_subordinate_ca import create_subordinate_ca
2728
from revoke_certificate import revoke_certificate
2829

2930
PROJECT = google.auth.default()[1]
30-
LOCATION = "us-central1"
3131
COMMON_NAME = "COMMON_NAME"
3232
ORGANIZATION = "ORGANIZATION"
3333
CA_DURATION = CERTIFICATE_LIFETIME = 1000000
@@ -41,8 +41,15 @@ def generate_name() -> str:
4141
# We are hitting 5 CAs per minute limit which can't be changed
4242
# We set the backoff function to use 4 as base - this way the 3rd try
4343
# should wait for 64 seconds and avoid per minute quota
44+
# Adding some random amount of time to the backoff timer, so that we
45+
# don't try to call the API at the same time in different tests running
46+
# simultaneously.
4447
def backoff_expo_wrapper():
45-
return backoff.expo(base=4)
48+
for exp in backoff.expo(base=4):
49+
if exp is None:
50+
yield None
51+
continue
52+
yield exp * (1 + random.random())
4653

4754

4855
@backoff.on_exception(backoff_expo_wrapper, Exception, max_tries=3)

0 commit comments

Comments
 (0)