1
|
|
|
"""Module to test the schedule.py file.""" |
2
|
|
|
import datetime |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import patch |
5
|
|
|
|
6
|
|
|
from apscheduler.triggers.cron import CronTrigger |
7
|
|
|
from pytz import utc |
8
|
|
|
|
9
|
|
|
from napps.kytos.mef_eline.models import EVC |
10
|
|
|
from napps.kytos.mef_eline.scheduler import CircuitSchedule, Scheduler |
11
|
|
|
from napps.kytos.mef_eline.tests.helpers import get_controller_mock |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class TestCircuitSchedule(TestCase): |
15
|
|
|
"""Tests to verify circuit_schedule class.""" |
16
|
|
|
|
17
|
|
|
def test_id(self): |
18
|
|
|
"""Test method id with different values.""" |
19
|
|
|
self.assertNotEqual(CircuitSchedule().id, CircuitSchedule().id) |
20
|
|
|
|
21
|
|
|
def test_with_date(self): |
22
|
|
|
"""Test create circuit schedule with date.""" |
23
|
|
|
time_fmt = "%Y-%m-%dT%H:%M:%S" |
24
|
|
|
options = { |
25
|
|
|
"action": "create", |
26
|
|
|
"date": datetime.datetime.now().strftime(time_fmt), |
27
|
|
|
} |
28
|
|
|
circuit_schedule = CircuitSchedule(**options) |
29
|
|
|
self.assertEqual("create", circuit_schedule.action) |
30
|
|
|
self.assertEqual(options["date"], circuit_schedule.date) |
31
|
|
|
|
32
|
|
|
def test_with_interval(self): |
33
|
|
|
"""Test create circuit schedule with interval.""" |
34
|
|
|
options = {"action": "create", "interval": {"hours": 2}} |
35
|
|
|
circuit_schedule = CircuitSchedule(**options) |
36
|
|
|
self.assertEqual("create", circuit_schedule.action) |
37
|
|
|
self.assertEqual(options["interval"], circuit_schedule.interval) |
38
|
|
|
|
39
|
|
|
def test_with_frequency(self): |
40
|
|
|
"""Test create circuit schedule with frequency.""" |
41
|
|
|
options = {"action": "create", "frequency": "1 * * * *"} |
42
|
|
|
circuit_schedule = CircuitSchedule(**options) |
43
|
|
|
self.assertEqual("create", circuit_schedule.action) |
44
|
|
|
self.assertEqual(options["frequency"], circuit_schedule.frequency) |
45
|
|
|
|
46
|
|
|
def test_from_dict(self): |
47
|
|
|
"""Test create circuit schedule from dict.""" |
48
|
|
|
circuit_schedule_dict = { |
49
|
|
|
"id": 52342432, |
50
|
|
|
"action": "create", |
51
|
|
|
"frequency": "1 * * * *", |
52
|
|
|
} |
53
|
|
|
circuit_schedule = CircuitSchedule.from_dict(circuit_schedule_dict) |
54
|
|
|
self.assertEqual(circuit_schedule.id, circuit_schedule_dict["id"]) |
55
|
|
|
self.assertEqual( |
56
|
|
|
circuit_schedule.action, circuit_schedule_dict["action"] |
57
|
|
|
) |
58
|
|
|
self.assertEqual( |
59
|
|
|
circuit_schedule.frequency, circuit_schedule_dict["frequency"] |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
def test_as_dict(self): |
63
|
|
|
"""Test method as_dict from circuit_schedule.""" |
64
|
|
|
options = { |
65
|
|
|
"id": 234243242, |
66
|
|
|
"action": "create", |
67
|
|
|
"frequency": "1 * * * *", |
68
|
|
|
} |
69
|
|
|
circuit_schedule_dict = CircuitSchedule(**options).as_dict() |
70
|
|
|
self.assertEqual(options, circuit_schedule_dict) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
class TestScheduler(TestCase): |
74
|
|
|
"""Class to test the structure Schedule.""" |
75
|
|
|
|
76
|
|
|
def setUp(self): |
77
|
|
|
"""Procedure executed before each test.""" |
78
|
|
|
self.scheduler = Scheduler() |
79
|
|
|
|
80
|
|
|
def tearDown(self): |
81
|
|
|
"""Proedure executed after each test.""" |
82
|
|
|
self.scheduler.shutdown() |
83
|
|
|
|
84
|
|
|
@patch("apscheduler.schedulers.background.BackgroundScheduler.add_job") |
85
|
|
|
@patch("napps.kytos.mef_eline.models.EVC._validate") |
86
|
|
|
def test_new_circuit_with_run_time( |
87
|
|
|
self, validate_mock, scheduler_add_job_mock |
88
|
|
|
): |
89
|
|
|
"""Test if add new circuit with run_time.""" |
90
|
|
|
scheduler_add_job_mock.return_value = True |
91
|
|
|
validate_mock.return_value = True |
92
|
|
|
time_fmt = "%Y-%m-%dT%H:%M:%S" |
93
|
|
|
date = datetime.datetime.now().strftime(time_fmt) |
94
|
|
|
circuit_scheduler = CircuitSchedule(action="remove", date=date) |
95
|
|
|
options = { |
96
|
|
|
"controller": get_controller_mock(), |
97
|
|
|
"name": "my evc1", |
98
|
|
|
"uni_a": "uni_a", |
99
|
|
|
"uni_z": "uni_z", |
100
|
|
|
"circuit_scheduler": [circuit_scheduler], |
101
|
|
|
} |
102
|
|
|
evc = EVC(**options) |
103
|
|
|
self.scheduler.add(evc) |
104
|
|
|
expected_parameters = { |
105
|
|
|
"id": circuit_scheduler.id, |
106
|
|
|
"run_date": circuit_scheduler.date, |
107
|
|
|
} |
108
|
|
|
scheduler_add_job_mock.assert_called_once_with( |
109
|
|
|
evc.remove, "date", **expected_parameters |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
@patch("apscheduler.schedulers.background.BackgroundScheduler.add_job") |
113
|
|
|
@patch("napps.kytos.mef_eline.models.EVC._validate") |
114
|
|
|
def test_new_circuit_with_interval( |
115
|
|
|
self, validate_mock, scheduler_add_job_mock |
116
|
|
|
): |
117
|
|
|
"""Test if add new circuit with interval.""" |
118
|
|
|
scheduler_add_job_mock.return_value = True |
119
|
|
|
validate_mock.return_value = True |
120
|
|
|
interval = {"hours": 2, "minutes": 3} |
121
|
|
|
circuit_scheduler = CircuitSchedule(action="create", interval=interval) |
122
|
|
|
options = { |
123
|
|
|
"controller": get_controller_mock(), |
124
|
|
|
"name": "my evc1", |
125
|
|
|
"uni_a": "uni_a", |
126
|
|
|
"uni_z": "uni_z", |
127
|
|
|
"start_date": "2019-08-09T19:25:06", |
128
|
|
|
"circuit_scheduler": [circuit_scheduler], |
129
|
|
|
} |
130
|
|
|
evc = EVC(**options) |
131
|
|
|
self.scheduler.add(evc) |
132
|
|
|
|
133
|
|
|
expected_parameters = { |
134
|
|
|
"id": circuit_scheduler.id, |
135
|
|
|
"hours": 2, |
136
|
|
|
"minutes": 3, |
137
|
|
|
"end_date": None, |
138
|
|
|
"start_date": datetime.datetime( |
139
|
|
|
2019, 8, 9, 19, 25, 6, 0, tzinfo=datetime.timezone.utc |
140
|
|
|
), |
141
|
|
|
} |
142
|
|
|
scheduler_add_job_mock.assert_called_once_with( |
143
|
|
|
evc.deploy, "interval", **expected_parameters |
144
|
|
|
) |
145
|
|
|
|
146
|
|
|
@patch("apscheduler.triggers.cron.CronTrigger.from_crontab") |
147
|
|
|
@patch("apscheduler.schedulers.background.BackgroundScheduler.add_job") |
148
|
|
|
@patch("napps.kytos.mef_eline.models.EVC._validate") |
149
|
|
|
def test_new_circuit_with_frequency( |
150
|
|
|
self, validate_mock, scheduler_add_job_mock, trigger_mock |
151
|
|
|
): |
152
|
|
|
"""Test if add new circuit with frequency.""" |
153
|
|
|
scheduler_add_job_mock.return_value = True |
154
|
|
|
validate_mock.return_value = True |
155
|
|
|
|
156
|
|
|
frequency = "* * * * *" |
157
|
|
|
circuit_scheduler = CircuitSchedule( |
158
|
|
|
action="create", frequency=frequency |
159
|
|
|
) |
160
|
|
|
|
161
|
|
|
trigger = CronTrigger.from_crontab( |
162
|
|
|
circuit_scheduler.frequency, timezone=utc |
163
|
|
|
) |
164
|
|
|
trigger_mock.return_value = trigger |
165
|
|
|
|
166
|
|
|
options = { |
167
|
|
|
"controller": get_controller_mock(), |
168
|
|
|
"name": "my evc1", |
169
|
|
|
"uni_a": "uni_a", |
170
|
|
|
"uni_z": "uni_z", |
171
|
|
|
"start_date": "2019-08-09T19:25:06", |
172
|
|
|
"circuit_scheduler": [circuit_scheduler], |
173
|
|
|
} |
174
|
|
|
evc = EVC(**options) |
175
|
|
|
self.scheduler.add(evc) |
176
|
|
|
expected_parameters = { |
177
|
|
|
"id": circuit_scheduler.id, |
178
|
|
|
"end_date": None, |
179
|
|
|
"start_date": datetime.datetime( |
180
|
|
|
2019, 8, 9, 19, 25, 6, 0, tzinfo=datetime.timezone.utc |
181
|
|
|
), |
182
|
|
|
} |
183
|
|
|
scheduler_add_job_mock.assert_called_once_with( |
184
|
|
|
evc.deploy, trigger, **expected_parameters |
185
|
|
|
) |
186
|
|
|
|