예제 #1
0
 def __init__(self, path):
     self._temporary_directory = mkdtemp(prefix="camp_")
     workspace = join_paths(self._temporary_directory, "acceptance")
     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
예제 #2
0
    def setUp(self):
        if isdir(self.WORKING_DIRECTORY):
            rmtree(self.WORKING_DIRECTORY)
        makedirs(self.WORKING_DIRECTORY)

        self._input = InputDirectory(self.WORKING_DIRECTORY)
        self._output = OutputDirectory(self.WORKING_DIRECTORY + "/out")

        self._input.create_template_file("server", "Dockerfile",
                                         self.DOCKER_FILE)
        self._input.create_template_file("jdk", "Dockerfile", self.DOCKER_FILE)

        self._builder = Builder()
예제 #3
0
    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()
예제 #4
0
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)))
예제 #5
0
    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",
            ("FROM openjdk:8-jre as builder\n"
             "RUN echo \"build the from the sources\"\n"
             "FROM camp/runtime\n"
             "COPY --from=builder /build/a.out /a.out\n"))

        self._input.create_template_file("tomcat", "Dockerfile",
                                         ("FROM camp/runtime\n"
                                          "RUN echo this is nice\n"))

        self._input.create_template_file("jdk", "Dockerfile",
                                         ("FROM openjdk:8-jre\n"
                                          "RUN echo this is nice\n"))

        self._builder = Builder()
예제 #6
0
 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
예제 #7
0
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)
예제 #8
0
 def _prepare_directories(self, arguments):
     self._input = InputDirectory(arguments.working_directory, self._codec)
     self._output = OutputDirectory(arguments.working_directory + "/out",
                                    self._codec)
