summaryrefslogtreecommitdiff
path: root/heat/tests/openstack/neutron/test_extraroute.py
blob: 6ef33a0f29407b8346772bccbb9e2010c99d089d (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

#    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.

from unittest import mock

from neutronclient.v2_0 import client as neutronclient

from heat.common import template_format
from heat.engine.clients.os import neutron
from heat.engine.resources.openstack.neutron import extraroute
from heat.engine import scheduler
from heat.tests import common
from heat.tests import utils


neutron_template = '''
{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "Template to test OS::Neutron::ExtraRoute resources",
  "Parameters" : {},
  "Resources" : {
    "router": {
      "Type": "OS::Neutron::Router"
    },
    "extraroute1": {
      "Type": "OS::Neutron::ExtraRoute",
      "Properties": {
        "router_id": { "Ref" : "router" },
        "destination" : "192.168.0.0/24",
        "nexthop": "1.1.1.1"
      }
    },
    "extraroute2": {
      "Type": "OS::Neutron::ExtraRoute",
      "Properties": {
        "router_id": { "Ref" : "router" },
        "destination" : "192.168.255.0/24",
        "nexthop": "1.1.1.1"
      }
    }
  }
}
'''


class NeutronExtraRouteTest(common.HeatTestCase):
    def setUp(self):
        super(NeutronExtraRouteTest, self).setUp()
        self.mockclient = mock.Mock(spec=neutronclient.Client)
        self.patchobject(neutronclient, 'Client',
                         return_value=self.mockclient)

        self.patchobject(neutron.NeutronClientPlugin, 'has_extension',
                         return_value=True)

    def create_extraroute(self, t, stack, resource_name, properties=None):
        properties = properties or {}
        t['Resources'][resource_name]['Properties'] = properties
        rsrc = extraroute.ExtraRoute(
            resource_name,
            stack.t.resource_definitions(stack)[resource_name],
            stack)
        scheduler.TaskRunner(rsrc.create)()
        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
        return rsrc

    def _test_extraroute(self, ipv6=False):

        if ipv6:
            route1 = {"destination": "ffff:f53b:82e4::56/46",
                      "nexthop": "dce7:f53b:82e4::56"}
            route2 = {"destination": "ffff:f53b:ffff::56/46",
                      "nexthop": "dce7:f53b:82e4::56"}
        else:
            route1 = {"destination": "192.168.0.0/24",
                      "nexthop": "1.1.1.1"}
            route2 = {"destination": "192.168.255.0/24",
                      "nexthop": "1.1.1.1"}

        self.stub_RouterConstraint_validate()

        self.mockclient.show_router.side_effect = [
            # add first route
            {'router': {'routes': []}},
            # add second route
            {'router': {'routes': [route1.copy()]}},
            # first delete
            {'router': {'routes': [route1.copy(), route2.copy()]}},
            # second delete
            {'router': {'routes': [route2.copy()]}},
        ]
        self.mockclient.update_router.return_value = None

        t = template_format.parse(neutron_template)
        stack = utils.parse_stack(t)

        if ipv6:
            rsrc1 = self.create_extraroute(
                t, stack, 'extraroute1', properties={
                    'router_id': '3e46229d-8fce-4733-819a-b5fe630550f8',
                    'destination': 'ffff:f53b:82e4::56/46',
                    'nexthop': 'dce7:f53b:82e4::56'})

            self.create_extraroute(
                t, stack, 'extraroute2', properties={
                    'router_id': '3e46229d-8fce-4733-819a-b5fe630550f8',
                    'destination': 'ffff:f53b:ffff::56/46',
                    'nexthop': 'dce7:f53b:82e4::56'})
        else:
            rsrc1 = self.create_extraroute(
                t, stack, 'extraroute1', properties={
                    'router_id': '3e46229d-8fce-4733-819a-b5fe630550f8',
                    'destination': '192.168.0.0/24',
                    'nexthop': '1.1.1.1'})

            self.create_extraroute(
                t, stack, 'extraroute2', properties={
                    'router_id': '3e46229d-8fce-4733-819a-b5fe630550f8',
                    'destination': '192.168.255.0/24',
                    'nexthop': '1.1.1.1'})

        scheduler.TaskRunner(rsrc1.delete)()
        rsrc1.state_set(rsrc1.CREATE, rsrc1.COMPLETE, 'to delete again')
        scheduler.TaskRunner(rsrc1.delete)()

        self.mockclient.show_router.assert_called_with(
            '3e46229d-8fce-4733-819a-b5fe630550f8')
        self.mockclient.update_router.assert_has_calls([
            # add first route
            mock.call('3e46229d-8fce-4733-819a-b5fe630550f8',
                      {'router': {'routes': [route1.copy()]}}),
            # add second route
            mock.call('3e46229d-8fce-4733-819a-b5fe630550f8',
                      {'router': {'routes': [route1.copy(),
                                             route2.copy()]}}),
            # first delete
            mock.call('3e46229d-8fce-4733-819a-b5fe630550f8',
                      {'router': {'routes': [route2.copy()]}}),
        ])

    def test_extraroute_ipv4(self):
        self._test_extraroute(ipv6=False)

    def test_extraroute_ipv6(self):
        self._test_extraroute(ipv6=True)