File: test_floating_ip.py

package info (click to toggle)
python-openstacksdk 0.8.1-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 2,748 kB
  • sloc: python: 15,505; makefile: 156; sh: 46
file content (86 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import mock
import testtools

from openstack.network.v2 import floating_ip

IDENTIFIER = 'IDENTIFIER'
EXAMPLE = {
    'id': IDENTIFIER,
    'fixed_ip_address': '1',
    'floating_ip_address': '127.0.0.1',
    'floating_network_id': '3',
    'port_id': '5',
    'tenant_id': '6',
    'router_id': '7',
}


class TestFloatingIP(testtools.TestCase):

    def test_basic(self):
        sot = floating_ip.FloatingIP()
        self.assertEqual('floatingip', sot.resource_key)
        self.assertEqual('floatingips', sot.resources_key)
        self.assertEqual('/floatingips', sot.base_path)
        self.assertEqual('network', sot.service.service_type)
        self.assertTrue(sot.allow_create)
        self.assertTrue(sot.allow_retrieve)
        self.assertTrue(sot.allow_update)
        self.assertTrue(sot.allow_delete)
        self.assertTrue(sot.allow_list)

    def test_make_it(self):
        sot = floating_ip.FloatingIP(EXAMPLE)
        self.assertEqual(EXAMPLE['fixed_ip_address'], sot.fixed_ip_address)
        self.assertEqual(EXAMPLE['floating_ip_address'],
                         sot.floating_ip_address)
        self.assertEqual(EXAMPLE['floating_network_id'],
                         sot.floating_network_id)
        self.assertEqual(EXAMPLE['id'], sot.id)
        self.assertEqual(EXAMPLE['port_id'], sot.port_id)
        self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
        self.assertEqual(EXAMPLE['router_id'], sot.router_id)

    def test_find_available(self):
        mock_session = mock.Mock()
        mock_get = mock.Mock()
        mock_session.get = mock_get
        mock_session.get_filter = mock.Mock(return_value={})
        data = {'id': 'one', 'floating_ip_address': '10.0.0.1'}
        fake_response = mock.Mock()
        body = {floating_ip.FloatingIP.resources_key: [data]}
        fake_response.json = mock.Mock(return_value=body)
        mock_get.return_value = fake_response

        result = floating_ip.FloatingIP.find_available(mock_session)

        self.assertEqual('one', result.id)
        p = {'fields': 'id', 'port_id': ''}
        mock_get.assert_called_with(
            floating_ip.FloatingIP.base_path,
            endpoint_filter=floating_ip.FloatingIP.service,
            headers={'Accept': 'application/json'},
            params=p)

    def test_find_available_nada(self):
        mock_session = mock.Mock()
        mock_get = mock.Mock()
        mock_session.get = mock_get
        fake_response = mock.Mock()
        body = {floating_ip.FloatingIP.resources_key: []}
        fake_response.json = mock.Mock(return_value=body)
        mock_get.return_value = fake_response

        self.assertIsNone(floating_ip.FloatingIP.find_available(mock_session))