root / snf-cyclades-app / synnefo / api / tests / flavors.py @ 9582e68e
History | View | Annotate | Download (5.9 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import json |
35 |
|
36 |
from snf_django.utils.testing import BaseAPITest |
37 |
from synnefo.db.models import Flavor |
38 |
from synnefo.db.models_factory import FlavorFactory |
39 |
from synnefo.lib.services import get_service_path |
40 |
from synnefo.cyclades_settings import cyclades_services |
41 |
from synnefo.lib import join_urls |
42 |
|
43 |
|
44 |
class FlavorAPITest(BaseAPITest): |
45 |
|
46 |
def setUp(self): |
47 |
self.flavor1 = FlavorFactory()
|
48 |
self.flavor2 = FlavorFactory(deleted=True) |
49 |
self.flavor3 = FlavorFactory()
|
50 |
self.compute_path = get_service_path(cyclades_services, 'compute', |
51 |
version='v2.0')
|
52 |
|
53 |
def myget(self, path): |
54 |
path = join_urls(self.compute_path, path)
|
55 |
return self.get(path) |
56 |
|
57 |
def test_flavor_list(self): |
58 |
"""Test if the expected list of flavors is returned."""
|
59 |
response = self.myget('flavors') |
60 |
self.assertSuccess(response)
|
61 |
|
62 |
api_flavors = json.loads(response.content)['flavors']
|
63 |
db_flavors = Flavor.objects.filter(deleted=False)
|
64 |
self.assertEqual(len(api_flavors), len(db_flavors)) |
65 |
for api_flavor in api_flavors: |
66 |
db_flavor = Flavor.objects.get(id=api_flavor['id'])
|
67 |
self.assertEqual(api_flavor['id'], db_flavor.id) |
68 |
self.assertEqual(api_flavor['name'], db_flavor.name) |
69 |
|
70 |
def test_flavors_details(self): |
71 |
"""Test if the flavors details are returned."""
|
72 |
response = self.myget('flavors/detail') |
73 |
self.assertSuccess(response)
|
74 |
|
75 |
db_flavors = Flavor.objects.filter(deleted=False)
|
76 |
api_flavors = json.loads(response.content)['flavors']
|
77 |
|
78 |
self.assertEqual(len(db_flavors), len(api_flavors)) |
79 |
|
80 |
for i in range(0, len(db_flavors)): |
81 |
api_flavor = api_flavors[i] |
82 |
db_flavor = Flavor.objects.get(id=db_flavors[i].id) |
83 |
self.assertEqual(api_flavor['vcpus'], db_flavor.cpu) |
84 |
self.assertEqual(api_flavor['id'], db_flavor.id) |
85 |
self.assertEqual(api_flavor['disk'], db_flavor.disk) |
86 |
self.assertEqual(api_flavor['name'], db_flavor.name) |
87 |
self.assertEqual(api_flavor['ram'], db_flavor.ram) |
88 |
self.assertEqual(api_flavor['SNF:disk_template'], |
89 |
db_flavor.disk_template) |
90 |
|
91 |
def test_flavor_details(self): |
92 |
"""Test if the expected flavor is returned."""
|
93 |
flavor = self.flavor3
|
94 |
|
95 |
response = self.myget('flavors/%d' % flavor.id) |
96 |
self.assertSuccess(response)
|
97 |
|
98 |
api_flavor = json.loads(response.content)['flavor']
|
99 |
db_flavor = Flavor.objects.get(id=flavor.id) |
100 |
self.assertEqual(api_flavor['vcpus'], db_flavor.cpu) |
101 |
self.assertEqual(api_flavor['id'], db_flavor.id) |
102 |
self.assertEqual(api_flavor['disk'], db_flavor.disk) |
103 |
self.assertEqual(api_flavor['name'], db_flavor.name) |
104 |
self.assertEqual(api_flavor['ram'], db_flavor.ram) |
105 |
self.assertEqual(api_flavor['SNF:disk_template'], |
106 |
db_flavor.disk_template) |
107 |
|
108 |
def test_deleted_flavor_details(self): |
109 |
"""Test that API returns details for deleted flavors"""
|
110 |
flavor = self.flavor2
|
111 |
response = self.myget('flavors/%d' % flavor.id) |
112 |
self.assertSuccess(response)
|
113 |
api_flavor = json.loads(response.content)['flavor']
|
114 |
self.assertEquals(api_flavor['name'], flavor.name) |
115 |
|
116 |
def test_deleted_flavors_list(self): |
117 |
"""Test that deleted flavors do not appear to flavors list"""
|
118 |
response = self.myget('flavors') |
119 |
self.assertSuccess(response)
|
120 |
api_flavors = json.loads(response.content)['flavors']
|
121 |
self.assertEqual(len(api_flavors), 2) |
122 |
|
123 |
def test_deleted_flavors_details(self): |
124 |
"""Test that deleted flavors do not appear to flavors detail list"""
|
125 |
FlavorFactory(deleted=True)
|
126 |
response = self.myget('flavors/detail') |
127 |
self.assertSuccess(response)
|
128 |
api_flavors = json.loads(response.content)['flavors']
|
129 |
self.assertEqual(len(api_flavors), 2) |
130 |
|
131 |
def test_wrong_flavor(self): |
132 |
"""Test 404 result when requesting a flavor that does not exist."""
|
133 |
|
134 |
flavor_id = max(Flavor.objects.values_list('id', flat=True)) + 1 |
135 |
response = self.myget('flavors/%d' % flavor_id) |
136 |
self.assertItemNotFound(response)
|
137 |
|
138 |
def test_catch_wrong_api_paths(self, *args): |
139 |
response = self.myget('nonexistent') |
140 |
self.assertEqual(response.status_code, 400) |
141 |
try:
|
142 |
json.loads(response.content) |
143 |
except ValueError: |
144 |
self.assertTrue(False) |