#!/usr/bin/python
from subprocess import Popen, PIPE
from MininetDriver import *
from time import sleep

class RepeaterTest(unittest.TestCase):
  
  def setUp(self):
    kill_controllers()
    self.mn = MininetTest(TreeTopo(depth=2,fanout=2),
                          Popen([CONTROLLER_PATH, "--repeater"]))

  def tearDown(self):
    self.mn.destroy()

  def testPing(self):
    for src in self.mn.hosts:
      for dst in self.mn.hosts:
        if src == dst:
          continue # OpenFlow flood doesn't send through inport
        self.assertEqual(self.mn.ping(src, dst, 0.5, 3), 0)

# A sanity-check: Reactive1Test tests reactive specialization by asserting
# that the controller receives a small number of PACKET_IN messages for a large
# number of pings. But, if we don't give the controller time to install the
# specialized rule after the first ping, it should see more PACKET_INs.
class Reactive1TestSanityCheck(unittest.TestCase):

  def setUp(self):
    kill_controllers()
    self.ctrl = Popen([CONTROLLER_PATH, "--reactive1"],stdout=PIPE)
    self.mn = MininetTest(TreeTopo(depth=1,fanout=2), self.ctrl)

  def tearDown(self):
    self.mn.destroy()

  def testSpecialization(self):
    src = self.mn.hosts[0]
    dst = self.mn.hosts[1]
    self.mn.ping(src, dst, 0.01, 100)
    self.ctrl.terminate()
    numPktIn = sum([1 for x in self.ctrl.stdout if "OFPT_PACKET_IN" in x])
    self.assertTrue(numPktIn > 5, "too few PACKET_IN messages")

class Query1(unittest.TestCase):

  def setUp(self):
    kill_controllers()
    self.ctrl = Popen([CONTROLLER_PATH, "--query1"],stdout=PIPE)
    self.mn = MininetTest(TreeTopo(depth=1,fanout=2), self.ctrl)

  def tearDown(self):
    self.mn.destroy()

  def testQuery(self):
    src = self.mn.hosts[0]
    dst = self.mn.hosts[1]
    self.assertEqual(self.mn.ping(src, dst, 1, 5), 0)
    sleep(2) # let the query pickup the last pair
    self.ctrl.terminate()
    output = list(self.ctrl.stdout)
    # 2 for initial ARP and 10 from the pings
    self.assertTrue("Counter is: 12" in output[-1], "expected 10 packets")


if __name__ == '__main__':
    unittest.main()

@@ -584,873 +584,8 @@
 0)%0A%0A
-# A sanity-check: Reactive1Test tests reactive specialization by asserting%0A# that the controller receives a small number of PACKET_IN messages for a large%0A# number of pings. But, if we don't give the controller time to install the%0A# specialized rule after the first ping, it should see more PACKET_INs.%0Aclass Reactive1TestSanityCheck(unittest.TestCase):%0A%0A  def setUp(self):%0A    kill_controllers()%0A    self.ctrl = Popen(%5BCONTROLLER_PATH, %22--reactive1%22%5D,stdout=PIPE)%0A    self.mn = MininetTest(TreeTopo(depth=1,fanout=2), self.ctrl)%0A%0A  def tearDown(self):%0A    self.mn.destroy()%0A%0A  def testSpecialization(self):%0A    src = self.mn.hosts%5B0%5D%0A    dst = self.mn.hosts%5B1%5D%0A    self.mn.ping(src, dst, 0.01, 100)%0A    self.ctrl.terminate()%0A    numPktIn = sum(%5B1 for x in self.ctrl.stdout if %22OFPT_PACKET_IN%22 in x%5D)%0A    self.assertTrue(numPktIn %3E 5, %22too few PACKET_IN messages%22)%0A
 %0Acla

test no longer relevant