rewritewip
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
from shlax import *
|
||||
|
||||
inner = Run()
|
||||
other = Run('ls')
|
||||
middle = Buildah('alpine', inner, other)
|
||||
outer = Localhost(middle)
|
||||
middle = outer.actions[0]
|
||||
other = middle.actions[1]
|
||||
inner = middle.actions[0]
|
||||
|
||||
|
||||
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
|
||||
@@ -1,7 +0,0 @@
|
||||
from shlax.cli import ConsoleScript
|
||||
|
||||
|
||||
def test_parser():
|
||||
parser = ConsoleScript.Parser(['@host'])
|
||||
parser.parse()
|
||||
assert parser.targets['host'] == Ssh('host')
|
||||
@@ -1,33 +0,0 @@
|
||||
import pytest
|
||||
import os
|
||||
|
||||
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):
|
||||
Image.ENV_TAGS = []
|
||||
im = Image(arg)
|
||||
for k, v in expected.items():
|
||||
assert getattr(im, k) == v
|
||||
|
||||
def test_args_env():
|
||||
os.environ['IMAGE_TEST_ARGS_ENV'] = 'foo'
|
||||
Image.ENV_TAGS = ['IMAGE_TEST_ARGS_ENV']
|
||||
im = Image('re/po:x,y')
|
||||
assert im.tags == ['x', 'y', 'foo']
|
||||
@@ -1,24 +0,0 @@
|
||||
import pytest
|
||||
from shlax import Output
|
||||
|
||||
|
||||
class Write:
|
||||
def __init__(self):
|
||||
self.output = ''
|
||||
def __call__(self, out):
|
||||
self.output += out.decode('utf8')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def write():
|
||||
return Write()
|
||||
|
||||
|
||||
def test_output_regexps(write):
|
||||
output = Output(
|
||||
regexps={'^(.*)$': '{red}\\1'},
|
||||
write=write,
|
||||
flush=lambda: None,
|
||||
)
|
||||
output('foo')
|
||||
assert write.output.strip() == output.colors['red'] + 'foo' + output.colors['reset']
|
||||
@@ -1,5 +0,0 @@
|
||||
from shlax.play import Play
|
||||
|
||||
|
||||
def test_play_call():
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
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', 'umount', ''),
|
||||
('buildah', 'rm', ''),
|
||||
]
|
||||
),
|
||||
(
|
||||
Buildah('alpine', Run('echo hi', user='root')),
|
||||
[
|
||||
('buildah', 'from', 'alpine'),
|
||||
('buildah', 'mount', ''),
|
||||
('buildah', 'run', '--user', 'root', '', '--', 'sh', '-euc', 'echo hi'),
|
||||
('buildah', 'umount', ''),
|
||||
('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', 'umount', ''),
|
||||
('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
|
||||
@@ -1,47 +0,0 @@
|
||||
import copy
|
||||
|
||||
|
||||
class Action:
|
||||
args = dict(
|
||||
step=None,
|
||||
)
|
||||
|
||||
|
||||
class
|
||||
user=dict(
|
||||
doc='Username',
|
||||
required=True,
|
||||
),
|
||||
steps=dict(
|
||||
up='Started',
|
||||
down='Stopped',
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class Target(Action):
|
||||
def __call__(self, action):
|
||||
action = copy.deepcopy(action)
|
||||
action.target = self
|
||||
|
||||
|
||||
class FakeAction(Action):
|
||||
|
||||
|
||||
def __init__(self, user, path, *steps, **kwargs)
|
||||
self.user = user
|
||||
self.path = path
|
||||
self.steps = steps
|
||||
self.kwargs = kwargs
|
||||
|
||||
|
||||
action = Action('root', '/test', 'up', 'rm')
|
||||
target = Target()
|
||||
@@ -1,6 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import pytest
|
||||
|
||||
if not os.getenv('CI'):
|
||||
pytest.skip('Please run with ./shlaxfile.py test', allow_module_level=True)
|
||||
@@ -0,0 +1,67 @@
|
||||
import pytest
|
||||
|
||||
from shlax.targets.base import Target
|
||||
from shlax.actions.run import Run
|
||||
from shlax.actions.parallel import Parallel
|
||||
from shlax.result import Result
|
||||
|
||||
|
||||
class Error:
|
||||
async def __call__(self, target):
|
||||
raise Exception('lol')
|
||||
|
||||
|
||||
class Target(Target):
|
||||
def exec(self, *args):
|
||||
print(*args)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_success():
|
||||
action = Run('echo hi')
|
||||
target = Target(action)
|
||||
await target()
|
||||
assert action.status == 'success'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_error():
|
||||
action = Error()
|
||||
target = Target(action)
|
||||
await target()
|
||||
assert action.status == 'failure'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested():
|
||||
nested = Error()
|
||||
|
||||
class Nesting:
|
||||
async def __call__(self, target):
|
||||
await target(nested)
|
||||
nesting = Nesting()
|
||||
|
||||
target = Target(nesting)
|
||||
await target()
|
||||
|
||||
assert len(target.results) == 2
|
||||
assert target.results == [nested, nesting]
|
||||
assert target.results[0].status == 'failure'
|
||||
assert target.results[1].status == 'failure'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parallel():
|
||||
winner = Run('echo hi')
|
||||
looser = Error()
|
||||
parallel = Parallel(winner, looser)
|
||||
|
||||
target = Target(parallel)
|
||||
await target()
|
||||
assert len(target.results) == 3
|
||||
assert target.results[0].status == 'success'
|
||||
assert target.results[0] == winner
|
||||
assert target.results[1].status == 'failure'
|
||||
assert target.results[1] == looser
|
||||
assert target.results[2].status == 'failure'
|
||||
assert target.results[2] == parallel
|
||||
Reference in New Issue
Block a user