use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / test / resources / linux / ns3 / ns3simulation.py
old mode 100644 (file)
new mode 100755 (executable)
index 25a11b2..5f7ce29
@@ -4,9 +4,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
-
-#
-# Network topology
-#
-#       n0    n1   n2   n3
-#       |     |    |    |
-#       -----------------
-#
-#  node n0 sends IGMP traffic to node n3
-
+from __future__ import print_function
 
 from nepi.execution.ec import ExperimentController 
 from nepi.execution.trace import TraceAttr
 
+from test_utils import skipIfNotAlive
+
 import os
 import time
 import unittest
@@ -49,14 +41,15 @@ def add_ns3_node(ec, simu):
     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
     ec.register_connection(node, icmp)
 
+    udp = ec.register_resource("ns3::UdpL4Protocol")
+    ec.register_connection(node, udp)
+
     return node
 
-def add_point2point_device(ec, node, address = None,  prefix = None):
+def add_point2point_device(ec, node, ip, prefix):
     dev = ec.register_resource("ns3::PointToPointNetDevice")
-    if address:
-       ec.set(dev, "ip", address)
-    if prefix:
-       ec.set(dev, "prefix", prefix)
+    ec.set(dev, "ip", ip)
+    ec.set(dev, "prefix", prefix)
     ec.register_connection(node, dev)
 
     queue = ec.register_resource("ns3::DropTailQueue")
@@ -64,12 +57,10 @@ def add_point2point_device(ec, node, address = None,  prefix = None):
 
     return dev
 
-def add_csma_device(ec, node, address = None, prefix = None):
+def add_csma_device(ec, node, ip, prefix):
     dev = ec.register_resource("ns3::CsmaNetDevice")
-    if address:
-        ec.set(dev, "ip", address)
-    if prefix:
-        ec.set(dev, "prefix", prefix)
+    ec.set(dev, "ip", ip)
+    ec.set(dev, "prefix", prefix)
     ec.register_connection(node, dev)
 
     queue = ec.register_resource("ns3::DropTailQueue")
@@ -77,13 +68,11 @@ def add_csma_device(ec, node, address = None, prefix = None):
 
     return dev
 
