Skip to content

Commit d124bdf

Browse files
committed
SNOW-2306184: config refactor - e2e fix attempt 3
1 parent 0019bde commit d124bdf

File tree

2 files changed

+118
-99
lines changed

2 files changed

+118
-99
lines changed

tests_e2e/conftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ def isolate_environment_variables(monkeypatch):
117117
"""
118118
Clear Snowflake-specific environment variables that could interfere with e2e tests.
119119
This ensures tests run in a clean environment and only use the config files they specify.
120+
Exception: Keep INTEGRATION connection vars for e2e testing.
120121
"""
121-
# Clear all SNOWFLAKE_CONNECTIONS_* environment variables
122+
# Clear all SNOWFLAKE_CONNECTIONS_* environment variables except INTEGRATION
122123
for env_var in list(os.environ.keys()):
123124
if env_var.startswith(("SNOWFLAKE_CONNECTIONS_", "SNOWSQL_")):
124-
monkeypatch.delenv(env_var, raising=False)
125+
# Preserve all INTEGRATION connection environment variables
126+
if not env_var.startswith("SNOWFLAKE_CONNECTIONS_INTEGRATION_"):
127+
monkeypatch.delenv(env_var, raising=False)
125128

126129

127130
def _create_venv(tmp_dir: Path) -> None:

tests_e2e/test_import_snowsql_connections.py

Lines changed: 113 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,37 @@
66
from tests_e2e.conftest import subprocess_check_output, subprocess_run
77

88

9-
@pytest.fixture()
10-
def _assert_json_output_matches_snapshot(snapshot):
11-
def f(cmd, stdin: Optional[str] = None):
12-
output = subprocess_check_output(cmd, stdin)
13-
parsed_json = json.loads(output)
14-
snapshot.assert_match(json.dumps(parsed_json))
15-
16-
return f
17-
18-
19-
@pytest.mark.e2e
20-
def test_import_of_snowsql_connections(
21-
snowcli, test_root_path, empty_config_file, _assert_json_output_matches_snapshot
22-
):
23-
_assert_json_output_matches_snapshot(
9+
def _get_connections_list(snowcli, config_file) -> list:
10+
"""Helper function to get connections list as parsed JSON."""
11+
output = subprocess_check_output(
2412
[
2513
snowcli,
2614
"--config-file",
27-
empty_config_file,
15+
config_file,
2816
"connection",
2917
"list",
3018
"--format",
3119
"json",
32-
],
20+
]
3321
)
22+
return json.loads(output)
23+
24+
25+
def _find_connection(connections: list, name: str) -> Optional[dict]:
26+
"""Helper function to find a connection by name."""
27+
for conn in connections:
28+
if conn["connection_name"] == name:
29+
return conn
30+
return None
31+
32+
33+
@pytest.mark.e2e
34+
def test_import_of_snowsql_connections(snowcli, test_root_path, empty_config_file):
35+
# Initially should have empty or minimal connections list
36+
initial_connections = _get_connections_list(snowcli, empty_config_file)
37+
initial_count = len(initial_connections)
3438

