示例#1
0
    def test_constructor(self):
        proxy_url = 'http://127.0.0.2:3128'
        os.environ['http_proxy'] = proxy_url
        conn = LibcloudConnection(host='localhost', port=80)
        self.assertEqual(conn.proxy_scheme, 'http')
        self.assertEqual(conn.proxy_host, '127.0.0.2')
        self.assertEqual(conn.proxy_port, 3128)

        _ = os.environ.pop('http_proxy', None)
        conn = LibcloudConnection(host='localhost', port=80)
        self.assertIsNone(conn.proxy_scheme)
        self.assertIsNone(conn.proxy_host)
        self.assertIsNone(conn.proxy_port)

        proxy_url = 'http://127.0.0.3:3128'
        conn.set_http_proxy(proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, 'http')
        self.assertEqual(conn.proxy_host, '127.0.0.3')
        self.assertEqual(conn.proxy_port, 3128)

        proxy_url = 'http://127.0.0.4:3128'
        conn = LibcloudConnection(host='localhost', port=80,
                                  proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, 'http')
        self.assertEqual(conn.proxy_host, '127.0.0.4')
        self.assertEqual(conn.proxy_port, 3128)

        os.environ['http_proxy'] = proxy_url
        proxy_url = 'http://127.0.0.5:3128'
        conn = LibcloudConnection(host='localhost', port=80,
                                  proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, 'http')
        self.assertEqual(conn.proxy_host, '127.0.0.5')
        self.assertEqual(conn.proxy_port, 3128)
示例#2
0
    def test_connection_to_unusual_port(self):
        conn = LibcloudConnection(host='localhost', port=8080)
        self.assertIsNone(conn.proxy_scheme)
        self.assertIsNone(conn.proxy_host)
        self.assertIsNone(conn.proxy_port)
        self.assertEqual(conn.host, 'http://localhost:8080')

        conn = LibcloudConnection(host='localhost', port=80)
        self.assertEqual(conn.host, 'http://localhost')
示例#3
0
    def test_connection_session_timeout(self):
        """
        Test that the connection timeout attribute is set correctly
        """
        conn = LibcloudConnection(host='localhost', port=8080)
        self.assertEqual(conn.session.timeout, 60)

        conn = LibcloudConnection(host='localhost', port=8080, timeout=10)
        self.assertEqual(conn.session.timeout, 10)
示例#4
0
 def test_connection_timeout_raised(self):
     """
     Test that the connection times out
     """
     conn = LibcloudConnection(host='localhost', port=8080, timeout=0.1)
     # use a not-routable address to test that the connection timeouts
     host = "http://10.255.255.1"
     with self.assertRaises(ConnectTimeout):
         conn.request('GET', host)
示例#5
0
    def test_request_custom_timeout_no_timeout(self):
        def response_hook(*args, **kwargs):
            # Assert timeout has been passed correctly
            self.assertEqual(kwargs["timeout"], 5)

        hooks = {"response": response_hook}

        connection = LibcloudConnection(host=self.listen_host,
                                        port=self.listen_port,
                                        timeout=5)
        connection.request(method="GET", url="/test", hooks=hooks)
示例#6
0
    def test_prepared_request_with_body(self):
        connection = LibcloudConnection(host=self.listen_host,
                                        port=self.listen_port)
        connection.prepared_request(method="GET",
                                    url="/test/prepared-request-3",
                                    body="test body",
                                    stream=True)

        self.assertEqual(connection.response.status_code, httplib.OK)
        self.assertEqual(connection.response.content,
                         b"/test/prepared-request-3")
示例#7
0
class TestHttpLibSSLTests(unittest.TestCase):
    def setUp(self):
        libcloud.security.VERIFY_SSL_CERT = False
        libcloud.security.CA_CERTS_PATH = ORIGINAL_CA_CERS_PATH
        self.httplib_object = LibcloudConnection('foo.bar', port=80)

    def test_custom_ca_path_using_env_var_doesnt_exist(self):
        os.environ['SSL_CERT_FILE'] = '/foo/doesnt/exist'

        try:
            reload(libcloud.security)
        except ValueError:
            e = sys.exc_info()[1]
            msg = 'Certificate file /foo/doesnt/exist doesn\'t exist'
            self.assertEqual(str(e), msg)
        else:
            self.fail('Exception was not thrown')

    def test_custom_ca_path_using_env_var_is_directory(self):
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.environ['SSL_CERT_FILE'] = file_path

        expected_msg = 'Certificate file can\'t be a directory'
        self.assertRaisesRegexp(ValueError, expected_msg, reload,
                                libcloud.security)

    def test_custom_ca_path_using_env_var_exist(self):
        # When setting a path we don't actually check that a valid CA file is
        # provided.
        # This happens later in the code in http.connect method
        file_path = os.path.abspath(__file__)
        os.environ['SSL_CERT_FILE'] = file_path

        reload(libcloud.security)

        self.assertEqual(libcloud.security.CA_CERTS_PATH, [file_path])

    @patch('warnings.warn')
    def test_setup_ca_cert(self, _):
        # verify = False, _setup_ca_cert should be a no-op
        self.httplib_object.verify = False
        self.httplib_object._setup_ca_cert()

        self.assertEqual(self.httplib_object.ca_cert, None)

        # verify = True, a valid path is provided, self.ca_cert should be set to
        # a valid path
        self.httplib_object.verify = True

        libcloud.security.CA_CERTS_PATH = [os.path.abspath(__file__)]
        self.httplib_object._setup_ca_cert()

        self.assertTrue(self.httplib_object.ca_cert is not None)
