From cee62eb28ec0aa8c38f3d483b536afde1c62d8ab Mon Sep 17 00:00:00 2001
From: Brian O'Connor <bocon@onlab.us>
Date: Wed, 13 Aug 2014 22:03:25 -0700
Subject: [PATCH] adding natnet example test

---
 examples/natnet.py           |  6 ++--
 examples/test/test_natnet.py | 57 ++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 examples/test/test_natnet.py

diff --git a/examples/natnet.py b/examples/natnet.py
index 7f51fd59..9fcc4bfd 100755
--- a/examples/natnet.py
+++ b/examples/natnet.py
@@ -7,7 +7,7 @@
            h0
            |
            s0
-           |        
+           |
     ----------------
     |              |
    nat1           nat2
@@ -26,7 +26,7 @@
 from mininet.util import irange
 
 class InternetTopo(Topo):
-    "TODO: Single switch connected to n hosts."
+    "Single switch connected to n hosts."
     def __init__(self, n=2, h=1, **opts):
         Topo.__init__(self, **opts)
 
@@ -57,7 +57,7 @@ def __init__(self, n=2, h=1, **opts):
             self.addLink(host, switch)
 
 def run():
-    "TODO: Create network and run simple performance test"
+    "Create network and run the CLI"
     topo = InternetTopo()
     net = Mininet(topo=topo)
     net.start()
diff --git a/examples/test/test_natnet.py b/examples/test/test_natnet.py
new file mode 100644
index 00000000..3addc92e
--- /dev/null
+++ b/examples/test/test_natnet.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+"""
+Test for natnet.py
+"""
+
+import unittest
+import pexpect
+from mininet.util import quietRun
+
+class testNATNet( unittest.TestCase ):
+
+    prompt = 'mininet>'
+
+    def setUp( self ):
+        self.net = pexpect.spawn( 'python -m mininet.examples.natnet' )
+        self.net.expect( self.prompt )
+
+    def testPublicPing( self ):
+        "Attempt to ping the public server (h0) from h1 and h2"
+        self.net.sendline( 'h1 ping -c 1 h0' )
+        self.net.expect ( '(\d+)% packet loss' )
+        percent = int( self.net.match.group( 1 ) ) if self.net.match else -1
+        self.assertEqual( percent, 0 )
+        self.net.expect( self.prompt )
+
+        self.net.sendline( 'h2 ping -c 1 h0' )
+        self.net.expect ( '(\d+)% packet loss' )
+        percent = int( self.net.match.group( 1 ) ) if self.net.match else -1
+        self.assertEqual( percent, 0 )
+        self.net.expect( self.prompt )
+
+    def testPrivatePing( self ):
+        "Attempt to ping h1 and h2 from public server"
+        self.net.sendline( 'h0 ping -c 1 -t 1 h1' )
+        result = self.net.expect ( [ 'unreachable', 'loss' ] )
+        self.assertEqual( result, 0 )
+        self.net.expect( self.prompt )
+
+        self.net.sendline( 'h0 ping -c 1 -t 1 h2' )
+        result = self.net.expect ( [ 'unreachable', 'loss' ] )
+        self.assertEqual( result, 0 )
+        self.net.expect( self.prompt )
+
+    def testPrivateToPrivatePing( self ):
+        "Attempt to ping from NAT'ed host h1 to NAT'ed host h2"
+        self.net.sendline( 'h1 ping -c 1 -t 1 h2' )
+        result = self.net.expect ( [ '[Uu]nreachable', 'loss' ] )
+        self.assertEqual( result, 0 )
+        self.net.expect( self.prompt )
+
+    def tearDown( self ):
+        self.net.sendline( 'exit' )
+        self.net.wait()
+
+if __name__ == '__main__':
+    unittest.main()
-- 
GitLab