Skip to content

Commit

Permalink
Integration test for deploy with volumes, refs #10
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 11, 2022
1 parent 0b9a966 commit fb09455
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
54 changes: 27 additions & 27 deletions datasette_publish_fly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ def fly(
"about_url": about_url,
}

if not generate_dir:
apps = existing_apps()
if app not in apps:
# Attempt to create the app
result = run(
[
"flyctl",
"apps",
"create",
"--name",
app,
"--json",
],
stderr=PIPE,
stdout=PIPE,
)
if result.returncode:
raise click.ClickException(
"Error calling 'flyctl apps create':\n\n{}".format(
# Don't include Usage: - could be confused for usage
# instructions for datasette publish fly
result.stderr.decode("utf-8")
.split("Usage:")[0]
.strip()
)
)

volume_to_mount = None

if create_volume and not generate_dir:
Expand Down Expand Up @@ -247,33 +274,6 @@ def fly(
lines[-1] += " /data/*.db"
open("Dockerfile", "w").write("\n".join(lines))

if not generate_dir:
apps = existing_apps()
if app not in apps:
# Attempt to create the app
result = run(
[
"flyctl",
"apps",
"create",
"--name",
app,
"--json",
],
stderr=PIPE,
stdout=PIPE,
)
if result.returncode:
raise click.ClickException(
"Error calling 'flyctl apps create':\n\n{}".format(
# Don't include Usage: - could be confused for usage
# instructions for datasette publish fly
result.stderr.decode("utf-8")
.split("Usage:")[0]
.strip()
)
)

if secrets_to_set and not generate_dir:
secrets_args = ["flyctl", "secrets", "set"]
for pair in secrets_to_set.items():
Expand Down
71 changes: 71 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,58 @@ def test_basic():
assert app["Deployed"] is True


def test_with_volume():
runner = CliRunner()
app_name = APP_PREFIX + "v-" + secrets.token_hex(4)
with runner.isolated_filesystem():
sqlite3.connect("test.db").execute("create table foo (id integer primary key)")
result = runner.invoke(
cli.cli,
[
"publish",
"fly",
"test.db",
"-a",
app_name,
"--create-volume",
"1",
"--create-db",
"writeme",
"--install",
"datasette-graphql",
"--plugin-secret",
"foo",
"bar",
"baz",
"--show-files",
],
catch_exceptions=False,
)
assert result.exit_code == 0, result.output
# These fragments are expected in Dockerfile or fly.toml or metadata.json
fragments = (
"CMD datasette serve --host 0.0.0.0 -i test.db",
"/data/writeme.db --create --port $PORT /data/*.db",
'destination = "/data"\n source = "datasette"' '"$env": "FOO_BAR"',
)
for fragment in fragments:
assert fragment in result.output
# Confirm app has deployed
apps = get_apps()
matches = [a for a in apps if a["Name"] == app_name]
assert matches, "No app found with expected name: " + app_name
app = matches[0]
assert app["Status"] == "running"
assert app["Deployed"] is True
# That app should also have a volume
volumes = get_volumes(app_name)
assert len(volumes) == 1
assert volumes[0]["Name"] == "datasette"
# Check secrets - there's no --json for that yet
app_secrets = get_secrets(app_name)
assert "FOO_BAR" in app_secrets


def cleanup_any_resources():
app_names = [app["Name"] for app in get_apps()]
# Delete any starting with publish-fly-temp-
Expand All @@ -59,3 +111,22 @@ def get_apps():
stderr=subprocess.PIPE,
)
return json.loads(process.stdout)


def get_volumes(app_name):
process = subprocess.run(
["flyctl", "volumes", "list", "-a", app_name, "--json"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
return json.loads(process.stdout)


def get_secrets(app_name):
# No --json for this yet, so just returns a big ugly string
process = subprocess.run(
["flyctl", "secrets", "list", "-a", app_name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
return process.stdout.decode("utf-8")

0 comments on commit fb09455

Please sign in to comment.