def test_runrecipe_eq() -> None: """Test equality of RunRecipes.""" r1 = RunRecipe() r2 = RunRecipe( script=["ls"], ports=[(123, 456), (2, 3)], sockets=[("/var/yargh", "/var/aaargh")], volumes=[("/fish", "/fingers")], ) r3 = RunRecipe( script=["ls"], ports=[(123, 456), (2, 3)], sockets=[("/var/yargh", "/var/aaargh")], volumes=[("/fish", "/fingers")], ) r4 = RunRecipe( script=["ls -l"], ports=[(123, 456), (2, 3)], sockets=[("/var/yargh", "/var/aaargh")], volumes=[("/fish", "/fingers")], ) assert r1 == r1 assert r2 == r3 and r3 == r2 assert r1 != r2 and r2 != r1 assert r2 != r4 and r4 != r2 assert RunRecipe() != 42
def test_runrecipe_add() -> None: """Test adding together RunRecipes.""" r1 = RunRecipe() r2 = RunRecipe( script=["1", "2"], ports={(1, 2), (3, 4), (128, 56)}, sockets={("s1", "s2")}, volumes={("v1", "v2")}, ) r3 = RunRecipe( script=["1", "3", "17"], ports={(57, 12), (79, 2), (128, 22)}, sockets={("s10", "s20")}, volumes={("v10", "v20")}, ) r4 = RunRecipe( script=["1", "2", "1", "3", "17"], ports={(3, 4), (57, 12), (79, 2), (128, 22)}, sockets={("s1", "s2"), ("s10", "s20")}, volumes={("v1", "v2"), ("v10", "v20")}, ) assert r2 + r3 == r4 r1 += r2 r1 += r3 assert r1 == r4
def test_environmentrecipe_instantiation() -> None: """Test creating Environment Recipes.""" EnvironmentRecipe(BuildRecipe(), RunRecipe(), "potato") EnvironmentRecipe( build=BuildRecipe(), run=RunRecipe(), name=None, )
def test_runrecipe_load_valid() -> None: """Test loading a valid run recipe from file.""" with open(RUNRECIPE_VALID) as fp: assert RunRecipe.load_from_file(fp) == RunRecipe( script=["ping example.com", "git init"], ports=[(3, 2), (7, 204)], sockets=[("/var/opt/example", "/var/opt/otherthing")], )
def test_runrecipe_instantiation() -> None: """Create some run recipes.""" RunRecipe() RunRecipe( script=["ping 127.0.0.1 -n 1"], ports=[(8080, 3030), (1, 2)], sockets=[("/var/sock/example", "/var/sock/otherexample")], volumes=[("/beans", "/fries"), ("/toast", "/bread")], )
def test_component_should_run() -> None: """Test whether components should be executed.""" c1 = Component(RunRecipe(), [ImageName("ubuntu"), ImageName("debian", "stretch")]) c2 = Component(BuildRecipe(["FROM nixos"])) i1 = ImageName("ubuntu") i2 = ImageName("ubuntu", "18.04") i3 = ImageName("debian") i4 = ImageName("debian", "stretch") i5 = ImageName("debian", "jessie") i6 = ImageName("arch") assert c1.should_run(i1) assert c1.should_run(i2) assert not c1.should_run(i3) assert c1.should_run(i4) assert not c1.should_run(i5) assert not c1.should_run(i6) assert c2.should_run(i1) assert c2.should_run(i2) assert c2.should_run(i3) assert c2.should_run(i4) assert c2.should_run(i5) assert c2.should_run(i6)
def test_component_initialisation() -> None: """Test creating components.""" Component(BuildRecipe(["RUN cd ."])) Component( RunRecipe(script=["a", "b"]), [ImageName.from_str("a/b:c"), ImageName("qwerty", "uiop")], None, "does absolutely nothing", )
def test_environmentdefinition_recipe() -> None: """Test converting environment definitions to environment recipes.""" r = EnvironmentRecipe( name="example-environment", build=BuildRecipe(commands=[ "FROM ubuntu", "RUN apt update && apt install zsh", "CMD zsh", ], ), run=RunRecipe(ports={ (3000, 3000), (8080, 8080), }, ), ) with open(ENV_VALID) as fp: assert EnvironmentDefinition.load_from_file(fp).recipe == r
def test_module_load_valid() -> None: """Test loading a module from a file.""" with open(MODULE_VALID) as fp: assert Module.load_from_file(fp) == Module( name="example module", description="A module which is also an example!", components=[ Component( BuildRecipe( commands=["FROM example-image", "RUN some-setup-cmd"], )), Component( RunRecipe(script=["./fix-bug.sh --version 19.07"]), compatible=[ImageName("example-image", "19.07")], ), ], )
def test_environmentdefinition_load_valid() -> None: """Test loading Environment Definition from file.""" m1 = Module( name="ubuntu", components=[ Component( recipe=BuildRecipe(commands=["FROM ubuntu"]), results=ImageName("ubuntu"), ) ], ) m2 = Module( name="zsh", components=[ Component( recipe=BuildRecipe( commands=["RUN apt update && apt install zsh"]), compatible=[ImageName("ubuntu"), ImageName("debian")], description="Installs zsh using apt.", ), Component( recipe=BuildRecipe(commands=["RUN pacman -S zsh"]), compatible=[ImageName("arch"), ImageName("manjaro")], description="Installs zsh using pacman.", ), Component( recipe=BuildRecipe(commands=["CMD zsh"]), description="Use zsh as the entry command.", ), Component(recipe=RunRecipe(ports=[(3000, 3000), (8080, 8080)]), ) ], ) env_valid = EnvironmentDefinition( name="example-environment", modules=[m1, m2], ) with open(ENV_VALID) as fp: assert EnvironmentDefinition.load_from_file(fp) == env_valid
def test_runrecipe_str() -> None: """Test string representations of RunRecipe.""" r = RunRecipe( script=["a"], ports=[(1, 2)], sockets=[("abc", "def")], volumes=[("ping", "pong")], ) assert str(r) == ( "RunRecipe (" f"\n\tscript={['a']}," f"\n\tports={{(1, 2)}}," f"\n\tsockets={{('abc', 'def')}}," f"\n\tvolumes={{('ping', 'pong')}}," "\n)" ) assert eval(repr(r)) == r
def recipe(self) -> EnvironmentRecipe: """Create a buildable Recipe from this definition.""" current_image = None build = BuildRecipe() run = RunRecipe() for m in self.modules: for c in m.components: if c.should_run(current_image): if isinstance(c.recipe, BuildRecipe): build += c.recipe elif isinstance(c.recipe, RunRecipe): run += c.recipe if c.results is not None: current_image = c.results return EnvironmentRecipe( build=build, run=run, name=self.name, )
def test_runrecipe_load_empty() -> None: """Test loading empty yaml as a run recipe.""" with open(YAML_EMPTY) as fp: assert RunRecipe.load_from_file(fp) == RunRecipe()
def test_runrecipe_load_invalid() -> None: """Test loading an invalid run recipe from a file.""" with pytest.raises(ValidationError): with open(RUNRECIPE_INVALID) as fp: RunRecipe.load_from_file(fp)
def test_module_instantiation() -> None: """Test creating modules.""" Module("example", [Component(RunRecipe())]) Module("banana", [], "adds random bananas to your shell")