def test_push_pop_vlan(self): """Test push pop combo is ignored rule1 rule2 M: VLAN_VID:1 M: VLAN_VID:1 A: PUSH_VLAN, VLAN_VID:2, POP_VLAN A: Expected rule1 Then try combos without rule1 match, and without rule2 match """ rule1 = Rule(priority=0, match=self.vlan_vid_1) rule1.instructions.apply_actions.append("PUSH_VLAN", 0x800) rule1.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 2)) rule1.instructions.apply_actions.append("POP_VLAN", None) rule2 = Rule(priority=0, match=self.vlan_vid_1) self.assertEqual(rule1.merge(rule2, False), rule1) # Check without rule2 match self.assertEqual(rule1.merge(Rule(priority=0), False), rule1) # Check without rule1 match rule1_nomatch = Rule(priority=0) rule1_nomatch.instructions.apply_actions = rule1.instructions.apply_actions.copy( ) self.assertEqual(rule1_nomatch.merge(rule2, False), rule1) # The resulting rule is valid openflow self.assertEqual(rule1_nomatch.merge(rule2, True), rule1) self.assertEqual(rule1_nomatch + rule2, rule1)
def test_merging_priorities(self): """ Merging priorities is supposed to add priorities. Doing this works with merging rules (which have already been priority adjusted) to result in the correct priority. """ rule1 = Rule(priority=1) rule2 = Rule(priority=2) rule100 = Rule(priority=100) rule200 = Rule(priority=200) self.assertEqual(rule1.merge(rule1).priority, 2) self.assertEqual(rule1.merge(rule2).priority, 3) self.assertEqual(rule200.merge(rule100).priority, 300) self.assertEqual((rule200 + rule100).priority, 300)
def test_pop_vlan(self): """ Test the POP_VLAN, MATCH VLAN_VID case """ # Test single pop, i.e. match 2nd header rule1 = Rule(priority=0, match=self.vlan_vid_1) rule1.instructions.apply_actions.append("POP_VLAN", None) rule2 = Rule(priority=0, match=self.vlan_vid_2) expt_m = Match([("VLAN_VID", 0x1001, None), ("VLAN_VID1", 0x1002, None)]) expt = Rule(priority=0, match=expt_m) expt.instructions.apply_actions.append("POP_VLAN", None) self.assertEqual(rule1.merge(rule2, False), expt) # Merging fails to return a valid openflow rule self.assertRaises(MergeException, lambda: rule1.merge(rule2, True)) self.assertRaises(MergeException, lambda: rule1 + rule2)
def test_push_vlan(self): """ Push and match, later pop """ rule1 = Rule(priority=0) rule1.instructions.apply_actions.append("PUSH_VLAN", 0x800) rule1.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 6)) rule2 = Rule(priority=0, match=Match([('VLAN_VID', 0x1006, None), ('VLAN_VID1', 0x1002, None)])) rule2.instructions.apply_actions.append("POP_VLAN", None) rule2.instructions.apply_actions.append("OUTPUT", 1) expt = Rule(priority=0, match=Match([('VLAN_VID', 0x1002, None)])) expt.instructions.apply_actions.append("PUSH_VLAN", 0x800) expt.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 6)) expt.instructions.apply_actions.append("POP_VLAN", None) expt.instructions.apply_actions.append("OUTPUT", 1) self.assertEqual(rule1.merge(rule2, False), expt) # The resulting rule is valid openflow self.assertEqual(rule1.merge(rule2, True), expt) self.assertEqual(rule1 + rule2, expt)
def test_simple_merge_match(self): """ Test merging matches works """ fsrc2 = Rule(priority=0, match=self.ipv4_src2) fdst2 = Rule(priority=0, match=self.ipv4_dst2) fsrc1 = Rule(priority=0, match=self.ipv4_src1) f_expected = Rule(priority=0, match=self.ipv4_src2_dst2) # Merge with two different fields self.assertEqual(fsrc2.merge(fdst2), f_expected) self.assertEqual(fdst2.merge(fsrc2), f_expected) self.assertEqual(fsrc2 + fdst2, f_expected) self.assertEqual(fdst2 + fsrc2, f_expected) # Merge self with self self.assertEqual(fsrc2 + fsrc2, fsrc2) # Sanity checks self.assertNotEqual(fsrc2, fdst2) self.assertNotEqual(fsrc2 + fsrc2, fdst2) # Exception if match set becomes empty IP:1 intersect IP:2 self.assertRaises(MergeException, lambda: fsrc2 + fsrc1) self.assertRaises(MergeException, lambda: (fdst2 + fsrc1) + fsrc2)
def test_push_pop_vlan_complex(self): """ Test complex combinations work associatively # rule1 # M: 1 # A: SET:2 # rule2 # M: * # A: Push: 6 # rule3 # M: 6 # A: POP # rule4 # M: 2 # A: Output:1 """ rule1 = Rule(priority=0, match=self.vlan_vid_1) rule1.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 2)) rule2 = Rule(priority=0) rule2.instructions.apply_actions.append("PUSH_VLAN", 0x800) rule2.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 6)) rule3 = Rule(priority=0, match=self.vlan_vid_6) rule3.instructions.apply_actions.append("POP_VLAN", None) rule4 = Rule(priority=0, match=self.vlan_vid_2) rule4.instructions.apply_actions.append("OUTPUT", 1) expt = Rule(priority=0, match=self.vlan_vid_1) expt.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 2)) expt.instructions.apply_actions.append("PUSH_VLAN", 0x800) expt.instructions.apply_actions.append("SET_FIELD", ('VLAN_VID', 6)) expt.instructions.apply_actions.append("POP_VLAN", None) expt.instructions.apply_actions.append("OUTPUT", 1) self.assertEqual( rule1.merge(rule2, False).merge(rule3, False).merge(rule4, False), expt) self.assertEqual( rule1.merge(rule2.merge(rule3, False).merge(rule4, False), False), expt) self.assertEqual( rule1.merge(rule2.merge(rule3.merge(rule4, False), False), False), expt) self.assertEqual( rule1.merge(rule2.merge(rule3, False), False).merge(rule4, False), expt)