def run(self, containers, family, app=app):
     apps = []
     with app.app_context():
         for container in containers:
             try:
                 repo, commit_hash = container[0].split(':')
             except ValueError:
                 raise ValueError('"{}" should look like repo:id'.format(
                     container[0]))
             build = Build.query.join(Commit).filter(
                 Commit.repository == repo,
                 or_(Commit.commit_hash == commit_hash,
                     Commit.tag == commit_hash),
             ).first()
             if build is None:
                 raise NoResultFound("No row found for {}/{}".format(
                     repo, commit_hash))
             apps.append(
                 ECSBuilder.DockerContainer(
                     build=build,
                     environment=container[1],
                     memory=container[2],
                     portmappings=[{
                         "hostPort": 8080,
                         "containerPort": 80
                     }] if repo == "adsws" else None,
                 ))
         tmpl = ECSBuilder(apps, family=family).render_template()
         print(tmpl)
         return tmpl
Example #2
0
 def setUp(self):
     self.commit = Commit(commit_hash='master', repository="adsws")
     self.build = Build(commit=self.commit)
     self.containers = [
         ECSBuilder.DockerContainer(self.build,
                                    environment="staging",
                                    memory=m,
                                    portmappings=[{
                                        "hostPort": 8080,
                                        "containerPort": 80
                                    }] if m == 10 else None)
         for m in range(10, 50, 10)
     ]
     self.builder = ECSBuilder(self.containers, family="unittest")
class TestECSbuilder(unittest.TestCase):
    """
    Test the ECSBuilder
    """
    def setUp(self):
        self.commit = Commit(
            commit_hash='master',
            repository="adsws"
        )
        self.build = Build(commit=self.commit)
        self.containers = [
            ECSBuilder.DockerContainer(
                self.build,
                environment="staging",
                memory=m,
                portmappings=[{
                    "hostPort": 8080,
                    "containerPort": 80}] if m == 10 else None
            ) for m in range(10, 50, 10)
        ]
        self.builder = ECSBuilder(self.containers, family="unittest")

    def test_init(self):
        """
        Initialized ECSBuilder should have a template and containers attribute
        of the appropriate types
        """
        self.assertIsInstance(
            self.builder.containers[0],
            ECSBuilder.DockerContainer
        )
        self.assertEqual(self.builder.containers, self.containers)
        self.assertIsInstance(
            self.builder.templates,
            jinja2.environment.Environment
        )

    def test_render_template(self):
        """
        instance method render_template should return a json template based
        on base.aws.template
        """
        t = self.builder.render_template()
        self.assertIsInstance(t, basestring)
        try:
            j = json.loads(t)
        except ValueError:
            print("Could not load json: {}".format(t))
            raise
        self.assertIn(self.commit.repository, t)
        for container in self.containers:
            self.assertIn("{}".format(container.memory), t)
            if container.portmappings is not None:
                self.assertEqual(
                    j['containerDefinitions'][self.containers.index(container)]['portMappings'],
                    container.portmappings,
                )
Example #4
0
class TestECSbuilder(unittest.TestCase):
    """
    Test the ECSBuilder
    """
    def setUp(self):
        self.commit = Commit(commit_hash='master', repository="adsws")
        self.build = Build(commit=self.commit)
        self.containers = [
            ECSBuilder.DockerContainer(self.build,
                                       environment="staging",
                                       memory=m,
                                       portmappings=[{
                                           "hostPort": 8080,
                                           "containerPort": 80
                                       }] if m == 10 else None)
            for m in range(10, 50, 10)
        ]
        self.builder = ECSBuilder(self.containers, family="unittest")

    def test_init(self):
        """
        Initialized ECSBuilder should have a template and containers attribute
        of the appropriate types
        """
        self.assertIsInstance(self.builder.containers[0],
                              ECSBuilder.DockerContainer)
        self.assertEqual(self.builder.containers, self.containers)
        self.assertIsInstance(self.builder.templates,
                              jinja2.environment.Environment)

    def test_render_template(self):
        """
        instance method render_template should return a json template based
        on base.aws.template
        """
        t = self.builder.render_template()
        self.assertIsInstance(t, basestring)
        try:
            j = json.loads(t)
        except ValueError:
            print("Could not load json: {}".format(t))
            raise
        self.assertIn(self.commit.repository, t)
        for container in self.containers:
            self.assertIn("{}".format(container.memory), t)
            if container.portmappings is not None:
                self.assertEqual(
                    j['containerDefinitions'][self.containers.index(container)]
                    ['portMappings'],
                    container.portmappings,
                )
 def setUp(self):
     self.commit = Commit(
         commit_hash='master',
         repository="adsws"
     )
     self.build = Build(commit=self.commit)
     self.containers = [
         ECSBuilder.DockerContainer(
             self.build,
             environment="staging",
             memory=m,
             portmappings=[{
                 "hostPort": 8080,
                 "containerPort": 80}] if m == 10 else None
         ) for m in range(10, 50, 10)
     ]
     self.builder = ECSBuilder(self.containers, family="unittest")