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