Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / neutron / tests / api.py @ f261896d

History | View | Annotate | Download (4.2 kB)

1
from snf_django.utils.testing import BaseAPITest
2
from django.utils import simplejson as json
3
from synnefo.cyclades_settings import cyclades_services
4
from synnefo.lib.services import get_service_path
5
from synnefo.lib import join_urls
6

    
7
import synnefo.neutron.models_factory as mf
8

    
9
NEUTRON_URL = get_service_path(cyclades_services, "neutron", "v2.0")
10
NETWORKS_URL = join_urls(NEUTRON_URL, "networks/")
11
SUBNETS_URL = join_urls(NEUTRON_URL, "subnets/")
12

    
13

    
14
class NetworkTest(BaseAPITest):
15

    
16
    def test_list_networks(self):
17
        response = self.get(NETWORKS_URL)
18
        self.assertSuccess(response)
19
        networks = json.loads(response.content)
20
        self.assertEqual(networks, {'networks': []})
21

    
22
    def test_create_network(self):
23
        request = {"network":
24
                    {"name": "sample_network"}
25
                }
26
        response = self.post(NETWORKS_URL, params=json.dumps(request))
27
        code = response.status_code
28
        self.assertEqual(code, 201)
29

    
30

    
31
    def test_get_unfound_network(self):
32
        url = join_urls(NETWORKS_URL,"123")
33
        response = self.get(url)
34
        self.assertItemNotFound(response)
35

    
36
    def test_get_network(self):
37
        test_net = mf.NetworkFactory.create()
38
        url = join_urls(NETWORKS_URL,str(test_net.id))
39
        response = self.get(url, user=test_net.userid)
40
        self.assertEqual(response.status_code, 200)
41

    
42
    def test_delete_unfound_network(self):
43
        url = join_urls(NETWORKS_URL,"123")
44
        response = self.delete(url)
45
        self.assertItemNotFound(response)
46

    
47
    def test_delete_network(self):
48
        test_net = mf.NetworkFactory.create()
49
        url = join_urls(NETWORKS_URL,str(test_net.id))
50
        response = self.delete(url, user=test_net.userid)
51
        self.assertEqual(response.status_code, 204)
52

    
53
    def test_put_unfound_network(self):
54
        url = join_urls(NETWORKS_URL,"123")
55
        response = self.delete(url)
56
        self.assertItemNotFound(response)
57

    
58
    def test_put_network(self):
59
        test_net = mf.NetworkFactory.create()
60
        url = join_urls(NETWORKS_URL,str(test_net.id))
61
        request = {"network":
62
            {"name": "new_name"}
63
        }
64
        response = self.put(url,params=json.dumps(request),user=test_net.userid)
65
        self.assertEqual(response.status_code, 200)
66

    
67
    def test_put_network_wrong(self):
68
        test_net = mf.NetworkFactory.create()
69
        url = join_urls(NETWORKS_URL,str(test_net.id))
70
        request = {"network":
71
            {"wronng_field": "new_name"}
72
        }
73
        response = self.put(url,params=json.dumps(request),user=test_net.userid)
74
        self.assertEqual(response.status_code, 400)
75

    
76

    
77
class SubnetTest(BaseAPITest):
78
    def test_list_subnets(self):
79
        '''Test Subnet list'''
80
        response = self.get(SUBNETS_URL)
81
        self.assertSuccess(response)
82
        subnets = json.loads(response.content)
83
        self.assertEqual(subnets, {'subnets': []})
84

    
85
    def test_get_subnet(self):
86
        '''Test get info of a subnet'''
87
        url = join_urls(SUBNETS_URL, '42')
88
        response = self.get(url)
89
        self.assertSuccess(response)
90
        subnet = json.loads(response.content)
91
        self.assertEqual(subnet, {'subnet': []})
92

    
93
    def test_get_subnet_404(self):
94
        '''Test get info of a subnet that doesn't exist'''
95
        url = join_urls(SUBNETS_URL, '52')
96
        response = self.get(url)
97
        self.assertItemNotFound(response)
98

    
99
    def test_subnet_delete_not_found(self):
100
        '''Test delete a subnet that doesn't exist'''
101
        url = join_urls(SUBNETS_URL, '52')
102
        response = self.get(url)
103
        self.assertItemNotFound(response)
104

    
105
    def test_subnet_delete(self):
106
        '''Test delete a subnet that's not in use'''
107
        url = join_urls(SUBNETS_URL, '42')
108
        response = self.get(url)
109
        self.assertItemNotFound(response)
110

    
111
    def test_create_subnet_with_malformed_ipversion(self):
112
        '''Create a subnet with a malformed ip_version type'''
113
        # PEP8 for {{}}?
114
        request = {'subnet': {'network_id':
115
                              'ed2e3c10-2e43-4297-9006-2863a2d1abbc',
116
                              'cidr': '10.0.3.0/24',
117
                              'ip_version': 8}}
118

    
119
        response = self.post(SUBNETS_URL, "user9", json.dumps(request), "json")
120
        self.assertBadRequest(response)