- comments
- share code between tests
- actually check status of instances
- use many VRIDs on an interface
- test dynamic priority changes
- reduce test time
- update after the recent api changes

test_vrrp_multi  avoid picking OVS local port

test_vrrp_multi: comment

test_vrrp_multi  be a little more deterministic

test_vrrp_linux_multi: comments

test_vrrp_linux_multi  assert states

test_vrrp_linux_multi  create many virtual routers

test_vrrp_linux_multi  reduce test time.  more checks.

test_vrrp_linux_multi: fix type of primary_ip_address

test_vrrp_linux_multi: remove commented out code

test_vrrp_linux_multi: remove unused _IP_ADDRESS

test_vrrp_linux_multi: update after the recent api changes

vrrp tests  catch up with the recent api changes

share code among vrrp tests

tests.integrated.vrrp_common  comments whitespace

vrrp_common  fix a message

vrrp_common  move checking code into a method

vrrp_common  shuffle priority

Signed-off-by: YAMAMOTO Takashi <[email protected]>
---
 ryu/tests/integrated/test_vrrp_linux_multi.py |  88 ++---------
 ryu/tests/integrated/test_vrrp_multi.py       |  95 +++--------
 ryu/tests/integrated/vrrp_common.py           | 216 ++++++++++++++++++++++++++
 3 files changed, 255 insertions(+), 144 deletions(-)
 create mode 100644 ryu/tests/integrated/vrrp_common.py

diff --git a/ryu/tests/integrated/test_vrrp_linux_multi.py 
b/ryu/tests/integrated/test_vrrp_linux_multi.py
index 833b65c..b13d1d4 100644
--- a/ryu/tests/integrated/test_vrrp_linux_multi.py
+++ b/ryu/tests/integrated/test_vrrp_linux_multi.py
@@ -24,11 +24,11 @@ PYTHONPATH=. ./bin/ryu-manager --verbose \
 
 ./ryu/services/vrrp/dumper.py is optional.
 
-                   ----------------
-      /--<--veth-->|              |
-   Ryu             | linux bridge |<--veth--> command to generate packets
-      \--<--veth-->|              |
-                   ----------------
+                    ----------------
+      /--<--veth0-->|              |
+   Ryu              | linux bridge |<--veth2--> command to generate packets
+      \--<--veth1-->|   (vrrpbr)   |
+                    ----------------
 
 
 # ip link add veth0 type veth peer name veth0-br
