2020-03-13 22:39:33 +00:00
|
|
|
"""Test Autostop class."""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-11-16 00:02:32 +00:00
|
|
|
import docker
|
2021-01-03 14:36:04 +00:00
|
|
|
from dockertidy import autostop
|
2020-03-13 22:39:33 +00:00
|
|
|
|
|
|
|
pytest_plugins = [
|
2020-04-06 20:14:31 +00:00
|
|
|
"dockertidy.test.fixtures.fixtures",
|
2020-03-13 22:39:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-01-03 14:36:04 +00:00
|
|
|
def autostop_fixture(mocker):
|
2020-03-16 19:12:55 +00:00
|
|
|
mocker.patch.object(
|
2021-01-03 14:36:04 +00:00
|
|
|
autostop.AutoStop,
|
2020-03-16 19:12:55 +00:00
|
|
|
"_get_docker_client",
|
|
|
|
return_value=mocker.create_autospec(docker.APIClient)
|
|
|
|
)
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
stop = autostop.AutoStop()
|
2020-03-13 22:39:33 +00:00
|
|
|
return stop
|
|
|
|
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
def test_stop_container(autostop_fixture, mocker):
|
2020-03-13 22:39:33 +00:00
|
|
|
client = mocker.create_autospec(docker.APIClient)
|
|
|
|
cid = "asdb"
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
autostop_fixture._stop_container(client, cid)
|
2020-03-13 22:39:33 +00:00
|
|
|
client.stop.assert_called_once_with(cid)
|
|
|
|
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
def test_build_container_matcher(autostop_fixture, mocker):
|
2020-03-13 22:39:33 +00:00
|
|
|
prefixes = ["one_", "two_"]
|
2021-01-03 14:36:04 +00:00
|
|
|
matcher = autostop_fixture._build_container_matcher(prefixes)
|
2020-03-13 22:39:33 +00:00
|
|
|
|
|
|
|
assert matcher("one_container")
|
|
|
|
assert matcher("two_container")
|
|
|
|
assert not matcher("three_container")
|
|
|
|
assert not matcher("one")
|
|
|
|
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
def test_has_been_running_since_true(autostop_fixture, container, later_time):
|
|
|
|
assert autostop_fixture._has_been_running_since(container, later_time)
|
2020-03-13 22:39:33 +00:00
|
|
|
|
|
|
|
|
2021-01-03 14:36:04 +00:00
|
|
|
def test_has_been_running_since_false(autostop_fixture, container, earlier_time):
|
|
|
|
assert not autostop_fixture._has_been_running_since(container, earlier_time)
|