示例#1
0
    def test_compose(self):
        from compose.config.config import ConfigFile, ConfigDetails
        from compose.config.config import load as load_config
        from compose.project import Project

        composefile = """
        version: '2'
        services:
          first:
            image: alpine
            command: sleep 3600
          
          second:
            image: alpine
            command: sleep 3600
          
          pygen:
            image: pygen-build
            command: >
              --template '#
                {% for c in containers %}
                  Name={{ c.name }}
                {% endfor %}

                1st={{ containers.matching("first")|length }}
                2nd={{ containers.matching("second")|length }}'
              --one-shot
            volumes:
              - /var/run/docker.sock:/var/run/docker.sock:ro
            depends_on:
              - first
              - second
        """

        with open('/tmp/pygen-composefile.yml', 'w') as target:
            target.write(composefile)

        config = ConfigFile.from_filename('/tmp/pygen-composefile.yml')
        details = ConfigDetails('/tmp', [config])
        project = Project.from_config('cmpse', load_config(details),
                                      self.remote_client.api)

        with self.suppress_stderr():
            project.up(detached=True, scale_override={'second': 2})

            pygen_service = project.get_service('pygen')
            pygen_container = next(iter(pygen_service.containers()))
            pygen_container.wait()

            output = ''.join(pygen_container.logs(stdout=True, stderr=False))

            self.assertIn('Name=cmpse_first_1', output)
            self.assertIn('Name=cmpse_second_1', output)
            self.assertIn('Name=cmpse_second_2', output)
            self.assertIn('Name=cmpse_pygen_1', output)

            self.assertIn('1st=1', output)
            self.assertIn('2nd=2', output)
    def __init__(self,
                 project_name,
                 directory,
                 composefile="docker-compose.yml",
                 output="{{ result }}",
                 **invocations):
        config = ConfigFile.from_filename("%s/%s" % (directory, composefile))
        details = ConfigDetails(directory, [config])
        self.project = Project.from_config(project_name, load_config(details),
                                           self.client.api)

        super(DockerComposeAction, self).__init__(output, **invocations)
    def start_compose_project(self,
                              name,
                              directory,
                              composefile_name,
                              composefile_contents=None):
        if composefile_contents:
            with open(relative_path('%s/%s' % (directory, composefile_name)),
                      'w') as composefile:
                composefile.write(composefile_contents)

        config = ConfigFile.from_filename(
            relative_path('%s/%s' % (directory, composefile_name)))
        details = ConfigDetails(relative_path(directory), [config])
        project = Project.from_config(name, load_config(details),
                                      self.docker_client.api)

        self.started_compose_projects.append(project)

        project.up(detached=True)

        if composefile_contents:
            os.remove(relative_path('%s/%s' % (directory, composefile_name)))

        return project
示例#4
0
    def test_restart_compose_service(self):
        from compose.config.config import ConfigFile, ConfigDetails
        from compose.config.config import load as load_config
        from compose.project import Project

        composefile = """
        version: '2'
        services:
          app:
            image: alpine
            command: sh -c 'date +%s ; sleep 3600'

          pygen:
            image: pygen-build
            command: >
              --template '#ok'
              --restart app
              --one-shot
            volumes:
              - /var/run/docker.sock:/var/run/docker.sock:ro
            depends_on:
              - app
        """

        with open('/tmp/pygen-composefile.yml', 'w') as target:
            target.write(composefile)

        config = ConfigFile.from_filename('/tmp/pygen-composefile.yml')
        details = ConfigDetails('/tmp', [config])
        project = Project.from_config('cmpse', load_config(details),
                                      self.remote_client.api)

        with self.suppress_stderr():
            project.up(detached=True,
                       service_names=['app'],
                       scale_override={'app': 2})

            app = project.get_service('app')

            for _ in range(60):
                if len(app.containers()) < 2 or not all(
                        c.is_running for c in app.containers()):
                    self.wait(0.5)

            initial_logs = list(''.join(
                c.logs(stdout=True) for c in app.containers()).splitlines())

            project.up(detached=True, scale_override={'app': 2})

            pygen_service = project.get_service('pygen')
            pygen_container = next(iter(pygen_service.containers()))
            pygen_container.wait()

            for _ in range(60):
                if len(app.containers()) < 2 or not all(
                        c.is_running for c in app.containers()):
                    self.wait(0.5)

            newer_logs = list(''.join(
                c.logs(stdout=True) for c in app.containers()).splitlines())

            self.assertNotEqual(tuple(sorted(newer_logs)),
                                tuple(sorted(initial_logs)))
            self.assertEqual(len(newer_logs), 4)