def test_expected_failures_are_ignored(self):
     # Any errors included in callbacks.expected_failures are simply
     # ignored by ensureOrDeleteMirrorCDImageSeries() because they've been
     # logged by logMissingURL() already and they're expected to happen
     # some times.
     logger = self.getLogger()
     callbacks = self.makeMirrorProberCallbacks()
     self.assertEqual(
         set(callbacks.expected_failures),
         set([
             BadResponseCode,
             ProberTimeout,
             ConnectionSkipped,
             RedirectToDifferentFile,
             UnknownURLSchemeAfterRedirect,
         ]))
     exceptions = [
         BadResponseCode(str(httplib.NOT_FOUND)),
         ProberTimeout('http://localhost/', 5),
         ConnectionSkipped(),
         RedirectToDifferentFile('/foo', '/bar'),
         UnknownURLSchemeAfterRedirect('https://localhost')
     ]
     for exception in exceptions:
         failure = callbacks.ensureOrDeleteMirrorCDImageSeries([
             (defer.FAILURE, Failure(exception))
         ])
         # Twisted callbacks may raise or return a failure; that's why we
         # check the return value.
         self.failIf(isinstance(failure, Failure))
         # Also, these failures are not logged to stdout/stderr since
         # they're expected to happen.
         self.failIf(logger.errorCalled)
 def test_mirrorcdimageseries_creation_and_deletion_some_404s(self):
     not_all_success = [(defer.FAILURE,
                         Failure(BadResponseCode(str(httplib.NOT_FOUND)))),
                        (defer.SUCCESS, '200')]
     callbacks = self.makeMirrorProberCallbacks()
     all_success = [(defer.SUCCESS, '200'), (defer.SUCCESS, '200')]
     mirror_cdimage_series = callbacks.ensureOrDeleteMirrorCDImageSeries(
         all_success)
     callbacks.ensureOrDeleteMirrorCDImageSeries(not_all_success)
     # If the prober gets at least one 404 status, we need to make sure
     # there's no MirrorCDImageSeries for that series and flavour.
     self.assertRaises(SQLObjectNotFound, mirror_cdimage_series.get,
                       mirror_cdimage_series.id)
    def test_mirrorseries_creation_and_deletion(self):
        callbacks = self.makeMirrorProberCallbacks()
        mirror_distro_series_source = callbacks.ensureMirrorSeries(
            str(httplib.OK))
        self.assertIsNot(
            mirror_distro_series_source, None,
            "If the prober gets a 200 Okay status, a new "
            "MirrorDistroSeriesSource/MirrorDistroArchSeries should be "
            "created.")

        callbacks.deleteMirrorSeries(
            Failure(BadResponseCode(str(httplib.NOT_FOUND))))
        # If the prober gets a 404 status, we need to make sure there's no
        # MirrorDistroSeriesSource/MirrorDistroArchSeries referent to
        # that url
        self.assertRaises(SQLObjectNotFound, mirror_distro_series_source.get,
                          mirror_distro_series_source.id)
    def test_failure_propagation(self):
        # Make sure that deleteMirrorSeries() does not propagate
        # ProberTimeout, BadResponseCode or ConnectionSkipped failures.
        callbacks = self.makeMirrorProberCallbacks()
        try:
            callbacks.deleteMirrorSeries(
                Failure(ProberTimeout('http://localhost/', 5)))
        except Exception as e:
            self.fail("A timeout shouldn't be propagated. Got %s" % e)
        try:
            callbacks.deleteMirrorSeries(
                Failure(BadResponseCode(str(httplib.INTERNAL_SERVER_ERROR))))
        except Exception as e:
            self.fail("A bad response code shouldn't be propagated. Got %s" %
                      e)
        try:
            callbacks.deleteMirrorSeries(Failure(ConnectionSkipped()))
        except Exception as e:
            self.fail("A ConnectionSkipped exception shouldn't be "
                      "propagated. Got %s" % e)

        # Make sure that deleteMirrorSeries() propagate any failure that is
        # not a ProberTimeout, a BadResponseCode or a ConnectionSkipped.
        d = defer.Deferred()
        d.addErrback(callbacks.deleteMirrorSeries)
        ok = []

        def got_result(result):
            self.fail("Any failure that's not a timeout/bad-response/skipped "
                      "should be propagated.")

        def got_failure(failure):
            ok.append(1)

        d.addCallbacks(got_result, got_failure)
        d.errback(Failure(ZeroDivisionError()))
        self.assertEqual([1], ok)