forked from Maxteabag/sqlit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ssh.py
More file actions
238 lines (211 loc) · 7.22 KB
/
Copy pathtest_ssh.py
File metadata and controls
238 lines (211 loc) · 7.22 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Integration tests for SSH tunnel functionality."""
from __future__ import annotations
import json
import time
import pytest
class TestSSHTunnelIntegration:
"""Integration tests for SSH tunnel database connections via CLI.
These tests require:
- A running SSH server (via Docker)
- A PostgreSQL instance accessible from the SSH server
Tests are skipped if SSH server is not available.
"""
@pytest.fixture(autouse=True)
def slow_down_ssh_tests(self):
"""Add a small delay between SSH tests to avoid overwhelming the server.
Note: SSH tests may fail when run together rapidly due to SSH server
connection limits. Run individually with: pytest tests/test_ssh.py -k <test_name>
"""
time.sleep(2) # Wait before each test
yield
time.sleep(1) # Wait after each test for connections to fully close
def test_create_ssh_connection(self, ssh_postgres_db, cli_runner):
"""Test creating a PostgreSQL connection with SSH tunnel via CLI."""
from .conftest import (
POSTGRES_PASSWORD,
POSTGRES_USER,
SSH_HOST,
SSH_PASSWORD,
SSH_PORT,
SSH_REMOTE_DB_HOST,
SSH_REMOTE_DB_PORT,
SSH_USER,
)
connection_name = "test_create_ssh"
try:
# Create connection with SSH tunnel
result = cli_runner(
"connections",
"add",
"postgresql",
"--name",
connection_name,
"--server",
SSH_REMOTE_DB_HOST,
"--port",
str(SSH_REMOTE_DB_PORT),
"--database",
ssh_postgres_db,
"--username",
POSTGRES_USER,
"--password",
POSTGRES_PASSWORD,
"--ssh-enabled",
"--ssh-host",
SSH_HOST,
"--ssh-port",
str(SSH_PORT),
"--ssh-username",
SSH_USER,
"--ssh-auth-type",
"password",
"--ssh-password",
SSH_PASSWORD,
)
assert result.returncode == 0
assert "created successfully" in result.stdout
# Verify it appears in list
result = cli_runner("connection", "list")
assert connection_name in result.stdout
assert "PostgreSQL" in result.stdout
finally:
# Cleanup
cli_runner("connection", "delete", connection_name, check=False)
def test_query_via_ssh_tunnel(self, ssh_connection, cli_runner):
"""Test executing SELECT query through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT * FROM test_users ORDER BY id",
)
assert result.returncode == 0
assert "Alice" in result.stdout
assert "Bob" in result.stdout
assert "Charlie" in result.stdout
assert "3 row(s) returned" in result.stdout
def test_query_with_where_via_ssh(self, ssh_connection, cli_runner):
"""Test executing SELECT with WHERE clause through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT name, email FROM test_users WHERE id = 1",
)
assert result.returncode == 0
assert "Alice" in result.stdout
assert "alice@example.com" in result.stdout
assert "1 row(s) returned" in result.stdout
def test_query_json_format_via_ssh(self, ssh_connection, cli_runner):
"""Test query output in JSON format through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT id, name FROM test_users ORDER BY id LIMIT 2",
"--format",
"json",
)
assert result.returncode == 0
# Parse JSON output (row count message goes to stderr, not stdout)
data = json.loads(result.stdout)
assert len(data) == 2
assert data[0]["name"] == "Alice"
assert data[1]["name"] == "Bob"
def test_query_csv_format_via_ssh(self, ssh_connection, cli_runner):
"""Test query output in CSV format through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT id, name FROM test_users ORDER BY id LIMIT 2",
"--format",
"csv",
)
assert result.returncode == 0
assert "id,name" in result.stdout
assert "1,Alice" in result.stdout
assert "2,Bob" in result.stdout
def test_query_aggregate_via_ssh(self, ssh_connection, cli_runner):
"""Test aggregate query through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT COUNT(*) as user_count FROM test_users",
)
assert result.returncode == 0
assert "3" in result.stdout
def test_insert_via_ssh(self, ssh_connection, cli_runner):
"""Test INSERT statement through SSH tunnel."""
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"INSERT INTO test_users (id, name, email) VALUES (4, 'David', 'david@example.com')",
)
assert result.returncode == 0
# Verify the insert
result = cli_runner(
"query",
"-c",
ssh_connection,
"-q",
"SELECT * FROM test_users WHERE id = 4",
)
assert "David" in result.stdout
def test_delete_ssh_connection(self, ssh_postgres_db, cli_runner):
"""Test deleting an SSH tunnel connection."""
from .conftest import (
POSTGRES_PASSWORD,
POSTGRES_USER,
SSH_HOST,
SSH_PASSWORD,
SSH_PORT,
SSH_REMOTE_DB_HOST,
SSH_REMOTE_DB_PORT,
SSH_USER,
)
connection_name = "test_delete_ssh"
# Create connection first
cli_runner(
"connections",
"add",
"postgresql",
"--name",
connection_name,
"--server",
SSH_REMOTE_DB_HOST,
"--port",
str(SSH_REMOTE_DB_PORT),
"--database",
ssh_postgres_db,
"--username",
POSTGRES_USER,
"--password",
POSTGRES_PASSWORD,
"--ssh-enabled",
"--ssh-host",
SSH_HOST,
"--ssh-port",
str(SSH_PORT),
"--ssh-username",
SSH_USER,
"--ssh-auth-type",
"password",
"--ssh-password",
SSH_PASSWORD,
)
# Delete it
result = cli_runner("connection", "delete", connection_name)
assert result.returncode == 0
assert "deleted successfully" in result.stdout
# Verify it's gone
result = cli_runner("connection", "list")
assert connection_name not in result.stdout