1
1
import os
2
2
import os .path
3
+ import shutil
4
+ import tempfile
3
5
import unittest
4
6
5
7
from docker .client import Client
13
15
14
16
import base
15
17
18
+ TEST_CERT_DIR = os .path .join (
19
+ os .path .dirname (__file__ ),
20
+ 'testdata/certs' ,
21
+ )
22
+
16
23
17
24
class UtilsTest (base .BaseTestCase ):
18
25
longMessage = True
@@ -75,11 +82,18 @@ def test_parse_host(self):
75
82
for host , expected in valid_hosts .items ():
76
83
self .assertEqual (parse_host (host ), expected , msg = host )
77
84
78
- def test_kwargs_from_env (self ):
85
+ def test_kwargs_from_env_empty (self ):
86
+ os .environ .update (DOCKER_HOST = '' ,
87
+ DOCKER_CERT_PATH = '' ,
88
+ DOCKER_TLS_VERIFY = '' )
89
+
90
+ kwargs = kwargs_from_env ()
91
+ self .assertEqual (None , kwargs .get ('base_url' ))
92
+ self .assertEqual (None , kwargs .get ('tls' ))
93
+
94
+ def test_kwargs_from_env_tls (self ):
79
95
os .environ .update (DOCKER_HOST = 'tcp://192.168.59.103:2376' ,
80
- DOCKER_CERT_PATH = os .path .join (
81
- os .path .dirname (__file__ ),
82
- 'testdata/certs' ),
96
+ DOCKER_CERT_PATH = TEST_CERT_DIR ,
83
97
DOCKER_TLS_VERIFY = '1' )
84
98
kwargs = kwargs_from_env (assert_hostname = False )
85
99
self .assertEqual ('https://192.168.59.103:2376' , kwargs ['base_url' ])
@@ -95,6 +109,24 @@ def test_kwargs_from_env(self):
95
109
except TypeError as e :
96
110
self .fail (e )
97
111
112
+ def test_kwargs_from_env_no_cert_path (self ):
113
+ try :
114
+ temp_dir = tempfile .mkdtemp ()
115
+ cert_dir = os .path .join (temp_dir , '.docker' )
116
+ shutil .copytree (TEST_CERT_DIR , cert_dir )
117
+
118
+ os .environ .update (HOME = temp_dir ,
119
+ DOCKER_CERT_PATH = '' ,
120
+ DOCKER_TLS_VERIFY = '1' )
121
+
122
+ kwargs = kwargs_from_env ()
123
+ self .assertIn (cert_dir , kwargs ['tls' ].verify )
124
+ self .assertIn (cert_dir , kwargs ['tls' ].cert [0 ])
125
+ self .assertIn (cert_dir , kwargs ['tls' ].cert [1 ])
126
+ finally :
127
+ if temp_dir :
128
+ shutil .rmtree (temp_dir )
129
+
98
130
def test_convert_filters (self ):
99
131
tests = [
100
132
({'dangling' : True }, '{"dangling": ["true"]}' ),
0 commit comments