summaryrefslogtreecommitdiff
path: root/designate/tests/unit/scheduler/test_basic.py
blob: 156a29add1e96890fb7ffe61cc0b9ed3fcff2fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# (c) Copyright 2016 Hewlett Packard Enterprise Development Company LP.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock

from designate import exceptions
from designate import objects
from designate import scheduler
from designate import tests


class SchedulerTest(tests.TestCase):

    def setUp(self):
        super(SchedulerTest, self).setUp()
        self.context = self.get_context()
        self.zone = objects.Zone(
            name="example.com.",
            type="PRIMARY",
            email="hostmaster@example.com"
        )

    def test_default_operation(self):
        attrs = {
            'find_pools.return_value': objects.PoolList.from_list(
                [{"id": "794ccc2c-d751-44fe-b57f-8894c9f5c842"}])
        }
        mock_storage = mock.Mock(**attrs)

        test_scheduler = scheduler.get_scheduler(storage=mock_storage)

        self.zone.pool_id = test_scheduler.schedule_zone(self.context,
                                                         self.zone)

        self.assertEqual(self.zone.pool_id,
                         "794ccc2c-d751-44fe-b57f-8894c9f5c842")

    def test_multiple_pools(self):
        attrs = {
            'find_pools.return_value': objects.PoolList.from_list(
                [
                    {"id": "794ccc2c-d751-44fe-b57f-8894c9f5c842"},
                    {"id": "5fabcd37-262c-4cf3-8625-7f419434b6df"}
                ]
            )
        }

        mock_storage = mock.Mock(**attrs)

        test_scheduler = scheduler.get_scheduler(storage=mock_storage)

        self.zone.pool_id = test_scheduler.schedule_zone(self.context,
                                                         self.zone)

        self.assertIn(
            self.zone.pool_id,
            [
                "794ccc2c-d751-44fe-b57f-8894c9f5c842",
                "5fabcd37-262c-4cf3-8625-7f419434b6df",
            ]
        )

    def test_no_pools(self):
        attrs = {
            'find_pools.return_value': objects.PoolList()
        }
        mock_storage = mock.Mock(**attrs)

        self.CONF.set_override(
            'scheduler_filters', ['random'], 'service:central'
        )

        test_scheduler = scheduler.get_scheduler(storage=mock_storage)

        self.assertRaisesRegex(
            exceptions.NoValidPoolFound,
            'There are no pools that matched your request',
            test_scheduler.schedule_zone, self.context, self.zone,
        )

    def test_no_filters_enabled(self):
        self.CONF.set_override(
            'scheduler_filters', [], 'service:central'
        )

        attrs = {
            'find_pools.return_value': objects.PoolList()
        }
        mock_storage = mock.Mock(**attrs)

        self.assertRaisesRegex(
            exceptions.NoFiltersConfigured,
            'There are no scheduling filters configured',
            scheduler.get_scheduler, mock_storage,
        )