Beispiel #1
0
    def test_bucket_retention_on_multi_buckets(self):
        """Test a rule that includes more than one bucket IDs"""

        rule_yaml = """
rules:
  - name: multiple buckets in a single rule
    applies_to:
      - bucket
    resource:
      - type: bucket
        resource_ids:
          - fake-bucket-1
          - fake-bucket-2
          - fake-bucket-3
    minimum_retention: 90

"""

        bucket_test_data=[
            frsd.FakeBucketDataInput(
                id='fake-bucket-1',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':89})]
            ),
            frsd.FakeBucketDataInput(
                id='fake-bucket-2',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':90})]
            ),
            frsd.FakeBucketDataInput(
                id='fake-bucket-3',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':89})]
            ),
            frsd.FakeBucketDataInput(
                id='fake-bucket-4',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':89})]
            ),
        ]

        _mock_bucket = get_mock_bucket_retention(bucket_test_data)

        with tempfile.NamedTemporaryFile(suffix='.yaml') as f:
            f.write(rule_yaml)
            f.flush()
            _fake_bucket_list = _mock_bucket(None, 'bucket')

            self.scanner = retention_scanner.RetentionScanner(
                {}, {}, mock.MagicMock(), '', '', f.name)

            mock_data_access = mock.MagicMock()
            mock_data_access.scanner_iter.side_effect = _mock_bucket

            mock_service_config = mock.MagicMock()
            mock_service_config.model_manager = mock.MagicMock()
            mock_service_config.model_manager.get.return_value = (
                mock.MagicMock(), mock_data_access)
            self.scanner.service_config = mock_service_config

            all_lifecycle_info = self.scanner._retrieve()
            all_violations = self.scanner._find_violations(all_lifecycle_info)

            res_map = {}
            for i in _fake_bucket_list:
                res_map[i.id] = i

            expected_violations = set([
                get_expect_violation_item(res_map, 'fake-bucket-1',
                                        'multiple buckets in a single rule', 0),
                get_expect_violation_item(res_map, 'fake-bucket-3',
                                        'multiple buckets in a single rule', 0)
            ])

            self.assertEqual(expected_violations, set(all_violations))
Beispiel #2
0
    def test_bucket_retention_on_multi_rules_on_a_project(self):
        """Test yaml file that includes more than one rules that works on the same project"""

        rule_yaml = """
rules:
  - name: project 1 min 90
    applies_to:
      - bucket
    resource:
      - type: project
        resource_ids:
          - def-project-1
    minimum_retention: 90
  - name: project 1 min 100
    applies_to:
      - bucket
    resource:
      - type: project
        resource_ids:
          - def-project-1
    minimum_retention: 100
  - name: project 1 min 110
    applies_to:
      - bucket
    resource:
      - type: project
        resource_ids:
          - def-project-1
    minimum_retention: 110

"""

        bucket_test_data=[
            frsd.FakeBucketDataInput(
                id='fake-bucket-11',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':90})]
            ),
            frsd.FakeBucketDataInput(
                id='fake-bucket-12',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':100})]
            ),
            frsd.FakeBucketDataInput(
                id='fake-bucket-13',
                project=frsd.PROJECT1,
                lifecycles=[frsd.LifecycleInput(action='Delete', conditions={'age':110})]
            ),
        ]

        _mock_bucket = get_mock_bucket_retention(bucket_test_data)

        with tempfile.NamedTemporaryFile(suffix='.yaml') as f:
            f.write(rule_yaml)
            f.flush()
            _fake_bucket_list = _mock_bucket(resource_type='bucket')

            self.scanner = retention_scanner.RetentionScanner(
                {}, {}, mock.MagicMock(), '', '', f.name)

            mock_data_access = mock.MagicMock()
            mock_data_access.scanner_iter.side_effect = _mock_bucket

            mock_service_config = mock.MagicMock()
            mock_service_config.model_manager = mock.MagicMock()
            mock_service_config.model_manager.get.return_value = (
                mock.MagicMock(), mock_data_access)
            self.scanner.service_config = mock_service_config

            all_lifecycle_info = self.scanner._retrieve()
            all_violations = self.scanner._find_violations(all_lifecycle_info)

            res_map = {}
            for i in _fake_bucket_list:
                res_map[i.id] = i

            expected_violations = set([
                get_expect_violation_item(res_map, 'fake-bucket-11',
                                        'project 1 min 100', 1),
                get_expect_violation_item(res_map, 'fake-bucket-11',
                                        'project 1 min 110', 2),
                get_expect_violation_item(res_map, 'fake-bucket-12',
                                        'project 1 min 110', 2)
            ])

            self.assertEqual(expected_violations, set(all_violations))