Build script suppotrs packages

This commit is contained in:
jpic
2020-01-24 19:26:43 +01:00
commit d5d924dd06
13 changed files with 673 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
import difflib
import os
import sys
from podctl.container import Container
from podctl.build import BuildScript
def script_test(name, result):
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('Fixture created test_build_packages.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():
result = str(BuildScript(Container()))
script_test('build_empty', result)
def test_build_packages():
result = str(BuildScript(Container(
base='alpine',
packages=['bash'],
)))
script_test('build_packages', result)
+11
View File
@@ -0,0 +1,11 @@
#/usr/bin/env bash
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
done
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
mounts=("$mnt" "${mounts[@]}")
+22
View File
@@ -0,0 +1,22 @@
#/usr/bin/env bash
base="alpine"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
done
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
mounts=("$mnt" "${mounts[@]}")
buildah run $ctr -- mkdir -p /var/cache/apk
mkdir -p $(pwd)/.cache/apk
mount -o bind $(pwd)/.cache/apk $mnt/var/cache/apk
mounts=("$mnt/var/cache/apk" "${mounts[@]}")
buildah run $ctr -- ln -s /var/cache/apk /etc/apk/cache
if [ -n "$(find .cache/apk/ -name APKINDEX.* -mtime +3)" ]; then
buildah run $ctr -- apk update
fi
buildah run $ctr -- apk upgrade
buildah run $ctr -- apk add bash
+56
View File
@@ -0,0 +1,56 @@
from .container import Container, switch
def test_container_configuration():
'''Attributes should be passable to constructor or as class attributes'''
assert Container(a='b')['a'] == 'b'
class Test(Container):
cfg = dict(a='b')
assert Test()['a'] == 'b'
def test_switch_simple():
assert Container(a=switch(default='expected'))['a'] == 'expected'
assert Container(a=switch(noise='noise'))['a'] == None
fixture = Container(
'test',
a=switch(default='noise', test='expected')
)
assert fixture['a'] == 'expected'
assert [*fixture.values()][0] == 'expected'
assert [*fixture.items()][0][1] == 'expected'
def test_switch_iterable():
class TContainer(Container):
cfg = dict(
a=switch(dev='test')
)
assert TContainer()['a'] is None
assert TContainer('dev')['a'] == 'test'
assert TContainer('dev', a=[switch(dev='y')])['a'] == ['y']
assert TContainer('dev', a=[switch(default='y')])['a'] == ['y']
def test_switch_value_list():
assert Container('test').switch_value(
[switch(default='noise', test=False)]
) == [False]
assert Container('none').switch_value(
[switch(noise='noise')]
) == []
def test_switch_value_dict():
assert Container('foo').switch_value(
dict(i=switch(default='expected', noise='noise'))
) == dict(i='expected')
assert Container('test').switch_value(
dict(i=switch(default='noise', test='expected'))
) == dict(i='expected')
assert Container('none').switch_value(
dict(i=switch(noise='noise'), j=dict(e=switch(none=1)))
) == dict(j=dict(e=1))
+10
View File
@@ -0,0 +1,10 @@
import os
from pathlib import Path
from pod import Pod
def test_pod_file():
path = Path(os.path.dirname(__file__)) / '..' / 'pod.py'
pod = Pod.factory(path)
assert pod['podctl']