示例#8
0
文件: ovh.py 项目: subha4/libcloud
    def request_consumer_key(self, user_id):
        action = self.request_path + '/auth/credential'
        data = json.dumps({
            'accessRules': DEFAULT_ACCESS_RULES,
            'redirection': 'http://ovh.com',
        })
        headers = {
            'Content-Type': 'application/json',
            'X-Ovh-Application': user_id,
        }
        httpcon = LibcloudConnection(host=self.host, port=443)

        try:
            httpcon.request(method='POST', url=action, body=data,
                            headers=headers)
        except Exception as e:
            handle_and_rethrow_user_friendly_invalid_region_error(
                host=self.host, e=e)

        response = OvhResponse(httpcon.getresponse(), httpcon)

        if response.status == httplib.UNAUTHORIZED:
            raise InvalidCredsError()

        json_response = response.parse_body()
        httpcon.close()
        return json_response
示例#9
0
    def test_prepared_request_empty_body_chunked_encoding_not_used(self):
        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port)
        connection.prepared_request(method='GET', url='/test/prepared-request-1', body='', stream=True)

        self.assertEqual(connection.response.status_code, httplib.OK)
        self.assertEqual(connection.response.content, b'/test/prepared-request-1')

        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port)
        connection.prepared_request(method='GET', url='/test/prepared-request-2', body=None, stream=True)

        self.assertEqual(connection.response.status_code, httplib.OK)
        self.assertEqual(connection.response.content, b'/test/prepared-request-2')
示例#10
0
class TestHttpLibSSLTests(unittest.TestCase):
    def setUp(self):
        libcloud.security.VERIFY_SSL_CERT = False
        libcloud.security.CA_CERTS_PATH = ORIGINAL_CA_CERTS_PATH
        self.httplib_object = LibcloudConnection("foo.bar", port=80)

    def test_custom_ca_path_using_env_var_doesnt_exist(self):
        os.environ["SSL_CERT_FILE"] = "/foo/doesnt/exist"

        try:
            reload(libcloud.security)
        except ValueError as e:
            msg = "Certificate file /foo/doesnt/exist doesn't exist"
            self.assertEqual(str(e), msg)
        else:
            self.fail("Exception was not thrown")

    def test_custom_ca_path_using_env_var_is_directory(self):
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.environ["SSL_CERT_FILE"] = file_path

        expected_msg = "Certificate file can't be a directory"
        assertRaisesRegex(self, ValueError, expected_msg, reload,
                          libcloud.security)

    def test_custom_ca_path_using_env_var_exist(self):
        # When setting a path we don't actually check that a valid CA file is
        # provided.
        # This happens later in the code in http.connect method
        file_path = os.path.abspath(__file__)
        os.environ["SSL_CERT_FILE"] = file_path

        reload(libcloud.security)

        self.assertEqual(libcloud.security.CA_CERTS_PATH, file_path)

    def test_ca_cert_list_warning(self):
        with warnings.catch_warnings(record=True) as w:
            self.httplib_object.verify = True
            self.httplib_object._setup_ca_cert(
                ca_cert=[ORIGINAL_CA_CERTS_PATH])
            self.assertEqual(self.httplib_object.ca_cert,
                             ORIGINAL_CA_CERTS_PATH)
            self.assertEqual(w[0].category, DeprecationWarning)

    def test_setup_ca_cert(self):
        # verify = False, _setup_ca_cert should be a no-op
        self.httplib_object.verify = False
        self.httplib_object._setup_ca_cert()

        self.assertIsNone(self.httplib_object.ca_cert)

        # verify = True, a valid path is provided, self.ca_cert should be set to
        # a valid path
        self.httplib_object.verify = True

        libcloud.security.CA_CERTS_PATH = os.path.abspath(__file__)
        self.httplib_object._setup_ca_cert()

        self.assertTrue(self.httplib_object.ca_cert is not None)
