def test_add_initial_rule_in_db(self):
        # With empty DB, add initial rule, then call initial rules complete
        # Verify that rule was added
        m = LCRuleManager()
        cookie1 = 1
        switch_id1 = 10
        rule1 = "FAKE RULE"
        status1 = RULE_STATUS_INSTALLING
        m.add_initial_rule(rule1, cookie1, switch_id1)

        (del_list, add_list) = m.initial_rules_complete()
        m.clear_initial_rules()
        print "  ^^^^ add_list: %s" % add_list
        print "  ^^^^ del_list: %s" % del_list

        #for (r, c, sw) in add_list:
        for rule in add_list:
            (r, c, sw) = rule
            print " ^^^^ ASDF       %s:%s:%s" % (r, c, sw)
            s = RULE_STATUS_INSTALLING
            m.add_rule(c, sw, r, s)
        for (r, c, sw) in del_list:
            m.rm_rule(c, sw)

        rules = m.get_rules(cookie1, switch_id1, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(r, rule1)
    def test_initial_rule_rm_rule(self):
        # Add two rules, add only one of them as an initial rule, call
        # initial rules complete, confirm that one still exists and the other is
        # removed.
        m = LCRuleManager()
        rule1 = "FAKE RULE"
        cookie1 = 1
        switch_id1 = 10
        status1 = RULE_STATUS_INSTALLING
        m.add_rule(cookie1, switch_id1, rule1, status1)

        rules = m.get_rules(cookie1, switch_id1, True)
        self.failIfEqual(rules, [])
        print "    ^&^&^&^&^ %s" % rules
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(rule1, r)

        rule2 = "TOTALLY REAL RULE"
        cookie2 = 2
        switch_id2 = 20
        status2 = RULE_STATUS_INSTALLING
        m.add_rule(cookie2, switch_id2, rule2, status2)

        rules = m.get_rules(cookie1, switch_id1, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            print "  $$$$ RULE: %s:%s:%s" % (c, sw, r)
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(rule1, r)
        rules = m.get_rules(cookie2, switch_id2, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            print "  $$$$ RULE: %s:%s:%s" % (c, sw, r)
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(rule2, r)

        m.add_initial_rule(rule1, cookie1, switch_id1)

        (del_list, add_list) = m.initial_rules_complete()
        m.clear_initial_rules()
        for (r, c, sw) in add_list:
            s = RULE_STATUS_INSTALLING
            m.add_rule(c, sw, r, s)
        for (r, c, sw) in del_list:
            m.rm_rule(c, sw)

        rules = m.get_rules(cookie1, switch_id1, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(r, rule1)

        rules = m.get_rules(cookie2, switch_id2, True)
        self.failUnlessEqual([], rules)
    def test_remove_known_rule(self):
        m = LCRuleManager()
        rule1 = "FAKE RULE"
        cookie1 = 1
        switch_id1 = 10
        status1 = RULE_STATUS_INSTALLING
        m.add_rule(cookie1, switch_id1, rule1, status1)

        pre_rule = m.get_rules(cookie1, switch_id1)
        self.failUnlessEqual(pre_rule, [rule1])

        m.rm_rule(cookie1, switch_id1)
        post_rule = m.get_rules(cookie1, switch_id1)
        self.failUnlessEqual(post_rule, [])
    def test_duplicate_remove_rule(self):
        m = LCRuleManager()
        rule1 = "FAKE RULE"
        cookie1 = 1
        switch_id1 = 10
        status1 = RULE_STATUS_INSTALLING
        m.add_rule(cookie1, switch_id1, rule1, status1)

        pre_rule = m.get_rules(cookie1, switch_id1)
        self.failUnlessEqual(pre_rule, [rule1])

        m.rm_rule(cookie1, switch_id1)
        post_rule = m.get_rules(cookie1, switch_id1)
        self.failUnlessEqual(post_rule, [])

        self.failUnlessRaises(LCRuleManagerDeletionError, m.rm_rule, cookie1,
                              switch_id1)
    def test_initial_rule_twice_rule(self):
        # Empty DB, add rule with initial_rules, remove same rule so DB's empty
        # again, add a different initial_rule, confirm that only the second
        # initial rule is added
        m = LCRuleManager()
        rule1 = "FAKE RULE"
        cookie1 = 1
        switch_id1 = 10
        status1 = RULE_STATUS_INSTALLING

        rule2 = "TOTALLY REAL RULE"
        cookie2 = 2
        switch_id2 = 20
        status2 = RULE_STATUS_INSTALLING

        m.add_initial_rule(rule1, cookie1, switch_id1)

        (del_list, add_list) = m.initial_rules_complete()
        m.clear_initial_rules()
        for (r, c, sw) in add_list:
            s = RULE_STATUS_INSTALLING
            m.add_rule(c, sw, r, s)
        for (r, c, sw) in del_list:
            m.rm_rule(c, sw)

        rules = m._find_rules()
        self.failUnlessEqual(1, len(rules))
        rules = m.get_rules(cookie1, switch_id1, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(rule1, r)
        rules = m.get_rules(cookie2, switch_id2)
        self.failUnlessEqual(rules, [])

        m.rm_rule(cookie1, switch_id1)

        rules = m._find_rules()
        self.failUnlessEqual(0, len(rules))  #empty

        m.add_initial_rule(rule2, cookie2, switch_id2)

        (del_list, add_list) = m.initial_rules_complete()
        m.clear_initial_rules()
        for (r, c, sw) in add_list:
            s = RULE_STATUS_INSTALLING
            m.add_rule(c, sw, r, s)
        for (r, c, sw) in del_list:
            m.rm_rule(c, sw)

        rules = m._find_rules()
        print "((((((((RULES %s" % rules
        self.failUnlessEqual(1, len(rules))
        rules = m.get_rules(cookie1, switch_id1)
        self.failUnlessEqual([], rules)

        rules = m.get_rules(cookie2, switch_id2, True)
        self.failIfEqual(rules, [])
        for rule in rules:
            (c, sw, r, s) = rule
            self.failUnlessEqual(rule2, r)