Adding linux ns3 server unit test
[nepi.git] / test / resources / linux / ns3 / ns3simulator.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21
22 #
23 # Network topology
24 #
25 #       n0    n1   n2   n3
26 #       |     |    |    |
27 #       -----------------
28 #
29 #  node n0 sends IGMP traffic to node n3
30
31
32 from nepi.execution.ec import ExperimentController 
33 from nepi.resources.ns3.ns3server import run_server
34
35 import os
36 import threading
37 import time
38 import unittest
39
40 class LinuxNS3ClientTest(unittest.TestCase):
41     def setUp(self):
42         self.socket_name = os.path.join("/", "tmp", "NS3WrapperServerSimu.sock")
43         if os.path.exists(self.socket_name):
44             os.remove(self.socket_name) 
45
46     def tearDown(self):
47         os.remove(self.socket_name) 
48
49     def test_runtime_attr_modify(self):
50         thread = threading.Thread(target = run_server,
51                 args = [self.socket_name], 
52                 kwargs = {"verbose" : True,
53                     "ns_log": "V4Ping:Node"})
54
55         thread.setDaemon(True)
56         thread.start()
57
58         time.sleep(1)
59
60         ec = ExperimentController(exp_id = "test-ns3-simu")
61         
62         node = ec.register_resource("LinuxNode")
63         ec.set(node, "hostname", "localhost")
64
65         simu = ec.register_resource("LinuxNS3Simulator")
66         ec.set(simu, "socketName", self.socket_name)
67         ec.register_connection(simu, node)
68
69         nsnode1 = ec.register_resource("ns3::Node")
70         ec.register_connection(nsnode1, simu)
71
72         ipv41 = ec.register_resource("ns3::Ipv4L3Protocol")
73         ec.register_connection(nsnode1, ipv41)
74
75         arp1 = ec.register_resource("ns3::ArpL3Protocol")
76         ec.register_connection(nsnode1, arp1)
77         
78         icmp1 = ec.register_resource("ns3::Icmpv4L4Protocol")
79         ec.register_connection(nsnode1, icmp1)
80
81         p1 = ec.register_resource("ns3::PointToPointNetDevice")
82         ec.set(p1, "ip", "10.0.0.1")
83         ec.set(p1, "prefix", "30")
84         ec.register_connection(nsnode1, p1)
85         q1 = ec.register_resource("ns3::DropTailQueue")
86         ec.register_connection(nsnode1, q1)
87
88         nsnode2 = ec.register_resource("ns3::Node")
89         ec.register_connection(nsnode2, simu)
90
91         ipv42 = ec.register_resource("ns3::Ipv4L3Protocol")
92         ec.register_connection(nsnode2, ipv42)
93
94         arp2 = ec.register_resource("ns3::ArpL3Protocol")
95         ec.register_connection(nsnode2, arp2)
96         
97         icmp2 = ec.register_resource("ns3::Icmpv4L4Protocol")
98         ec.register_connection(nsnode2, icmp2)
99
100         p2 = ec.register_resource("ns3::PointToPointNetDevice")
101         ec.set(p2, "ip", "10.0.0.2")
102         ec.set(p2, "prefix", "30")
103         ec.register_connection(nsnode2, p2)
104         q2 = ec.register_resource("ns3::DropTailQueue")
105         ec.register_connection(nsnode2, q2)
106
107         # Create channel
108         chan = ec.register_resource("ns3::PointToPointChannel")
109         ec.set(chan, "Delay", "0s")
110         ec.register_connection(chan, p1)
111         ec.register_connection(chan, p2)
112
113         ### create pinger
114         ping = ec.register_resource("ns3::V4Ping")
115         ec.set (ping, "Remote", "10.0.0.2")
116         ec.set (ping, "Interval", "1s")
117         ec.set (ping, "Verbose", True)
118         ec.set (ping, "StartTime", "0s")
119         ec.set (ping, "StopTime", "20s")
120         ec.register_connection(ping, nsnode1)
121
122         ec.deploy()
123
124         time.sleep(5)
125
126         ec.shutdown()
127  
128
129 if __name__ == '__main__':
130     unittest.main()
131