예제 #9
0
class Camp(object):
    def __init__(self, codec, solver, realize):
        self._codec = codec
        self._problem = solver
        self._builder = realize
        self._input = None
        self._output = None
        self._ui = UI()

    def show_versions(self):
        self._ui.welcome()

        os_version = system() + " " + release() + " (" + " ".join(
            dist()).strip() + ")"

        python = version_info

        import z3
        z3_version = z3.get_version_string()

        docker_version = check_output(["docker", "--version"], shell=False)\
                         .decode("utf-8")
        compose_version = check_output(["docker-compose", "--version"],
                                       shell=False)\
                          .decode("utf-8")

        self._ui.show_versions(os_version, python, z3_version,
                               docker_version.strip(), compose_version.strip())

    def generate(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            configurations = self._generate_configurations(arguments, model)

            count = 0
            for index, each_configuration in enumerate(configurations, 1):
                self._save(index, each_configuration)
                count += 1

            if count == 0:
                self._ui.no_configuration_generated()

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _prepare_directories(self, arguments):
        self._input = InputDirectory(arguments.working_directory, self._codec)
        self._output = OutputDirectory(arguments.working_directory + "/out",
                                       self._codec)

    def _load_model(self):
        path, model, _ = self._input.model
        self._ui.model_loaded(path, model)
        model.accept(Checker(workspace=self._input.path))
        return model

    def _generate_configurations(self, arguments, model):
        problem = self._problem.from_model(model)
        if arguments.mode == Generate.COVERING:
            return problem.coverage()
        elif arguments.mode == Generate.ATOMIC:
            return problem.atomic()
        else:
            return problem.all_solutions()

    def _save(self, index, configuration):
        yaml_file = self._output.save_as_yaml(index, configuration)
        self._output.save_as_graphviz(index, configuration)
        self._ui.new_configuration(index, configuration, yaml_file)

    def realize(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            for path, each_configuration in self._load_configurations(model):
                self._builder.build(each_configuration,
                                    arguments.working_directory, path)
                self._ui.configuration_realized(path)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except NoConfigurationFound as error:
            self._ui.no_configuration_found(error)

        except InvalidSubstitution as error:
            self._ui.invalid_substitution(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _load_configurations(self, model):
        configurations = self._output.existing_configurations(model)
        self._ui.configurations_loaded(self._output.path)
        return configurations

    def execute(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            testing = model.tests
            configurations = self._load_configurations(model)
            selected_configurations = self._filter(arguments,
                                                   list(configurations))
            with open("camp_execute.log", "wb") as log_file:
                shell = self._select_shell(arguments, log_file)
                engine = Engine(testing, shell, arguments, self._ui)
                reports = engine.execute(selected_configurations)
                self._output.save_reports(reports)
                self._ui.summarize_execution(reports)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except NoConfigurationFound as error:
            self._ui.no_configuration_found(error)

        except ShellCommandFailed as error:
            self._ui.shell_command_failed(error)

        except ServiceNotReady as error:
            self._ui.service_not_ready(error)

        except ReportFormatNotSupported as error:
            self._ui.report_format_not_supported(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _select_shell(self, arguments, log_file):
        shell = Shell(log_file, ".", self._ui)
        if arguments.is_simulated:
            shell = SimulatedShell(log_file, ".", self._ui)
        return shell

    def _filter(self, arguments, configurations):
        if len(arguments._included) == 0:
            return configurations

        selection = []
        for each_index in arguments._included:
            found = self._search_for(each_index, configurations)
            if not found:
                self._ui.no_configuration_with_index(each_index)
            else:
                selection.append(found)
        return selection

    @staticmethod
    def _search_for(index, configurations):
        marker = "config_{}".format(index)
        for any_path, any_config in configurations:
            if marker in any_path:
                return (any_path, any_config)
        return None
예제 #10
0
class Camp(object):
    def __init__(self, codec, solver, realize):
        self._codec = codec
        self._problem = solver
        self._builder = realize
        self._input = None
        self._output = None
        self._ui = UI()

    def generate(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            configurations = self._generate_configurations(arguments, model)
            for index, each_configuration in enumerate(configurations, 1):
                self._save(index, each_configuration)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _prepare_directories(self, arguments):
        self._input = InputDirectory(arguments.working_directory, self._codec)
        self._output = OutputDirectory(arguments.working_directory + "/out",
                                       self._codec)

    def _load_model(self):
        path, model, _ = self._input.model
        self._ui.model_loaded(path, model)
        model.accept(Checker(workspace=self._input.path))
        return model

    def _generate_configurations(self, arguments, model):
        problem = self._problem.from_model(model)
        if arguments.only_coverage:
            return problem.coverage()
        return problem.all_solutions()

    def _save(self, index, configuration):
        self._output.save_as_graphviz(index, configuration)
        yaml_file = self._output.save_as_yaml(index, configuration)
        self._ui.new_configuration(index, configuration, yaml_file)

    def realize(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            for path, each_configuration in self._load_configurations(model):
                self._builder.build(each_configuration,
                                    arguments.working_directory, path)
                self._ui.configuration_realized(path)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except NoConfigurationFound as error:
            self._ui.no_configuration_found(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _load_configurations(self, model):
        configurations = self._output.existing_configurations(model)
        self._ui.configurations_loaded(self._output.path)
        return configurations

    def execute(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            testing = model.tests
            configurations = self._load_configurations(model)
            with open("camp_execute.log", "wb") as log_file:
                shell = self._select_shell(arguments, log_file)
                engine = Engine(testing, shell, self._ui)
                reports = engine.execute(configurations)
                self._output.save_reports(reports)
                self._ui.summarize_execution(reports)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except NoConfigurationFound as error:
            self._ui.no_configuration_found(error)

        except ShellCommandFailed as error:
            self._ui.shell_command_failed(error)

        except ReportFormatNotSupported as error:
            self._ui.report_format_not_supported(error)

        except Exception as error:
            stack_trace = extract_tb(exc_info()[2])
            self._ui.unexpected_error(error, stack_trace)

        finally:
            self._ui.goodbye()

    def _select_shell(self, arguments, log_file):
        shell = Shell(log_file, ".", self._ui)
        if arguments.is_simulated:
            shell = SimulatedShell(log_file, ".", self._ui)
        return shell
예제 #11
0
 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
예제 #12
0
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)
예제 #13
0
class Camp(object):


    def __init__(self, codec, solver, realize):
        self._codec = codec
        self._problem = solver
        self._builder = realize
        self._input = None
        self._output = None
        self._ui = UI()


    def generate(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            configurations = self._generate_configurations(arguments, model)
            for index, each_configuration in enumerate(configurations, 1):
                self._save(index, each_configuration)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except Exception as error:
            self._ui.unexpected_error(error)

        finally:
            self._ui.goodbye()


    def _prepare_directories(self, arguments):
        self._input = InputDirectory(arguments.working_directory,
                                               self._codec)
        self._output = OutputDirectory(arguments.working_directory + "/out",
                                               self._codec)


    def _load_model(self):
        path, model, warnings = self._input.model
        self._ui.model_loaded(path, model)
        model.accept(Checker(workspace=self._input.path))
        return model


    def _generate_configurations(self, arguments, model):
        problem = self._problem.from_model(model)
        if arguments.only_coverage:
            return  problem.coverage()
        return problem.all_solutions()


    def _save(self, index, configuration):
        self._output.save_as_graphviz(index, configuration)
        yaml_file = self._output.save_as_yaml(index, configuration)
        self._ui.new_configuration(index, configuration, yaml_file)


    def realize(self, arguments):
        self._ui.welcome()
        self._prepare_directories(arguments)
        try:
            model = self._load_model()
            for path, each_configuration in self._load_configurations(model):
                self._builder.build(each_configuration,
                                    arguments.working_directory,
                                    path)
                self._ui.configuration_realized(path)

        except InvalidYAMLModel as error:
            self._ui.invalid_yaml_model(error)

        except InvalidModel as error:
            self._ui.invalid_model(error)

        except MissingModel as error:
            self._ui.missing_model(error)

        except NoConfigurationFound as error:
            self._ui.no_configuration_found(error)

        except Exception as error:
            self._ui.unexpected_error(error)

        finally:
            self._ui.goodbye()


    def _load_configurations(self, model):
        configurations = self._output.existing_configurations(model)
        self._ui.configurations_loaded(self._output.path)
        return configurations


    @staticmethod
    def execute(self, arguments):
        self._ui.welcome()
        try:
            parser = ConfigINIParser()
            config = parser.parse(arguments.configuration_file)
            experiment = ConductExperimentRunner(config)
            experiment.run()

        except:
            self._ui.error(sys.exc_info()[0])

        finally:
            self._ui.goodbye()