55"""
66
77import unittest
8- try :
9- from unittest import mock
10- except ImportError :
11- import mock
8+ from unittest import mock
129
1310from django .conf import settings
1411from rest_framework .exceptions import PermissionDenied
12+ from registry import publish_release , RegistryException
1513from registry .dockerclient import DockerClient
1614
1715
@@ -24,24 +22,131 @@ def setUp(self):
2422
2523 def test_publish_release (self , mock_client ):
2624 self .client = DockerClient ()
27- self .client .publish_release ('ozzy/embryo:git-f2a8020' , 'ozzy/embryo:v4' , True )
25+
26+ # Make sure login is not called when there are no creds
27+ publish_release ('ozzy/embryo:git-f2a8020' , 'ozzy/embryo:v4' , False )
28+ self .assertFalse (self .client .client .login .called )
29+
30+ creds = {
31+ 'username' : 'fake' ,
32+ 'password' : 'fake' ,
33+ 'email' : 'fake' ,
34+ 'registry' : 'quay.io'
35+ }
36+
37+ client = {}
38+ client ['Status' ] = 'Login Succeeded'
39+ self .client .client .login .return_value = client
40+ publish_release ('ozzy/embryo:git-f2a8020' , 'ozzy/embryo:v4' , False , creds )
41+ self .assertTrue (self .client .client .login .called )
42+ self .assertTrue (self .client .client .pull .called )
43+ self .assertTrue (self .client .client .tag .called )
44+ self .assertTrue (self .client .client .push .called )
45+
46+ publish_release ('ozzy/embryo:git-f2a8020' , 'ozzy/embryo:v4' , True )
2847 self .assertTrue (self .client .client .pull .called )
2948 self .assertTrue (self .client .client .tag .called )
3049 self .assertTrue (self .client .client .push .called )
50+
3151 # Test that a registry host prefix is replaced with deis-registry for the target
32- self . client . publish_release ('ozzy/embryo:git-f2a8020' , 'quay.io/ozzy/embryo:v4' , True )
52+ publish_release ('ozzy/embryo:git-f2a8020' , 'quay.io/ozzy/embryo:v4' , True )
3353 docker_push = self .client .client .push
3454 docker_push .assert_called_with (
3555 'localhost:5000/ozzy/embryo' , tag = 'v4' , insecure_registry = True ,
3656 decode = True , stream = True )
57+
3758 # Test that blacklisted image names can't be published
3859 with self .assertRaises (PermissionDenied ):
39- self . client . publish_release (
60+ publish_release (
4061 'deis/controller:v1.11.1' , 'deis/controller:v1.11.1' , True )
4162 with self .assertRaises (PermissionDenied ):
42- self . client . publish_release (
63+ publish_release (
4364 'localhost:5000/deis/controller:v1.11.1' , 'deis/controller:v1.11.1' , True )
4465
66+ def test_login (self , mock_client ):
67+ self .client = DockerClient ()
68+
69+ # success
70+ client = {}
71+ client ['Status' ] = 'Login Succeeded'
72+ self .client .client .login .return_value = client
73+
74+ creds = {
75+ 'username' : 'fake' ,
76+ 'password' : 'fake' ,
77+ 'email' : 'fake' ,
78+ 'registry' : 'quay.io'
79+ }
80+ self .client .login ('quay.io/deis/foobar' , creds )
81+ docker_login = self .client .client .login
82+ docker_login .assert_called_with (
83+ username = 'fake' , password = 'fake' ,
84+ email = 'fake' , registry = 'quay.io'
85+ )
86+
87+ # username matches
88+ client = {}
89+ client ['username' ] = 'fake'
90+ self .client .client .login .return_value = client
91+
92+ creds = {
93+ 'username' : 'fake' ,
94+ 'password' : 'fake' ,
95+ 'email' : 'fake' ,
96+ 'registry' : 'quay.io'
97+ }
98+ self .client .login ('quay.io/deis/foobar' , creds )
99+ docker_login = self .client .client .login
100+ docker_login .assert_called_with (
101+ username = 'fake' , password = 'fake' ,
102+ email = 'fake' , registry = 'quay.io'
103+ )
104+
105+ def test_login_failed (self , mock_client ):
106+ self .client = DockerClient ()
107+
108+ # failed login
109+ client = {}
110+ client ['Status' ] = 'Login Failed'
111+ self .client .client .login .return_value = client
112+
113+ creds = {
114+ 'username' : 'fake' ,
115+ 'password' : 'fake' ,
116+ 'email' : 'fake' ,
117+ 'registry' : 'quay.io'
118+ }
119+
120+ with self .assertRaises (PermissionDenied ):
121+ self .client .login ('quay.io/deis/foobar' , creds )
122+ docker_login = self .client .client .login
123+ docker_login .assert_called_with (
124+ username = 'fake' , password = 'fake' ,
125+ email = 'fake' , registry = 'quay.io'
126+ )
127+
128+ def test_login_bad_creds (self , mock_client ):
129+ self .client = DockerClient ()
130+
131+ # missing parts of credentials
132+ with self .assertRaises (PermissionDenied ):
133+ creds = {
134+ 'username' : 'fake' ,
135+ 'email' : 'fake' ,
136+ 'registry' : 'quay.io'
137+ }
138+ self .client .login ('quay.io/deis/foobar' , creds )
139+
140+ # bad credentials
141+ with self .assertRaises (PermissionDenied ):
142+ creds = {
143+ 'username' : 'fake' ,
144+ 'password' : 'fake' ,
145+ 'email' : 'fake' ,
146+ 'registry' : 'quay.io'
147+ }
148+ self .client .login ('quay.io/deis/foobar' , creds )
149+
45150 def test_pull (self , mock_client ):
46151 self .client = DockerClient ()
47152 self .client .pull ('alpine' , '3.2' )
@@ -67,8 +172,15 @@ def test_tag(self, mock_client):
67172 docker_tag = self .client .client .tag
68173 docker_tag .assert_called_once_with (
69174 'ozzy/embryo:git-f2a8020' , 'ozzy/embryo' , tag = 'v4' , force = True )
175+
176+ # fake failed tag
177+ self .client .client .tag .return_value = False
178+ with self .assertRaises (RegistryException ):
179+ self .client .tag ('foo/bar:latest' , 'foo/bar' , 'v1.11.1' )
180+
70181 # Test that blacklisted image names can't be tagged
71182 with self .assertRaises (PermissionDenied ):
72183 self .client .tag ('deis/controller:v1.11.1' , 'deis/controller' , 'v1.11.1' )
184+
73185 with self .assertRaises (PermissionDenied ):
74186 self .client .tag ('localhost:5000/deis/controller:v1.11.1' , 'deis/controller' , 'v1.11.1' )
0 commit comments