39+
# Import snowsql connections
3540
result = subprocess_run(
3641
[
3742
snowcli,
@@ -47,35 +52,49 @@ def test_import_of_snowsql_connections(
4752
)
4853
assert result.returncode == 0
4954

50-
_assert_json_output_matches_snapshot(
51-
[
52-
snowcli,
53-
"--config-file",
54-
empty_config_file,
55-
"connection",
56-
"list",
57-
"--format",
58-
"json",
59-
]
60-
)
55+
# After import, should have multiple connections
56+
final_connections = _get_connections_list(snowcli, empty_config_file)
57+
58+
# Should have more connections than initially
59+
assert len(final_connections) > initial_count
60+
61+
# Check that expected connections exist
62+
connection_names = {conn["connection_name"] for conn in final_connections}
63+
expected_names = {"snowsql1", "snowsql2", "snowsql3", "example", "default"}
64+
assert expected_names.issubset(connection_names)
65+
66+
# Check specific connection details
67+
snowsql1 = _find_connection(final_connections, "snowsql1")
68+
assert snowsql1 is not None
69+
assert snowsql1["parameters"]["account"] == "a1"
70+
assert snowsql1["parameters"]["user"] == "u1"
71+
assert snowsql1["parameters"]["host"] == "h1_override" # From overriding config
72+
assert snowsql1["is_default"] is False
73+
74+
snowsql2 = _find_connection(final_connections, "snowsql2")
75+
assert snowsql2 is not None
76+
assert snowsql2["parameters"]["account"] == "a2"
77+
assert snowsql2["parameters"]["port"] == 1234
78+
assert snowsql2["is_default"] is False
79+
80+
default_conn = _find_connection(final_connections, "default")
81+
assert default_conn is not None
82+
assert default_conn["parameters"]["account"] == "default_connection_account"
83+
assert (
84+
default_conn["parameters"]["database"] == "default_connection_database_override"
85+
) # From overriding config
86+
assert default_conn["is_default"] is True
6187

6288

6389
@pytest.mark.e2e
6490
def test_import_prompt_for_different_default_connection_name_on_conflict(
65-
snowcli, test_root_path, empty_config_file, _assert_json_output_matches_snapshot
91+
snowcli, test_root_path, empty_config_file
6692
):
67-
_assert_json_output_matches_snapshot(
68-
[
69-
snowcli,
70-
"--config-file",
71-
empty_config_file,
72-
"connection",
73-
"list",
74-
"--format",
75-
"json",
76-
],
77-
)
93+
# Initially should have empty or minimal connections list
94+
initial_connections = _get_connections_list(snowcli, empty_config_file)
95+
initial_count = len(initial_connections)
7896

97+
# Import with different default connection name
7998
result = subprocess_run(
8099
[
81100
snowcli,
@@ -94,38 +113,39 @@ def test_import_prompt_for_different_default_connection_name_on_conflict(
94113
)
95114
assert result.returncode == 0
96115

97-
_assert_json_output_matches_snapshot(
98-
[
99-
snowcli,
100-
"--config-file",
101-
empty_config_file,
102-
"connection",
103-
"list",
104-
"--format",
105-
"json",
106-
]
107-
)
116+
# After import, snowsql2 should be the default
117+
final_connections = _get_connections_list(snowcli, empty_config_file)
118+
119+
# Should have more connections than initially
120+
assert len(final_connections) > initial_count
121+
122+
snowsql2 = _find_connection(final_connections, "snowsql2")
123+
assert snowsql2 is not None
124+
assert snowsql2["is_default"] is True
125+
126+
default_conn = _find_connection(final_connections, "default")
127+
assert default_conn is not None
128+
assert default_conn["is_default"] is False
108129

109130

110131
@pytest.mark.e2e
111132
def test_import_confirm_on_conflict_with_existing_cli_connection(
112133
snowcli,
113134
test_root_path,
114135
example_connection_config_file,
115-
_assert_json_output_matches_snapshot,
116136
):
117-
_assert_json_output_matches_snapshot(
118-
[
119-
snowcli,
120-
"--config-file",
121-
example_connection_config_file,
122-
"connection",
123-
"list",
124-
"--format",
125-
"json",
126-
],
127-
)
137+
# Initially should have example and integration connections
138+
initial_connections = _get_connections_list(snowcli, example_connection_config_file)
128139

140+
example_conn = _find_connection(initial_connections, "example")
141+
assert example_conn is not None
142+
assert example_conn["parameters"]["user"] == "u1"
143+
assert example_conn["parameters"]["authenticator"] == "SNOWFLAKE_JWT"
144+
145+
integration_conn = _find_connection(initial_connections, "integration")
146+
assert integration_conn is not None
147+
148+
# Import with confirmation (y)
129149
result = subprocess_run(
130150
[
131151
snowcli,
@@ -142,38 +162,32 @@ def test_import_confirm_on_conflict_with_existing_cli_connection(
142162
)
143163
assert result.returncode == 0
144164

145-
_assert_json_output_matches_snapshot(
146-
[
147-
snowcli,
148-
"--config-file",
149-
example_connection_config_file,
150-
"connection",
151-
"list",
152-
"--format",
153-
"json",
154-
],
155-
)
165+
# After import, example connection should be overwritten with snowsql data
166+
final_connections = _get_connections_list(snowcli, example_connection_config_file)
167+
168+
example_conn = _find_connection(final_connections, "example")
169+
assert example_conn is not None
170+
assert example_conn["parameters"]["account"] == "accountname"
171+
assert example_conn["parameters"]["user"] == "username"
172+
# Should not have the old JWT authenticator
173+
assert "authenticator" not in example_conn["parameters"]
156174

157175

158176
@pytest.mark.e2e
159177
def test_import_reject_on_conflict_with_existing_cli_connection(
160178
snowcli,
161179
test_root_path,
162180
example_connection_config_file,
163-
_assert_json_output_matches_snapshot,
164181
):
165-
_assert_json_output_matches_snapshot(
166-
[
167-
snowcli,
168-
"--config-file",
169-
example_connection_config_file,
170-
"connection",
171-
"list",
172-
"--format",
173-
"json",
174-
],
175-
)
182+
# Initially should have example and integration connections
183+
initial_connections = _get_connections_list(snowcli, example_connection_config_file)
184+
185+
example_conn = _find_connection(initial_connections, "example")
186+
assert example_conn is not None
187+
original_user = example_conn["parameters"]["user"]
188+
original_auth = example_conn["parameters"]["authenticator"]
176189

190+
# Import with rejection (n)
177191
result = subprocess_run(
178192
[
179193
snowcli,
@@ -190,17 +204,18 @@ def test_import_reject_on_conflict_with_existing_cli_connection(
190204
)
191205
assert result.returncode == 0
192206

193-
_assert_json_output_matches_snapshot(
194-
[
195-
snowcli,
196-
"--config-file",
197-
example_connection_config_file,
198-
"connection",
199-
"list",
200-
"--format",
201-
"json",
202-
],
203-
)
207+
# After import, example connection should remain unchanged
208+
final_connections = _get_connections_list(snowcli, example_connection_config_file)
209+
210+
example_conn = _find_connection(final_connections, "example")
211+
assert example_conn is not None
212+
assert example_conn["parameters"]["user"] == original_user
213+
assert example_conn["parameters"]["authenticator"] == original_auth
214+
215+
# But other connections should still be imported
216+
snowsql1 = _find_connection(final_connections, "snowsql1")
217+
assert snowsql1 is not None
218+
assert snowsql1["parameters"]["account"] == "a1"
204219

205220

206221
@pytest.mark.e2e
@@ -218,6 +233,7 @@ def test_connection_imported_from_snowsql(snowcli, test_root_path, empty_config_
218233
)
219234
assert result.returncode == 0
220235

236+
# Test that the imported integration connection works
221237
result = subprocess_run(
222238
[
223239
snowcli,

0 commit comments

Comments
 (0)