1
+ """
2
+ How to run locally:
3
+ Start your local registry:
4
+ `INDEX_ENDPOINT=https://indexstaging-docker.dotcloud.com \
5
+ SETTINGS_FLAVOR=test DOCKER_REGISTRY_CONFIG=config_sample.yml docker-registry`
6
+
7
+ Start the tests:
8
+ `DOCKER_REGISTRY_ENDPOINT=http://localhost:5000 SETTINGS_FLAVOR=test \
9
+ DOCKER_REGISTRY_CONFIG=config_sample.yml DOCKER_CREDS=USER:PASS \
10
+ nosetests --tests=tests/workflow.py`
11
+ """
12
+
1
13
import hashlib
2
14
import os
3
15
19
31
20
32
cfg = config .load ()
21
33
22
- ua = 'docker/1.0.0 registry test pretending to be docker'
34
+ ua = 'docker/0.11 registry test pretending to be docker'
23
35
24
36
25
37
class TestWorkflow (base .TestCase ):
@@ -28,13 +40,10 @@ class TestWorkflow(base.TestCase):
28
40
registry_endpoint = os .environ .get (
29
41
'DOCKER_REGISTRY_ENDPOINT' ,
30
42
'https://registrystaging-docker.dotcloud.com' )
31
- #registry_endpoint = 'http://localhost:5000'
32
43
index_endpoint = os .environ .get (
33
44
'DOCKER_INDEX_ENDPOINT' ,
34
45
'https://indexstaging-docker.dotcloud.com' )
35
- # export DOCKER_CREDS="login:password"
36
46
user_credentials = os .environ ['DOCKER_CREDS' ].split (':' )
37
- cookies = None
38
47
39
48
def generate_chunk (self , data ):
40
49
bufsize = 1024
@@ -46,12 +55,8 @@ def generate_chunk(self, data):
46
55
yield buf
47
56
io .close ()
48
57
49
- def update_cookies (self , response ):
50
- cookies = response .cookies
51
- if cookies :
52
- self .cookies = cookies
53
-
54
58
def upload_image (self , image_id , parent_id , token ):
59
+ # XXX revert
55
60
layer = self .gen_random_string (7 * 1024 * 1024 )
56
61
json_obj = {
57
62
'id' : image_id
@@ -68,26 +73,34 @@ def upload_image(self, image_id, parent_id, token):
68
73
headers = {'Authorization' : 'Token ' + token ,
69
74
'User-Agent' : ua ,
70
75
'X-Docker-Checksum' : layer_checksum },
71
- cookies = self .cookies )
76
+ )
77
+
72
78
self .assertEqual (resp .status_code , 200 , resp .text )
73
- self .update_cookies (resp )
74
79
resp = requests .put ('{0}/v1/images/{1}/layer' .format (
75
80
self .registry_endpoint , image_id ),
76
81
data = self .generate_chunk (layer ),
77
82
headers = {'Authorization' : 'Token ' + token ,
78
83
'User-Agent' : ua },
79
- cookies = self .cookies )
84
+ )
85
+
86
+ resp = requests .put ('{0}/v1/images/{1}/checksum' .format (
87
+ self .registry_endpoint , image_id ),
88
+ data = {},
89
+ headers = {'Authorization' : 'Token ' + token ,
90
+ 'X-Docker-Checksum-Payload' : layer_checksum ,
91
+ 'User-Agent' : ua }
92
+ )
93
+
80
94
self .assertEqual (resp .status_code , 200 , resp .text )
81
- self .update_cookies (resp )
82
95
return {'id' : image_id , 'checksum' : layer_checksum }
83
96
84
97
def update_tag (self , namespace , repos , image_id , tag_name ):
85
98
resp = requests .put ('{0}/v1/repositories/{1}/{2}/tags/{3}' .format (
86
99
self .registry_endpoint , namespace , repos , tag_name ),
87
100
data = json .dumps (image_id ),
88
- cookies = self .cookies )
101
+ )
102
+
89
103
self .assertEqual (resp .status_code , 200 , resp .text )
90
- self .update_cookies (resp )
91
104
92
105
def docker_push (self ):
93
106
# Test Push
@@ -106,11 +119,11 @@ def docker_push(self):
106
119
'User-Agent' : ua },
107
120
data = images_json )
108
121
self .assertEqual (resp .status_code , 200 , resp .text )
109
- token = resp .headers .get ('x-docker-token' )
122
+ self . token = resp .headers .get ('x-docker-token' )
110
123
# Docker -> Registry
111
124
images_json = []
112
- images_json .append (self .upload_image (parent_id , None , token ))
113
- images_json .append (self .upload_image (image_id , parent_id , token ))
125
+ images_json .append (self .upload_image (parent_id , None , self . token ))
126
+ images_json .append (self .upload_image (image_id , parent_id , self . token ))
114
127
# Updating the tags does not need a token, it will use the Cookie
115
128
self .update_tag (namespace , repos , image_id , 'latest' )
116
129
# Docker -> Index
@@ -127,41 +140,62 @@ def fetch_image(self, image_id):
127
140
"""Return image json metadata, checksum and its blob."""
128
141
resp = requests .get ('{0}/v1/images/{1}/json' .format (
129
142
self .registry_endpoint , image_id ),
130
- cookies = self . cookies )
143
+ )
131
144
self .assertEqual (resp .status_code , 200 , resp .text )
132
- self .update_cookies (resp )
145
+
146
+ resp = requests .get ('{0}/v1/images/{1}/json' .format (
147
+ self .registry_endpoint , image_id ),
148
+ headers = {'Authorization' : 'Token ' + self .token }
149
+ )
150
+ self .assertEqual (resp .status_code , 200 , resp .text )
151
+
133
152
json_data = resp .text
134
- checksum = resp .headers ['x-docker-checksum' ]
153
+ checksum = resp .headers ['x-docker-payload-checksum' ]
154
+
135
155
resp = requests .get ('{0}/v1/images/{1}/layer' .format (
136
156
self .registry_endpoint , image_id ),
137
- cookies = self .cookies )
157
+ )
158
+ self .assertEqual (resp .status_code , 200 , resp .text )
159
+
160
+ resp = requests .get ('{0}/v1/images/{1}/layer' .format (
161
+ self .registry_endpoint , image_id ),
162
+ headers = {'Authorization' : 'Token ' + self .token }
163
+ )
138
164
self .assertEqual (resp .status_code , 200 , resp .text )
139
- self .update_cookies (resp )
140
165
return (json_data , checksum , resp .text )
141
166
142
167
def docker_pull (self , namespace , repos ):
143
168
# Test pull
144
169
# Docker -> Index
170
+
171
+ resp = requests .get ('{0}/v1/repositories/{1}/{2}/images' .format (
172
+ self .index_endpoint , namespace , repos ),)
173
+ self .assertEqual (resp .status_code , 200 )
174
+
145
175
resp = requests .get ('{0}/v1/repositories/{1}/{2}/images' .format (
146
176
self .index_endpoint , namespace , repos ),
147
177
auth = tuple (self .user_credentials ),
148
178
headers = {'X-Docker-Token' : 'true' })
149
179
self .assertEqual (resp .status_code , 200 )
150
- token = resp .headers .get ('x-docker-token' )
180
+ self . token = resp .headers .get ('x-docker-token' )
151
181
# Here we should use the 'X-Endpoints' returned in a real environment
152
182
# Docker -> Registry
153
183
resp = requests .get ('{0}/v1/repositories/{1}/{2}/tags/latest' .format (
154
184
self .registry_endpoint , namespace , repos ),
155
- headers = {'Authorization' : 'Token ' + token })
185
+ headers = {'Authorization' : 'Token ' + self . token })
156
186
self .assertEqual (resp .status_code , 200 , resp .text )
157
- self .cookies = resp .cookies
187
+
188
+ resp = requests .get ('{0}/v1/repositories/{1}/{2}/tags/latest' .format (
189
+ self .registry_endpoint , namespace , repos ),
190
+ )
191
+ self .assertEqual (resp .status_code , 200 , resp .text )
192
+
158
193
# Docker -> Registry
159
194
# Note(dmp): unicode patch XXX not applied assume requests does the job
160
195
image_id = json .loads (resp .text )
161
196
resp = requests .get ('{0}/v1/images/{1}/ancestry' .format (
162
197
self .registry_endpoint , image_id ),
163
- cookies = self .cookies )
164
- self .update_cookies (resp )
198
+ )
165
199
self .assertEqual (resp .status_code , 200 , resp .text )
166
200
# Note(dmp): unicode patch XXX not applied assume requests does the job
167
201
ancestry = json .loads (resp .text )
@@ -177,14 +211,19 @@ def docker_pull(self, namespace, repos):
177
211
tmpfile .close ()
178
212
self .assertEqual (checksum , computed_checksum )
179
213
# Remove the repository
180
- resp = requests .delete ('{0}/v1/repositories/{1}/{2}/' .format (
181
- self .registry_endpoint , namespace , repos ), cookies = self .cookies )
182
- self .assertEqual (resp .status_code , 200 , resp .text )
183
- self .update_cookies (resp )
214
+ resp = requests .delete ('{0}/v1/repositories/{1}/{2}/images' .format (
215
+ self .registry_endpoint , namespace , repos ), )
216
+ self .assertEqual (resp .status_code , 204 , resp .text )
184
217
# Remove image_id, then parent_id
185
218
store = storage .load ()
186
- store .remove (os .path .join (store .images , self .image_id ))
187
- store .remove (os .path .join (store .images , self .parent_id ))
219
+ try :
220
+ store .remove (os .path .join (store .images , self .image_id ))
221
+ except Exception :
222
+ pass
223
+ try :
224
+ store .remove (os .path .join (store .images , self .parent_id ))
225
+ except Exception :
226
+ pass
188
227
189
228
def test_workflow (self ):
190
229
(namespace , repos ) = self .docker_push ()
0 commit comments