Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / test / flavors.py @ d2b8ec7b

History | View | Annotate | Download (5.3 kB)

1
# Copyright 2012 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

    
40

    
41
class FlavorAPITest(BaseAPITest):
42

    
43
    def setUp(self):
44
        self.flavor1 = FlavorFactory()
45
        self.flavor2 = FlavorFactory(deleted=True)
46
        self.flavor3 = FlavorFactory()
47

    
48
    def test_flavor_list(self):
49
        """Test if the expected list of flavors is returned."""
50
        response = self.get('/api/v1.1/flavors')
51
        self.assertSuccess(response)
52

    
53
        api_flavors = json.loads(response.content)['flavors']['values']
54
        db_flavors = Flavor.objects.filter(deleted=False)
55
        self.assertEqual(len(api_flavors), len(db_flavors))
56
        for api_flavor in api_flavors:
57
            db_flavor = Flavor.objects.get(id=api_flavor['id'])
58
            self.assertEqual(api_flavor['id'], db_flavor.id)
59
            self.assertEqual(api_flavor['name'], db_flavor.name)
60

    
61
    def test_flavors_details(self):
62
        """Test if the flavors details are returned."""
63
        response = self.get('/api/v1.1/flavors/detail')
64
        self.assertSuccess(response)
65

    
66
        db_flavors = Flavor.objects.filter(deleted=False)
67
        api_flavors = json.loads(response.content)['flavors']['values']
68

    
69
        self.assertEqual(len(db_flavors), len(api_flavors))
70

    
71
        for i in range(0, len(db_flavors)):
72
            api_flavor = api_flavors[i]
73
            db_flavor = Flavor.objects.get(id=db_flavors[i].id)
74
            self.assertEqual(api_flavor['cpu'], db_flavor.cpu)
75
            self.assertEqual(api_flavor['id'], db_flavor.id)
76
            self.assertEqual(api_flavor['disk'], db_flavor.disk)
77
            self.assertEqual(api_flavor['name'], db_flavor.name)
78
            self.assertEqual(api_flavor['ram'], db_flavor.ram)
79
            self.assertEqual(api_flavor['SNF:disk_template'],
80
                                        db_flavor.disk_template)
81

    
82
    def test_flavor_details(self):
83
        """Test if the expected flavor is returned."""
84
        flavor = self.flavor3
85

    
86
        response = self.get('/api/v1.1/flavors/%d' % flavor.id)
87
        self.assertSuccess(response)
88

    
89
        api_flavor = json.loads(response.content)['flavor']
90
        db_flavor = Flavor.objects.get(id=flavor.id)
91
        self.assertEqual(api_flavor['cpu'], db_flavor.cpu)
92
        self.assertEqual(api_flavor['id'], db_flavor.id)
93
        self.assertEqual(api_flavor['disk'], db_flavor.disk)
94
        self.assertEqual(api_flavor['name'], db_flavor.name)
95
        self.assertEqual(api_flavor['ram'], db_flavor.ram)
96
        self.assertEqual(api_flavor['SNF:disk_template'],
97
                         db_flavor.disk_template)
98

    
99
    def test_deleted_flavor_details(self):
100
        """Test that API returns details for deleted flavors"""
101
        flavor = self.flavor2
102
        response = self.get('/api/v1.1/flavors/%d' % flavor.id)
103
        self.assertSuccess(response)
104
        api_flavor = json.loads(response.content)['flavor']
105
        self.assertEquals(api_flavor['name'], flavor.name)
106

    
107
    def test_deleted_flavors_list(self):
108
        """Test that deleted flavors do not appear to flavors list"""
109
        response = self.get('/api/v1.1/flavors')
110
        self.assertSuccess(response)
111
        api_flavors = json.loads(response.content)['flavors']['values']
112
        self.assertEqual(len(api_flavors), 2)
113

    
114
    def test_deleted_flavors_details(self):
115
        """Test that deleted flavors do not appear to flavors detail list"""
116
        FlavorFactory(deleted=True)
117
        response = self.get('/api/v1.1/flavors/detail')
118
        self.assertSuccess(response)
119
        api_flavors = json.loads(response.content)['flavors']['values']
120
        self.assertEqual(len(api_flavors), 2)
121

    
122
    def test_wrong_flavor(self):
123
        """Test 404 result when requesting a flavor that does not exist."""
124

    
125
        response = self.get('/api/v1.1/flavors/%d' % 22)
126
        self.assertItemNotFound(response)