class BuilderTest(TestCase): def setUp(self): self._workspace = create_temporary_workspace(self.WORKING_DIRECTORY) self._input = InputDirectory(self._workspace) self._output = OutputDirectory(self._workspace + "/out") self._input.create_template_file("server", "Dockerfile", self.DOCKER_FILE) self._input.create_template_file("jdk", "Dockerfile", self.DOCKER_FILE) self._builder = Builder() WORKING_DIRECTORY = "realize/docker" DOCKER_FILE = ("FROM openjdk:8-jre\n" "RUN echo this is nice\n") def build(self, configuration): destination = join_paths(self._workspace, "out/config_0") self._builder.build(configuration, self._workspace, destination) def assert_directory_structure(self, expected_components): self.assertTrue(isdir(self.image_directory), "No image directory!") self.assertItemsEqual(expected_components, self.generated_components()) def assert_generated(self, file_path, with_patterns): self.assertTrue( self._output.has_file(file_path), "Missing file '%s'!" % file_path) content = self._output.content_of(file_path) for each_pattern in with_patterns: if not search(each_pattern, content): self.fail("The file '%s' does not contains pattern '%s'!\n" "Content is %s" % (file_path, each_pattern, content)) def assert_no_image_generated(self, config_index): self.assertEqual(0, len(self._output.images_generated_for(config_index)))
class Scenario(object): @staticmethod def from_sample(relative_path): scenario = Scenario() sample_directory = join_paths(SAMPLE_DIRECTORY, relative_path) working_directory = scenario.directory # Copy all the content for item in listdir(sample_directory): source = join_paths(sample_directory, item) destination = join_paths(working_directory, item) if isdir(source): copytree(source, destination) else: copy2(source, destination) return scenario def __init__(self, path=""): temporary_directory = join_paths(mkdtemp(prefix="camp_"), path) makedirs(temporary_directory, exist_ok=True) self._input = InputDirectory(temporary_directory, YAML()) self._output = OutputDirectory(join_paths(self._input.path, "out"), YAML()) self._model = None @property def directory(self): return self._input.path @property def output_directory(self): return self._output.path @property def generated_configurations(self): _, model, warnings = self._input.model if warnings: error = ("There are warnings!\n" "\n".join(each for each in warnings)) raise AssertionError(error) return [GeneratedConfiguration(path, configuration) \ for path, configuration in self._output.existing_configurations(model)] def fetch_test_report(self): return self._output.load_reports() @property def model(self): return self._input.model def create_configuration(self, index, content): file_name = self._output._yaml_configuration_file(index) self._output.create_file(file_name, content) def create_model(self, content): self._input.create_model(content) def create_template(self, component, relative_path, content="whatever"): self._input.create_template_file(component, relative_path, content)
class Sample(object): def __init__(self, path, workspace): self._source = join_paths("samples", path) self._input = InputDirectory(self._copy(self._source, workspace), YAML()) self._output = OutputDirectory(join_paths(self._input.path, "out"), YAML()) self._model = None @staticmethod def _copy(source, workspace): destination = join_paths(workspace, basename(source)) if source and exists(source): if isdir(destination): rmtree(destination) copytree(source, destination) else: if isdir(destination): rmtree(destination) makedirs(destination) return destination @property def directory(self): return self._input.path @property def output_directory(self): return self._output.path @property def generated_configurations(self): _, model, warnings = self._input.model if warnings: error = ("There are warnings!\n" "\n".join(each for each in warnings)) raise AssertionError(error) return [GeneratedConfiguration(path, configuration) \ for path, configuration in self._output.existing_configurations(model)] @property def model(self): return self._input.model def create_configuration(self, index, content): file_name = self._output._yaml_configuration_file(index) self._output.create_file(file_name, content) def create_model(self, content): self._input.create_model(content) def create_template(self, component, relative_path, content): self._input.create_template_file(component, relative_path, content)