def test_add_new_rule_to_existing(self):
        e1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "glacier",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
        e2 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="enabled",
                  state="present")
        existing_rules = [create_s3_lc_rule(e1), create_s3_lc_rule(e2)]

        a1 = dict(rule_id="rule3",
                  prefix="/f2",
                  expiration={"days": 7},
                  status="enabled",
                  state="present")
        ansible_rules = [a1, e1, e2]
        ret = calculate_net_rules(existing_rules, ansible_rules)
        self.assertEqual(3,
                         ret.rules.__len__(),
                         msg="Should contain 3 rules, only {} found".format(
                             ret.rules.__len__()))
        self.assertTrue(ret.changed)
        found_rule = None
        for rule in ret.rules:
            if rule.id == a1['rule_id']:
                found_rule = rule
                break
        self.assertIsNotNone(found_rule)
        self.assertTrue(is_same_rule(create_s3_lc_rule(a1), found_rule))
    def test_remove_rule_from_existing(self):
        e1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "glacier",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
        e2 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="enabled",
                  state="present")
        existing_rules = [create_s3_lc_rule(e1), create_s3_lc_rule(e2)]
        a1 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="enabled",
                  state="absent")
        ansible_rules = [a1]
        ret = calculate_net_rules(existing_rules, ansible_rules)
        self.assertTrue(ret.changed,
                        msg='rules not changed {}'.format(ret.rules))
        self.assertEqual(1,
                         ret.rules.__len__(),
                         msg="Should contain 1 rules, only {} found".format(
                             ret.rules.__len__()))

        found_rule = False
        for rule in ret.rules:
            if rule.id == a1['rule_id']:
                found_rule = rule
                break
        self.assertFalse(found_rule, msg='Remove not removed')
    def test_changed_rule_list(self):
        e1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "glacier",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
        e2 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="enabled",
                  state="present")
        existing_rules = [create_s3_lc_rule(e1), create_s3_lc_rule(e2)]

        a1 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 7},
                  status="enabled",
                  state="present")
        ansible_rules = [a1]

        ret = calculate_net_rules(existing_rules, ansible_rules)
        self.assertTrue(ret.changed)
        found_rule = None
        for rule in ret.rules:
            if rule.id == a1['rule_id']:
                found_rule = rule
                break
        self.assertIsNotNone(found_rule)
        self.assertTrue(is_same_rule(create_s3_lc_rule(a1), found_rule))
 def test_same_rule_id_mismatch(self):
     rule_a = create_s3_lc_rule({
         "rule_id": "myrule",
         "prefix": "/f1",
         "status": "enabled",
         "expiration": {
             "days": 10
         }
     })
     rule_b = create_s3_lc_rule({
         "rule_id": "myruler",
         "prefix": "/f1",
         "status": "enabled",
         "expiration": {
             "days": 10
         }
     })
     self.assertFalse(is_same_rule(rule_a, rule_b))
 def test_same_rule(self):
     rule_a = create_s3_lc_rule({
         "rule_id": "myrule",
         "prefix": None,
         "status": "enabled",
         "expiration": {
             "days": 10
         }
     })
     rule_b = create_s3_lc_rule({
         "rule_id": "myrule",
         "prefix": None,
         "status": "enabled",
         "expiration": {
             "days": 10
         }
     })
     self.assertTrue(is_same_rule(rule_a, rule_b))
    def test_disable_existing_rule(self):
        e1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "glacier",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
        e2 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="enabled",
                  state="present")
        existing_rules = [create_s3_lc_rule(e1), create_s3_lc_rule(e2)]

        a1 = dict(rule_id="rule2",
                  prefix="/f1",
                  expiration={"days": 31},
                  status="disabled",
                  state="present")
        a2 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "glacier",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
        ansible_rules = [a1, a2]

        ret = calculate_net_rules(existing_rules, ansible_rules)
        self.assertTrue(ret.changed)
        self.assertEqual(2,
                         ret.rules.__len__(),
                         msg="Should contain 2 rules, but {} found".format(
                             ret.rules.__len__()))
        found_rule = None
        for rule in ret.rules:
            if rule.id == a1['rule_id']:
                found_rule = rule
                break
        self.assertIsNotNone(found_rule)
        self.assertEqual(found_rule.status,
                         "Disabled",
                         msg="Rule should be disabled {}".format(found_rule))
 def test_same_rule_type_mismatch(self):
     rule_a = create_s3_lc_rule({
         "rule_id": "myrule",
         "prefix": None,
         "status": "enabled",
         "expiration": {
             "days": 10
         }
     })
     rule_b = create_s3_lc_rule({
         "rule_id": "myrule",
         "prefix": None,
         "status": "enabled",
         "transition": {
             "storage_class": "glacier",
             "days": 10
         }
     })
     self.assertFalse(is_same_rule(rule_a, rule_b))
 def test_same_rule_list(self):
     rule1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "frozen",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
     existing_rules = [create_s3_lc_rule(rule1)]
     ansible_rules = [rule1]
     ret = calculate_net_rules(existing_rules, ansible_rules)
     self.assertFalse(ret.changed)
 def test_new_rule_list(self):
     existing_rules = None
     rule1 = dict(rule_id="rule1",
                  transition={
                      "storage_class": "frozen",
                      "days": 90
                  },
                  status="enabled",
                  state="present")
     lc_rule = create_s3_lc_rule(rule1)
     ansible_rules = [rule1]
     ret = calculate_net_rules(existing_rules, ansible_rules)
     self.assertTrue(ret.changed)
     self.assertTrue(is_same_rule(lc_rule, ret.rules[0]))