-def add_wifi_device(ec, node, address = None, prefix = None
+def add_wifi_device(ec, node, ip, prefix
         access_point = False):
     dev = ec.register_resource("ns3::WifiNetDevice")
-    if address:
-        ec.set(dev, "ip", address)
-    if prefix:
-        ec.set(dev, "prefix", prefix)
+    ec.set(dev, "ip", ip)
+    ec.set(dev, "prefix", prefix)
     ec.register_connection(node, dev)
 
     phy = ec.register_resource("ns3::YansWifiPhy")
@@ -138,25 +127,29 @@ def add_wifi_channel(ec):
 
     return channel
 
-class LinuxNS3ClientTest(unittest.TestCase):
+class LinuxNS3SimulationTest(unittest.TestCase):
     def setUp(self):
-        #self.fedora_host = "nepi2.pl.sophia.inria.fr"
-        self.fedora_host = "planetlabpc1.upf.edu"
-        #self.fedora_host = "peeramide.irisa.fr"
+        self.fedora_host = "nepi2.pl.sophia.inria.fr"
         self.fedora_user = "inria_nepi"
         self.fedora_identity = "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'])
 
-    def test_simple_p2p_ping(self):
+    @skipIfNotAlive
+    def t_p2p_ping(self, host, user = None, identity = None):
         ec = ExperimentController(exp_id = "test-ns3-p2p-ping")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+        
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
+        ec.set(simu, "verbose", True)
         ec.register_connection(simu, node)
 
         nsnode1 = add_ns3_node(ec, simu)
@@ -191,17 +184,23 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def test_simple_cmsa_ping(self):
+    @skipIfNotAlive
+    def t_csma_ping(self, host, user = None, identity = None):
         ec = ExperimentController(exp_id = "test-ns3-csma-ping")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+        
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
+        ec.set(simu, "verbose", True)
         ec.register_connection(simu, node)
 
         nsnode1 = add_ns3_node(ec, simu)
@@ -236,20 +235,27 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def test_compile_local_source(self):
-        ec = ExperimentController(exp_id = "test-ns3-local-source")
+    @skipIfNotAlive
+    def t_user_sources(self, host, user = None, identity = None):
+        ec = ExperimentController(exp_id = "test-ns3-user-sources")
+        
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
+        ec.set(simu, "verbose", True)
         sources = os.path.join(os.path.dirname(os.path.realpath(__file__)), 
                 "ns-3.18-user.tar.gz")
         ec.set(simu, "sources", sources)
+        ec.set(simu, "pybindgenVersion", "834")
         ec.register_connection(simu, node)
 
         nsnode1 = add_ns3_node(ec, simu)
@@ -284,17 +290,22 @@ class LinuxNS3ClientTest(unittest.TestCase):
         
         ec.shutdown()
 
-    def test_compile_debug_mode(self):
+    @skipIfNotAlive
+    def t_compile_debug_mode(self, host, user = None, identity = None):
         ec = ExperimentController(exp_id = "test-ns3-debug-mode")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", host)
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
         ec.set(simu, "verbose", True)
         ec.set(simu, "nsLog", "V4Ping:Node")
         ec.set(simu, "buildMode", "debug")
@@ -336,17 +347,22 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def test_real_time(self):
+    @skipIfNotAlive
+    def t_real_time(self, host, user = None, identity = None):
         ec = ExperimentController(exp_id = "test-ns3-real-time")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
-        ec.set(node, "cleanProcesses", True)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+            ec.set(node, "cleanProcesses", True)
+
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
         ec.set(simu, "simulatorImplementationType", "ns3::RealtimeSimulatorImpl")
         ec.set(simu, "checksumEnabled", True)
         ec.set(simu, "verbose", True)
@@ -385,24 +401,30 @@ class LinuxNS3ClientTest(unittest.TestCase):
         rm = ec.get_resource(ping)
         start_time = rm.start_time
         stop_time = rm.stop_time
-        delta =  stop_time - start_time
+        delta = stop_time - start_time
 
-        self.assertTrue(delta.seconds >= 20)
-        self.assertTrue(delta.seconds < 25)
+        self.assertTrue(delta.seconds >= 20, "Time elapsed %d" % delta.seconds)
+        self.assertTrue(delta.seconds < 25, "Time elapsed %d" % delta.seconds)
 
         ec.shutdown()
 
-    def test_dev2p_traces(self):
-        ec = ExperimentController(exp_id = "test-ns3-dev2p-traces")
+    @skipIfNotAlive
+    def t_traces(self, host, user = None, identity = None):
+        ec = ExperimentController(exp_id = "test-ns3-traces")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", self.fedora_host)
+            ec.set(node, "username", self.fedora_user)
+            ec.set(node, "identity", self.fedora_identity)
+
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
+        ec.set(simu, "verbose", True)
         ec.register_connection(simu, node)
 
         nsnode1 = add_ns3_node(ec, simu)
@@ -465,21 +487,26 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def test_simple_wifi_ping(self):
+    @skipIfNotAlive
+    def t_wifi_ping(self, host, user = None, identity = None):
         bounds_width = bounds_height = 200
         x = y = 100
         speed = 1
 
         ec = ExperimentController(exp_id = "test-ns3-wifi-ping")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
         ec.set(simu, "verbose", True)
         ec.register_connection(simu, node)
 
@@ -517,7 +544,8 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def test_routing(self):
+    @skipIfNotAlive
+    def t_routing(self, host, user = None, identity = None):
         """ 
         network topology:
                                 n4
@@ -530,14 +558,18 @@ class LinuxNS3ClientTest(unittest.TestCase):
         """
         ec = ExperimentController(exp_id = "test-ns3-routes")
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", host)
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
         ec.set(simu, "verbose", True)
         ec.register_connection(simu, node)
 
@@ -629,7 +661,8 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
-    def ztest_automatic_routing(self):
+    @skipIfNotAlive
+    def t_automatic_routing(self, host, user = None, identity = None):
         """ 
         network topology:
                                 n4
@@ -640,16 +673,20 @@ class LinuxNS3ClientTest(unittest.TestCase):
            
 
         """
-        ec = ExperimentController(exp_id = "test-ns3-auto-routes")
+        ec = ExperimentController(exp_id = "test-ns3-routing")
+        
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", "localhost")
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
         
-        node = ec.register_resource("LinuxNode")
-        ec.set(node, "hostname", self.fedora_host)
-        ec.set(node, "username", self.fedora_user)
-        ec.set(node, "identity", self.fedora_identity)
         ec.set(node, "cleanProcesses", True)
         #ec.set(node, "cleanHome", True)
 
-        simu = ec.register_resource("LinuxNS3Simulation")
+        simu = ec.register_resource("linux::ns3::Simulation")
         ec.set(simu, "verbose", True)
         ec.set(simu, "populateRoutingTables", True)
         ec.register_connection(simu, node)
@@ -709,13 +746,142 @@ class LinuxNS3ClientTest(unittest.TestCase):
         
         stdout = ec.trace(simu, "stdout")
 
-        print stdout
+        print(stdout)
 
         expected = "20 packets transmitted, 20 received, 0% packet loss"
         self.assertTrue(stdout.find(expected) > -1)
 
         ec.shutdown()
 
+    @skipIfNotAlive
+    def t_dce(self, host, user = None, identity = None):
+        ec = ExperimentController(exp_id = "test-ns3-dce")
+        
+        node = ec.register_resource("linux::Node")
+        if host == "localhost":
+            ec.set(node, "hostname", host)
+        else:
+            ec.set(node, "hostname", host)
+            ec.set(node, "username", user)
+            ec.set(node, "identity", identity)
+
+        ec.set(node, "cleanProcesses", True)
+        #ec.set(node, "cleanHome", True)
+
+        simu = ec.register_resource("linux::ns3::Simulation")
+        ec.set(simu, "verbose", True)
+        ec.set(simu, "buildMode", "debug")
+        ec.set(simu, "nsLog", "DceApplication")
+        ec.register_connection(simu, node)
+
+        nsnode1 = add_ns3_node(ec, simu)
+        p2p1 = add_point2point_device(ec, nsnode1, "10.0.0.1", "30")
+        ec.set(p2p1, "DataRate", "5Mbps")
+
+        nsnode2 = add_ns3_node(ec, simu)
+        p2p2 = add_point2point_device(ec, nsnode2, "10.0.0.2", "30")
+        ec.set(p2p2, "DataRate", "5Mbps")
+
+        # Create channel
+        chan = ec.register_resource("ns3::PointToPointChannel")
+        ec.set(chan, "Delay", "2ms")
+
+        ec.register_connection(chan, p2p1)
+        ec.register_connection(chan, p2p2)
+
+        ### create applications
+        udp_perf = ec.register_resource("linux::ns3::dce::Application")
+        ec.set (udp_perf, "binary", "udp-perf")
+        ec.set (udp_perf, "stackSize", 1<<20)
+        ec.set (udp_perf, "arguments", "--duration=10;--nodes=2")
+        ec.set (udp_perf, "StartTime", "1s")
+        ec.set (udp_perf, "StopTime", "20s")
+        ec.register_connection(udp_perf, nsnode1)
+
+        udp_perf_client = ec.register_resource("linux::ns3::dce::Application")
+        ec.set (udp_perf_client, "binary", "udp-perf")
+        ec.set (udp_perf_client, "stackSize", 1<<20)
+        ec.set (udp_perf_client, "arguments", "--client;--nodes=2;--host=10.0.0.1;--duration=10")
+        ec.set (udp_perf_client, "StartTime", "2s")
+        ec.set (udp_perf_client, "StopTime", "20s")
+        ec.register_connection(udp_perf_client, nsnode2)
+
+        ec.deploy()
+
+        ec.wait_finished([udp_perf_client])
+
+        expected = "udp-perf --duration=10 --nodes=2"
+        cmdline = ec.trace(udp_perf, "cmdline")
+        self.assertTrue(cmdline.find(expected) > -1, cmdline)
+
+        expected = "Start Time: NS3 Time:          1s ("
+        status = ec.trace(udp_perf, "status")
+        self.assertTrue(status.find(expected) > -1, status)
+
+        expected = "received=1500 bytes, 1 reads (@1500 bytes) 1500"
+        stdout = ec.trace(udp_perf, "stdout")
+        self.assertTrue(stdout.find(expected) > -1, stdout)
+
+        stderr = ec.trace(simu, "stderr")
+        expected = "DceApplication:StartApplication"
+        self.assertTrue(stderr.find(expected) > -1, stderr)
+
+        ec.shutdown()
+    
+    def test_p2p_ping_fedora(self):
+        self.t_p2p_ping(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_p2p_ping_local(self):
+        self.t_p2p_ping("localhost")
+
+    def test_csma_ping_fedora(self):
+        self.t_csma_ping(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_csma_ping_local(self):
+        self.t_csma_ping("localhost")
+
+    def test_wifi_ping_fedora(self):
+        self.t_wifi_ping(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_wifi_ping_local(self):
+        self.t_wifi_ping("localhost")
+
+    def test_user_sources_fedora(self):
+        self.t_user_sources(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_user_sources_local(self):
+        self.t_user_sources("localhost")
+
+    def test_compile_debug_mode_fedora(self):
+        self.t_compile_debug_mode(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_compile_debug_mode_local(self):
+        self.t_compile_debug_mode("localhost")
+
+    def test_real_time_fedora(self):
+        self.t_real_time(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_real_time_local(self):
+        self.t_real_time("localhost")
+
+    def test_traces_fedora(self):
+        self.t_traces(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_traces_local(self):
+        self.t_traces("localhost")
+
+    def test_routing_fedora(self):
+        self.t_routing(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_routing_local(self):
+        self.t_routing("localhost")
+
+    def test_dce_fedora(self):
+        self.t_dce(self.fedora_host, self.fedora_user, self.fedora_identity)
+
+    def test_dce_local(self):
+        self.t_dce("localhost")
+
 
 if __name__ == '__main__':
     unittest.main()