-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_registry.py
More file actions
142 lines (120 loc) · 5.48 KB
/
test_registry.py
File metadata and controls
142 lines (120 loc) · 5.48 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
import json
import requests_mock
from django.core.cache import cache
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from api.tests import adapter, DeisTransactionTestCase
@requests_mock.Mocker(real_http=True, adapter=adapter)
class TestRegistry(DeisTransactionTestCase):
"""Tests setting and updating config values"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = Token.objects.get(user=self.user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_registry(self, mock_requests):
"""
Test that registry information can be set on an application
"""
app_id = self.create_app()
# check default
url = '/v2/apps/{app_id}/config'.format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('registry', response.data)
self.assertEqual(response.data['registry'], {})
# set some registry information without PORT
body = {'registry': json.dumps({'username': 'bob'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
registry1 = response.data
# set required PORT
body = {'values': json.dumps({'PORT': '80'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry1 = response.data
# set some registry information
body = {'registry': json.dumps({'username': 'bob'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry1 = response.data
# check registry information again
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('registry', response.data)
registry = response.data['registry']
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bob')
# set an additional value
# set them upper case, internally it should translate to lower
body = {'registry': json.dumps({'PASSWORD': 's3cur3pw1'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry2 = response.data
self.assertNotEqual(registry1['uuid'], registry2['uuid'])
registry = response.data['registry']
self.assertIn('password', registry)
self.assertEqual(registry['password'], 's3cur3pw1')
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bob')
# read the registry information again
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
registry3 = response.data
self.assertEqual(registry2, registry3)
registry = response.data['registry']
self.assertIn('password', registry)
self.assertEqual(registry['password'], 's3cur3pw1')
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bob')
# unset a value
body = {'registry': json.dumps({'password': None})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry4 = response.data
self.assertNotEqual(registry3['uuid'], registry4['uuid'])
self.assertNotIn('password', json.dumps(response.data['registry']))
# bad registry key values
body = {'registry': json.dumps({'pa$$w0rd': 'woop'})}
response = self.client.post(url, body)
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
# disallow put/patch/delete
response = self.client.put(url)
self.assertEqual(response.status_code, 405, response.data)
response = self.client.patch(url)
self.assertEqual(response.status_code, 405, response.data)
response = self.client.delete(url)
self.assertEqual(response.status_code, 405, response.data)
def test_registry_deploy(self, mock_requests):
"""
Test that registry information can be applied
"""
app_id = self.create_app()
# Set mandatory PORT
response = self.client.post(
'/v2/apps/{app_id}/config'.format(**locals()),
{'values': json.dumps({'PORT': '4999'})}
)
# Set registry information
body = {'registry': json.dumps({
'username': 'bob',
'password': 's3cur3pw1'
})}
response = self.client.post(
'/v2/apps/{app_id}/config'.format(**locals()),
body
)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('username', response.data['registry'])
self.assertIn('password', response.data['registry'])
self.assertEqual(response.data['registry']['username'], 'bob')
self.assertEqual(response.data['registry']['password'], 's3cur3pw1')
# post a new build
url = "/v2/apps/{app_id}/builds".format(**locals())
body = {'image': 'autotest/example'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)