19
19
import random
20
20
import string
21
21
import time
22
+ import uuid
22
23
23
24
import pytest
24
25
34
35
from tests .test_utils .gcp_system_helpers import CLOUD_DAG_FOLDER , GoogleSystemTest , provide_gcp_context
35
36
36
37
GCP_PROJECT_ID = os .environ .get ('GCP_PROJECT_ID' , 'project-id' )
38
+ CLOUD_SQL_BUCKET_NAME = os .environ .get ('CLOUD_SQL_BUCKET_NAME' , 'INVALID BUCKET NAME' )
37
39
38
40
SQL_QUERY_TEST_HELPER = CloudSqlQueryTestHelper ()
39
41
40
42
43
+ @pytest .fixture (scope = 'class' )
44
+ def env_patch ():
45
+ """
46
+ A convenient fixture for environment variables patching.
47
+ All modifications will be undone after the requesting test class has finished.
48
+ """
49
+ from _pytest .monkeypatch import MonkeyPatch
50
+
51
+ mpatch = MonkeyPatch ()
52
+ yield mpatch
53
+ mpatch .undo ()
54
+
55
+
56
+ @pytest .fixture (scope = 'module' )
57
+ def default_instances ():
58
+ """
59
+ Collect list of environment variables default values
60
+ """
61
+ mysql_instance_name = os .environ .get ('GCSQL_MYSQL_INSTANCE_NAME' , 'test-mysql' ) + '%s'
62
+ mysql_instance_name2 = os .environ .get ('GCSQL_MYSQL_INSTANCE_NAME2' , 'test-mysql2' ) + '%s'
63
+ mysql_instance_name_query = mysql_instance_name + QUERY_SUFFIX
64
+ postgres_instance_name = os .environ .get ('GCSQL_POSTGRES_INSTANCE_NAME' , 'test-postgres' ) + '%s'
65
+ postgres_instance_name2 = os .environ .get ('GCSQL_POSTGRES_INSTANCE_NAME2' , 'test-postgres2' ) + '%s'
66
+ postgres_instance_name_query = postgres_instance_name + QUERY_SUFFIX
67
+ instances = [
68
+ ('GCSQL_MYSQL_INSTANCE_NAME' , mysql_instance_name ),
69
+ ('GCSQL_MYSQL_INSTANCE_NAME2' , mysql_instance_name2 ),
70
+ ('GCSQL_MYSQL_INSTANCE_NAME_QUERY' , mysql_instance_name_query ),
71
+ ('GCSQL_POSTGRES_INSTANCE_NAME' , postgres_instance_name ),
72
+ ('GCSQL_POSTGRES_INSTANCE_NAME2' , postgres_instance_name2 ),
73
+ ('GCSQL_POSTGRES_INSTANCE_NAME_QUERY' , postgres_instance_name_query ),
74
+ ]
75
+ return instances
76
+
77
+
78
+ @pytest .fixture (scope = 'class' , autouse = True )
79
+ def set_unique_postfix (env_patch , default_instances ):
80
+ """
81
+ Generate a unique postfix and add it to an instance name to avoid 409 HTTP error
82
+ in case of the instance name was already used during last week
83
+ """
84
+ unique_postfix = f'-{ uuid .uuid4 ().hex [:5 ]} ' # generate 5 digits unique postfix
85
+ for instance in default_instances :
86
+ env_variable , value = instance
87
+ env_patch .setenv (env_variable , value % unique_postfix )
88
+
89
+
90
+ @pytest .fixture
91
+ def set_mysql_ip (monkeypatch ):
92
+ """Set ip address of MySQL instance to GCSQL_MYSQL_PUBLIC_IP env variable"""
93
+ with open (os .environ ["GCSQL_MYSQL_PUBLIC_IP_FILE" ]) as file :
94
+ env , ip_address = file .read ().split ("=" )
95
+ monkeypatch .setenv (env , ip_address )
96
+
97
+
98
+ @pytest .fixture
99
+ def set_postgres_ip (monkeypatch ):
100
+ """Set ip address of Postgres instance to GCSQL_POSTGRES_PUBLIC_IP env variable"""
101
+ with open (os .environ ["GCSQL_POSTGRES_PUBLIC_IP_FILE" ]) as file :
102
+ env , ip_address = file .read ().split ("=" )
103
+ monkeypatch .setenv (env , ip_address )
104
+
105
+
41
106
@pytest .mark .backend ("mysql" , "postgres" )
42
107
@pytest .mark .credential_file (GCP_CLOUDSQL_KEY )
43
108
class CloudSqlExampleDagsIntegrationTest (GoogleSystemTest ):
109
+ @provide_gcp_context (GCP_CLOUDSQL_KEY )
44
110
def setUp (self ):
45
111
super ().setUp ()
112
+ # 1. Create Fine-grained bucket to provide ACLs
113
+ self .log .info (f'Creating { CLOUD_SQL_BUCKET_NAME } bucket...' )
114
+ self .execute_cmd (["gsutil" , "mb" , "-b" , "off" , f"gs://{ CLOUD_SQL_BUCKET_NAME } " ])
46
115
47
116
@provide_gcp_context (GCP_CLOUDSQL_KEY )
48
117
def tearDown (self ):
49
118
if os .path .exists (TEARDOWN_LOCK_FILE ):
50
119
self .log .info ("Skip deleting instances as they were created manually (helps to iterate on tests)" )
51
120
else :
52
- # Delete instances just in case the test failed and did not cleanup after itself
121
+ # 1. Delete instances just in case the test failed and did not cleanup after itself
53
122
SQL_QUERY_TEST_HELPER .delete_instances (instance_suffix = "-failover-replica" )
54
123
SQL_QUERY_TEST_HELPER .delete_instances (instance_suffix = "-read-replica" )
55
124
SQL_QUERY_TEST_HELPER .delete_instances ()
56
125
SQL_QUERY_TEST_HELPER .delete_instances (instance_suffix = "2" )
57
126
SQL_QUERY_TEST_HELPER .delete_service_account_acls ()
127
+
128
+ # 2. Delete bucket
129
+ self .log .info (f'Deleting { CLOUD_SQL_BUCKET_NAME } bucket...' )
130
+ self .execute_cmd (["gsutil" , "rm" , "-r" , f"gs://{ CLOUD_SQL_BUCKET_NAME } " ])
58
131
super ().tearDown ()
59
132
60
133
@provide_gcp_context (GCP_CLOUDSQL_KEY )
@@ -79,7 +152,6 @@ class CloudSqlProxySystemTest(GoogleSystemTest):
79
152
@classmethod
80
153
@provide_gcp_context (GCP_CLOUDSQL_KEY )
81
154
def setUpClass (cls ):
82
- SQL_QUERY_TEST_HELPER .set_ip_addresses_in_env ()
83
155
if os .path .exists (TEARDOWN_LOCK_FILE_QUERY ):
84
156
print (
85
157
"Skip creating and setting up instances as they were created manually "
@@ -176,5 +248,6 @@ def test_start_proxy_with_all_instances_specific_version(self):
176
248
assert runner .get_proxy_version () == "1.13"
177
249
178
250
@provide_gcp_context (GCP_CLOUDSQL_KEY )
251
+ @pytest .mark .usefixtures ('set_mysql_ip' , 'set_postgres_ip' )
179
252
def test_run_example_dag_cloudsql_query (self ):
180
253
self .run_dag ('example_gcp_sql_query' , CLOUD_DAG_FOLDER )
0 commit comments