-
Notifications
You must be signed in to change notification settings - Fork 28
/
run_testing_worker.py
77 lines (56 loc) · 2 KB
/
run_testing_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# Copyright 2020 Google LLC.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
import math
import os
import random
import time
from google.cloud.spanner_v1 import Client
REGION = "regional-us-central1"
WORKER_INDEX = int(os.getenv("DJANGO_WORKER_INDEX", 0))
WORKER_COUNT = int(os.getenv("DJANGO_WORKER_COUNT", 1))
class TestInstance:
def __enter__(self):
project = os.getenv(
"GOOGLE_CLOUD_PROJECT",
os.getenv("PROJECT_ID", "emulator-test-project"),
)
client = Client(project=project)
config = f"{client.project_name}/instanceConfigs/{REGION}"
name = "sp-dj-test-{}-{}".format(
str(WORKER_INDEX), str(int(time.time()))
)
self._instance = client.instance(name, config)
created_op = self._instance.create()
created_op.result(120) # block until completion
return name
def __exit__(self, exc, exc_value, traceback):
self._instance.delete()
if WORKER_INDEX >= WORKER_COUNT:
print(
"WORKER_INDEX ({wi}) > WORKER_COUNT ({wc})".format(
wi=WORKER_INDEX,
wc=WORKER_COUNT,
)
)
exit()
with open("django_test_apps.txt", "r") as file:
all_apps = file.read().split("\n")
apps_per_worker = math.ceil(len(all_apps) / WORKER_COUNT)
start_index = min(WORKER_INDEX * apps_per_worker, len(all_apps))
end_index = min(start_index + apps_per_worker, len(all_apps))
test_apps = all_apps[start_index:end_index]
print("test apps: ", test_apps)
if not test_apps:
exit()
delay = random.randint(10, 60)
print("creating instance with delay: {} seconds".format(delay))
time.sleep(delay)
with TestInstance() as instance_name:
os.system(
"""DJANGO_TEST_APPS="{apps}" SPANNER_TEST_INSTANCE={instance} bash ./django_test_suite_4.2.sh""".format(
apps=" ".join(test_apps), instance=instance_name
)
)