-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtests.py
More file actions
206 lines (171 loc) · 7.25 KB
/
tests.py
File metadata and controls
206 lines (171 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Unit tests for the Deis registry app.
Run the tests with "./manage.py test registry"
"""
import unittest
from unittest import mock
from django.conf import settings
from rest_framework.exceptions import PermissionDenied
from registry import publish_release, get_port, RegistryException
from registry.dockerclient import DockerClient
@mock.patch('docker.Client')
class DockerClientTest(unittest.TestCase):
"""Test that the client makes appropriate Docker engine API calls."""
def setUp(self):
settings.REGISTRY_HOST, settings.REGISTRY_PORT = 'localhost', 5000
def test_get_port(self, mock_client):
self.client = DockerClient()
# Make sure login is not called when there are no creds
get_port('ozzy/embryo:git-f2a8020', False)
self.assertFalse(self.client.client.login.called)
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
client = {}
client['Status'] = 'Login Succeeded'
self.client.client.login.return_value = client
get_port('ozzy/embryo:git-f2a8020', False, creds)
self.assertTrue(self.client.client.login.called)
self.assertTrue(self.client.client.pull.called)
self.assertTrue(self.client.client.inspect_image.called)
def test_publish_release(self, mock_client):
self.client = DockerClient()
# Make sure login is not called when there are no creds
publish_release('ozzy/embryo:git-f2a8020', 'ozzy/embryo:v4', False)
self.assertFalse(self.client.client.login.called)
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
client = {}
client['Status'] = 'Login Succeeded'
self.client.client.login.return_value = client
publish_release('ozzy/embryo:git-f2a8020', 'ozzy/embryo:v4', False, creds)
self.assertTrue(self.client.client.login.called)
self.assertTrue(self.client.client.pull.called)
self.assertTrue(self.client.client.tag.called)
self.assertTrue(self.client.client.push.called)
publish_release('ozzy/embryo:git-f2a8020', 'ozzy/embryo:v4', True)
self.assertTrue(self.client.client.pull.called)
self.assertTrue(self.client.client.tag.called)
self.assertTrue(self.client.client.push.called)
# Test that a registry host prefix is replaced with deis-registry for the target
publish_release('ozzy/embryo:git-f2a8020', 'quay.io/ozzy/embryo:v4', True)
docker_push = self.client.client.push
docker_push.assert_called_with(
'localhost:5000/ozzy/embryo', tag='v4', decode=True, stream=True
)
# Test that blacklisted image names can't be published
with self.assertRaises(PermissionDenied):
publish_release(
'deis/controller:v1.11.1', 'deis/controller:v1.11.1', True)
with self.assertRaises(PermissionDenied):
publish_release(
'localhost:5000/deis/controller:v1.11.1', 'deis/controller:v1.11.1', True)
def test_login(self, mock_client):
self.client = DockerClient()
# success
client = {}
client['Status'] = 'Login Succeeded'
self.client.client.login.return_value = client
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
self.client.login('quay.io/deis/foobar', creds)
docker_login = self.client.client.login
docker_login.assert_called_with(
username='fake', password='fake',
email='fake', registry='quay.io'
)
# username matches
client = {}
client['username'] = 'fake'
self.client.client.login.return_value = client
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
self.client.login('quay.io/deis/foobar', creds)
docker_login = self.client.client.login
docker_login.assert_called_with(
username='fake', password='fake',
email='fake', registry='quay.io'
)
def test_login_failed(self, mock_client):
self.client = DockerClient()
# failed login
client = {}
client['Status'] = 'Login Failed'
self.client.client.login.return_value = client
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
with self.assertRaises(PermissionDenied):
self.client.login('quay.io/deis/foobar', creds)
docker_login = self.client.client.login
docker_login.assert_called_with(
username='fake', password='fake',
email='fake', registry='quay.io'
)
def test_login_bad_creds(self, mock_client):
self.client = DockerClient()
# missing parts of credentials
with self.assertRaises(PermissionDenied):
creds = {
'username': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
self.client.login('quay.io/deis/foobar', creds)
# bad credentials
with self.assertRaises(PermissionDenied):
creds = {
'username': 'fake',
'password': 'fake',
'email': 'fake',
'registry': 'quay.io'
}
self.client.login('quay.io/deis/foobar', creds)
def test_pull(self, mock_client):
self.client = DockerClient()
self.client.pull('alpine', '3.2')
docker_pull = self.client.client.pull
docker_pull.assert_called_once_with('alpine', tag='3.2', decode=True, stream=True)
# Test that blacklisted image names can't be pulled
with self.assertRaises(PermissionDenied):
self.client.pull('deis/controller', 'v1.11.1')
with self.assertRaises(PermissionDenied):
self.client.pull('localhost:5000/deis/controller', 'v1.11.1')
def test_push(self, mock_client):
self.client = DockerClient()
self.client.push('ozzy/embryo', 'v4')
docker_push = self.client.client.push
docker_push.assert_called_once_with('ozzy/embryo', tag='v4', decode=True, stream=True)
def test_tag(self, mock_client):
self.client = DockerClient()
self.client.tag('ozzy/embryo:git-f2a8020', 'ozzy/embryo', 'v4')
docker_tag = self.client.client.tag
docker_tag.assert_called_once_with(
'ozzy/embryo:git-f2a8020', 'ozzy/embryo', tag='v4', force=True)
# fake failed tag
self.client.client.tag.return_value = False
with self.assertRaises(RegistryException):
self.client.tag('foo/bar:latest', 'foo/bar', 'v1.11.1')
# Test that blacklisted image names can't be tagged
with self.assertRaises(PermissionDenied):
self.client.tag('deis/controller:v1.11.1', 'deis/controller', 'v1.11.1')
with self.assertRaises(PermissionDenied):
self.client.tag('localhost:5000/deis/controller:v1.11.1', 'deis/controller', 'v1.11.1')