Refactor into visitor pattern

This commit is contained in:
jpic
2020-01-25 16:52:37 +01:00
parent d5d924dd06
commit a866ba5a0d
29 changed files with 590 additions and 444 deletions
+43 -6
View File
@@ -4,9 +4,16 @@ import sys
from podctl.container import Container
from podctl.build import BuildScript
from podctl.visitors import (
Base,
Copy,
Packages,
User,
)
def script_test(name, result):
def script_test(name, *visitors):
result = str(Container(*visitors).script('build'))
path = os.path.join(
os.path.dirname(__file__),
f'test_{name}.sh',
@@ -15,7 +22,7 @@ def script_test(name, result):
if not os.path.exists(path):
with open(path, 'w+') as f:
f.write(result)
raise Exception('Fixture created test_build_packages.sh')
raise Exception(f'Fixture created test_{name}.sh')
with open(path, 'r') as f:
expected = f.read()
result = difflib.unified_diff(
@@ -28,13 +35,43 @@ def script_test(name, result):
def test_build_empty():
result = str(BuildScript(Container()))
script_test('build_empty', result)
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',
packages=['bash'],
files=[
Directory('/app', '0500').add('setup.py', 'podctl'),
]
)))
script_test('build_packages', result)
'''
+18
View File
@@ -0,0 +1,18 @@
#/usr/bin/env bash
base="alpine"
repo="None"
tag="None"
image="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
echo $mounts
mounts=("${mounts[@]/$i}")
echo $mounts
done
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
mounts=("$mnt" "${mounts[@]}")
+7
View File
@@ -1,8 +1,15 @@
#/usr/bin/env bash
base="alpine"
repo="None"
tag="None"
image="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
echo $mounts
mounts=("${mounts[@]/$i}")
echo $mounts
done
}
trap umounts 0
+14 -5
View File
@@ -1,22 +1,31 @@
#/usr/bin/env bash
base="alpine"
repo="None"
tag="None"
image="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
echo $mounts
mounts=("${mounts[@]/$i}")
echo $mounts
done
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
mounts=("$mnt" "${mounts[@]}")
buildah run $ctr -- mkdir -p /var/cache/apk
buildah run --user root $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
old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"
if [ -n "$old" ] || ! ls .cache/apk/APKINDEX.*; then
buildah run --user root $ctr -- apk update
else
echo Cache recent enough, skipping index update.
fi
buildah run $ctr -- apk upgrade
buildah run $ctr -- apk add bash
buildah run --user root $ctr -- apk upgrade
buildah run --user root $ctr -- apk add bash
+38
View File
@@ -0,0 +1,38 @@
#/usr/bin/env bash
base="alpine"
repo="None"
tag="None"
image="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
echo $mounts
mounts=("${mounts[@]/$i}")
echo $mounts
done
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
mounts=("$mnt" "${mounts[@]}")
buildah run --user root $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
old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"
if [ -n "$old" ] || ! ls .cache/apk/APKINDEX.*; then
buildah run --user root $ctr -- apk update
else
echo Cache recent enough, skipping index update.
fi
buildah run --user root $ctr -- apk upgrade
buildah run --user root $ctr -- apk add shadow
if buildah run $ctr -- id 1000; then
i=$(buildah run $ctr -- id -n 1000)
buildah run $ctr -- usermod --home-dir /app --no-log-init 1000 $i
else
buildah run $ctr -- useradd --home-dir /app --uid 1000 app
fi
buildah config --user app $ctr
-56
View File
@@ -1,56 +0,0 @@
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
@@ -1,10 +0,0 @@
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']
+91
View File
@@ -0,0 +1,91 @@
from unittest.mock import MagicMock
from podctl.script import Script
from podctl.visitable import Visitable
class Visitor0:
def __init__(self, name=None):
self.name = name or 'visit0'
class Visitor1:
def pre_build(self, script):
script.append('pre_build')
def build(self, script):
script.append('build')
def post_build(self, script):
script.append('post_build')
def test_visitable_visitor():
visitable = Visitable(Visitor0(), Visitor1(), build=Script())
script = visitable.script('build')
assert 'pre_build' in script
assert 'build' in script
assert 'post_build' in script
def test_visitable_visitor():
x = Visitor0()
assert Visitable(x).visitor('visitor0') is x
def test_visitable_variable():
assert Visitable(Visitor0('foo')).variable('name') == 'foo'
#
#
#def test_visitable_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))