@@ -62,9 +62,6 @@ from veth2 by packet generator like packeth
 NOTE: vrid: 7 and ip address: 10.0.0.1... are hardcoded below
 """
 
-import netaddr
-import time
-
 from ryu.base import app_manager
 from ryu.lib import hub
 from ryu.lib import mac as lib_mac
@@ -72,65 +69,29 @@ from ryu.lib.packet import vrrp
 from ryu.services.vrrp import api as vrrp_api
 from ryu.services.vrrp import event as vrrp_event
 
+from . import vrrp_common
 
-_VRID = 7
-_IP_ADDRESS = '10.0.0.1'
-
-_IFNAME0 = 'veth0'
-_PRIMARY_IP_ADDRESS0 = '10.0.0.2'
-
-_IFNAME1 = 'veth1'
-_PRIMARY_IP_ADDRESS1 = '10.0.0.3'
 
-# _IFNAME = 'eth2'
-# _VRID = 1
-# _IP_ADDRESS = '172.17.107.2'
+class VRRPConfigApp(vrrp_common.VRRPCommon):
+    _IFNAME0 = 'veth0'
+    _IFNAME1 = 'veth1'
 
-
-class VRRPConfigApp(app_manager.RyuApp):
     def __init__(self, *args, **kwargs):
         super(VRRPConfigApp, self).__init__(*args, **kwargs)
-        self.logger.info(
-            'virtual router mac address = %s',
-            lib_mac.haddr_to_str(vrrp.vrrp_ipv4_src_mac_address(_VRID)))
 
     def start(self):
         hub.spawn(self._main)
 
-    def _main(self):
-        time.sleep(1)
-        self._main_version(vrrp.VRRP_VERSION_V3)
-        time.sleep(5)
-        self._main_version(vrrp.VRRP_VERSION_V2)
-
-    def _main_version(self, vrrp_version):
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_ADDRESS_OWNER)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_MAX)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_DEFAULT)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_MIN)
-
-    def _main_version_priority(self, vrrp_version, priority):
-        self._main_version_priority_sleep(vrrp_version, priority, False)
-        time.sleep(5)
-        self._main_version_priority_sleep(vrrp_version, priority, True)
-
     def _configure_vrrp_router(self, vrrp_version, priority,
-                               primary_ip_address, ifname):
-        primary_ip_address = netaddr.IPAddress(primary_ip_address)
+                               primary_ip_address, ifname, vrid):
         interface = vrrp_event.VRRPInterfaceNetworkDevice(
-            lib_mac.DONTCARE, primary_ip_address, None, ifname)
+            lib_mac.DONTCARE_STR, primary_ip_address, None, ifname)
         self.logger.debug('%s', interface)
 
-        ip_addresses = [netaddr.IPAddress(_IP_ADDRESS).value]
+        vip = '10.0.%d.1' % vrid
+        ip_addresses = [vip]
         config = vrrp_event.VRRPConfig(
-            version=vrrp_version, vrid=_VRID, priority=priority,
+            version=vrrp_version, vrid=vrid, priority=priority,
             ip_addresses=ip_addresses)
         self.logger.debug('%s', config)
 
@@ -138,24 +99,3 @@ class VRRPConfigApp(app_manager.RyuApp):
         self.logger.debug('%s', rep)
 
         return rep
-
-    def _main_version_priority_sleep(self, vrrp_version, priority, do_sleep):
-        app_mgr = app_manager.AppManager.get_instance()
-        self.logger.debug('%s', app_mgr.applications)
-        vrrp_mgr = app_mgr.applications['VRRPManager']
-
-        rep0 = self._configure_vrrp_router(vrrp_version, priority,
-                                           _PRIMARY_IP_ADDRESS0, _IFNAME0)
-        rep1 = self._configure_vrrp_router(
-            vrrp_version, vrrp.VRRP_PRIORITY_BACKUP_DEFAULT,
-            _PRIMARY_IP_ADDRESS1, _IFNAME1)
-
-        self.logger.debug('%s', vrrp_mgr._instances)
-
-        if do_sleep:
-            time.sleep(10)
-
-        vrrp_api.vrrp_shutdown(self, rep0.instance_name)
-        if do_sleep:
-            time.sleep(10)
-        vrrp_api.vrrp_shutdown(self, rep1.instance_name)
diff --git a/ryu/tests/integrated/test_vrrp_multi.py 
b/ryu/tests/integrated/test_vrrp_multi.py
index bf6414e..b1d1771 100644
--- a/ryu/tests/integrated/test_vrrp_multi.py
+++ b/ryu/tests/integrated/test_vrrp_multi.py
@@ -19,14 +19,16 @@ Usage:
 PYTHONPATH=. ./bin/ryu-manager --verbose \
              ./ryu/topology/switches.py \
              ./ryu/services/vrrp/manager.py \
-             ./ryu/tests/integrated/test_vrrp.py \
+             ./ryu/tests/integrated/test_vrrp_multi.py \
              ./ryu/services/vrrp/dumper.py
 
 ./ryu/services/vrrp/dumper.py is optional.
 
-  -----          --------------          -----
-  |OVS|<--veth-->|Linux bridge|<--veth-->|OVS|
-  -----          --------------          -----
+         +---+          ----------------
+      /--|OVS|<--veth-->|              |
+   Ryu   +---+          | linux bridge |<--veth--> command to generate packets
+      \--|OVS|<--veth-->|              |
+         +---+          ----------------
 
 configure OVSs to connect ryu
 example
@@ -76,22 +78,24 @@ ovs-system              0000.122038293b55       no
 # ip link b0 set up
 """
 
-import netaddr
-import time
-
 from ryu.base import app_manager
 from ryu.controller import handler
 from ryu.lib import dpid as lib_dpid
 from ryu.lib import hub
-from ryu.lib import mac as lib_mac
+from ryu.lib import addrconv
 from ryu.lib.packet import vrrp
 from ryu.services.vrrp import api as vrrp_api
 from ryu.services.vrrp import event as vrrp_event
 from ryu.topology import event as topo_event
 from ryu.topology import api as topo_api
 
