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') def test_get_apps(run): with open(os.path.join(TEST_DATA_DIR, 'podman_pod_ps.json')) as f: run.return_value.stdout = f.read() pods = podman.get_apps() expected_cmd = ['podman', 'pod', 'ps', '--format', 'json'] run.assert_called_once_with(expected_cmd, check=True, stdout=subprocess.PIPE, text=True) assert pods == {'someapp': '0.1', 'anotherapp': '0.2', 'yetanotherapp': '0.3'} @patch('subprocess.run') def test_get_volumes_for_app(run): with open(os.path.join(TEST_DATA_DIR, 'podman_volume_ls.json')) as f: run.return_value.stdout = f.read() volumes = podman.get_volumes_for_app('someapp') expected_cmd = ['podman', 'volume', 'ls', '--filter', 'label=spoc.app=someapp', '--format', 'json'] run.asert_called_once_with(expected_cmd, check=True, stdout=subprocess.PIPE, text=True) assert volumes == {'someapp-conf', 'someapp-data'} @patch('subprocess.run') def test_start_pod(run): podman.start_pod('someapp') expected_cmd = ['podman', 'pod', 'start', 'someapp'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') def test_stop_pod(run): podman.stop_pod('someapp') expected_cmd = ['podman', 'pod', 'stop', '--ignore', 'someapp'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') def test_get_pod_status(run): run.return_value.stdout = 'RESULT\n' status = podman.get_pod_status('someapp') expected_cmd = ['podman', 'pod', 'ps', '--filter', 'label=spoc.app=someapp'] run.assert_called_once_with(expected_cmd, check=True, stdout=subprocess.PIPE, text=True) assert status == 'RESULT' @patch('subprocess.run') def test_get_pod_status_all(run): run.return_value.stdout = 'RESULT\n' status = podman.get_pod_status() expected_cmd = ['podman', 'pod', 'ps'] run.assert_called_once_with(expected_cmd, check=True, stdout=subprocess.PIPE, text=True) assert status == 'RESULT' @patch('subprocess.run') def test_create_volume(run): podman.create_volume('someapp', 'someapp-vol') expected_cmd = ['podman', 'volume', 'create', '--label', 'spoc.app=someapp', 'someapp-vol'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') def test_remove_volume(run): podman.remove_volume('someapp-vol') expected_cmd = ['podman', 'volume', 'rm', 'someapp-vol'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') 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'] run.assert_called_once_with(expected_cmd, check=True) @patch('spoc.podman.stop_pod') @patch('subprocess.run') 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'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') 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'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') 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'] run.assert_called_once_with(expected_cmd, check=True) @patch('subprocess.run') def test_prune(run): podman.prune() run.assert_has_calls([ call(['podman', 'image', 'prune', '--all', '--force'], check=True), call(['podman', 'volume', 'prune', '--force'], check=True), ])