| 1 |
from click.testing import CliRunner |
| 2 |
from datasette import cli |
| 3 |
from unittest import mock |
| 4 |
import json |
| 5 |
import os |
| 6 |
import pytest |
| 7 |
import textwrap |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.serial |
| 11 |
@mock.patch("shutil.which") |
| 12 |
def test_publish_cloudrun_requires_gcloud(mock_which, tmp_path_factory): |
| 13 |
mock_which.return_value = False |
| 14 |
runner = CliRunner() |
| 15 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 16 |
with open("test.db", "w") as fp: |
| 17 |
fp.write("data") |
| 18 |
result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"]) |
| 19 |
assert result.exit_code == 1 |
| 20 |
assert "Publishing to Google Cloud requires gcloud" in result.output |
| 21 |
|
| 22 |
|
| 23 |
@mock.patch("shutil.which") |
| 24 |
def test_publish_cloudrun_invalid_database(mock_which): |
| 25 |
mock_which.return_value = True |
| 26 |
runner = CliRunner() |
| 27 |
result = runner.invoke(cli.cli, ["publish", "cloudrun", "woop.db"]) |
| 28 |
assert result.exit_code == 2 |
| 29 |
assert "Path 'woop.db' does not exist" in result.output |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.serial |
| 33 |
@mock.patch("shutil.which") |
| 34 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 35 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 36 |
@mock.patch("datasette.publish.cloudrun.get_existing_services") |
| 37 |
def test_publish_cloudrun_prompts_for_service( |
| 38 |
mock_get_existing_services, mock_call, mock_output, mock_which, tmp_path_factory |
| 39 |
): |
| 40 |
mock_get_existing_services.return_value = [ |
| 41 |
{"name": "existing", "created": "2019-01-01", "url": "http://www.example.com/"} |
| 42 |
] |
| 43 |
mock_output.return_value = "myproject" |
| 44 |
mock_which.return_value = True |
| 45 |
runner = CliRunner() |
| 46 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 47 |
with open("test.db", "w") as fp: |
| 48 |
fp.write("data") |
| 49 |
result = runner.invoke( |
| 50 |
cli.cli, ["publish", "cloudrun", "test.db"], input="input-service" |
| 51 |
) |
| 52 |
assert ( |
| 53 |
"Please provide a service name for this deployment\n\n" |
| 54 |
"Using an existing service name will over-write it\n\n" |
| 55 |
"Your existing services:\n\n" |
| 56 |
" existing - created 2019-01-01 - http://www.example.com/\n\n" |
| 57 |
"Service name: input-service" |
| 58 |
) == result.output.strip() |
| 59 |
assert 0 == result.exit_code |
| 60 |
tag = "us-docker.pkg.dev/myproject/datasette/datasette-input-service" |
| 61 |
mock_call.assert_has_calls( |
| 62 |
[ |
| 63 |
mock.call( |
| 64 |
"gcloud services enable artifactregistry.googleapis.com --project myproject --quiet", |
| 65 |
shell=True, |
| 66 |
), |
| 67 |
mock.call( |
| 68 |
"gcloud artifacts repositories describe datasette --project myproject --location us --quiet", |
| 69 |
shell=True, |
| 70 |
), |
| 71 |
mock.call(f"gcloud builds submit --tag {tag}", shell=True), |
| 72 |
mock.call( |
| 73 |
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} input-service --max-instances 1".format( |
| 74 |
tag |
| 75 |
), |
| 76 |
shell=True, |
| 77 |
), |
| 78 |
] |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.serial |
| 83 |
@mock.patch("shutil.which") |
| 84 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 85 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 86 |
def test_publish_cloudrun(mock_call, mock_output, mock_which, tmp_path_factory): |
| 87 |
mock_output.return_value = "myproject" |
| 88 |
mock_which.return_value = True |
| 89 |
runner = CliRunner() |
| 90 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 91 |
with open("test.db", "w") as fp: |
| 92 |
fp.write("data") |
| 93 |
result = runner.invoke( |
| 94 |
cli.cli, ["publish", "cloudrun", "test.db", "--service", "test"] |
| 95 |
) |
| 96 |
assert 0 == result.exit_code |
| 97 |
tag = f"us-docker.pkg.dev/{mock_output.return_value}/datasette/datasette-test" |
| 98 |
mock_call.assert_has_calls( |
| 99 |
[ |
| 100 |
mock.call( |
| 101 |
f"gcloud services enable artifactregistry.googleapis.com --project {mock_output.return_value} --quiet", |
| 102 |
shell=True, |
| 103 |
), |
| 104 |
mock.call( |
| 105 |
f"gcloud artifacts repositories describe datasette --project {mock_output.return_value} --location us --quiet", |
| 106 |
shell=True, |
| 107 |
), |
| 108 |
mock.call(f"gcloud builds submit --tag {tag}", shell=True), |
| 109 |
mock.call( |
| 110 |
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} test --max-instances 1".format( |
| 111 |
tag |
| 112 |
), |
| 113 |
shell=True, |
| 114 |
), |
| 115 |
] |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
@pytest.mark.serial |
| 120 |
@mock.patch("shutil.which") |
| 121 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 122 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 123 |
@pytest.mark.parametrize( |
| 124 |
"memory,cpu,timeout,min_instances,max_instances,expected_gcloud_args", |
| 125 |
[ |
| 126 |
["1Gi", None, None, None, None, "--memory 1Gi"], |
| 127 |
["2G", None, None, None, None, "--memory 2G"], |
| 128 |
["256Mi", None, None, None, None, "--memory 256Mi"], |
| 129 |
[ |
| 130 |
"4", |
| 131 |
None, |
| 132 |
None, |
| 133 |
None, |
| 134 |
None, |
| 135 |
None, |
| 136 |
], |
| 137 |
[ |
| 138 |
"GB", |
| 139 |
None, |
| 140 |
None, |
| 141 |
None, |
| 142 |
None, |
| 143 |
None, |
| 144 |
], |
| 145 |
[None, 1, None, None, None, "--cpu 1"], |
| 146 |
[None, 2, None, None, None, "--cpu 2"], |
| 147 |
[None, 3, None, None, None, None], |
| 148 |
[None, 4, None, None, None, "--cpu 4"], |
| 149 |
["2G", 4, None, None, None, "--memory 2G --cpu 4"], |
| 150 |
[None, None, 1800, None, None, "--timeout 1800"], |
| 151 |
[None, None, None, 2, None, "--min-instances 2"], |
| 152 |
[None, None, None, 2, 4, "--min-instances 2 --max-instances 4"], |
| 153 |
[None, 2, None, None, 4, "--cpu 2 --max-instances 4"], |
| 154 |
], |
| 155 |
) |
| 156 |
def test_publish_cloudrun_memory_cpu( |
| 157 |
mock_call, |
| 158 |
mock_output, |
| 159 |
mock_which, |
| 160 |
memory, |
| 161 |
cpu, |
| 162 |
timeout, |
| 163 |
min_instances, |
| 164 |
max_instances, |
| 165 |
expected_gcloud_args, |
| 166 |
tmp_path_factory, |
| 167 |
): |
| 168 |
mock_output.return_value = "myproject" |
| 169 |
mock_which.return_value = True |
| 170 |
runner = CliRunner() |
| 171 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 172 |
with open("test.db", "w") as fp: |
| 173 |
fp.write("data") |
| 174 |
args = ["publish", "cloudrun", "test.db", "--service", "test"] |
| 175 |
if memory: |
| 176 |
args.extend(["--memory", memory]) |
| 177 |
if cpu: |
| 178 |
args.extend(["--cpu", str(cpu)]) |
| 179 |
if timeout: |
| 180 |
args.extend(["--timeout", str(timeout)]) |
| 181 |
result = runner.invoke(cli.cli, args) |
| 182 |
if expected_gcloud_args is None: |
| 183 |
assert 2 == result.exit_code |
| 184 |
return |
| 185 |
assert 0 == result.exit_code |
| 186 |
tag = f"us-docker.pkg.dev/{mock_output.return_value}/datasette/datasette-test" |
| 187 |
expected_call = ( |
| 188 |
"gcloud run deploy --allow-unauthenticated --platform=managed" |
| 189 |
" --image {} test".format(tag) |
| 190 |
) |
| 191 |
expected_build_call = f"gcloud builds submit --tag {tag}" |
| 192 |
if memory: |
| 193 |
expected_call += " --memory {}".format(memory) |
| 194 |
if cpu: |
| 195 |
expected_call += " --cpu {}".format(cpu) |
| 196 |
if timeout: |
| 197 |
expected_build_call += f" --timeout {timeout}" |
| 198 |
# max_instances defaults to 1 |
| 199 |
expected_call += " --max-instances 1" |
| 200 |
mock_call.assert_has_calls( |
| 201 |
[ |
| 202 |
mock.call( |
| 203 |
f"gcloud services enable artifactregistry.googleapis.com --project {mock_output.return_value} --quiet", |
| 204 |
shell=True, |
| 205 |
), |
| 206 |
mock.call( |
| 207 |
f"gcloud artifacts repositories describe datasette --project {mock_output.return_value} --location us --quiet", |
| 208 |
shell=True, |
| 209 |
), |
| 210 |
mock.call(expected_build_call, shell=True), |
| 211 |
mock.call( |
| 212 |
expected_call, |
| 213 |
shell=True, |
| 214 |
), |
| 215 |
] |
| 216 |
) |
| 217 |
|
| 218 |
|
| 219 |
@pytest.mark.serial |
| 220 |
@mock.patch("shutil.which") |
| 221 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 222 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 223 |
def test_publish_cloudrun_plugin_secrets( |
| 224 |
mock_call, mock_output, mock_which, tmp_path_factory |
| 225 |
): |
| 226 |
mock_which.return_value = True |
| 227 |
mock_output.return_value = "myproject" |
| 228 |
|
| 229 |
runner = CliRunner() |
| 230 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 231 |
with open("test.db", "w") as fp: |
| 232 |
fp.write("data") |
| 233 |
with open("metadata.yml", "w") as fp: |
| 234 |
fp.write( |
| 235 |
textwrap.dedent( |
| 236 |
""" |
| 237 |
title: Hello from metadata YAML |
| 238 |
plugins: |
| 239 |
datasette-auth-github: |
| 240 |
foo: bar |
| 241 |
""" |
| 242 |
).strip() |
| 243 |
) |
| 244 |
result = runner.invoke( |
| 245 |
cli.cli, |
| 246 |
[ |
| 247 |
"publish", |
| 248 |
"cloudrun", |
| 249 |
"test.db", |
| 250 |
"--metadata", |
| 251 |
"metadata.yml", |
| 252 |
"--service", |
| 253 |
"datasette", |
| 254 |
"--plugin-secret", |
| 255 |
"datasette-auth-github", |
| 256 |
"client_id", |
| 257 |
"x-client-id", |
| 258 |
"--show-files", |
| 259 |
"--secret", |
| 260 |
"x-secret", |
| 261 |
], |
| 262 |
) |
| 263 |
assert result.exit_code == 0 |
| 264 |
dockerfile = ( |
| 265 |
result.output.split("==== Dockerfile ====\n")[1] |
| 266 |
.split("\n====================\n")[0] |
| 267 |
.strip() |
| 268 |
) |
| 269 |
expected = textwrap.dedent( |
| 270 |
r""" |
| 271 |
FROM python:3.11.0-slim-bullseye |
| 272 |
COPY . /app |
| 273 |
WORKDIR /app |
| 274 |
|
| 275 |
ENV DATASETTE_AUTH_GITHUB_CLIENT_ID 'x-client-id' |
| 276 |
ENV DATASETTE_SECRET 'x-secret' |
| 277 |
RUN pip install -U datasette |
| 278 |
RUN datasette inspect test.db --inspect-file inspect-data.json |
| 279 |
ENV PORT 8001 |
| 280 |
EXPOSE 8001 |
| 281 |
CMD datasette serve --host 0.0.0.0 -i test.db --cors --inspect-file inspect-data.json --metadata metadata.json --setting force_https_urls on --port $PORT""" |
| 282 |
).strip() |
| 283 |
assert expected == dockerfile |
| 284 |
metadata = ( |
| 285 |
result.output.split("=== metadata.json ===\n")[1] |
| 286 |
.split("\n==== Dockerfile ====\n")[0] |
| 287 |
.strip() |
| 288 |
) |
| 289 |
assert { |
| 290 |
"title": "Hello from metadata YAML", |
| 291 |
"plugins": { |
| 292 |
"datasette-auth-github": { |
| 293 |
"client_id": {"$env": "DATASETTE_AUTH_GITHUB_CLIENT_ID"}, |
| 294 |
"foo": "bar", |
| 295 |
}, |
| 296 |
}, |
| 297 |
} == json.loads(metadata) |
| 298 |
|
| 299 |
|
| 300 |
@pytest.mark.serial |
| 301 |
@mock.patch("shutil.which") |
| 302 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 303 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 304 |
def test_publish_cloudrun_apt_get_install( |
| 305 |
mock_call, mock_output, mock_which, tmp_path_factory |
| 306 |
): |
| 307 |
mock_which.return_value = True |
| 308 |
mock_output.return_value = "myproject" |
| 309 |
|
| 310 |
runner = CliRunner() |
| 311 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 312 |
with open("test.db", "w") as fp: |
| 313 |
fp.write("data") |
| 314 |
result = runner.invoke( |
| 315 |
cli.cli, |
| 316 |
[ |
| 317 |
"publish", |
| 318 |
"cloudrun", |
| 319 |
"test.db", |
| 320 |
"--service", |
| 321 |
"datasette", |
| 322 |
"--show-files", |
| 323 |
"--secret", |
| 324 |
"x-secret", |
| 325 |
"--apt-get-install", |
| 326 |
"ripgrep", |
| 327 |
"--spatialite", |
| 328 |
], |
| 329 |
) |
| 330 |
assert result.exit_code == 0 |
| 331 |
dockerfile = ( |
| 332 |
result.output.split("==== Dockerfile ====\n")[1] |
| 333 |
.split("\n====================\n")[0] |
| 334 |
.strip() |
| 335 |
) |
| 336 |
expected = textwrap.dedent( |
| 337 |
r""" |
| 338 |
FROM python:3.11.0-slim-bullseye |
| 339 |
COPY . /app |
| 340 |
WORKDIR /app |
| 341 |
|
| 342 |
RUN apt-get update && \ |
| 343 |
apt-get install -y ripgrep python3-dev gcc libsqlite3-mod-spatialite && \ |
| 344 |
rm -rf /var/lib/apt/lists/* |
| 345 |
|
| 346 |
ENV DATASETTE_SECRET 'x-secret' |
| 347 |
ENV SQLITE_EXTENSIONS '/usr/lib/x86_64-linux-gnu/mod_spatialite.so' |
| 348 |
RUN pip install -U datasette |
| 349 |
RUN datasette inspect test.db --inspect-file inspect-data.json |
| 350 |
ENV PORT 8001 |
| 351 |
EXPOSE 8001 |
| 352 |
CMD datasette serve --host 0.0.0.0 -i test.db --cors --inspect-file inspect-data.json --setting force_https_urls on --port $PORT |
| 353 |
""" |
| 354 |
).strip() |
| 355 |
assert expected == dockerfile |
| 356 |
|
| 357 |
|
| 358 |
@pytest.mark.serial |
| 359 |
@mock.patch("shutil.which") |
| 360 |
@mock.patch("datasette.publish.cloudrun.check_output") |
| 361 |
@mock.patch("datasette.publish.cloudrun.check_call") |
| 362 |
@pytest.mark.parametrize( |
| 363 |
"extra_options,expected", |
| 364 |
[ |
| 365 |
("", "--setting force_https_urls on"), |
| 366 |
( |
| 367 |
"--setting base_url /foo", |
| 368 |
"--setting base_url /foo --setting force_https_urls on", |
| 369 |
), |
| 370 |
("--setting force_https_urls off", "--setting force_https_urls off"), |
| 371 |
], |
| 372 |
) |
| 373 |
def test_publish_cloudrun_extra_options( |
| 374 |
mock_call, mock_output, mock_which, extra_options, expected, tmp_path_factory |
| 375 |
): |
| 376 |
mock_which.return_value = True |
| 377 |
mock_output.return_value = "myproject" |
| 378 |
|
| 379 |
runner = CliRunner() |
| 380 |
os.chdir(tmp_path_factory.mktemp("runner")) |
| 381 |
with open("test.db", "w") as fp: |
| 382 |
fp.write("data") |
| 383 |
result = runner.invoke( |
| 384 |
cli.cli, |
| 385 |
[ |
| 386 |
"publish", |
| 387 |
"cloudrun", |
| 388 |
"test.db", |
| 389 |
"--service", |
| 390 |
"datasette", |
| 391 |
"--show-files", |
| 392 |
"--extra-options", |
| 393 |
extra_options, |
| 394 |
], |
| 395 |
) |
| 396 |
assert result.exit_code == 0 |
| 397 |
dockerfile = ( |
| 398 |
result.output.split("==== Dockerfile ====\n")[1] |
| 399 |
.split("\n====================\n")[0] |
| 400 |
.strip() |
| 401 |
) |
| 402 |
last_line = dockerfile.split("\n")[-1] |
| 403 |
extra_options = ( |
| 404 |
last_line.split("--inspect-file inspect-data.json")[1] |
| 405 |
.split("--port")[0] |
| 406 |
.strip() |
| 407 |
) |
| 408 |
assert extra_options == expected |