def test_bucket_on_multi_project_no_vio(self):
        """Test that a rule with a resource.type equal to 'project' does not
        produce violations when the resource_ids includes more than one projects"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_bucket_retention_on_multi_projects)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket_1', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=90)
        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])

        data_creater = frsd.FakeBucketDataCreater('fake_bucket_2', frsd.PROJECT2)
        data_creater.AddLifecycleDict(action="Delete", age=90)
        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_min_less_else(self):
        """Test that a rule whose action is not 'Delete' cannot break minimum_retention"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="SetStorageClass", age=89)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_min_no_condition(self):
        """Test that a rule with minimum_retention does not produce violations.
        The minimum_retention is guaranteed when there is no condition at all"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_min_normal_del(self):
        """Test that a rule with minimum_retention does not produce violations."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=90)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_max_normal_delete(self):
        """Test that a bucket's rule can guarantee the maximum_retention if its
        action is 'Delete' and the only condition is an age(<= maximum_retention)"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=365)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_bucket_on_incorrect_project_no_vio(self):
        """Test that a rule with a resource.type equal to 'project' does not
        produce violations because the project ID does not match"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_bucket_retention_on_wrong_project)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=90)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_min_ver1_del(self):
        """Test that a rule with minimum_retention does not produce violations.
        A bucket's rule cannot break minimum_retention, if its number of newer versions
        is larger than 0"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", num_newer_versions=1)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_bucket_on_correct_project_has_vio(self):
        """Test that a rule with a resource.type equal to 'project' produces violations."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_bucket_retention_on_correct_project)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=89)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'bucket retention on correct project')
        self.assertEqual(got_violations, expected_violations)
    def test_only_max_normal_del_lgr_del(self):
        """Test that a bucket could have more than one rules. If one of them can
        guarantee the maximum_retention, there is no violation."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=365)
        data_creater.AddLifecycleDict(action="Delete", age=366)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_max_no_condition(self):
        """Test that a rule with maximum_retention produces a violation,
        if a bucket has no condition at all."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_both_min_max_3_conditions(self):
        """Test that a rule with both minimum_retention and maximum_retention
        does not produce violations when there are more than one conditions."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_both_min_and_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=100)
        data_creater.AddLifecycleDict(action="SetStorageClass", age=89)
        data_creater.AddLifecycleDict(action="Delete", age=500)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_max_larger_delete(self):
        """Test that a bucket's rule cannot guarantee the maximum_retention
        if its age condition is larger than maximum_retention"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=366)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_both_min_max_no_condition(self):
        """Test that a rule with both minimum_retention and maximum_retention
        produces violations. A bucket's rule break it, if the bucket breakes the
        maximum_retention part."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_both_min_and_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'both min and max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_min_old_del(self):
        """Test that a rule with minimum_retention does not produce violations.
        A bucket's rule cannot break minimum_retention, if its created before time
        is earlier than the date that is today minus minimum_retention"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        d = datetime.today() - timedelta(days=90)
        dstr = d.strftime('%Y-%m-%d')
        data_creater.AddLifecycleDict(action="Delete", created_before=dstr)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_max_anynormal_del(self):
        """Test that a rule with maximum_retention produces a violation.
        If a condition whose age comes along with any other conditions, it cannot
        guarantee the maximum_retention"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=365, num_newer_versions=5)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_min_ver0_del(self):
        """Test that a rule with minimum_retention produces violations.
        A bucket's rule may break minimum_retention, if its number of newer versions
        is equal to 0"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", num_newer_versions=0)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only min retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_min_less_del(self):
        """Test that a rule with minimum_retention produces violations.
        A bucket's rule breaks minimum_retention, if its age is smaller than
        minimum_retention and its action is 'Delete'"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=89)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only min retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_max_normal_del_anynormal_del(self):
        """Test that a bucket's rules can guarantee the maximum_retention
        if they include a rule whose action is 'Delete' and the only condition
        is an age(<= maximum_retention)"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=365)
        d = datetime.today() - timedelta(days=90)
        dstr = d.strftime('%Y-%m-%d')
        data_creater.AddLifecycleDict(action="Delete", age=365, created_before=dstr)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        self.assertEqual(got_violations, [])
    def test_only_min_less_del_normal_del(self):
        """Test that a rule with minimum_retention produces violations.
        A rule that does not produce violations cannot prevent another rule from
        producing violations"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=89)
        data_creater.AddLifecycleDict(action="Delete", age=90)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only min retention')
        self.assertEqual(got_violations, expected_violations)
    def test_both_min_max_normal_del_any_del(self):
        """Test that a rule with both minimum_retention and maximum_retention
        produces violations. A bucket's rule break it, if the bucket breakes the
        minimum_retention part."""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_both_min_and_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=100)
        data_creater.AddLifecycleDict(action="Delete", is_live=True)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'both min and max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_max_lgr_del_anynormal_del(self):
        """Test that a bucket's rule cannot guarantee the maximum_retention
        if its age comes along with any other conditions"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_max_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        data_creater.AddLifecycleDict(action="Delete", age=366)
        d = datetime.today() - timedelta(days=90)
        dstr = d.strftime('%Y-%m-%d')
        data_creater.AddLifecycleDict(action="Delete", age=365, created_before=dstr)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only max retention')
        self.assertEqual(got_violations, expected_violations)
    def test_only_min_new_del(self):
        """Test that a rule with minimum_retention produces violations.
        A bucket's rule may break minimum_retention, if its created before time
        is later than today minus minimum_retention"""
        rules_engine = get_rules_engine_with_rule(RetentionRulesEngineTest.yaml_str_only_min_retention)

        data_creater = frsd.FakeBucketDataCreater('fake_bucket', frsd.PROJECT1)
        d = datetime.today() - timedelta(days=88)
        dstr = d.strftime('%Y-%m-%d')
        data_creater.AddLifecycleDict(action="Delete", created_before=dstr)

        fake_bucket = data_creater.get_resource()
        got_violations = list(rules_engine.find_violations(fake_bucket))
        expected_violations = frsd.build_bucket_violations(
            fake_bucket, 'only min retention')
        self.assertEqual(got_violations, expected_violations)