+from . import vrrp_common
+
+
+class VRRPConfigApp(vrrp_common.VRRPCommon):
+    _IFNAME0 = 0
+    _IFNAME1 = 1
 
-class VRRPConfigApp(app_manager.RyuApp):
     def __init__(self, *args, **kwargs):
         super(VRRPConfigApp, self).__init__(*args, **kwargs)
         self.start_main = False
@@ -106,84 +110,35 @@ class VRRPConfigApp(app_manager.RyuApp):
             return
 
         self.start_main = True
+        app_mgr = app_manager.AppManager.get_instance()
+        self.logger.debug('%s', app_mgr.applications)
+        self.switches = app_mgr.applications['switches']
         hub.spawn(self._main)
 
-    def _main(self):
-        time.sleep(1)
-        self.logger.debug('########## test start ##########')
-        self._main_version(vrrp.VRRP_VERSION_V3)
-        time.sleep(5)
-        self._main_version(vrrp.VRRP_VERSION_V2)
-        self.logger.debug('########## test done ##########')
-
-    def _main_version(self, vrrp_version):
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_ADDRESS_OWNER)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_MAX)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_DEFAULT)
-        time.sleep(5)
-        self._main_version_priority(vrrp_version,
-                                    vrrp.VRRP_PRIORITY_BACKUP_MIN)
-
-    def _main_version_priority(self, vrrp_version, priority):
-        self._main_version_priority_sleep(vrrp_version, priority, False)
-        time.sleep(5)
-        self._main_version_priority_sleep(vrrp_version, priority, True)
-
-    def _config_switch(self, switches, switch_index,
-                       vrrp_version, ip_addr, priority):
+    def _configure_vrrp_router(self, vrrp_version, priority,
+                               ip_addr, switch_index, vrid):
+        switches = self.switches
         self.logger.debug('%s', switches.dps)
-        dpid = switches.dps.keys()[switch_index]
+        dpid = sorted(switches.dps.keys())[switch_index]
         self.logger.debug('%s', lib_dpid.dpid_to_str(dpid))
         self.logger.debug('%s', switches.port_state)
-        port_no = switches.port_state[dpid].keys()[0]
+        # hack: use the smallest port no to avoid picking OVS local port
+        port_no = sorted(switches.port_state[dpid].keys())[0]
         self.logger.debug('%d', port_no)
         port = switches.port_state[dpid][port_no]
         self.logger.debug('%s', port)
-        mac = port.hw_addr
-        self.logger.debug('%s', lib_mac.haddr_to_str(mac))
+        mac = addrconv.mac.bin_to_text(port.hw_addr)
+        self.logger.debug('%s', mac)
 
-        ip_addr = netaddr.IPAddress(ip_addr).value
         interface = vrrp_event.VRRPInterfaceOpenFlow(
             mac, ip_addr, None, dpid, port_no)
         self.logger.debug('%s', interface)
 
         config = vrrp_event.VRRPConfig(
-            version=vrrp_version, vrid=7, priority=priority,
+            version=vrrp_version, vrid=vrid, priority=priority,
             ip_addresses=[ip_addr])
         self.logger.debug('%s', config)
 
         rep = vrrp_api.vrrp_config(self, interface, config)
         self.logger.debug('%s', rep)
         return rep
