Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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
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

    
54
    def myget(self, path):
55
        path = join_urls(self.compute_path, path)
56
        return self.get(path)
57

    
58

    
59
    def test_flavor_list(self):
60
        """Test if the expected list of flavors is returned."""
61
        response = self.myget('flavors')
62
        self.assertSuccess(response)
63

    
64
        api_flavors = json.loads(response.content)['flavors']
65
        db_flavors = Flavor.objects.filter(deleted=False)
66
        self.assertEqual(len(api_flavors), len(db_flavors))
67
        for api_flavor in api_flavors:
68
            db_flavor = Flavor.objects.get(id=api_flavor['id'])
69
            self.assertEqual(api_flavor['id'], db_flavor.id)
70
            self.assertEqual(api_flavor['name'], db_flavor.name)
71

    
72
    def test_flavors_details(self):
73
        """Test if the flavors details are returned."""
74
        response = self.myget('flavors/detail')
75
        self.assertSuccess(response)
76

    
77
        db_flavors = Flavor.objects.filter(deleted=False)
78
        api_flavors = json.loads(response.content)['flavors']
79

    
80
        self.assertEqual(len(db_flavors), len(api_flavors))
81

    
82
        for i in range(0, len(db_flavors)):
83
            api_flavor = api_flavors[i]
84
            db_flavor = Flavor.objects.get(id=db_flavors[i].id)
85
            self.assertEqual(api_flavor['vcpus'], db_flavor.cpu)
86
            self.assertEqual(api_flavor['id'], db_flavor.id)
87
            self.assertEqual(api_flavor['disk'], db_flavor.disk)
88
            self.assertEqual(api_flavor['name'], db_flavor.name)
89
            self.assertEqual(api_flavor['ram'], db_flavor.ram)
90
            self.assertEqual(api_flavor['SNF:disk_template'],
91
                                        db_flavor.disk_template)
92

    
93
    def test_flavor_details(self):
94
        """Test if the expected flavor is returned."""
95
        flavor = self.flavor3
96

    
97
        response = self.myget('flavors/%d' % flavor.id)
98
        self.assertSuccess(response)
99

    
100
        api_flavor = json.loads(response.content)['flavor']
101
        db_flavor = Flavor.objects.get(id=flavor.id)
102
        self.assertEqual(api_flavor['vcpus'], db_flavor.cpu)
103
        self.assertEqual(api_flavor['id'], db_flavor.id)
104
        self.assertEqual(api_flavor['disk'], db_flavor.disk)
105
        self.assertEqual(api_flavor['name'], db_flavor.name)
106
        self.assertEqual(api_flavor['ram'], db_flavor.ram)
107
        self.assertEqual(api_flavor['SNF:disk_template'],
108
                         db_flavor.disk_template)
109

    
110
    def test_deleted_flavor_details(self):
111
        """Test that API returns details for deleted flavors"""
112
        flavor = self.flavor2
113
        response = self.myget('flavors/%d' % flavor.id)
114
        self.assertSuccess(response)
115
        api_flavor = json.loads(response.content)['flavor']
116
        self.assertEquals(api_flavor['name'], flavor.name)
117

    
118
    def test_deleted_flavors_list(self):
119
        """Test that deleted flavors do not appear to flavors list"""
120
        response = self.myget('flavors')
121
        self.assertSuccess(response)
122
        api_flavors = json.loads(response.content)['flavors']
123
        self.assertEqual(len(api_flavors), 2)
124

    
125
    def test_deleted_flavors_details(self):
126
        """Test that deleted flavors do not appear to flavors detail list"""
127
        FlavorFactory(deleted=True)
128
        response = self.myget('flavors/detail')
129
        self.assertSuccess(response)
130
        api_flavors = json.loads(response.content)['flavors']
131
        self.assertEqual(len(api_flavors), 2)
132

    
133
    def test_wrong_flavor(self):
134
        """Test 404 result when requesting a flavor that does not exist."""
135

    
136
        # XXX: flavors/22 below fails for no apparent reason
137
        response = self.myget('flavors/%d' % 23)
138
        self.assertItemNotFound(response)
139

    
140
    def test_catch_wrong_api_paths(self, *args):
141
        response = self.myget('nonexistent')
142
        self.assertEqual(response.status_code, 400)
143
        try:
144
            error = json.loads(response.content)
145
        except ValueError:
146
            self.assertTrue(False)