Kaydet (Commit) 08089056 authored tarafından shin-'s avatar shin-

Refactoring, Python 3 compatibility, Tests working with python 3, cleaned up imports.

......@@ -13,4 +13,3 @@
# limitations under the License.
from .client import Client, APIError
import auth
from .auth import *
\ No newline at end of file
......@@ -18,10 +18,11 @@ import os
import six
import utils
import docker.utils as utils
INDEX_URL = 'https://index.docker.io/v1/'
def swap_protocol(url):
if url.startswith('http://'):
return url.replace('http://', 'https://', 1)
......@@ -84,8 +85,10 @@ def resolve_authconfig(authconfig, registry):
def decode_auth(auth):
if isinstance(auth, six.string_types):
auth = auth.encode('ascii')
s = base64.b64decode(auth)
login, pwd = s.split(':')
login, pwd = s.split(b':')
return login, pwd
......
......@@ -20,9 +20,9 @@ import requests
import requests.exceptions
import six
import auth
import unixconn
import utils
import docker.auth as auth
import docker.unixconn as unixconn
import docker.utils as utils
class APIError(requests.exceptions.HTTPError):
......@@ -75,7 +75,7 @@ class Client(requests.Session):
"""Raises stored :class:`APIError`, if one occurred."""
try:
response.raise_for_status()
except requests.exceptions.HTTPError, e:
except requests.exceptions.HTTPError as e:
raise APIError(e, response=response, explanation=explanation)
def _result(self, response, json=False):
......@@ -384,6 +384,7 @@ class Client(requests.Session):
'fromImage': repository
}
headers = {}
if utils.compare_version('1.5', self._version) >= 0:
if getattr(self, '_cfg', None) is None:
self._cfg = auth.load_config()
......
from .unixconn import UnixAdapter
......@@ -11,8 +11,12 @@
# 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 six
import httplib
if six.PY3:
from http import client as httplib
else:
import httplib
import requests.adapters
import socket
......
from .utils import mkbuildcontext, tar, compare_version
......@@ -12,24 +12,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import tarfile
import tempfile
import requests
import six
if six.PY3:
from io import StringIO
else:
from StringIO import StringIO
def mkbuildcontext(dockerfile):
f = tempfile.TemporaryFile()
f = tempfile.NamedTemporaryFile()
t = tarfile.open(mode='w', fileobj=f)
if isinstance(dockerfile, StringIO):
if isinstance(dockerfile, io.StringIO):
dfinfo = tarfile.TarInfo('Dockerfile')
if six.PY3:
raise TypeError('Please use io.BytesIO to create in-memory '
'Dockerfiles with Python 3')
else:
dfinfo.size = len(dockerfile.getvalue())
elif isinstance(dockerfile, io.BytesIO):
dfinfo = tarfile.TarInfo('Dockerfile')
dfinfo.size = dockerfile.len
dfinfo.size = len(dockerfile.getvalue())
else:
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')
t.addfile(dfinfo, dockerfile)
......@@ -39,7 +42,7 @@ def mkbuildcontext(dockerfile):
def tar(path):
f = tempfile.TemporaryFile()
f = tempfile.NamedTemporaryFile()
t = tarfile.open(mode='w', fileobj=f)
t.add(path, arcname='.')
t.close()
......
import json
CURRENT_VERSION = 'v1.4'
FAKE_CONTAINER_ID = '3cc2351ab11b'
FAKE_IMAGE_ID = 'e9aa60c60128'
### Each method is prefixed with HTTP method (get, post...)
### for clarity and readability
def get_fake_version():
status_code = 200
response = {'GoVersion': '1', 'Version': '1.1.1'}
return status_code, response
def get_fake_info():
status_code = 200
response = {'Containers': 1, 'Images': 1, 'Debug': ''}
return status_code, response
def get_fake_search():
status_code = 200
response = [{'Name': 'busybox', 'Description': 'Fake Description'}]
return status_code, response
def get_fake_images():
status_code = 200
response = [
{'Id': FAKE_IMAGE_ID, 'Created': '2 days ago', 'Repository': 'busybox', 'Tag': 'latest'}
]
return status_code, response
def get_fake_containers():
status_code = 200
response = [
{'Id': FAKE_CONTAINER_ID,
'Image': 'busybox:latest',
'Created': '2 days ago',
'Command': 'true',
'Status': 'fake status'}
]
return status_code, response
def post_fake_start_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_create_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def get_fake_inspect_container():
status_code = 200
response = {
'Id': FAKE_CONTAINER_ID,
'Config': {'Privileged': True},
'ID': FAKE_CONTAINER_ID,
'Image': 'busybox:latest',
"State": {
"Running": True,
"Pid": 0,
"ExitCode": 0,
"StartedAt": "2013-09-25T14:01:18.869545111+02:00",
"Ghost": False
},
}
return status_code, response
def get_fake_wait():
status_code = 200
response = {'StatusCode': 0}
return status_code, response
def get_fake_logs():
status_code = 200
response = 'Flowering Nights (Sakuya Iyazoi)'
return status_code, response
def get_fake_diff():
status_code = 200
response = [{'Path': '/test', 'Kind': 1}]
return status_code, response
def post_fake_stop_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_kill_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_restart_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def delete_fake_remove_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_image_create():
status_code = 200
response = {'Id': FAKE_IMAGE_ID}
return status_code, response
def delete_fake_remove_image():
status_code = 200
response = {'Id': FAKE_IMAGE_ID}
return status_code, response
def post_fake_commit():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_build_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
## maps real api url to fake response callback
fake_responses = {
'unix://var/run/docker.sock/{0}/version'.format(CURRENT_VERSION): get_fake_version,
'unix://var/run/docker.sock/{0}/info'.format(CURRENT_VERSION): get_fake_info,
'unix://var/run/docker.sock/{0}/images/search'.format(CURRENT_VERSION): get_fake_search,
'unix://var/run/docker.sock/{0}/images/json'.format(CURRENT_VERSION): get_fake_images,
'unix://var/run/docker.sock/{0}/containers/ps'.format(CURRENT_VERSION): get_fake_containers,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/start'.format(CURRENT_VERSION): post_fake_start_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION): get_fake_inspect_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION): get_fake_wait,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/attach'.format(CURRENT_VERSION): get_fake_logs,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/changes'.format(CURRENT_VERSION): get_fake_diff,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION): post_fake_stop_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION): post_fake_kill_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION): post_fake_restart_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION): delete_fake_remove_container,
'unix://var/run/docker.sock/{0}/images/create'.format(CURRENT_VERSION): post_fake_image_create,
'unix://var/run/docker.sock/{0}/images/e9aa60c60128'.format(CURRENT_VERSION): delete_fake_remove_image,
'unix://var/run/docker.sock/{0}/commit'.format(CURRENT_VERSION): post_fake_commit,
'unix://var/run/docker.sock/{0}/containers/create'.format(CURRENT_VERSION): post_fake_create_container,
'unix://var/run/docker.sock/{0}/build'.format(CURRENT_VERSION): post_fake_build_container
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment