2021-07-06 18:06:54 +02:00
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
from unittest.mock import patch, call
|
|
|
|
|
|
|
|
from spoc import podman
|
|
|
|
|
|
|
|
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'test_data')
|
|
|
|
|
|
|
|
@patch('subprocess.run')
|
2021-07-11 00:58:28 +02:00
|
|
|
def test_run(run):
|
|
|
|
process = podman.run(['foo', 'bar'])
|
|
|
|
|
|
|
|
run.assert_called_once_with(['foo', 'bar'], check=True, env=podman.ENV)
|
|
|
|
assert process == run.return_value
|
|
|
|
|
|
|
|
@patch('subprocess.run')
|
|
|
|
def test_out(run):
|
|
|
|
run.return_value.stdout = 'RESULT\n'
|
|
|
|
output = podman.out(['foo', 'bar'], arg1=123, arg2=True)
|
|
|
|
|
|
|
|
run.assert_called_once_with(['foo', 'bar'], stdout=subprocess.PIPE, text=True, check=True,
|
|
|
|
env=podman.ENV, arg1=123, arg2=True)
|
|
|
|
assert output == 'RESULT'
|
|
|
|
|
|
|
|
@patch('spoc.podman.out')
|
|
|
|
def test_get_apps(out):
|
2021-07-06 18:06:54 +02:00
|
|
|
with open(os.path.join(TEST_DATA_DIR, 'podman_pod_ps.json')) as f:
|
2021-07-11 00:58:28 +02:00
|
|
|
out.return_value = f.read()
|
2021-07-06 18:06:54 +02:00
|
|
|
|
|
|
|
pods = podman.get_apps()
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'ps', '--format', 'json']
|
2021-07-11 00:58:28 +02:00
|
|
|
out.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
assert pods == {'someapp': '0.1', 'anotherapp': '0.2', 'yetanotherapp': '0.3'}
|
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.out')
|
|
|
|
def test_get_volumes_for_app(out):
|
2021-07-06 18:06:54 +02:00
|
|
|
with open(os.path.join(TEST_DATA_DIR, 'podman_volume_ls.json')) as f:
|
2021-07-11 00:58:28 +02:00
|
|
|
out.return_value = f.read()
|
2021-07-06 18:06:54 +02:00
|
|
|
|
|
|
|
volumes = podman.get_volumes_for_app('someapp')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'volume', 'ls', '--filter', 'label=spoc.app=someapp',
|
|
|
|
'--format', 'json']
|
2021-07-11 00:58:28 +02:00
|
|
|
out.asert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
assert volumes == {'someapp-conf', 'someapp-data'}
|
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_start_pod(run):
|
|
|
|
podman.start_pod('someapp')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'start', 'someapp']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_stop_pod(run):
|
|
|
|
podman.stop_pod('someapp')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'stop', '--ignore', 'someapp']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.out')
|
|
|
|
def test_get_pod_status(out):
|
|
|
|
out.return_value = 'RESULT'
|
2021-07-06 18:06:54 +02:00
|
|
|
status = podman.get_pod_status('someapp')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'ps', '--filter', 'label=spoc.app=someapp']
|
2021-07-11 00:58:28 +02:00
|
|
|
out.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
assert status == 'RESULT'
|
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.out')
|
|
|
|
def test_get_pod_status_all(out):
|
|
|
|
out.return_value = 'RESULT'
|
2021-07-06 18:06:54 +02:00
|
|
|
status = podman.get_pod_status()
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'ps']
|
2021-07-11 00:58:28 +02:00
|
|
|
out.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
assert status == 'RESULT'
|
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_create_volume(run):
|
|
|
|
podman.create_volume('someapp', 'someapp-vol')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'volume', 'create', '--label', 'spoc.app=someapp', 'someapp-vol']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_remove_volume(run):
|
|
|
|
podman.remove_volume('someapp-vol')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'volume', 'rm', 'someapp-vol']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_create_pod(run):
|
|
|
|
podman.create_pod('someapp', '0.1')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'pod', 'create', '--name', 'someapp',
|
|
|
|
'--label', 'spoc.app=someapp', '--label', 'spoc.version=0.1']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
|
|
|
@patch('spoc.podman.stop_pod')
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_remove_pod(run, stop_pod):
|
|
|
|
podman.remove_pod('someapp')
|
|
|
|
|
|
|
|
stop_pod.assert_called_once_with('someapp')
|
|
|
|
expected_cmd = ['podman', 'pod', 'rm', '--ignore', 'someapp']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_create_container(run):
|
|
|
|
podman.create_container('someapp', 'someapp-cnt', 'example.com/someapp:0.23.6-210515',
|
|
|
|
env_file='/var/lib/spoc/someapp.env',
|
|
|
|
volumes={'someapp-srv': '/srv', 'someapp-mnt': '/mnt'},
|
|
|
|
requires={'someapp-cnt3', 'someapp-cnt2'},
|
|
|
|
hosts={'cnt2', 'cnt3', 'cnt'})
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'container', 'create', '--name', 'someapp-cnt', '--pod', 'someapp',
|
|
|
|
'--restart', 'unless-stopped', '--env-file', '/var/lib/spoc/someapp.env',
|
|
|
|
'--requires', 'someapp-cnt2,someapp-cnt3', '--volume', 'someapp-mnt:/mnt',
|
|
|
|
'--volume', 'someapp-srv:/srv', '--add-host', 'cnt:127.0.0.1',
|
|
|
|
'--add-host', 'cnt2:127.0.0.1', '--add-host', 'cnt3:127.0.0.1',
|
|
|
|
'example.com/someapp:0.23.6-210515']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_create_container_minimal(run):
|
|
|
|
podman.create_container('someapp', 'someapp-cnt', 'example.com/someapp:0.23.6-210515')
|
|
|
|
|
|
|
|
expected_cmd = ['podman', 'container', 'create', '--name', 'someapp-cnt', '--pod', 'someapp',
|
|
|
|
'--restart', 'unless-stopped', 'example.com/someapp:0.23.6-210515']
|
2021-07-11 00:58:28 +02:00
|
|
|
run.assert_called_once_with(expected_cmd)
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@patch('spoc.podman.run')
|
2021-07-06 18:06:54 +02:00
|
|
|
def test_prune(run):
|
|
|
|
podman.prune()
|
|
|
|
|
|
|
|
run.assert_has_calls([
|
2021-07-11 00:58:28 +02:00
|
|
|
call(['podman', 'image', 'prune', '--all', '--force']),
|
|
|
|
call(['podman', 'volume', 'prune', '--force']),
|
2021-07-06 18:06:54 +02:00
|
|
|
])
|