Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dk-installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,6 @@ def execute(self, args):
except KeyboardInterrupt:
# The empty print forces the terminal cursor to move to the first column
print()
pass

proc.terminate()

Expand Down
30 changes: 28 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from argparse import Namespace
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -78,8 +79,14 @@ def analytics_mock():


@pytest.fixture
def execute_mock(action):
with patch.object(action, "execute") as mock:
def execute_mock(action_cls):
with patch.object(action_cls, "execute") as mock:
yield mock


@pytest.fixture
def execute_with_log_mock(action_cls):
with patch.object(action_cls, "execute_with_log") as mock:
yield mock


Expand All @@ -92,6 +99,15 @@ def tmp_data_folder(action_cls):
yield data_folder


@pytest.fixture
def demo_config_path(tmp_data_folder):
path = Path(tmp_data_folder).joinpath("demo-config.json")
config = {"api_host": "demo-api-host", "api_key": "demo-api-key"}
path.write_text(json.dumps(config))
yield path
path.unlink()


@pytest.fixture
def compose_path(tmp_data_folder):
return Path(tmp_data_folder).joinpath("docker-compose.yml")
Expand Down Expand Up @@ -144,4 +160,14 @@ def args_mock():
ns.obs_export = False
ns.skip_verify = False

# Observability defaults
ns.profile = "dk-observability"
ns.namespace = "datakitchen"
ns.driver = "docker"
ns.memory = "4096m"
ns.helm_timeout = 10
ns.app_values = None
ns.docker_username = None
ns.docker_password = None

yield ns
68 changes: 68 additions & 0 deletions tests/test_installer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest

from .installer import Installer, Action, AbortAction, InstallerError


@pytest.fixture
def installer(action_cls, execute_with_log_mock):
class ActionOne(Action):
args_cmd = "one"

def get_parser(self, sub_parsers):
parser = super().get_parser(sub_parsers)
parser.add_argument("--int-arg", type=int, action="store", default=4)
return parser

installer = Installer()
installer.add_product(
"product",
[
ActionOne(),
type("ActionTwo", (Action,), {"args_cmd": "two"})(),
],
)
yield installer


@pytest.mark.integration
@pytest.mark.parametrize("arg_val,arg_expected", ((None, 4), (25, 25)))
def test_calls_action(arg_val, arg_expected, installer, execute_with_log_mock):
ret = installer.run(["product", "one"] + ([] if arg_val is None else [f"--int-arg={arg_val}"]))

assert ret == 0
execute_with_log_mock.assert_called_once()
args = execute_with_log_mock.call_args_list[0].args[0]
assert args.prod == "product"
assert args.int_arg == arg_expected


@pytest.mark.integration
@pytest.mark.parametrize(
"args,expected_in_err",
(
(["other", "one"], "invalid choice: 'other'"),
(["product", "three"], "invalid choice: 'three'"),
(["product", "one", "no-no-no"], "unrecognized arguments: no-no-no"),
(["product", "one", "--int-arg=x"], "invalid int value: 'x'"),
),
ids=("invalid product", "invalid action", "invalid argument", "invalid value"),
)
def test_invalid_args(args, expected_in_err, installer, capfd):
with pytest.raises(SystemExit) as exc_info:
installer.run(args)

assert expected_in_err in capfd.readouterr().err
assert exc_info.value.args == (2,)


@pytest.mark.integration
@pytest.mark.parametrize(
"exc_class,expected_code",
((AbortAction, 1), (InstallerError, 2)),
)
def test_return_codes(exc_class, expected_code, installer, execute_with_log_mock):
execute_with_log_mock.side_effect = exc_class
ret = installer.run(["product", "two"])

assert ret == expected_code
execute_with_log_mock.assert_called_once()
30 changes: 30 additions & 0 deletions tests/test_obs_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import partial
from unittest.mock import call, patch

import pytest

from tests.installer import ObsDeleteAction


@pytest.fixture
def obs_delete_action(action_cls, args_mock, tmp_data_folder, start_cmd_mock):
action = ObsDeleteAction()
args_mock.prod = "obs"
args_mock.action = "delete"
with patch.object(action, "execute", new=partial(action.execute, args_mock)):
yield action


@pytest.mark.integration
def test_obs_delete(obs_delete_action, start_cmd_mock):
obs_delete_action.execute()

def_call = partial(call, raise_on_non_zero=True, env=None)

start_cmd_mock.assert_has_calls(
[
def_call("minikube", "-p", "dk-observability", "delete"),
def_call("docker", "network", "rm", "datakitchen-network"),
],
any_order=True,
)
44 changes: 44 additions & 0 deletions tests/test_obs_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest.mock import call

import pytest

from tests.installer import ObsRunDemoAction, ObsRunHeartbeatDemoAction, ObsDeleteDemoAction


@pytest.mark.integration
@pytest.mark.parametrize(
"action_class,arg_action,demo_cmd",
(
(ObsRunDemoAction, "run-demo", "obs-run-demo"),
(ObsDeleteDemoAction, "delete-demo", "obs-delete-demo"),
(ObsRunHeartbeatDemoAction, "run-heartbeat-demo", "obs-heartbeat-demo"),
),
ids=("run-demo", "delete-demo", "run-heartbeat-demo"),
)
def test_obs_demo_action(action_class, arg_action, demo_cmd, args_mock, start_cmd_mock, demo_config_path):
action = action_class()
args_mock.prod = "obs"
args_mock.action = arg_action

