shlax/tests/test_build.py
2020-01-25 16:58:57 +01:00

83 lines
1.5 KiB
Python

import difflib
import os
import sys
from podctl.container import Container
from podctl.build import BuildScript
from podctl.visitors import (
Base,
Copy,
Packages,
User,
)
from unittest import mock
from podctl.visitors import packages
packages.subprocess.check_call = mock.Mock()
def script_test(name, *visitors):
result = str(Container(*visitors).script('build'))
path = os.path.join(
os.path.dirname(__file__),
f'test_{name}.sh',
)
if not os.path.exists(path):
with open(path, 'w+') as f:
f.write(result)
raise Exception(f'Fixture created test_{name}.sh')
with open(path, 'r') as f:
expected = f.read()
result = difflib.unified_diff(
expected,
result,
fromfile='expected',
tofile='result'
)
assert not list(result), sys.stdout.writelines(result)
def test_build_empty():
script_test(
'build_empty',
Base('alpine'),
)
def test_build_packages():
script_test(
'build_packages',
Base('alpine'),
Packages('bash'),
)
def test_build_user():
script_test(
'build_user',
Base('alpine'),
User('app', 1000, '/app'),
)
def test_build_copy():
script_test(
'build_copy',
Base('alpine'),
Copy(os.path.dirname(__file__), '/app'),
)
'''
def test_build_files():
result = str(BuildScript(Container(
base='alpine',
files=[
Directory('/app', '0500').add('setup.py', 'podctl'),
]
)))
'''