1#!/usr/bin/env python
2# Copyright 2012 Red Hat
3# All Rights Reserved.
4#
5#    Licensed under the Apache License, Version 2.0 (the "License"); you may
6#    not use this file except in compliance with the License. You may obtain
7#    a copy of the License at
8#
9#         http://www.apache.org/licenses/LICENSE-2.0
10#
11#    Unless required by applicable law or agreed to in writing, software
12#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14#    License for the specific language governing permissions and limitations
15#    under the License.
16
17import sys
18
19from neutronclient.neutron.v2_0 import floatingip as fip
20from neutronclient.tests.unit import test_cli20
21
22
23class CLITestV20FloatingIpsJSON(test_cli20.CLITestV20Base):
24
25    non_admin_status_resources = ['floatingip']
26
27    def test_create_floatingip(self):
28        # Create floatingip: fip1.
29        resource = 'floatingip'
30        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
31        name = 'fip1'
32        myid = 'myid'
33        args = [name, '--description', 'floats like a butterfly']
34        position_names = ['floating_network_id']
35        position_values = [name]
36        self._test_create_resource(resource, cmd, name, myid, args,
37                                   position_names, position_values,
38                                   description='floats like a butterfly')
39
40    def test_create_floatingip_and_port(self):
41        # Create floatingip: fip1.
42        resource = 'floatingip'
43        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
44        name = 'fip1'
45        myid = 'myid'
46        pid = 'mypid'
47        args = [name, '--port_id', pid]
48        position_names = ['floating_network_id', 'port_id']
49        position_values = [name, pid]
50        self._test_create_resource(resource, cmd, name, myid, args,
51                                   position_names, position_values)
52
53        # Test dashed options
54        args = [name, '--port-id', pid]
55        position_names = ['floating_network_id', 'port_id']
56        self._test_create_resource(resource, cmd, name, myid, args,
57                                   position_names, position_values)
58
59    def test_create_floatingip_and_port_and_address(self):
60        # Create floatingip: fip1 with a given port and address.
61        resource = 'floatingip'
62        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
63        name = 'fip1'
64        myid = 'myid'
65        pid = 'mypid'
66        addr = '10.0.0.99'
67        args = [name, '--port_id', pid, '--fixed_ip_address', addr]
68        position_names = ['floating_network_id', 'port_id', 'fixed_ip_address']
69        position_values = [name, pid, addr]
70        self._test_create_resource(resource, cmd, name, myid, args,
71                                   position_names, position_values)
72        # Test dashed options
73        args = [name, '--port-id', pid, '--fixed-ip-address', addr]
74        position_names = ['floating_network_id', 'port_id', 'fixed_ip_address']
75        self._test_create_resource(resource, cmd, name, myid, args,
76                                   position_names, position_values)
77
78    def test_create_floatingip_with_ip_address_of_floating_ip(self):
79        # Create floatingip: fip1 with a given IP address of floating IP.
80        resource = 'floatingip'
81        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
82        name = 'fip1'
83        myid = 'myid'
84        addr = '10.0.0.99'
85
86        args = [name, '--floating-ip-address', addr]
87        position_values = [name, addr]
88        position_names = ['floating_network_id', 'floating_ip_address']
89        self._test_create_resource(resource, cmd, name, myid, args,
90                                   position_names, position_values)
91
92    def test_create_floatingip_with_subnet_id(self):
93        # Create floatingip: fip1 on a given subnet id.
94        resource = 'floatingip'
95        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
96        name = 'fip1'
97        myid = 'myid'
98        subnet_id = 'mysubnetid'
99
100        args = [name, '--subnet', subnet_id]
101        position_values = [name, subnet_id]
102        position_names = ['floating_network_id', 'subnet_id']
103        self._test_create_resource(resource, cmd, name, myid, args,
104                                   position_names, position_values)
105
106    def test_create_floatingip_with_subnet_id_and_port(self):
107        # Create floatingip: fip1 on a given subnet id and port.
108        resource = 'floatingip'
109        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
110        name = 'fip1'
111        myid = 'myid'
112        pid = 'mypid'
113        subnet_id = 'mysubnetid'
114
115        args = [name, '--subnet', subnet_id, '--port-id', pid]
116        position_values = [name, subnet_id, pid]
117        position_names = ['floating_network_id', 'subnet_id', 'port_id']
118        self._test_create_resource(resource, cmd, name, myid, args,
119                                   position_names, position_values)
120
121    def test_create_floatingip_with_dns_name_and_dns_domain(self):
122        # Create floatingip: fip1 with dns name and dns domain.
123        resource = 'floatingip'
124        cmd = fip.CreateFloatingIP(test_cli20.MyApp(sys.stdout), None)
125        name = 'fip1'
126        myid = 'myid'
127        dns_name_name = 'my-floatingip'
128        dns_domain_name = 'my-domain.org.'
129        args = [name, '--dns-name', dns_name_name, '--dns-domain',
130                dns_domain_name]
131        position_names = ['floating_network_id', 'dns_name', 'dns_domain']
132        position_values = [name, dns_name_name, dns_domain_name]
133        self._test_create_resource(resource, cmd, name, myid, args,
134                                   position_names, position_values)
135
136    def test_list_floatingips(self):
137        # list floatingips: -D.
138        resources = 'floatingips'
139        cmd = fip.ListFloatingIP(test_cli20.MyApp(sys.stdout), None)
140        self._test_list_resources(resources, cmd, True)
141
142    def test_list_floatingips_pagination(self):
143        resources = 'floatingips'
144        cmd = fip.ListFloatingIP(test_cli20.MyApp(sys.stdout), None)
145        self._test_list_resources_with_pagination(resources, cmd)
146
147    def test_list_floatingips_sort(self):
148        # list floatingips:
149        # --sort-key name --sort-key id --sort-key asc --sort-key desc
150        resources = 'floatingips'
151        cmd = fip.ListFloatingIP(test_cli20.MyApp(sys.stdout), None)
152        self._test_list_resources(resources, cmd,
153                                  sort_key=["name", "id"],
154                                  sort_dir=["asc", "desc"])
155
156    def test_list_floatingips_limit(self):
157        # list floatingips: -P.
158        resources = 'floatingips'
159        cmd = fip.ListFloatingIP(test_cli20.MyApp(sys.stdout), None)
160        self._test_list_resources(resources, cmd, page_size=1000)
161
162    def test_delete_floatingip(self):
163        # Delete floatingip: fip1.
164        resource = 'floatingip'
165        cmd = fip.DeleteFloatingIP(test_cli20.MyApp(sys.stdout), None)
166        myid = 'myid'
167        args = [myid]
168        self._test_delete_resource(resource, cmd, myid, args)
169
170    def test_show_floatingip(self):
171        # Show floatingip: --fields id.
172        resource = 'floatingip'
173        cmd = fip.ShowFloatingIP(test_cli20.MyApp(sys.stdout), None)
174        args = ['--fields', 'id', self.test_id]
175        self._test_show_resource(resource, cmd, self.test_id,
176                                 args, ['id'])
177
178    def test_disassociate_ip(self):
179        # Disassociate floating IP: myid.
180        resource = 'floatingip'
181        cmd = fip.DisassociateFloatingIP(test_cli20.MyApp(sys.stdout), None)
182        args = ['myid']
183        self._test_update_resource(resource, cmd, 'myid',
184                                   args, {"port_id": None}
185                                   )
186
187    def test_associate_ip(self):
188        # Associate floating IP: myid portid.
189        resource = 'floatingip'
190        cmd = fip.AssociateFloatingIP(test_cli20.MyApp(sys.stdout), None)
191        args = ['myid', 'portid']
192        self._test_update_resource(resource, cmd, 'myid',
193                                   args, {"port_id": "portid"}
194                                   )
195