Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / tests / ports.py @ 5db2001a

History | View | Annotate | Download (5.6 kB)

1
# Copyright 2011-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.i
33

    
34
from snf_django.utils.testing import (BaseAPITest, override_settings)
35
from snf_django.utils.testing import BaseAPITest
36
from django.utils import simplejson as json
37
from synnefo.cyclades_settings import cyclades_services
38
from synnefo.lib.services import get_service_path
39
from synnefo.lib import join_urls
40
import synnefo.db.models_factory as dbmf
41

    
42
COMPUTE_URL = get_service_path(cyclades_services, 'compute',
43
                               version='v2.0')
44
PORTS_URL = join_urls(COMPUTE_URL, "ports/")
45

    
46

    
47
class PortTest(BaseAPITest):
48
    def test_get_ports(self):
49
        url = join_urls(PORTS_URL)
50
        response = self.get(url)
51
        self.assertEqual(response.status_code, 200)
52
        ports = json.loads(response.content)
53
        self.assertEqual(ports, {"ports": []})
54

    
55
    def test_get_port_unfound(self):
56
        url = join_urls(PORTS_URL, "123")
57
        response = self.get(url)
58
        self.assertEqual(response.status_code, 404)
59

    
60
    def test_get_port(self):
61
        nic = dbmf.NetworkInterfaceFactory.create()
62
        url = join_urls(PORTS_URL, str(nic.id))
63
        response = self.get(url, user=nic.network.userid)
64
        self.assertEqual(response.status_code, 200)
65

    
66
    def test_delete_port(self):
67
        nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
68
        url = join_urls(PORTS_URL, str(nic.id))
69
        response = self.delete(url, user=nic.network.userid)
70
        self.assertEqual(response.status_code, 204)
71

    
72
    def test_update_port_name(self):
73
        nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
74
        url = join_urls(PORTS_URL, str(nic.id))
75
        request = {'port': {"name": "test-name"}}
76
        response = self.put(url, params=json.dumps(request),
77
                            user=nic.network.userid)
78
        self.assertEqual(response.status_code, 200)
79
        res = json.loads(response.content)
80
        self.assertEqual(res['port']['name'], "test-name")
81

    
82
    def test_update_port_sg_unfound(self):
83
        sg1 = dbmf.SecurityGroupFactory.create()
84
        nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
85
        nic.security_groups.add(sg1)
86
        nic.save()
87
        url = join_urls(PORTS_URL, str(nic.id))
88
        request = {'port': {"security_groups": ["123"]}}
89
        response = self.put(url, params=json.dumps(request),
90
                            user=nic.network.userid)
91
        self.assertEqual(response.status_code, 404)
92

    
93
    def test_update_port_sg(self):
94
        sg1 = dbmf.SecurityGroupFactory.create()
95
        sg2 = dbmf.SecurityGroupFactory.create()
96
        sg3 = dbmf.SecurityGroupFactory.create()
97
        nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
98
        nic.security_groups.add(sg1)
99
        nic.save()
100
        url = join_urls(PORTS_URL, str(nic.id))
101
        request = {'port': {"security_groups": [str(sg2.id), str(sg3.id)]}}
102
        response = self.put(url, params=json.dumps(request),
103
                            user=nic.network.userid)
104
        res = json.loads(response.content)
105
        self.assertEqual(res['port']['security_groups'],
106
                         [str(sg2.id), str(sg3.id)])
107

    
108
    def test_create_port_no_network(self):
109
        request = {
110
            "port": {
111
                "device_id": "123",
112
                "name": "port1",
113
                "network_id": "123"
114
            }
115
        }
116
        response = self.post(PORTS_URL, params=json.dumps(request))
117
        self.assertEqual(response.status_code, 404)
118

    
119
    def test_create_port(self):
120
        net = dbmf.NetworkFactory.create()
121
        subnet1 = dbmf.IPv4SubnetFactory.create(network=net)
122
        subnet2 = dbmf.IPv6SubnetFactory.create(network=net)
123
        sg1 = dbmf.SecurityGroupFactory.create()
124
        sg2 = dbmf.SecurityGroupFactory.create()
125
        vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
126
        request = {
127
            "port": {
128
                "name": "port1",
129
                "network_id": str(net.id),
130
                "device_id": str(vm.id),
131
                "security_groups": [str(sg1.id), str(sg2.id)]
132
            }
133
        }
134
        response = self.post(PORTS_URL, params=json.dumps(request),
135
                             user=net.userid)
136
        self.assertEqual(response.status_code, 201)