Skip to content

Commit

Permalink
Merge pull request #58 from AgarFu/proxy_support
Browse files Browse the repository at this point in the history
Proxy support
  • Loading branch information
mbrossard authored Jul 31, 2017
2 parents 8f014f6 + 58b9c78 commit a9f261e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM centos:latest

RUN mkdir -p /app;\
yum install -y createrepo make rpm-build epel-release;\
yum install -y python2-mock
ADD . /app
WORKDIR /app
ENTRYPOINT ["/usr/bin/make"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Currently the plugin does not support:

Use `make test` to run some simple tests.

### Testing with docker compose:
docker-compose -f docker-compose.tests.yml run yum-s3-iam test
docker-compose -f docker-compose.tests.yml down --volumes --rmi all

## License

Apache 2.0 license. See LICENSE.
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'
services:
yum-s3-iam:
build:
context: .
dockerfile: Dockerfile.test
image: s3-iam-plugin
volumes:
- .:/app/
working_dir: /app/
network_mode: 'bridge'
16 changes: 14 additions & 2 deletions s3iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
__email__ = "julius@seporaitis.net"
__copyright__ = "Copyright 2012, Julius Seporaitis"
__license__ = "Apache 2.0"
__version__ = "1.2.0"
__version__ = "1.2.1"


__all__ = ['requires_api_version', 'plugin_type', 'CONDUIT',
Expand All @@ -48,7 +48,7 @@
BUFFER_SIZE = 1024 * 1024
OPTIONAL_ATTRIBUTES = ['priority', 'base_persistdir', 'metadata_expire',
'skip_if_unavailable', 'keepcache', 'priority']
UNSUPPORTED_ATTRIBUTES = ['mirrorlist', 'proxy']
UNSUPPORTED_ATTRIBUTES = ['mirrorlist']


def config_hook(conduit):
Expand Down Expand Up @@ -162,6 +162,18 @@ def __init__(self, repoid, repo):
msg = "%s: Unsupported attribute: %s." % (__file__, attr)
raise yum.plugins.PluginYumExit(msg)

proxy_config = {}
if 'https_proxy' in os.environ:
proxy_config['https'] = os.environ['https_proxy']
if 'http_proxy' in os.environ:
proxy_config['http'] = os.environ['http_proxy']
if repo.proxy:
proxy_config['https'] = proxy_config['http'] = repo.proxy
if proxy_config:
proxy = urllib2.ProxyHandler(proxy_config)
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

self.iamrole = None
self.grabber = None
self.enable()
Expand Down
57 changes: 57 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import createrepo
sys.path.append('.')
import s3iam
from mock import patch, ANY, MagicMock


PACKAGE_NAME = 'yum-plugin-s3-iam'
Expand Down Expand Up @@ -167,5 +168,61 @@ def test_urls(self):
self.assertEqual(r, 'cn-north-1')
self.assertEqual(p, '/path')


class S3RepositoryTest(unittest.TestCase):

def setUp(self):
self.orig_http_proxy = os.environ['http_proxy'] if 'http_proxy' in os.environ else None
os.environ['https_proxy'] = 'http://https_proxy_host:https_proxy_port'
self.orig_https_proxy = os.environ['https_proxy'] if 'https_proxy' in os.environ else None
os.environ['http_proxy'] = 'http://http_proxy_host:http_proxy_port'
self.repo = MagicMock(
baseurl = 'https://s3.cn-north-1.amazonaws.com.cn/bar/path',
name = 'test repo',
region = 'cn-north-1',
basecachedir = '',
gpgcheck = False,
gpgkey = None,
key_id = None,
secret_key = None,
enablegroups = False,
delegated_role = None,
retries = 1,
backoff = None,
delay = 0,
mirrorlist = None,
proxy = None
)

def tearDown(self):
if self.orig_https_proxy is not None:
os.environ['https_proxy'] = self.orig_https_proxy
if self.orig_http_proxy is not None:
os.environ['http_proxy'] = self.orig_http_proxy

@patch('s3iam.urllib2')
def test_config_proxy_from_env(self, urllib2_mock):
s3_repo = s3iam.S3Repository('repo-id', self.repo)
urllib2_mock.ProxyHandler.assert_called_once_with({
'http':'http://http_proxy_host:http_proxy_port',
'https':'http://https_proxy_host:https_proxy_port'
})
urllib2_mock.build_opener.assert_called_once_with(urllib2_mock.ProxyHandler.return_value)
urllib2_mock.install_opener.assert_called_once_with(ANY)

@patch('s3iam.urllib2')
def test_config_proxy_from_yum_conf(self, urllib2_mock):
del(os.environ['http_proxy'])
del(os.environ['https_proxy'])
self.repo.proxy = 'http://same_proxy_for_all:port'
s3_repo = s3iam.S3Repository('repo-id', self.repo)
urllib2_mock.ProxyHandler.assert_called_once_with({
'http':'http://same_proxy_for_all:port',
'https':'http://same_proxy_for_all:port'
})
urllib2_mock.build_opener.assert_called_once_with(urllib2_mock.ProxyHandler.return_value)
urllib2_mock.install_opener.assert_called_once_with(ANY)


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

0 comments on commit a9f261e

Please sign in to comment.