def test_raises_error_when_no_match_if_found_in_target(self): """ See Issue #40 """ model = Model( components=[ Component(name="server", provided_services=[Service("Awesome")], variables=[ Variable( name="memory", value_type=str, values=["1GB", "2GB"], realization=[ Substitution( targets=["docker-compose.yml"], pattern="pattern that does not exist", replacements=["mem=1", "mem=2"]) ]) ], implementation=DockerFile("server/Dockerfile")) ], goals=Goals(services=[Service("Awesome")])) server = model.resolve("server") configuration = Configuration( model, instances = [ Instance(name="server_0", definition=server, configuration=[(server.variables[0], "2GB")]) ]) with self.assertRaises(Exception): self.realize(configuration)
def test_substitution_in_component_files(self): model = Model( components=[ Component(name="server", provided_services=[Service("Awesome")], variables=[ Variable( name="memory", value_type=str, values=["1GB", "2GB"], realization=[ Substitution( targets=["server/Dockerfile", "server/server.cfg"], pattern="mem=XXX", replacements=["mem=1", "mem=2"]) ]) ], implementation=DockerFile("server/Dockerfile")) ], goals=Goals(services=[Service("Awesome")])) server = model.resolve("server") configuration = Configuration( model, instances = [ Instance(name="server_0", definition=server, configuration=[(server.variables[0], "2GB")]) ]) self.realize(configuration) self.assert_file_contains("config_1/images/server_0/Dockerfile", "mem=2") self.assert_file_contains("config_1/images/server_0/server.cfg", "mem=2")
def test_given_a_component_with_a_variable_with_substitution(self): self.assert_complete( "components:\n" " server:\n" " provides_services: [ Wonderful ]\n" " variables:\n" " memory:\n" " type: Text\n" " values: [1GB, 2GB, 4GB]\n" " realization:\n" " - targets: [ file1, path/to/file2 ]\n" " pattern: xmem=1GB\n" " replacements: [xmem=1, xmem=2, xmem=4]\n" "goals:\n" " running:\n" " - Wonderful\n", expectations={ "services": ["Wonderful"], "features": [], "components": { "server": { "provided_services": ["Wonderful"], "required_services": [], "provided_features": [], "required_features": [], "implementation": None, "variables": { "memory": { "values": ["1GB", "2GB", "4GB"], "realization": [ Substitution( targets=["file1", "path/to/file2"], pattern="xmem=1GB", replacements=[ "xmem=1", "xmem=2", "xmem=4" ]) ] } } } }, "goals": { "services": ["Wonderful"], "features": [] } })
def test_succeeds_in_inner_component_files(self): """ See Issue #48 """ self.create_template_file( component="server", resource="src/config/settings.ini", content="parameter=XYZ") model = Model( components=[ Component(name="server", provided_services=[Service("Awesome")], variables=[ Variable( name="memory", value_type=str, values=["1GB", "2GB"], realization=[ Substitution( targets=["server/src/config/settings.ini"], pattern="parameter=XYZ", replacements=["parameter=1GB", "parameter=2GB"]) ]) ], implementation=DockerFile("server/Dockerfile")) ], goals=Goals(services=[Service("Awesome")])) server = model.resolve("server") configuration = Configuration( model, instances = [ Instance(name="server_0", definition=server, configuration=[(server.variables[0], "2GB")]) ]) self.realize(configuration) self.assert_file_contains( "config_1/images/server_0/src/config/settings.ini", "parameter=2GB")
def test_substitution_with_pattern_longer_than_replacement(self): """ See Issue 57 """ self.create_template_file(component="server", resource="config.ini", content=("value: This is a very very long pattern\n" "Here is the end\n")) model = Model( components=[ Component(name="server", provided_services=[Service("Awesome")], variables=[ Variable( name="config", value_type=str, values=["v1"], realization=[ Substitution( targets=["server/config.ini"], pattern="value: This is a very very long pattern", replacements=["value: v1"]) ]) ], implementation=DockerFile("server/Dockerfile")) ], goals=Goals(services=[Service("Awesome")])) server = model.resolve("server") configuration = Configuration( model, instances = [ Instance(name="server_0", definition=server, configuration=[(server.variables[0], "v1")]) ]) self.realize(configuration) self.assert_file_contains_exactly( "config_1/images/server_0/config.ini", ("value: v1\n" "Here is the end\n"))
def _parse_substitution(self, component, variable, index, data): path = [Keys.COMPONENTS, component, Keys.VARIABLES, variable, Keys.REALIZATION, "#%d" % index] targets = [] pattern = self.UNDEFINED_PATTERN replacements = [] for key, item in data.items(): if key == Keys.TARGETS: if not isinstance(data[key], list): self._wrong_type(list, type(item), *(path + [key])) continue targets = [each for each in item] elif key == Keys.PATTERN: pattern = data[key] elif key == Keys.REPLACEMENTS: if not isinstance(data[key], list): self._wrong_type(list, type(item), *(path + [key])) continue replacements = [each for each in item] else: self._ignore(*(path + [key])) if not targets: self._missing([Keys.TARGETS], *path) if pattern == self.UNDEFINED_PATTERN: self._missing([Keys.PATTERN], *path) if not replacements: self._missing([Keys.REPLACEMENTS], *path) return Substitution(targets, pattern, replacements)
def test_substitute_pattern_that_contains_regex_sensitive_character(self): """ See Issue #56 """ self.create_template_file(content="\"resolve\": \"^1.1.6\"") model = Model( components=[ Component(name="server", provided_services=[Service("Awesome")], variables=[ Variable( name="memory", value_type=str, values=["1GB", "2GB"], realization=[ Substitution( targets=["server/server.cfg"], pattern="\"resolve\": \"^1.1.6\"", replacements=["\"resolve\": 1", "\"resolve\": 2"]) ]) ], implementation=DockerFile("server/Dockerfile")) ], goals=Goals(services=[Service("Awesome")])) server = model.resolve("server") configuration = Configuration( model, instances = [ Instance(name="server_0", definition=server, configuration=[(server.variables[0], "2GB")]) ]) self.realize(configuration) self.assert_file_contains( "config_1/images/server_0/server.cfg", "\"resolve\": 2")