def test_when_a_component_is_implemented_by_a_docker_image(self):

        model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      implementation=DockerImage("fchauvel/test:1.0.0"))
        ], Goals(services=[Service("Awesome")]))

        instance = Instance("server_0", model.resolve("server"))
        configuration = Configuration(model, [instance])

        self.build(configuration)

        self.assert_no_image_generated(0)
    def test_when_a_component_is_implemented_by_a_docker_file(self):
        model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      implementation=DockerFile("server/Dockerfile"))
        ], Goals(services=[Service("Awesome")]))

        instance = Instance("server_0", model.resolve("server"))
        configuration = Configuration(model, [instance])

        self.build(configuration)

        self.assert_generated(
            "config_0/images/server_0/Dockerfile",
            with_patterns=["FROM openjdk:8-jre", "FROM camp/runtime"])
Esempio n. 3
0
    def load_model_from(self, stream):
        data = load_yaml(stream)
        components = []
        goals = Goals()
        constraints = []
        for key, item in data.items():
            if key == Keys.COMPONENTS:
                if not isinstance(item, dict):
                    self._wrong_type(dict, type(item), key)
                    continue
                components = self._parse_components(item)
            elif key == Keys.GOALS:
                if not isinstance(item, dict):
                    self._wrong_type(dict, type(item), key)
                    continue
                goals = self._parse_goals(item)
            elif key == Keys.CONSTRAINTS:
                if not isinstance(item, list):
                    self._wrong_type(list, type(item), key)
                    continue
                constraints.extend(item)

            else:
                self._ignore(key)

        if self._warnings:
            raise InvalidYAMLModel(self._warnings)

        return Model(components, goals, constraints)
    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 test_select_a_specifc_resource(self):
        self.create_template_file(component="server",
                                  resource="apache_config.ini")
        self.create_template_file(component="server",
                                  resource="nginx_config.ini")

        model = Model(
            components=[
                Component(name="server",
                          provided_services=[Service("Awesome")],
                          variables=[
                              Variable(
                                  name="provider",
                                  value_type=str,
                                  values=["apache", "nginx"],
                                  realization=[
                                      ResourceSelection(
                                          "server/config.ini",
                                          [
                                              "server/apache_config.ini",
                                              "server/nginx_config.ini"
                                          ]
                                      )
                                  ])
                          ],
                          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], "nginx")])
            ])

        self.realize(configuration)

        self.assert_exists("config_1/images/server_0/config.ini")
        self.assert_does_not_exist("config_1/images/server_0/apache_config.ini")
Esempio n. 7
0
    def test_when_a_component_host_is_implemented_by_a_docker_image(self):
        model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      required_features=[Feature("JDK")],
                      implementation=DockerFile("server/Dockerfile")),
            Component("jdk",
                      provided_features=[Feature("JDK")],
                      implementation=DockerImage("fchauvel/test:1.0.1")),
        ], Goals(services=[Service("Awesome")]))

        server_0 = Instance("server_0", model.resolve("server"))
        jdk_0 = Instance("jdk_0", model.resolve("jdk"))
        server_0.feature_provider = jdk_0

        configuration = Configuration(model, [server_0, jdk_0])
        self.build(configuration)

        self.assert_generated("config_0/images/server_0/Dockerfile",
                              with_patterns=["FROM fchauvel/test:1.0.1"])
    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")
    def setUp(self):
        super().setUp()
        self.model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      required_features=[Feature("ServletContainer")],
                      implementation=DockerFile("server/Dockerfile")),
            Component("tomcat",
                      provided_features=[Feature("ServletContainer")],
                      required_features=[Feature("JDK")],
                      implementation=DockerFile("tomcat/Dockerfile")),
            Component("jdk",
                      provided_features=[Feature("JDK")],
                      implementation=DockerFile("jdk/Dockerfile"))
        ], Goals(services=[Service("Awesome")]))

        self.server_0 = Instance("server_0", self.model.resolve("server"))
        self.tomcat_0 = Instance("tomcat_0", self.model.resolve("tomcat"))
        self.jdk_0 = Instance("jdk_0", self.model.resolve("jdk"))

        self.server_0.feature_provider = self.tomcat_0
        self.tomcat_0.feature_provider = self.jdk_0
