Repository: docker/docker-py

Issue:
Create container bind volume with Unicode folder name
If bind volume folder name is Unicode, for example Japanese, It will raise exception:
Host volume and container volume both should handle Unicode.

```
  File "/home/vagrant/.local/share/virtualenvs/qnap/local/lib/python2.7/site-packages/docker/utils/utils.py", line 461, in create_host_config
    host_config['Binds'] = convert_volume_binds(binds)
  File "/home/vagrant/.local/share/virtualenvs/qnap/local/lib/python2.7/site-packages/docker/utils/utils.py", line 208, in convert_volume_binds
    k, v['bind'], mode
```

Discussion:
shin-: @aanand Thanks, didn't know about `six.u`. PTAL?
aanand: Cool. Perhaps the first test should be rewritten so it tests byte string input on all Python versions:

```python
    def test_convert_volume_bindings_binary_input(self):
        expected = [six.u('/mnt/지연:/unicode/박:rw')]

        data = {
            b'/mnt/지연': {
                'bind': b'/unicode/박',
                'mode': 'rw'
            }
        }
        self.assertEqual(
            convert_volume_binds(data), expected
        )
```
shin-: Okay, I've tried all sorts of combination, I've settled on just separating py2 and py3 tests completely (so it's 2+2 now). On the bright side, results were consistent all along (and we have tests to prove it)!
aanand: OK, I see the rationale, but instead of entirely separate test methods, could we just switch on the Python version *inside* the method?

```python
def test_convert_volume_binds_unicode_bytes_input(self):
    if six.PY2:
        expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
        data = {
            '/mnt/지연': {
                'bind': '/unicode/박',
                'mode': 'rw'
            }
        }
    else:
        expected = ['/mnt/지연:/unicode/박:rw']
        data = {
            bytes('/mnt/지연', 'utf-8'): {
                'bind': bytes('/unicode/박', 'utf-8'),
                'mode': 'rw'
            }
        }

    self.assertEqual(convert_volume_binds(data), expected)

def test_convert_volume_binds_unicode_unicode_input(self):
    if six.PY2:
        expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
        data = {
            unicode('/mnt/지연', 'utf-8'): {
                'bind': unicode('/unicode/박', 'utf-8'),
                'mode': 'rw'
            }
        }
    else:
        expected = ['/mnt/지연:/unicode/박:rw']
        data = {
            '/mnt/지연': {
                'bind': '/unicode/박',
                'mode': 'rw'
            }
        }

    self.assertEqual(convert_volume_binds(data), expected)
```

Patch:
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index 36edf8de..1fce1377 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -242,6 +242,9 @@ def convert_volume_binds(binds):
 
     result = []
     for k, v in binds.items():
+        if isinstance(k, six.binary_type):
+            k = k.decode('utf-8')
+
         if isinstance(v, dict):
             if 'ro' in v and 'mode' in v:
                 raise ValueError(
@@ -249,6 +252,10 @@ def convert_volume_binds(binds):
                     .format(repr(v))
                 )
 
+            bind = v['bind']
+            if isinstance(bind, six.binary_type):
+                bind = bind.decode('utf-8')
+
             if 'ro' in v:
                 mode = 'ro' if v['ro'] else 'rw'
             elif 'mode' in v:
@@ -256,11 +263,15 @@ def convert_volume_binds(binds):
             else:
                 mode = 'rw'
 
-            result.append('{0}:{1}:{2}'.format(
-                k, v['bind'], mode
-            ))
+            result.append(
+                six.text_type('{0}:{1}:{2}').format(k, bind, mode)
+            )
         else:
-            result.append('{0}:{1}:rw'.format(k, v))
+            if isinstance(v, six.binary_type):
+                v = v.decode('utf-8')
+            result.append(
+                six.text_type('{0}:{1}:rw').format(k, v)
+            )
     return result
 
 


Test patch:
diff --git a/tests/utils_test.py b/tests/utils_test.py
index 45929f73..8ac1dcb9 100644
--- a/tests/utils_test.py
+++ b/tests/utils_test.py
@@ -1,15 +1,20 @@
+# -*- coding: utf-8 -*-
+
 import os
 import os.path
 import shutil
 import tempfile
 
+import pytest
+import six
+
 from docker.client import Client
 from docker.constants import DEFAULT_DOCKER_API_VERSION
 from docker.errors import DockerException
 from docker.utils import (
     parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
     create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
-    exclude_paths,
+    exclude_paths, convert_volume_binds,
 )
 from docker.utils.ports import build_port_bindings, split_port
 from docker.auth import resolve_repository_name, resolve_authconfig
@@ -17,7 +22,6 @@ from docker.auth import resolve_repository_name, resolve_authconfig
 from . import base
 from .helpers import make_tree
 
-import pytest
 
 TEST_CERT_DIR = os.path.join(
     os.path.dirname(__file__),
@@ -192,6 +196,89 @@ class UtilsTest(base.BaseTestCase):
         local_tempfile.close()
         return local_tempfile.name
 
+    def test_convert_volume_binds_empty(self):
+        self.assertEqual(convert_volume_binds({}), [])
+        self.assertEqual(convert_volume_binds([]), [])
+
+    def test_convert_volume_binds_list(self):
+        data = ['/a:/a:ro', '/b:/c:z']
+        self.assertEqual(convert_volume_binds(data), data)
+
+    def test_convert_volume_binds_complete(self):
+        data = {
+            '/mnt/vol1': {
+                'bind': '/data',
+                'mode': 'ro'
+            }
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:ro'])
+
+    def test_convert_volume_binds_compact(self):
+        data = {
+            '/mnt/vol1': '/data'
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:rw'])
+
+    def test_convert_volume_binds_no_mode(self):
+        data = {
+            '/mnt/vol1': {
+                'bind': '/data'
+            }
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:rw'])
+
+    def test_convert_volume_binds_unicode_bytes_input(self):
+        if six.PY2:
+            expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
+
+            data = {
+                '/mnt/지연': {
+                    'bind': '/unicode/박',
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+        else:
+            expected = ['/mnt/지연:/unicode/박:rw']
+
+            data = {
+                bytes('/mnt/지연', 'utf-8'): {
+                    'bind': bytes('/unicode/박', 'utf-8'),
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+
+    def test_convert_volume_binds_unicode_unicode_input(self):
+        if six.PY2:
+            expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
+
+            data = {
+                unicode('/mnt/지연', 'utf-8'): {
+                    'bind': unicode('/unicode/박', 'utf-8'),
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+        else:
+            expected = ['/mnt/지연:/unicode/박:rw']
+
+            data = {
+                '/mnt/지연': {
+                    'bind': '/unicode/박',
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+
     def test_parse_repository_tag(self):
         self.assertEqual(parse_repository_tag("root"),
                          ("root", None))