def test_buildrecipe_load_valid() -> None: """Test loading a valid BuildRecipe from file.""" with open(BUILDRECIPE_VALID) as fp: assert BuildRecipe.load_from_file(fp) == BuildRecipe(commands=[ "FROM test", "RUN cd .", ])
def test_environmentrecipe_instantiation() -> None: """Test creating Environment Recipes.""" EnvironmentRecipe(BuildRecipe(), RunRecipe(), "potato") EnvironmentRecipe( build=BuildRecipe(), run=RunRecipe(), name=None, )
def test_buildrecipe_repr() -> None: """Test evaluable string representation of BuildRecipe.""" b1 = BuildRecipe([]) b2 = BuildRecipe( ["dfafuho&*(*&^%", "8$&__*!", "@~@}{?>?"], ) assert eval(repr(b1)) == b1 assert eval(repr(b2)) == b2
def test_buildrecipe_build_str() -> None: """Test Dockerfile strings from BuildRecipe.""" b1 = BuildRecipe([]) b2 = BuildRecipe(["FROM example:17", "RUN setup-1", "EXPOSE 8080"]) assert b1.build_str == "" assert b2.build_str == ( "FROM example:17\n" "RUN setup-1\n" "EXPOSE 8080\n" )
def test_buildrecipe_str() -> None: """Test human-readable string representation of BuildRecipe.""" b1 = BuildRecipe([]) b2 = BuildRecipe(["FROM java", "RUN cd ."]) assert str(b1) == "BuildRecipe [\n]" assert str(b2) == ( "BuildRecipe [\n" "\tFROM java\n" "\tRUN cd .\n" "]" )
def test_buildrecipe_eq() -> None: """Test equality of build recipes.""" b1 = BuildRecipe(["a", "b"]) b2 = BuildRecipe(["a", "b"]) b3 = BuildRecipe(["a"]) b4 = BuildRecipe(["a", "q"]) assert b1 == b2 assert b2 == b1 assert b1 != b3 and b3 != b1 assert b1 != b4 and b4 != b1 assert BuildRecipe([]) != "I did not hit her, I did not! Oh, hi Mark."
def test_buildrecipe_add() -> None: """Test adding build recipes.""" b1 = BuildRecipe() b2 = BuildRecipe(["1", "2", "3"]) b3 = BuildRecipe() b4 = BuildRecipe(["4", "5"]) b5 = BuildRecipe(["1", "2", "3", "4", "5"]) assert b1 + b2 + b3 + b4 == b5 b2 += b1 b2 += b4 b2 += b3 assert b2 == b5
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_load_valid() -> None: """Test loading a component from a file.""" with open(COMPONENT_VALID) as fp: assert Component.load_from_file(fp) == Component( recipe=BuildRecipe(commands=["RUN cd .."]), compatible=[ImageName("example"), ImageName("test", "13.2")], description="A very exciting component.", )
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_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_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_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 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_buildrecipe_load_invalid() -> None: """Test loading an invalid BuildRecipe.""" with pytest.raises(ValidationError): with open(BUILDRECIPE_INVALID) as fp: BuildRecipe.load_from_file(fp)
def test_buildrecipe() -> None: """Create some build recipes.""" BuildRecipe(["FROM ubuntu", "RUN rm -rf /", "EXPOSE 8080"]) BuildRecipe([])
def test_buildrecipe_load_empty() -> None: """Test loading empty yaml as a BuildRecipe.""" with open(YAML_EMPTY) as fp: assert BuildRecipe.load_from_file(fp) == BuildRecipe()