示例#1
0
    def test_fetch_with_errors(self, fetch_mock):
        """
        L{ReleaseUpgrader.fetch} logs a warning in case any of the upgrade tool
        files fails to be fetched.
        """
        tarball_url = "http://some/where/karmic.tar.gz"
        signature_url = "http://some/where/karmic.tar.gz.gpg"

        method_returns = {
            tarball_url: succeed(b"tarball"),
            signature_url: fail(HTTPCodeError(404, b"not found"))}

        def side_effect(param):
            return method_returns[param]

        fetch_mock.side_effect = side_effect

        result = self.upgrader.fetch(tarball_url,
                                     signature_url)

        def check_failure(failure):
            self.assertIn("WARNING: Couldn't fetch file from %s (Server return"
                          "ed HTTP code 404)" % signature_url,
                          self.logfile.getvalue())
            self.assertIn("WARNING: Couldn't fetch all upgrade-tool files",
                          self.logfile.getvalue())
            calls = [mock.call(tarball_url), mock.call(signature_url)]
            fetch_mock.assert_has_calls(calls, any_order=True)

        result.addCallback(self.fail)
        result.addErrback(check_failure)
        return result
 def test_exchange_error_with_404_downgrades_server_api(self):
     """
     If we get a 404, we try to donwgrade our server API version.
     """
     self.mstore.set_server_api(b"3.3")
     self.transport.responses.append(HTTPCodeError(404, ""))
     self.exchanger.exchange()
     self.assertEqual(b"3.2", self.mstore.get_server_api())
示例#3
0
 def test_fetch_ec2_meta_data_bad_result_max_retry(self):
     """
     L{_fetch_ec2_meta_data} returns C{None} and logs an error when
     crossing the retry threshold C{METADATA_RETRY_MAX}.
     """
     self.log_helper.ignore_errors(HTTPCodeError)
     self.add_query_result("ami-id", HTTPCodeError(404, "notfound"))
     plugin = ComputerInfo(fetch_async=self.fetch_func)
     plugin._cloud_retries = METADATA_RETRY_MAX
     result = yield plugin._fetch_ec2_meta_data()
     self.assertIn(
         "INFO: No cloud meta-data available. Server returned "
         "HTTP code 404", self.logfile.getvalue())
     self.assertEqual(None, result)
    def test_fetch_attachment_failure(self, mock_fetch):
        """
        If the plugin fails to retrieve the attachments with a
        L{HTTPCodeError}, a specific error code is shown.
        """
        self.manager.config.url = "https://localhost/message-system"
        persist = Persist(
            filename=os.path.join(self.config.data_path, "broker.bpickle"))
        registration_persist = persist.root_at("registration")
        registration_persist.set("secure-id", "secure_id")
        persist.save()
        headers = {
            "User-Agent": "landscape-client/%s" % VERSION,
            "Content-Type": "application/octet-stream",
            "X-Computer-ID": "secure_id"
        }

        mock_fetch.return_value = fail(HTTPCodeError(404, "Not found"))

        self.manager.add(ScriptExecutionPlugin())
        result = self._send_script("/bin/sh",
                                   "echo hi",
                                   attachments={u"file1": 14})

        def got_result(ignored):
            self.assertMessages(
                self.broker_service.message_store.get_pending_messages(),
                [{
                    "type": "operation-result",
                    "operation-id": 123,
                    "result-text": "Server returned HTTP code 404",
                    "result-code": FETCH_ATTACHMENTS_FAILED_RESULT,
                    "status": FAILED
                }])
            mock_fetch.assert_called_with("https://localhost/attachment/14",
                                          headers=headers,
                                          cainfo=None)

        return result.addCallback(got_result)
示例#5
0
    def test_fetch_ec2_meta_data_error_on_any_item_error(self):
        """
        L{_fetch_ec2_meta_data} returns a deferred C{Failure} containing the
        error message when an error occurs on any of the queried meta-data
        items C{instance-id}, C{ami-id} or C{instance-type}.
        """
        self.log_helper.ignore_errors(HTTPCodeError)
        error = HTTPCodeError(404, "notfound")
        metadata_items = ["instance-id", "ami-id", "instance-type"]
        for item in metadata_items:
            # reset all item data adding the error to only 1 item per iteration
            for setup_item in metadata_items:
                if setup_item == item:
                    self.add_query_result(item, error)
                else:
                    self.add_query_result(setup_item, "value%s" % setup_item)

            deferred = fetch_ec2_meta_data(fetch=self.fetch_func)
            failure = self.failureResultOf(deferred)
            self.assertEqual(
                "Server returned HTTP code 404",
                failure.getErrorMessage())
示例#6
0
 def test_fetch_ec2_meta_data_bad_result_retry(self):
     """
     L{_fetch_ec2_meta_data} returns C{None} when faced with spurious
     errors from the EC2 api. The method increments L{_cloud_retries}
     counter which allows L{_fetch_ec2_meta_data} to run again next
     message exchange.
     """
     self.log_helper.ignore_errors(HTTPCodeError)
     self.add_query_result("ami-id", HTTPCodeError(404, "notfound"))
     plugin = ComputerInfo(fetch_async=self.fetch_func)
     result = yield plugin._fetch_ec2_meta_data()
     self.assertEqual(1, plugin._cloud_retries)
     self.assertEqual(None, result)
     # Fix the error condition for the retry.
     self.add_query_result("ami-id", b"ami-00002")
     result = yield plugin._fetch_ec2_meta_data()
     self.assertEqual(
         {
             "instance-id": u"i00001",
             "ami-id": u"ami-00002",
             "instance-type": u"hs1.8xlarge"
         }, result)
示例#7
0
 def test_http_error_repr(self):
     self.assertEqual(repr(HTTPCodeError(501, "")),
                      "<HTTPCodeError http_code=501>")
示例#8
0
 def test_http_error_str(self):
     self.assertEqual(str(HTTPCodeError(501, "")),
                      "Server returned HTTP code 501")