Ejemplo n.º 1
0
    def test_listen_events_attach_only_first(self):
        """
        When we're listening for events and receive multiple
        ``event_stream_attached`` events, only the first received event should
        trigger the initial sync.
        """
        self.marathon_acme.listen_events()

        # First attach event is from ourselves attaching...
        assert_that(self.fake_marathon_api.check_called_get_apps(),
                    Equals(True))

        # Some other client attaches
        other_client = MarathonClient(['http://localhost:8080'],
                                      client=self.fake_marathon_api.client)
        other_client_events = []
        other_client.get_events(
            {'event_stream_attached': other_client_events.append})

        # Make very sure that two clients are attached and event sent
        assert_that(other_client_events, HasLength(1))
        assert_that(self.fake_marathon_api.event_requests, HasLength(2))

        # No second sync from marathon-acme
        assert_that(self.fake_marathon_api.check_called_get_apps(),
                    Equals(False))
    def test_listen_events_attach_only_first(self):
        """
        When we're listening for events and receive multiple
        ``event_stream_attached`` events, only the first received event should
        trigger the initial sync.
        """
        marathon_acme = self.mk_marathon_acme()
        marathon_acme.listen_events()

        # First attach event is from ourselves attaching...
        assert_that(
            self.fake_marathon_api.check_called_get_apps(), Equals(True))

        # Some other client attaches
        other_client = MarathonClient(
            ['http://localhost:8080'], client=self.fake_marathon_api.client)
        other_client_events = []
        other_client.get_events(
            {'event_stream_attached': other_client_events.append})

        # Make very sure that two clients are attached and event sent
        assert_that(other_client_events, HasLength(1))
        assert_that(self.fake_marathon_api.event_requests, HasLength(2))

        # No second sync from marathon-acme
        assert_that(
            self.fake_marathon_api.check_called_get_apps(), Equals(False))
Ejemplo n.º 3
0
def create_marathon_acme(storage_dir, acme_directory, acme_email,
                         marathon_addrs, mlb_addrs, group, reactor):
    """
    Create a marathon-acme instance.

    :param storage_dir:
        Path to the storage directory for certificates and the client key.
    :param acme_directory: Address for the ACME directory to use.
    :param acme_email:
        Email address to use when registering with the ACME service.
    :param marathon_addr:
        Address for the Marathon instance to find app domains that require
        certificates.
    :param mlb_addrs:
        List of addresses for marathon-lb instances to reload when a new
        certificate is issued.
    :param group:
        The marathon-lb group (``HAPROXY_GROUP``) to consider when finding
        app domains.
    :param reactor: The reactor to use.
    """
    storage_path, certs_path = init_storage_dir(storage_dir)
    acme_url = URL.fromText(_to_unicode(acme_directory))
    key = maybe_key(storage_path)

    return MarathonAcme(MarathonClient(marathon_addrs, reactor=reactor), group,
                        DirectoryStore(certs_path),
                        MarathonLbClient(mlb_addrs, reactor=reactor),
                        create_txacme_client_creator(reactor, acme_url, key),
                        reactor, acme_email)
Ejemplo n.º 4
0
    def test_sync_failure(self):
        """
        When a sync is run and something fails, the failure is propagated to
        the sync's deferred.
        """
        self.marathon_acme.marathon_client = MarathonClient(
            'http://localhost:8080', client=failing_client)

        d = self.marathon_acme.sync()
        assert_that(d,
                    failed(MatchesStructure(value=IsInstance(RuntimeError))))
Ejemplo n.º 5
0
    def test_request_fallback_all_failed(self):
        """
        When we make a request and there are multiple Marathon endpoints
        specified, and all the endpoints fail, the last failure should be
        returned.
        """
        agent = PerLocationAgent()
        agent.add_agent(b'localhost:8080', FailingAgent(RuntimeError('8080')))
        agent.add_agent(b'localhost:9090', FailingAgent(RuntimeError('9090')))
        client = MarathonClient(
            ['http://localhost:8080', 'http://localhost:9090'],
            client=treq_HTTPClient(agent))

        d = self.cleanup_d(client.request('GET', path='/my-path'))

        yield wait0()
        self.assertThat(d, failed(WithErrorTypeAndMessage(
            RuntimeError, '9090')))

        flush_logged_errors(RuntimeError)
    def test_request_fallback_all_failed(self):
        """
        When we make a request and there are multiple Marathon endpoints
        specified, and all the endpoints fail, the last failure should be
        returned.
        """
        agent = PerLocationAgent()
        agent.add_agent(b'localhost:8080', FailingAgent(RuntimeError('8080')))
        agent.add_agent(b'localhost:9090', FailingAgent(RuntimeError('9090')))
        client = MarathonClient(
            ['http://localhost:8080', 'http://localhost:9090'],
            client=treq_HTTPClient(agent))

        d = self.cleanup_d(client.request('GET', path='/my-path'))

        yield wait0()
        self.assertThat(d, failed(WithErrorTypeAndMessage(
            RuntimeError, '9090')))

        flush_logged_errors(RuntimeError)
Ejemplo n.º 7
0
    def test_request_success(self):
        """
        When we make a request and there are multiple Marathon endpoints
        specified, the first endpoint is used.
        """
        agent = PerLocationAgent()
        agent.add_agent(b'localhost:8080', self.fake_server.get_agent())
        agent.add_agent(b'localhost:9090', FailingAgent())
        client = MarathonClient(
            ['http://localhost:8080', 'http://localhost:9090'],
            client=treq_HTTPClient(agent))

        d = self.cleanup_d(client.request('GET', path='/my-path'))

        request = yield self.requests.get()
        self.assertThat(request, HasRequestProperties(
            method='GET', url='http://localhost:8080/my-path'))

        request.setResponseCode(200)
        request.finish()

        yield d
    def test_request_success(self):
        """
        When we make a request and there are multiple Marathon endpoints
        specified, the first endpoint is used.
        """
        agent = PerLocationAgent()
        agent.add_agent(b'localhost:8080', self.fake_server.get_agent())
        agent.add_agent(b'localhost:9090', FailingAgent())
        client = MarathonClient(
            ['http://localhost:8080', 'http://localhost:9090'],
            client=treq_HTTPClient(agent))

        d = self.cleanup_d(client.request('GET', path='/my-path'))

        request = yield self.requests.get()
        self.assertThat(request, HasRequestProperties(
            method='GET', url='http://localhost:8080/my-path'))

        request.setResponseCode(200)
        request.finish()

        yield d
Ejemplo n.º 9
0
    def setup_method(self):
        self.fake_marathon = FakeMarathon()
        self.fake_marathon_api = FakeMarathonAPI(self.fake_marathon)
        marathon_client = MarathonClient(['http://localhost:8080'],
                                         client=self.fake_marathon_api.client)

        self.cert_store = MemoryStore()

        self.fake_marathon_lb = FakeMarathonLb()
        mlb_client = MarathonLbClient(['http://localhost:9090'],
                                      client=self.fake_marathon_lb.client)

        key = JWKRSA(key=generate_private_key(u'rsa'))
        clock = Clock()
        clock.rightNow = (datetime.now() -
                          datetime(1970, 1, 1)).total_seconds()
        self.txacme_client = FailableTxacmeClient(key, clock)

        self.marathon_acme = MarathonAcme(marathon_client, 'external',
                                          self.cert_store, mlb_client,
                                          lambda: succeed(self.txacme_client),
                                          clock)
Ejemplo n.º 10
0
 def get_client(self, client):
     return MarathonClient(['http://localhost:8080'], client=client)