Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Add http_proxy configuration with unittest #15

Merged
merged 3 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ def __init__(self):
# check host name
# Set this to True/False to enable/disable SSL hostname verification.
self.assert_hostname = None

# urllib3 connection pool's maximum number of connections saved
# per pool. Increasing this is useful for cases when you are
# making a lot of possibly parallel requests to the same host,
# which is often the case here.
# When set to `None`, will default to whatever urllib3 uses
self.connection_pool_maxsize = None
# http proxy setting
self.http_proxy_url = None

# WebSocket subprotocol to use for exec and portforward.
self.ws_streaming_protocol = "v4.channel.k8s.io"
Expand Down
11 changes: 8 additions & 3 deletions rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ def __init__(self, pools_size=4, config=configuration):
kwargs['assert_hostname'] = config.assert_hostname

# https pool manager
self.pool_manager = urllib3.PoolManager(
**kwargs
)
if config.http_proxy_url is not None:
self.pool_manager = urllib3.proxy_from_url(
config.http_proxy_url, **kwargs
)
else:
self.pool_manager = urllib3.PoolManager(
**kwargs
)

def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True,
Expand Down
42 changes: 42 additions & 0 deletions rest_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import urllib3

from mock import patch

from kubernetes.client import ConfigurationObject
from kubernetes.client.rest import RESTClientObject


class RestTest(unittest.TestCase):

def test_poolmanager(self):
'Test that a poolmanager is created for rest client'
with patch.object(urllib3, 'PoolManager') as pool:
RESTClientObject(config=ConfigurationObject())
pool.assert_called_once()

def test_proxy(self):
'Test that proxy is created when the config especifies it'
config = ConfigurationObject()
config.http_proxy_url = 'http://proxy.example.com'

with patch.object(urllib3, 'proxy_from_url') as proxy:
RESTClientObject(config=config)
proxy.assert_called_once()


if __name__ == '__main__':
unittest.main()