示例#11
0
    def test_request_custom_timeout_timeout(self):
        def response_hook(*args, **kwargs):
            # Assert timeout has been passed correctly
            self.assertEqual(kwargs['timeout'], 0.5)

        hooks = {'response': response_hook}

        connection = LibcloudConnection(host=self.listen_host,
                                        port=self.listen_port,
                                        timeout=0.5)
        self.assertRaisesRegex(requests.exceptions.ReadTimeout,
                               'Read timed out',
                               connection.request,
                               method='GET',
                               url='/test-timeout',
                               hooks=hooks)
示例#12
0
    def request_consumer_key(self, user_id):
        action = self.request_path + '/auth/credential'
        data = json.dumps({
            'accessRules': DEFAULT_ACCESS_RULES,
            'redirection': 'http://ovh.com',
        })
        headers = {
            'Content-Type': 'application/json',
            'X-Ovh-Application': user_id,
        }
        httpcon = LibcloudConnection(host=self.host, port=443)
        httpcon.request(method='POST', url=action, body=data, headers=headers)
        response = httpcon.getresponse()

        if response.status == httplib.UNAUTHORIZED:
            raise InvalidCredsError()

        body = response.read()
        json_response = json.loads(body)
        httpcon.close()
        return json_response
示例#13
0
 def setUp(self):
     libcloud.security.VERIFY_SSL_CERT = False
     libcloud.security.CA_CERTS_PATH = ORIGINAL_CA_CERTS_PATH
     self.httplib_object = LibcloudConnection('foo.bar', port=80)
示例#14
0
 def setUp(self):
     self.mock_connection = LibcloudConnection(host="mock.com", port=80)
     self.mock_connection.driver = None
示例#15
0
    def test_constructor(self):
        proxy_url = "http://127.0.0.2:3128"
        os.environ["http_proxy"] = proxy_url
        conn = LibcloudConnection(host="localhost", port=80)
        self.assertEqual(conn.proxy_scheme, "http")
        self.assertEqual(conn.proxy_host, "127.0.0.2")
        self.assertEqual(conn.proxy_port, 3128)
        self.assertEqual(
            conn.session.proxies,
            {
                "http": "http://127.0.0.2:3128",
                "https": "http://127.0.0.2:3128"
            },
        )

        _ = os.environ.pop("http_proxy", None)
        conn = LibcloudConnection(host="localhost", port=80)
        self.assertIsNone(conn.proxy_scheme)
        self.assertIsNone(conn.proxy_host)
        self.assertIsNone(conn.proxy_port)

        proxy_url = "http://127.0.0.3:3128"
        conn.set_http_proxy(proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, "http")
        self.assertEqual(conn.proxy_host, "127.0.0.3")
        self.assertEqual(conn.proxy_port, 3128)
        self.assertEqual(
            conn.session.proxies,
            {
                "http": "http://127.0.0.3:3128",
                "https": "http://127.0.0.3:3128"
            },
        )

        proxy_url = "http://127.0.0.4:3128"
        conn = LibcloudConnection(host="localhost",
                                  port=80,
                                  proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, "http")
        self.assertEqual(conn.proxy_host, "127.0.0.4")
        self.assertEqual(conn.proxy_port, 3128)
        self.assertEqual(
            conn.session.proxies,
            {
                "http": "http://127.0.0.4:3128",
                "https": "http://127.0.0.4:3128"
            },
        )

        os.environ["http_proxy"] = proxy_url
        proxy_url = "http://127.0.0.5:3128"
        conn = LibcloudConnection(host="localhost",
                                  port=80,
                                  proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, "http")
        self.assertEqual(conn.proxy_host, "127.0.0.5")
        self.assertEqual(conn.proxy_port, 3128)
        self.assertEqual(
            conn.session.proxies,
            {
                "http": "http://127.0.0.5:3128",
                "https": "http://127.0.0.5:3128"
            },
        )

        os.environ["http_proxy"] = proxy_url
        proxy_url = "https://127.0.0.6:3129"
        conn = LibcloudConnection(host="localhost",
                                  port=80,
                                  proxy_url=proxy_url)
        self.assertEqual(conn.proxy_scheme, "https")
        self.assertEqual(conn.proxy_host, "127.0.0.6")
        self.assertEqual(conn.proxy_port, 3129)
        self.assertEqual(
            conn.session.proxies,
            {
                "http": "https://127.0.0.6:3129",
                "https": "https://127.0.0.6:3129"
            },
        )