def test_bucket_on_multi_project_has_vio(self): """Test that a rule with a resource.type equal to 'project' produces 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=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 multi projects') self.assertEqual(got_violations, expected_violations) data_creater = frsd.FakeBucketDataCreater('fake_bucket_2', frsd.PROJECT2) 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 multi projects') self.assertEqual(got_violations, expected_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_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_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_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_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_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_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_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_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)
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)