Skip to content

Commit 801c935

Browse files
author
Takashi Matsuo
authored
fix(cloud-sql): dump cloud sql log file upon failure (GoogleCloudPlatform#5437)
* fix(cloud-sql): dump cloud sql log file upon failure fixes GoogleCloudPlatform#5308 It doesn't fix failure, but at least we can examine the log upon next failure. * add license header
1 parent 210cd2a commit 801c935

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

cloud-sql/mysql/sqlalchemy/connection_test.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from contextlib import contextmanager
216
import logging
317
import os
418
from typing import Dict
519
import uuid
620

7-
import pymysql
821
import pytest
22+
import sqlalchemy
923

1024
import main
1125

@@ -57,24 +71,31 @@ def unix_db_connection():
5771

5872

5973
def _common_setup():
74+
pool = main.init_connection_engine()
75+
76+
table_name: str = uuid.uuid4().hex
77+
6078
try:
61-
pool = main.init_connection_engine()
62-
except pymysql.err.OperationalError as e:
79+
with pool.connect() as conn:
80+
conn.execute(
81+
f"CREATE TABLE IF NOT EXISTS `{table_name}`"
82+
"( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, "
83+
"candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );"
84+
)
85+
except sqlalchemy.exc.OperationalError as e:
6386
logger.warning(
6487
"Could not connect to the production database. "
6588
"If running tests locally, is the cloud_sql_proxy currently running?"
6689
)
90+
# If there is cloud sql proxy log, dump the contents.
91+
home_dir = os.environ.get("HOME", "")
92+
log_file = f"{home_dir}/cloud_sql_proxy.log"
93+
if home_dir and os.path.isfile(log_file):
94+
print(f"Dumping the contents of {log_file}")
95+
with open(log_file, "r") as f:
96+
print(f.read())
6797
raise e
6898

69-
table_name: str = uuid.uuid4().hex
70-
71-
with pool.connect() as conn:
72-
conn.execute(
73-
f"CREATE TABLE IF NOT EXISTS `{table_name}`"
74-
"( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, "
75-
"candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );"
76-
)
77-
7899
yield pool
79100

80101
with pool.connect() as conn:

0 commit comments

Comments
 (0)