-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_resource.py
More file actions
127 lines (111 loc) · 4.55 KB
/
test_resource.py
File metadata and controls
127 lines (111 loc) · 4.55 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
# -*- coding: utf-8 -*-
"""
Unit tests for the Drycc api app.
Run the tests with "./manage.py test api"
"""
from django.contrib.auth.models import User
from django.core.cache import cache
from django.conf import settings
from rest_framework.authtoken.models import Token
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
@requests_mock.Mocker(real_http=True, adapter=adapter)
class ResourceTest(DryccTransactionTestCase):
"""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)
self.app_id = self.create_app()
def tearDown(self):
# Restore default tags to empty string
settings.DRYCC_DEFAULT_CONFIG_TAGS = ''
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_resources_create(self, mock_requests):
"""Test that the serialized response contains only relevant data."""
app_id = self.create_app()
response = self.client.post(
'/v2/apps/{}/resources'.format(app_id),
data={'name': 'mysql', 'plan': 'mysql:5.6'}
)
self.assertEqual(response.status_code, 201, response.data)
for key in response.data:
self.assertIn(key,
['uuid', 'owner', 'created', 'updated', 'app', 'plan',
'options', 'data', 'status', 'binding', 'name'])
expected = {
'owner': self.user.username,
'app': app_id,
'name': 'mysql',
'plan': 'mysql:5.6'
}
self.assertDictContainsSubset(expected, response.data)
def test_resources_list(self, mock_requests):
"""
Test that list resources from a app
"""
# create
app_id = self.create_app()
data = [
{'name': 'mysql', 'plan': 'mysql:5.6'}
]
for _ in data:
self.client.post('/v2/apps/{}/resources'.format(app_id), data=_)
# Fetch
url = '/v2/apps/{app_id}/resources'.format(app_id=app_id)
response = self.client.get(url)
expected = [res['name'] for res in response.data['results']]
self.assertEqual(sorted([_['name'] for _ in data]), sorted(expected))
def test_resource_get(self, mock_requests):
"""
Test that resource is get detail from a app
"""
# create
app_id = self.create_app()
data = {'name': 'mysql', 'plan': 'mysql:5.6'}
self.client.post('/v2/apps/{}/resources/'.format(app_id), data=data)
# Get
url = '/v2/apps/{app_id}/resources/{name}/'.format(app_id=app_id,
name='mysql')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_resource_delete(self, mock_requests):
"""
Test that resource is delete from a app
"""
# create
app_id = self.create_app()
data = [
{'name': 'mysql', 'plan': 'mysql:5.6'}
]
for _ in data:
self.client.post('/v2/apps/{}/resources'.format(app_id), data=_)
# Delete
url = '/v2/apps/{app_id}/resources/{name}'.format(app_id=app_id,
name='mysql')
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
def test_resource_bind(self, mock_requests):
# create
app_id = self.create_app()
data = {'name': 'mysql', 'plan': 'mysql:5.6'}
self.client.post('/v2/apps/{}/resources'.format(app_id), data=data)
# bind
url = '/v2/apps/{app_id}/resources/mysql/binding/'.format(app_id=app_id)
data = {"bind_action": "bind"}
response = self.client.patch(url, data=data)
# expected = response.data['path']
self.assertEqual(response.status_code, 400)
def test_resource_unbind(self, mock_requests):
# create
app_id = self.create_app()
data = {'name': 'mysql', 'plan': 'mysql:5.6'}
self.client.post('/v2/apps/{}/resources'.format(app_id), data=data)
# unbind
url = '/v2/apps/{app_id}/resources/mysql/binding/'.format(app_id=app_id)
data = {"bind_action": "unbind"}
response = self.client.patch(url, data=data)
# expected = response.data['path']
self.assertEqual(response.status_code, 400)