2022-12-13 01:44:52 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# pylint: disable=missing-function-docstring
|
|
|
|
|
|
|
|
import pytest
|
2022-12-27 00:15:02 +00:00
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
from selfprivacy_api.jobs import JobStatus, Jobs, Job
|
2024-02-03 19:10:01 +00:00
|
|
|
from tests.test_graphql.common import get_data, assert_errorcode, assert_ok
|
2023-08-03 01:47:57 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.jobs.nix_collect_garbage import (
|
|
|
|
get_dead_packages,
|
|
|
|
parse_line,
|
|
|
|
CLEAR_COMPLETED,
|
|
|
|
COMPLETED_WITH_ERROR,
|
|
|
|
RESULT_WAS_NOT_FOUND_ERROR,
|
2024-02-03 19:10:01 +00:00
|
|
|
ShellException,
|
2023-08-03 01:47:57 +00:00
|
|
|
)
|
2022-12-13 01:44:52 +00:00
|
|
|
|
2023-04-05 10:49:56 +00:00
|
|
|
OUTPUT_PRINT_DEAD = """
|
2022-12-19 02:27:44 +00:00
|
|
|
finding garbage collector roots...
|
|
|
|
determining live/dead paths...
|
|
|
|
/nix/store/02k8pmw00p7p7mf2dg3n057771w7liia-python3.10-cchardet-2.1.7
|
|
|
|
/nix/store/03vc6dznx8njbvyd3gfhfa4n5j4lvhbl-python3.10-async-timeout-4.0.2
|
|
|
|
/nix/store/03ybv2dvfk7c3cpb527y5kzf6i35ch41-python3.10-pycparser-2.21
|
|
|
|
/nix/store/04dn9slfqwhqisn1j3jv531lms9w5wlj-python3.10-hypothesis-6.50.1.drv
|
|
|
|
/nix/store/04hhx2z1iyi3b48hxykiw1g03lp46jk7-python-remove-bin-bytecode-hook
|
|
|
|
"""
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:49:56 +00:00
|
|
|
OUTPUT_COLLECT_GARBAGE = """
|
2022-12-19 02:27:44 +00:00
|
|
|
removing old generations of profile /nix/var/nix/profiles/per-user/def/channels
|
|
|
|
finding garbage collector roots...
|
|
|
|
deleting garbage...
|
|
|
|
deleting '/nix/store/02k8pmw00p7p7mf2dg3n057771w7liia-python3.10-cchardet-2.1.7'
|
|
|
|
deleting '/nix/store/03vc6dznx8njbvyd3gfhfa4n5j4lvhbl-python3.10-async-timeout-4.0.2'
|
|
|
|
deleting '/nix/store/03ybv2dvfk7c3cpb527y5kzf6i35ch41-python3.10-pycparser-2.21'
|
|
|
|
deleting '/nix/store/04dn9slfqwhqisn1j3jv531lms9w5wlj-python3.10-hypothesis-6.50.1.drv'
|
|
|
|
deleting '/nix/store/04hhx2z1iyi3b48hxykiw1g03lp46jk7-python-remove-bin-bytecode-hook'
|
|
|
|
deleting unused links...
|
|
|
|
note: currently hard linking saves -0.00 MiB
|
|
|
|
190 store paths deleted, 425.51 MiB freed
|
|
|
|
"""
|
2022-12-13 01:44:52 +00:00
|
|
|
|
2023-04-12 00:59:40 +00:00
|
|
|
OUTPUT_COLLECT_GARBAGE_ZERO_TRASH = """
|
|
|
|
removing old generations of profile /nix/var/nix/profiles/per-user/def/profile
|
|
|
|
removing old generations of profile /nix/var/nix/profiles/per-user/def/channels
|
|
|
|
finding garbage collector roots...
|
|
|
|
deleting garbage...
|
|
|
|
deleting unused links...
|
|
|
|
note: currently hard linking saves 0.00 MiB
|
|
|
|
0 store paths deleted, 0.00 MiB freed
|
|
|
|
"""
|
|
|
|
|
2023-04-05 10:49:56 +00:00
|
|
|
log_event = []
|
|
|
|
|
|
|
|
|
2024-01-30 14:04:43 +00:00
|
|
|
@pytest.fixture
|
2024-02-03 19:10:01 +00:00
|
|
|
def mock_delete_old_gens_and_return_dead_report(mocker):
|
|
|
|
mock = mocker.patch("selfprivacy_api.jobs.nix_collect_garbage.delete_old_gens_and_return_dead_report", autospec=True, return_value=OUTPUT_PRINT_DEAD)
|
2024-01-30 14:04:43 +00:00
|
|
|
return mock
|
|
|
|
|
2024-02-03 19:10:01 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_run_nix_collect_garbage_returns_gibberish(mocker):
|
|
|
|
mock = mocker.patch("selfprivacy_api.jobs.nix_collect_garbage.run_nix_collect_garbage", autospec=True, return_value=b" oiuojkhjkhjkhjkhkjh")
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
2023-04-05 10:49:56 +00:00
|
|
|
# ---
|
|
|
|
|
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
def test_parse_line():
|
|
|
|
txt = "note: currently hard linking saves -0.00 MiB 190 store paths deleted, 425.51 MiB freed"
|
|
|
|
|
|
|
|
job = Jobs.add(
|
|
|
|
name="name",
|
|
|
|
type_id="parse_line",
|
|
|
|
description="description",
|
2022-12-19 02:27:44 +00:00
|
|
|
)
|
2022-12-13 01:44:52 +00:00
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
output = parse_line(job, txt)
|
|
|
|
assert output.result == '425.51 MiB have been cleared'
|
|
|
|
assert output.status == JobStatus.FINISHED
|
|
|
|
assert output.error is None
|
2022-12-13 01:44:52 +00:00
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
|
|
|
|
def test_parse_line_with_blank_line():
|
2022-12-19 02:27:44 +00:00
|
|
|
txt = ""
|
2023-11-29 09:02:59 +00:00
|
|
|
job = Jobs.add(
|
|
|
|
name="name",
|
|
|
|
type_id="parse_line",
|
|
|
|
description="description",
|
2022-12-19 02:27:44 +00:00
|
|
|
)
|
2023-11-29 09:02:59 +00:00
|
|
|
|
2024-02-03 19:10:01 +00:00
|
|
|
with pytest.raises(ShellException):
|
|
|
|
output = parse_line(job, txt)
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
def test_get_dead_packages():
|
2023-04-05 10:49:56 +00:00
|
|
|
assert get_dead_packages(OUTPUT_PRINT_DEAD) == (5, 20.0)
|
2022-12-19 02:27:44 +00:00
|
|
|
|
|
|
|
|
2023-11-29 09:02:59 +00:00
|
|
|
def test_get_dead_packages_zero():
|
2023-08-03 01:47:57 +00:00
|
|
|
assert get_dead_packages("") == (0, 0)
|
|
|
|
|
|
|
|
|
2023-10-16 16:39:58 +00:00
|
|
|
RUN_NIX_COLLECT_GARBAGE_MUTATION = """
|
2023-08-07 12:56:35 +00:00
|
|
|
mutation CollectGarbage {
|
|
|
|
system {
|
|
|
|
nixCollectGarbage {
|
|
|
|
success
|
|
|
|
message
|
2023-10-11 22:59:31 +00:00
|
|
|
code
|
|
|
|
job {
|
|
|
|
uid,
|
|
|
|
typeId,
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
status,
|
|
|
|
statusText,
|
|
|
|
progress,
|
|
|
|
createdAt,
|
|
|
|
updatedAt,
|
|
|
|
finishedAt,
|
|
|
|
error,
|
|
|
|
result,
|
|
|
|
}
|
2023-08-07 12:56:35 +00:00
|
|
|
}
|
2023-08-03 01:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2024-02-03 19:10:01 +00:00
|
|
|
def test_graphql_nix_collect_garbage(authorized_client, mock_delete_old_gens_and_return_dead_report):
|
2023-08-03 01:47:57 +00:00
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
2023-10-16 16:39:58 +00:00
|
|
|
"query": RUN_NIX_COLLECT_GARBAGE_MUTATION,
|
2023-08-03 01:47:57 +00:00
|
|
|
},
|
2022-12-27 00:15:02 +00:00
|
|
|
)
|
|
|
|
|
2024-02-03 19:10:01 +00:00
|
|
|
output = get_data(response)["system"]["nixCollectGarbage"]
|
|
|
|
assert_ok(output)
|
|
|
|
assert output["job"] is not None
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_nix_collect_garbage_with_problems(authorized_client, mock_run_nix_collect_garbage_returns_gibberish):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": RUN_NIX_COLLECT_GARBAGE_MUTATION,
|
|
|
|
},
|
|
|
|
)
|
2023-10-11 22:59:31 +00:00
|
|
|
|
2024-02-03 19:10:01 +00:00
|
|
|
output = get_data(response)["system"]["nixCollectGarbage"]
|
|
|
|
assert_ok(output)
|
|
|
|
assert output["job"] is not None
|
|
|
|
assert output["job"]["status"] == "ERROR"
|