action.execute(args_mock)

start_cmd_mock.assert_has_calls(
[
call(
"docker",
"run",
"--rm",
"--mount",
f"type=bind,source={demo_config_path},target=/dk/demo-config.json",
"--name",
"dk-demo",
"--network",
"datakitchen-network",
"--add-host",
"host.docker.internal:host-gateway",
"datakitchen/data-observability-demo:latest",
demo_cmd,
),
],
any_order=True,
)
57 changes: 57 additions & 0 deletions tests/test_obs_expose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
from functools import partial
from unittest.mock import call, patch

import pytest

from tests.installer import ObsExposeAction


@pytest.fixture
def obs_expose_action(action_cls, args_mock, tmp_data_folder, start_cmd_mock):
action = ObsExposeAction()
args_mock.prod = "obs"
args_mock.action = "expose"
with patch.object(action, "execute", new=partial(action.execute, args_mock)):
yield action


@pytest.mark.integration
def test_obs_expose(obs_expose_action, start_cmd_mock, stdout_mock, proc_mock, demo_config_path, console_msg_mock):
proc_mock.poll.side_effect = [None, 0]
stdout_mock.return_value = [b"some output"]

obs_expose_action.execute()

start_cmd_mock.assert_has_calls(
[
call(
"minikube",
"kubectl",
"--profile",
"dk-observability",
"--",
"--namespace",
"datakitchen",
"--address",
"0.0.0.0",
"port-forward",
"service/observability-ui",
"8501:http",
raise_on_non_zero=False,
),
]
)
assert json.loads(demo_config_path.read_text()) == {
"api_host": "http://host.docker.internal:8501/api",
"api_key": "demo-api-key",
}
console_msg_mock.assert_has_calls(
[
call(" User Interface: http://localhost:8501"),
call(" Event Ingestion API: http://localhost:8501/api/events/v1"),
call(" Observability API: http://localhost:8501/api/observability/v1"),
call(" Agent Heartbeat API: http://localhost:8501/api/agent/v1"),
],
any_order=True,
)
94 changes: 94 additions & 0 deletions tests/test_obs_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from functools import partial
from pathlib import Path
from unittest.mock import call, patch

import pytest

from tests.installer import ObsInstallAction


@pytest.fixture
def obs_install_action(action_cls, args_mock, tmp_data_folder, start_cmd_mock):
action = ObsInstallAction()
args_mock.prod = "obs"
args_mock.action = "install"
with patch.object(action, "execute", new=partial(action.execute, args_mock)):
yield action


@pytest.mark.integration
def test_obs_install(obs_install_action, start_cmd_mock, tmp_data_folder):
obs_install_action.execute()

def_call = partial(call, raise_on_non_zero=True, env=None)

start_cmd_mock.assert_has_calls(
[
def_call("minikube", "-p", "dk-observability", "status", "-o", "json", raise_on_non_zero=False),
def_call("docker", "network", "inspect", "datakitchen-network"),
def_call(
"minikube",
"start",
"--memory=4096m",
"--profile=dk-observability",
"--namespace=datakitchen",
"--driver=docker",
"--kubernetes-version=v1.32.0",
"--network=datakitchen-network",
"--static-ip=192.168.60.5",
"--embed-certs",
"--extra-config=apiserver.service-node-port-range=1-65535",
"--extra-config=kubelet.allowed-unsafe-sysctls=net.core.somaxconn",
),
def_call(
"helm",
"repo",
"add",
"datakitchen",
"https://datakitchen.github.io/dataops-observability/",
"--force-update",
),
def_call("helm", "repo", "update"),
def_call(
"helm",
"install",
"dataops-observability-services",
"datakitchen/dataops-observability-services",
"--namespace=datakitchen",
"--create-namespace",
"--wait",
"--timeout=10m",
),
def_call(
"helm",
"install",
"dataops-observability-app",
"datakitchen/dataops-observability-app",
"--namespace=datakitchen",
"--create-namespace",
"--wait",
"--timeout=10m",
),
def_call(
"minikube",
"kubectl",
"--profile",
"dk-observability",
"--",
"--namespace",
"datakitchen",
"exec",
"-i",
"deployments/agent-api",
"--",
"/dk/bin/cli",
"init",
"--demo",
"--json",
),
def_call("minikube", "profile", "dk-observability"),
],
any_order=True,
)

assert Path(tmp_data_folder).joinpath("dk-obs-credentials.txt").stat().st_size > 0
11 changes: 0 additions & 11 deletions tests/test_tg_run_demo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import json
from functools import partial
from pathlib import Path
from unittest.mock import call, patch

import pytest
Expand All @@ -17,15 +15,6 @@ def tg_run_demo_action(action_cls, args_mock, tmp_data_folder, start_cmd_mock):
yield action


@pytest.fixture
def demo_config_path(tmp_data_folder):
path = Path(tmp_data_folder).joinpath("demo-config.json")
config = {"api_host": "demo-api-host", "api_key": "demo-api-key"}
path.write_text(json.dumps(config))
yield path
path.unlink()


@pytest.mark.integration
@pytest.mark.parametrize("obs_export", (False, True))
def test_tg_run_demo(obs_export, tg_run_demo_action, args_mock, start_cmd_mock, stdout_mock, compose_path, request):
Expand Down