|
| 1 | +# |
| 2 | +# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +from test.integ.conftest import create_connection |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +pytestmark = pytest.mark.skipolddriver |
| 10 | + |
| 11 | +import os.path |
| 12 | +from logging import getLogger |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +try: |
| 16 | + import tomlkit |
| 17 | + |
| 18 | + from snowflake.connector.config_manager import CONFIG_MANAGER |
| 19 | + from snowflake.connector.constants import CONFIG_FILE |
| 20 | +except ModuleNotFoundError: |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="function") |
| 25 | +def log_directory(tmp_path_factory): |
| 26 | + return tmp_path_factory.mktemp("log") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="function") |
| 30 | +def temp_config_file(tmp_path_factory): |
| 31 | + return tmp_path_factory.mktemp("config_file_path") / "config.toml" |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="function") |
| 35 | +def config_file_setup(request, temp_config_file, log_directory): |
| 36 | + param = request.param |
| 37 | + CONFIG_MANAGER.file_path = Path(temp_config_file) |
| 38 | + configs = { |
| 39 | + "save_logs": {"log": {"save_logs": True, "path": str(log_directory)}}, |
| 40 | + "no_save_logs": {"log": {"save_logs": False, "path": str(log_directory)}}, |
| 41 | + } |
| 42 | + try: |
| 43 | + # create temp config file |
| 44 | + with open(temp_config_file, "w") as f: |
| 45 | + f.write(tomlkit.dumps(configs[param])) |
| 46 | + yield |
| 47 | + finally: |
| 48 | + # remove created dir and file, including log paths and config file paths |
| 49 | + CONFIG_MANAGER.file_path = CONFIG_FILE |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("config_file_setup", ["save_logs"], indirect=True) |
| 53 | +def test_save_logs(db_parameters, config_file_setup, log_directory): |
| 54 | + create_connection("default") |
| 55 | + |
| 56 | + assert os.path.exists(os.path.join(log_directory, "python-connector.log")) |
| 57 | + with open(os.path.join(log_directory, "python-connector.log")) as f: |
| 58 | + data = f.read() |
| 59 | + try: |
| 60 | + assert "Snowflake Connector for Python" in data |
| 61 | + finally: |
| 62 | + # set logger back to default |
| 63 | + getLogger("snowflake.connector").setLevel(10) |
| 64 | + getLogger("botocore").setLevel(0) |
| 65 | + getLogger("boto3").setLevel(0) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("config_file_setup", ["no_save_logs"], indirect=True) |
| 69 | +def test_no_save_logs(config_file_setup, log_directory): |
| 70 | + create_connection("default") |
| 71 | + |
| 72 | + assert not os.path.exists(os.path.join(log_directory, "python-connector.log")) |
0 commit comments