Esempio n. 10
0
 def setUp(self):
     start_over()
     self._context = Context()
     components = [
         Component(
             "c1",
             provided_services=[Service("S1")],
             provided_features=[Feature("F1")],
             variables=[Variable("memory", str, values=["1GB", "2GB"])])
     ]
     self._model = Model(components, Goals(services=[Service("S1")]))
     self._context.load_metamodel()
     self._context.load_model(self._model)
Esempio n. 11
0
    def test_when_a_component_is_implemented_by_a_docker_file(self):
        model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      required_features=[Feature("JDK")],
                      implementation=DockerFile("server/Dockerfile")),
            Component("jdk",
                      provided_features=[Feature("JDK")],
                      implementation=DockerFile("jdk/Dockerfile"))
        ], Goals(services=[Service("Awesome")]))

        server_0 = Instance("server_0", model.resolve("server"))
        jdk_0 = Instance("jdk_0", model.resolve("jdk"))

        server_0.feature_provider = jdk_0
        configuration = Configuration(model, [server_0, jdk_0])

        self.build(configuration)

        self.assert_generated("config_0/images/build_images.sh",
                              with_patterns=[
                                  "docker build -t camp-server_0 ./server",
                                  "docker build -t camp-jdk_0 ./jdk_0"
                              ])
class BuildImagesIsGenerated(BuilderTest):
    def setUp(self):
        super().setUp()
        self.model = Model([
            Component("server",
                      provided_services=[Service("Awesome")],
                      required_features=[Feature("ServletContainer")],
                      implementation=DockerFile("server/Dockerfile")),
            Component("tomcat",
                      provided_features=[Feature("ServletContainer")],
                      required_features=[Feature("JDK")],
                      implementation=DockerFile("tomcat/Dockerfile")),
            Component("jdk",
                      provided_features=[Feature("JDK")],
                      implementation=DockerFile("jdk/Dockerfile"))
        ], Goals(services=[Service("Awesome")]))

        self.server_0 = Instance("server_0", self.model.resolve("server"))
        self.tomcat_0 = Instance("tomcat_0", self.model.resolve("tomcat"))
        self.jdk_0 = Instance("jdk_0", self.model.resolve("jdk"))

        self.server_0.feature_provider = self.tomcat_0
        self.tomcat_0.feature_provider = self.jdk_0

    def verify_script(self):
        expected_command_order = (
            "docker build --force-rm --no-cache -t camp-jdk_0 ./jdk_0\n"
            "    docker build --force-rm --no-cache -t camp-tomcat_0 ./tomcat_0\n"
            "    docker build --force-rm --no-cache -t camp-server_0 ./server_0\n"
        )

        self.assert_generated("config_0/images/build_images.sh",
                              with_patterns=[expected_command_order])

    def test_ordering_jdk_tc_svr(self):
        configuration = Configuration(
            self.model, [self.jdk_0, self.tomcat_0, self.server_0])

        self.build(configuration)

        self.verify_script()

    def test_ordering_jdk_svr_tc(self):
        configuration = Configuration(
            self.model, [self.jdk_0, self.server_0, self.tomcat_0])

        self.build(configuration)

        self.verify_script()

    def test_ordering_tc_jdk_svr(self):
        configuration = Configuration(self.model, [
            self.tomcat_0,
            self.jdk_0,
            self.server_0,
        ])

        self.build(configuration)

        self.verify_script()

    def test_ordering_tc_svr_jdk(self):
        configuration = Configuration(self.model, [
            self.tomcat_0,
            self.server_0,
            self.jdk_0,
        ])

        self.build(configuration)

        self.verify_script()

    def test_ordering_svr_jdk_tc(self):
        configuration = Configuration(self.model, [
            self.server_0,
            self.jdk_0,
            self.tomcat_0,
        ])

        self.build(configuration)

        self.verify_script()

    def test_ordering_svr_tc_jdk(self):
        configuration = Configuration(self.model, [
            self.server_0,
            self.tomcat_0,
            self.jdk_0,
        ])

        self.build(configuration)

        self.verify_script()