-
-    def _main_version_priority_sleep(self, vrrp_version, priority, do_sleep):
-        self.logger.debug('########## '
-                          'test vrrp_verson %s priority %d do_sleep %d '
-                          '##########',
-                          vrrp_version, priority, do_sleep)
-        app_mgr = app_manager.AppManager.get_instance()
-        self.logger.debug('%s', app_mgr.applications)
-        vrrp_mgr = app_mgr.applications['VRRPManager']
-        switches = app_mgr.applications['switches']
-
-        rep1 = self._config_switch(switches, 1, vrrp_version, '10.0.0.2',
-                                   vrrp.VRRP_PRIORITY_BACKUP_DEFAULT)
-        if do_sleep:
-            time.sleep(5)
-        rep0 = self._config_switch(switches, 0, vrrp_version, '10.0.0.1',
-                                   priority)
-
-        self.logger.debug('%s', vrrp_mgr._instances)
-
-        if do_sleep:
-            time.sleep(10)
-
-        vrrp_api.vrrp_shutdown(self, rep0.instance_name)
-        if do_sleep:
-            time.sleep(10)
-        vrrp_api.vrrp_shutdown(self, rep1.instance_name)
diff --git a/ryu/tests/integrated/vrrp_common.py 
b/ryu/tests/integrated/vrrp_common.py
new file mode 100644
index 0000000..fb150f7
--- /dev/null
+++ b/ryu/tests/integrated/vrrp_common.py
@@ -0,0 +1,216 @@
+# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2013 Isaku Yamahata <yamahata at valinux co jp>
+#
+# 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 time
+import random
+
+from ryu.base import app_manager
+from ryu.lib import hub
+from ryu.lib import mac as lib_mac
+from ryu.lib.packet import vrrp
+from ryu.services.vrrp import api as vrrp_api
+from ryu.services.vrrp import event as vrrp_event
+
+
+_VRID = 7
+_PRIMARY_IP_ADDRESS0 = '10.0.0.2'
+_PRIMARY_IP_ADDRESS1 = '10.0.0.3'
+
+
+class VRRPCommon(app_manager.RyuApp):
+    _IFNAME0 = None
+    _IFNAME1 = None
+
+    def __init__(self, *args, **kwargs):
+        super(VRRPCommon, self).__init__(*args, **kwargs)
+
+    def _main(self):
+        self._main_version(vrrp.VRRP_VERSION_V3)
+        self._main_version(vrrp.VRRP_VERSION_V2)
+        print "done!"
+
+    def _main_version(self, vrrp_version):
+        self._main_version_priority(vrrp_version,
+                                    vrrp.VRRP_PRIORITY_ADDRESS_OWNER)
+        self._main_version_priority(vrrp_version,
+                                    vrrp.VRRP_PRIORITY_BACKUP_MAX)
+        self._main_version_priority(vrrp_version,
+                                    vrrp.VRRP_PRIORITY_BACKUP_DEFAULT)
+        self._main_version_priority(vrrp_version,
+                                    vrrp.VRRP_PRIORITY_BACKUP_MIN)
+
+    def _main_version_priority(self, vrrp_version, priority):
+        self._main_version_priority_sleep(vrrp_version, priority, False)
+        self._main_version_priority_sleep(vrrp_version, priority, True)
+
+    def _check(self, vrrp_api, instances):
+        while True:
+            while True:
+                rep = vrrp_api.vrrp_list(self)
+                if len(rep.instance_list) >= len(instances) * 2:
+                    if any(i.state == vrrp_event.VRRP_STATE_INITIALIZE
+                           for i in rep.instance_list):
+                        continue
+                    break
+                print len(rep.instance_list), '/', len(instances) * 2
+                time.sleep(1)
+
+#                for i in rep.instance_list:
+#                    print i.instance_name, i.monitor_name, i.config, \
+#                          i.interface, i.state
+            assert len(rep.instance_list) == len(instances) * 2
+            num_of_master = 0
+            d = dict(((i.instance_name, i) for i in rep.instance_list))
+            bad = 0
+            for i in rep.instance_list:
+                assert i.state in (vrrp_event.VRRP_STATE_MASTER,
+                                   vrrp_event.VRRP_STATE_BACKUP)
+                if i.state == vrrp_event.VRRP_STATE_MASTER:
+                    num_of_master += 1
+
+                vr = instances[i.config.vrid]
+                if (vr[0].config.priority > vr[1].config.priority and
+                        i.instance_name == vr[1].instance_name) or \
+                   (vr[0].config.priority < vr[1].config.priority and
+                        i.instance_name == vr[0].instance_name):
+                    if i.state == vrrp_event.VRRP_STATE_MASTER:
+                        print "bad master:"
+                        print d[vr[0].instance_name].state, \
+                            d[vr[0].instance_name].config.priority
+                        print d[vr[1].instance_name].state, \
+                            d[vr[1].instance_name].config.priority
+                        bad += 1
+#                       assert i.state != vrrp_event.VRRP_STATE_MASTER
+            if bad > 0:
+                # this could be a transient state
+                print bad, "bad masters"
+                time.sleep(1)
+                continue
+            if num_of_master >= len(instances):
+                assert num_of_master == len(instances)
+                break
+            print num_of_master, '/', len(instances)
+            time.sleep(1)
+            continue
+
+    def _main_version_priority_sleep(self, vrrp_version, priority, do_sleep):
+        app_mgr = app_manager.AppManager.get_instance()
+        self.logger.debug('%s', app_mgr.applications)
+        vrrp_mgr = app_mgr.applications['VRRPManager']
+
+        step = 5
+        instances = {}
+        for vrid in xrange(1, 256, step):
+            if vrid == _VRID:
+                continue
+            print "vrid", vrid
+            l = {}
+            prio = max(vrrp.VRRP_PRIORITY_BACKUP_MIN,
+                       min(vrrp.VRRP_PRIORITY_BACKUP_MAX, vrid))
+            rep0 = self._configure_vrrp_router(vrrp_version,
+                                               prio,
+                                               _PRIMARY_IP_ADDRESS0,
+                                               self._IFNAME0,
+                                               vrid)
+            assert not rep0.instance_name is None
+            l[0] = rep0
+            prio = max(vrrp.VRRP_PRIORITY_BACKUP_MIN,
+                       min(vrrp.VRRP_PRIORITY_BACKUP_MAX, 256 - vrid))
+            rep1 = self._configure_vrrp_router(vrrp_version,
+                                               prio,
+                                               _PRIMARY_IP_ADDRESS1,
+                                               self._IFNAME1,
+                                               vrid)
+            assert not rep1.instance_name is None
+            l[1] = rep1
+            instances[vrid] = l
+
+        print "vrid", _VRID
+        l = {}
+        rep0 = self._configure_vrrp_router(vrrp_version, priority,
+                                           _PRIMARY_IP_ADDRESS0,
+                                           self._IFNAME0, _VRID)
+        assert not rep0.instance_name is None
+        l[0] = rep0
+        rep1 = self._configure_vrrp_router(
+            vrrp_version, vrrp.VRRP_PRIORITY_BACKUP_DEFAULT,
+            _PRIMARY_IP_ADDRESS1, self._IFNAME1, _VRID)
+        assert not rep1.instance_name is None
+        l[1] = rep1
+        instances[_VRID] = l
+
+        self.logger.debug('%s', vrrp_mgr._instances)
+
+        if do_sleep:
+            print "priority", priority
+            print "waiting for instances starting"
+
+            self._check(vrrp_api, instances)
+
+        for vrid in instances.keys():
+            if vrid == _VRID:
+                continue
+            which = vrid & 1
+            new_priority = int(random.uniform(vrrp.VRRP_PRIORITY_BACKUP_MIN,
+                                              vrrp.VRRP_PRIORITY_BACKUP_MAX))
+            i = instances[vrid][which]
+            vrrp_api.vrrp_config_change(self, i.instance_name,
+                                        priority=new_priority)
+            i.config.priority = new_priority
+
+        if do_sleep:
+            print "priority shuffled"
+
+            self._check(vrrp_api, instances)
+
+        for vrid in instances.keys():
+            if vrid == _VRID:
+                continue
+            which = vrid & 1
+            vrrp_api.vrrp_shutdown(self, instances[vrid][which].instance_name)
+        vrrp_api.vrrp_shutdown(self, instances[_VRID][0].instance_name)
+
+        if do_sleep:
+            print "shutting down instances"
+            while True:
+                rep = vrrp_api.vrrp_list(self)
+                if len(rep.instance_list) <= len(instances):
+                    break
+                print "left", len(rep.instance_list)
+                time.sleep(1)
+            assert len(rep.instance_list) == len(instances)
+            print "waiting for the rest becoming master"
+            while True:
+                rep = vrrp_api.vrrp_list(self)
+                if all(i.state == vrrp_event.VRRP_STATE_MASTER
+                       for i in rep.instance_list):
+                    break
+                time.sleep(1)
+
+        vrrp_api.vrrp_shutdown(self, instances[_VRID][1].instance_name)
+        for vrid in instances.keys():
+            if vrid == _VRID:
+                continue
+            which = 1 - (vrid & 1)
+            vrrp_api.vrrp_shutdown(self, instances[vrid][which].instance_name)
+
+        print "waiting for instances shutting down"
+        while True:
+            rep = vrrp_api.vrrp_list(self)
+            if not rep.instance_list:
+                break
+            print "left", len(rep.instance_list)
+            time.sleep(1)
-- 
1.8.1.5


------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to