'Start a container for testing restart policies.
:param unicode mode: Mode of container. One of
- ``"failure"``: The container will always exit with a failure.
- ``"success-then-sleep"``: The container will exit with success
once, then sleep forever.
- ``"failure-then-sucess"``: The container will exit with failure
once, then with failure.
:param IRestartPolicy restart_policy: The restart policy to use for
the container.
:returns Deferred: A deferred that fires with the number of times the
container was started.'

def start_restart_policy_container(self, mode, restart_policy):

    docker_dir = FilePath(__file__).sibling('retry-docker')
    name = random_name(self)
    data = FilePath(self.mktemp())
    data.makedirs()
    count = data.child('count')
    count.setContent('0')
    marker = data.child('marker')
    image = DockerImageBuilder(test=self, source_dir=docker_dir)
    d = image.build()
    def image_built(image_name):
        if (mode == u'success-then-sleep'):
            expected_states = (u'active',)
        else:
            expected_states = (u'inactive',)
        return self.start_container(name, image_name=image_name, restart_policy=restart_policy, environment=Environment(variables={u'mode': mode}), volumes=[Volume(node_path=data, container_path=FilePath('/data'))], expected_states=expected_states)
    d.addCallback(image_built)
    if (mode == u'success-then-sleep'):
        d.addCallback((lambda ignored: loop_until(reactor, marker.exists)))
    d.addCallback((lambda ignored: count.getContent()))
    return d
