Skip to content

Commit

Permalink
Log drenv log in tests
Browse files Browse the repository at this point in the history
Like limactl, drenv logs only to stderr, so when running it in tests
with:

    commands.run("drenv", "start", ...)

we don't see anything in the test logs. If the test is blocking for long
time, we have no way to debug this. The helpful log lines are buffered
in the command error buffer.

Use the new stderr= argument to watch and log the command output, and
add helpers for drenv in a consistent way.

Signed-off-by: Nir Soffer <[email protected]>
  • Loading branch information
nirs committed Sep 17, 2024
1 parent 2cbca30 commit 9afeed7
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions test/drenv/drenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# SPDX-License-Identifier: Apache-2.0

import json
import logging
import os
import subprocess

import yaml
import pytest
Expand All @@ -19,47 +21,47 @@
def test_start_unknown():
# Cluster does not exists, so it should fail.
with pytest.raises(commands.Error):
commands.run("drenv", "start", "--name-prefix", "unknown-", EXTERNAL_ENV)
watch("drenv", "start", "--name-prefix", "unknown-", EXTERNAL_ENV, "--verbose")


def test_start(tmpenv):
commands.run("drenv", "start", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV)
watch("drenv", "start", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV, "--verbose")
assert cluster.status(tmpenv.prefix + "cluster") == cluster.READY


def test_dump_without_prefix():
out = commands.run("drenv", "dump", EXAMPLE_ENV)
out = run("drenv", "dump", EXAMPLE_ENV)
dump = yaml.safe_load(out)
assert dump["profiles"][0]["name"] == "ex1"
assert dump["profiles"][1]["name"] == "ex2"


def test_dump_with_prefix():
out = commands.run("drenv", "dump", "--name-prefix", "test-", EXAMPLE_ENV)
out = run("drenv", "dump", "--name-prefix", "test-", EXAMPLE_ENV)
dump = yaml.safe_load(out)
assert dump["profiles"][0]["name"] == "test-ex1"
assert dump["profiles"][1]["name"] == "test-ex2"


def test_stop_unknown():
# Does nothing, so should succeed.
commands.run("drenv", "stop", "--name-prefix", "unknown-", EXTERNAL_ENV)
run("drenv", "stop", "--name-prefix", "unknown-", EXTERNAL_ENV)


def test_stop(tmpenv):
# Stop does nothing, so cluster must be ready.
commands.run("drenv", "stop", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV)
run("drenv", "stop", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV)
assert cluster.status(tmpenv.prefix + "cluster") == cluster.READY


def test_delete_unknown():
# Does nothing, so should succeed.
commands.run("drenv", "delete", "--name-prefix", "unknown-", EXTERNAL_ENV)
run("drenv", "delete", "--name-prefix", "unknown-", EXTERNAL_ENV)


def test_delete(tmpenv):
# Delete does nothing, so cluster must be ready.
commands.run("drenv", "delete", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV)
run("drenv", "delete", "--name-prefix", tmpenv.prefix, EXTERNAL_ENV)
assert cluster.status(tmpenv.prefix + "cluster") == cluster.READY


Expand All @@ -76,7 +78,7 @@ def test_missing_addon(tmpdir):
path = tmpdir.join("missing-addon.yaml")
path.write(content)
with pytest.raises(commands.Error):
commands.run("drenv", "start", str(path))
run("drenv", "start", str(path))


def test_kustomization(tmpdir):
Expand Down Expand Up @@ -153,3 +155,12 @@ def get_config(context=None, kubeconfig=None):
args.append(f"--kubeconfig={kubeconfig}")
out = kubectl.config(*args, context=context)
return json.loads(out)


def run(*args):
return commands.run(*args)


def watch(*args):
for line in commands.watch(*args, stderr=subprocess.STDOUT):
logging.debug("%s", line)

0 comments on commit 9afeed7

Please sign in to comment.