Test Failed
Pull Request — master (#64)
by
unknown
03:02
created

build.tests.unit.test_models.TestMW.setUp()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 1
nop 1
1
"""Tests for the models module."""
2
import datetime
3
from unittest import TestCase
4
from unittest.mock import patch, MagicMock
5
6
from attrs import evolve
7
import pytz
8
from kytos.lib.helpers import get_controller_mock
9
from napps.kytos.maintenance.models import MaintenanceWindow as MW, Status
10
11
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z"
12
13
14
class TestMW(TestCase):
15
    """Test of the MaintenanceWindow class."""
16
17
    # pylint: disable=protected-access
18
19
    def setUp(self):
20
        """Initialize before tests are executed."""
21
        self.controller = get_controller_mock()
22
        self.start = datetime.datetime.now(pytz.utc)
23
        self.start += datetime.timedelta(days=1)
24
        self.end = self.start + datetime.timedelta(hours=6)
25
        self.switches = [
26
            "01:23:45:67:89:ab:cd:ef"
27
        ]
28
        self.maintenance = MW(start = self.start, end = self.end,
29
                              switches = self.switches)
30
31
    def test_as_dict(self):
32
        """Test as_dict method."""
33
        mw_dict = self.maintenance.dict()
34
        expected_dict = {
35
            'description': '',
36
            'start': self.start,
37
            'end': self.end,
38
            'id': self.maintenance.id,
39
            'switches': self.switches,
40
            'interfaces': [],
41
            'links': [],
42
            'status': Status.PENDING
43
        }
44
        self.assertEqual(mw_dict, expected_dict)
45
46
    @patch('kytos.core.buffers.KytosEventBuffer.put')
47
    def test_start_mw_case_1(self, buffer_put_mock):
48
        """Test the method that starts a maintenance."""
49
        maintenance = self.maintenance.copy(
50
            update = {
51
                'switches': [
52
                    '01:23:45:67:89:ab:cd:ef',
53
                    '01:23:45:67:65:ab:cd:ef'
54
                ],
55
                'interfaces': [],
56
                'links': [],
57
            }
58
        )
59
        next_win = maintenance.start_mw(self.controller)
60
        buffer_put_mock.assert_called_once()
61
        self.assertEqual(next_win.status, Status.RUNNING)
62
63
    @patch('kytos.core.buffers.KytosEventBuffer.put')
64
    def test_start_mw_case_2(self, buffer_put_mock):
65
        """Test the method that starts a maintenance."""
66
        interface_id = "interface_1"
67
        maintenance = self.maintenance.copy(
68
            update = {
69
                'switches': [
70
                    '01:23:45:67:89:ab:cd:ef',
71
                    '01:23:45:67:65:ab:cd:ef'
72
                ],
73
                'interfaces': [interface_id],
74
                'links': [],
75
            }
76
        )
77
        next_win = maintenance.start_mw(self.controller)
78
        self.assertEqual(buffer_put_mock.call_count, 2)
79
        self.assertEqual(next_win.status, Status.RUNNING)
80
81
    @patch('kytos.core.buffers.KytosEventBuffer.put')
82
    def test_start_mw_case_3(self, buffer_put_mock):
83
        """Test the method that starts a maintenance."""
84
        interface_id = "interface_1"
85
        link1 = "link_1"
86
        link2 = "link_2"
87
        maintenance = self.maintenance.copy(
88
            update = {
89
                'switches': [
90
                ],
91
                'interfaces': [interface_id],
92
                'links': [link1, link2],
93
            }
94
        )
95
        next_win = maintenance.start_mw(self.controller)
96
        self.assertEqual(buffer_put_mock.call_count, 2)
97
        self.assertEqual(next_win.status, Status.RUNNING)
98
99
    @patch('kytos.core.buffers.KytosEventBuffer.put')
100
    def test_end_mw_case_1(self, buffer_put_mock):
101
        """Test the method that starts a maintenance."""
102
        maintenance = self.maintenance.copy(
103
            update = {
104
                'switches': [
105
                    '01:23:45:67:89:ab:cd:ef',
106
                    '01:23:45:67:65:ab:cd:ef'
107
                ],
108
                'interfaces': [],
109
                'links': [],
110
                'status': Status.RUNNING,
111
            }
112
        )
113
        next_win = maintenance.end_mw(self.controller)
114
        buffer_put_mock.assert_called_once()
115
        self.assertEqual(next_win.status, Status.FINISHED)
116
117
    @patch('kytos.core.buffers.KytosEventBuffer.put')
118
    def test_end_mw_case_2(self, buffer_put_mock):
119
        """Test the method that starts a maintenance."""
120
        interface_id = "interface_1"
121
        maintenance = self.maintenance.copy(
122
            update = {
123
                'switches': [
124
                    '01:23:45:67:89:ab:cd:ef',
125
                    '01:23:45:67:65:ab:cd:ef'
126
                ],
127
                'interfaces': [interface_id],
128
                'links': [],
129
                'status': Status.RUNNING,
130
            }
131
        )
132
        next_win = maintenance.end_mw(self.controller)
133
        self.assertEqual(buffer_put_mock.call_count, 2)
134
        self.assertEqual(next_win.status, Status.FINISHED)
135
136
    @patch('kytos.core.buffers.KytosEventBuffer.put')
137
    def test_end_mw_case_3(self, buffer_put_mock):
138
        """Test the method that starts a maintenance."""
139
        interface_id = "interface_1"
140
        link1 = "link_1"
141
        link2 = "link_2"
142
        maintenance = self.maintenance.copy(
143
            update = {
144
                'switches': [
145
                ],
146
                'interfaces': [interface_id],
147
                'links': [link1, link2],
148
                'status': Status.RUNNING,
149
            }
150
        )
151
        next_win = maintenance.end_mw(self.controller)
152
        self.assertEqual(buffer_put_mock.call_count, 2)
153
        self.assertEqual(next_win.status, Status.FINISHED)
154