Extracted shlax from podctl

This commit is contained in:
jpic
2020-02-14 20:14:56 +01:00
parent c78874a7e0
commit 2e94928aef
51 changed files with 750 additions and 940 deletions
+50
View File
@@ -0,0 +1,50 @@
from shlax import *
inner = Run()
other = Run('ls')
middle = Buildah('alpine', inner, other)
outer = Localhost(middle)
def test_action_init_args():
assert other.args == ('ls',)
def test_action_parent_autoset():
assert list(outer.actions) == [middle]
assert middle.parent == outer
assert inner.parent == middle
assert other.parent == middle
def test_action_context():
assert outer.context is inner.context
assert middle.context is inner.context
assert middle.context is outer.context
assert other.context is outer.context
def test_action_sibblings():
assert inner.sibblings() == [other]
assert inner.sibblings(lambda s: s.args[0] == 'ls') == [other]
assert inner.sibblings(lambda s: s.args[0] == 'foo') == []
assert inner.sibblings(type='run') == [other]
assert inner.sibblings(args=('ls',)) == [other]
def test_actions_parents():
assert other.parents() == [middle, outer]
assert other.parents(lambda p: p.base == 'alpine') == [middle]
assert inner.parents(type='localhost') == [outer]
assert inner.parents(type='buildah') == [middle]
def test_action_childrens():
assert middle.children() == [inner, other]
assert middle.children(lambda a: a.args[0] == 'ls') == [other]
assert outer.children() == [middle, inner, other]
def test_action_getattr():
assert other.exec == middle.exec
assert other.shargs == middle.shargs
-19
View File
@@ -1,19 +0,0 @@
from podctl.visitors.commit import Commit
def test_name_parse():
commit = Commit('foo.ee/bar/test:y')
assert commit.registry == 'foo.ee'
assert commit.repo == 'bar/test'
assert commit.tags == ['y']
commit = Commit('foo.ee/bar/test')
assert commit.registry == 'foo.ee'
assert commit.repo == 'bar/test'
commit = Commit('bar/test')
assert commit.repo == 'bar/test'
commit = Commit('bar/test:y')
assert commit.repo == 'bar/test'
assert commit.tags == ['y']
+31
View File
@@ -0,0 +1,31 @@
import pytest
from shlax import Image
tests = {
'docker://a.b:1337/re/po:x,y': ('docker', 'a.b:1337', 're/po', 'x,y'),
'docker://a.b/re/po:x,y': ('docker', 'a.b', 're/po', 'x,y'),
'a.b:1337/re/po:x,y': (None, 'a.b:1337', 're/po', 'x,y'),
'a.b/re/po:x,y': (None, 'a.b', 're/po', 'x,y'),
're/po:x,y': (None, None, 're/po', 'x,y'),
're/po': (None, None, 're/po', 'latest'),
'docker://re/po': ('docker', None, 're/po', 'latest'),
'docker://re/po:x,y': ('docker', None, 're/po', 'x,y'),
}
@pytest.mark.parametrize(
'arg,expected', [(k, dict(
backend=v[0], registry=v[1], repository=v[2], tags=v[3].split(',')
)) for k, v in tests.items()]
)
def test_args(arg, expected):
im = Image(arg)
for k, v in expected.items():
assert getattr(im, k) == v
def test_args_env():
import os
os.environ['CIRCLE_TAG'] = 'foo'
im = Image('re/po:x,y')
assert im.tags == ['x', 'y', 'foo']
+62
View File
@@ -0,0 +1,62 @@
import pytest
from unittest.mock import patch
from shlax import *
from shlax import proc
test_args_params = [
(
Localhost(Run('echo hi')),
[('sh', '-euc', 'echo hi')]
),
(
Localhost(Run('echo hi', user='jimi')),
[('sudo', '-u', 'jimi', 'sh', '-euc', 'echo hi')]
),
(
Localhost(Run('echo hi', user='root')),
[('sudo', 'sh', '-euc', 'echo hi')]
),
(
Ssh('host', Run('echo hi', user='root')),
[('ssh', 'host', 'sudo', 'sh', '-euc', 'echo hi')]
),
(
Buildah('alpine', Run('echo hi')),
[
('buildah', 'from', 'alpine'),
('buildah', 'mount', ''),
('buildah', 'run', '', '--', 'sh', '-euc', 'echo hi'),
('buildah', 'rm', ''),
]
),
(
Buildah('alpine', Run('echo hi', user='root')),
[
('buildah', 'from', 'alpine'),
('buildah', 'mount', ''),
('buildah', 'run', '--user', 'root', '', '--', 'sh', '-euc', 'echo hi'),
('buildah', 'rm', ''),
]
),
(
Ssh('host', Buildah('alpine', Run('echo hi', user='root'))),
[
('ssh', 'host', 'buildah', 'from', 'alpine'),
('ssh', 'host', 'buildah', 'mount', ''),
('ssh', 'host', 'buildah', 'run', '--user', 'root', '', '--', 'sh', '-euc', 'echo hi'),
('ssh', 'host', 'buildah', 'rm', ''),
]
),
]
@pytest.mark.parametrize(
'script,commands',
test_args_params
)
@pytest.mark.asyncio
async def test_args(script, commands):
with Proc.mock():
await script()
assert commands == Proc.test
-91
View File
@@ -1,91 +0,0 @@
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))