Example #1
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)))
Example #2
0
class VariablesAreRealized(TestCase):


    def setUp(self):
        self._builder = Builder()
        self._workspace = create_temporary_workspace(self.DIRECTORY)
        self.create_docker_file()
        self.create_config_file()
        self.create_docker_compose_file()

    DIRECTORY = "realize/variables"


    def create_docker_file(self):
        directory = join_paths(self._workspace, "template", "server")
        makedirs(directory)
        path = join_paths(directory, "Dockerfile")
        with open(path, "w") as docker_file:
            docker_file.write("FROM debian:jessie\n"
                              "mem=XXX")


    def create_config_file(self):
        path = join_paths(self._workspace, "template", "server", "server.cfg")
        with open(path, "w") as docker_file:
            docker_file.write("mem=XXX")





    def create_docker_compose_file(self):
        path = join_paths(self._workspace, "template", "docker-compose.yml")
        with open(path, "w") as docker_file:
            docker_file.write("mem=XXX")


    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_substitution_in_inner_component_files(self):
        """
        See Issue #48, https://github.com/STAMP-project/camp/issues/48
        """
        self._create_inner_configuration_file()
        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 _create_inner_configuration_file(self):
        path = join_paths(self._workspace,
                          "template",
                          "server",
                          "src",
                          "config")
        makedirs(path)
        resource = join_paths(path, "settings.ini")
        with open(resource, "w") as config_file:
            config_file.write("parameter=XYZ")



    def test_substitution_in_orchestration_file(self):
        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="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/docker-compose.yml", "mem=2")


    def realize(self, configuration):
        source = self._workspace
        destination = join_paths(self._workspace, "config_1")
        self._builder.build(configuration, source, destination)


    def assert_file_contains(self, resource, pattern):
        path = join_paths(self._workspace, resource)
        with open(path, "r") as resource_file:
            content = resource_file.read()
            self.assertIn(pattern, content)
class Realization(TestCase):


    def setUp(self):
        self._builder = Builder()
        self._workspace = create_temporary_workspace(self.DIRECTORY)
        self.create_template_file(
            component="server",
            resource="Dockerfile",
            content=("FROM debian:jessie\n"
                     "mem=XXX"))
        self.create_template_file()
        self.create_template_file(
            component="",
            resource="docker-compose.yml",
            content="mem=XXX")


    DIRECTORY = "realize/variables"


    def create_template_file(self,
                           component="server",
                           resource="server.cfg",
                           content="mem=XXX"):
        resource = join_paths(self._workspace, "template", component, resource)
        directory = dirname(resource)
        makedirs(directory, exist_ok=True)
        with open(resource, "w") as resource_file:
            resource_file.write(content)


    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")


    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_substitute_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_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 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_succeeds_in_orchestration_file(self):
        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="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/docker-compose.yml", "mem=2")


    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 realize(self, configuration):
        source = self._workspace
        destination = join_paths(self._workspace, "config_1")
        self._builder.build(configuration, source, destination)


    def assert_file_contains(self, resource, pattern):
        path = join_paths(self._workspace, resource)
        with open(path, "r") as resource_file:
            content = resource_file.read()
            self.assertIn(pattern, content)

    def assert_file_contains_exactly(self, resource, expected):
        path = join_paths(self._workspace, resource)
        with open(path, "r") as resource_file:
            content = resource_file.read()
            self.assertEquals(expected, content)


    def assert_exists(self, resource):
        path = join_paths(self._workspace, resource)
        self.assertTrue(isfile(path))


    def assert_does_not_exist(self, resource):
        path = join_paths(self._workspace, resource)
        self.assertFalse(isfile(path))
Example #4
0
class VariablesAreRealized(TestCase):


    def setUp(self):
        self._builder = Builder()
        self.create_workspace()
        self.create_docker_file()
        self.create_config_file()
        self.create_docker_compose_file()

    def create_workspace(self):
        if isdir(self.WORKSPACE):
            rmtree(self.WORKSPACE)
        makedirs(self.WORKSPACE)

    WORKSPACE = "temp/realize/variables"


    def create_docker_file(self):
        directory = join_paths(self.WORKSPACE, "template", "server")
        makedirs(directory)
        path = join_paths(directory, "Dockerfile")
        with open(path, "w") as docker_file:
            docker_file.write("FROM debian:jessie\n"
                              "mem=XXX")


    def create_config_file(self):
        path = join_paths(self.WORKSPACE, "template", "server", "server.cfg")
        with open(path, "w") as docker_file:
            docker_file.write("mem=XXX")


    def create_docker_compose_file(self):
        path = join_paths(self.WORKSPACE, "template", "docker-compose.yml")
        with open(path, "w") as docker_file:
            docker_file.write("mem=XXX")


    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_substitution_in_orchestration_file(self):
        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="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/docker-compose.yml", "mem=2")


    def realize(self, configuration):
        source = self.WORKSPACE
        destination = join_paths(self.WORKSPACE, "config_1")
        self._builder.build(configuration, source, destination)


    def assert_file_contains(self, resource, pattern):
        path = join_paths(self.WORKSPACE, resource)
        with open(path, "r") as resource_file:
            content = resource_file.read()
            self.assertIn(pattern, content)