test_persistence.py
8.61 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'eduardo'
import unittest
import cocar.tests
import requests
import time
import json
from mock import patch
from ..xml_utils import NmapXML
from ..csv_utils import NetworkCSV
from ..model.computer import Computer
from ..model.printer import Printer, PrinterCounter
from ..model.network import Network
from . import fake_urlopen
class TestPersistence(unittest.TestCase):
"""
Testa identificação de ativos de rede
"""
def setUp(self):
"""
Carrega parâmetros iniciais
"""
self.data_dir = cocar.tests.cocar.cocar_data_dir
self.network_file = cocar.tests.test_dir + "/fixtures/192.168.0.0-24.xml"
self.localhost_file = cocar.tests.test_dir + "/fixtures/127.0.0.1.xml"
self.printer_file = cocar.tests.test_dir + "/fixtures/printer.xml"
self.network_csv = cocar.tests.test_dir + "/fixtures/networks.csv"
self.session = cocar.tests.cocar.Session
self.patcher = patch('requests.put', fake_urlopen)
self.patcher.start()
def test_connect(self):
"""
Testa conexão do SQLAlchemy
"""
db_session = self.session
self.assertIsNotNone(db_session)
def test_persist_computer(self):
"""
Grava computador no banco de dados
"""
hostname = '127.0.0.1'
nmap_xml = NmapXML(self.localhost_file)
host = nmap_xml.parse_xml()
assert host
computer = nmap_xml.identify_host(hostname)
self.assertIsInstance(computer, Computer)
# Agora testa a persistência
self.session.add(computer)
self.session.flush()
# Tenta ver se gravou
results = self.session.query(Computer).first()
self.assertIsNotNone(results)
def test_persist_printer(self):
"""
Grava impressora no banco de dados
"""
hostname = '10.72.168.3'
nmap_xml = NmapXML(self.printer_file)
host = nmap_xml.parse_xml()
assert host
printer = nmap_xml.identify_host(hostname)
self.assertIsInstance(printer, Printer)
# Agora testa a persistência
self.session.add(printer)
self.session.flush()
# Tenta ver se gravou
results = self.session.query(Printer).first()
self.assertIsNotNone(results)
def test_persist_network(self):
"""
Testa gravação dos dados de rede
"""
rede = Network(
network_ip='192.168.0.0',
netmask='255.255.255.0',
network_file='/tmp/network.xml',
name='Rede de Teste'
)
self.session.add(rede)
self.session.flush()
# Tenta ver se gravou
results = self.session.query(Network).first()
self.assertIsNotNone(results)
def test_load_networks(self):
"""
Carrega no banco redes oriundas de arquivo CSV
"""
network_csv = NetworkCSV(csv_file=self.network_csv)
network = network_csv.parse_csv()
self.assertIsInstance(network[0], Network)
for elm in network:
self.assertIsInstance(elm, Network)
self.session.add(elm)
self.session.flush()
# Tenta ver se gravou
results = self.session.query(Network).first()
self.assertIsNotNone(results)
def test_printer_counter(self):
"""
Testa inserção do contador em uma impressora
"""
hostname = '10.72.168.4'
ports = {
"9100": {
"state": "open",
"protocol": "tcp",
"service": "vnc-http"
},
"631": {
"state": "open",
"protocol": "tcp",
"service": "vnc-http"
}
}
# Agora verifica a inserção do contador
counter = PrinterCounter(
ip_address=hostname,
mac_address=None,
hostname=None,
inclusion_date=time.time(),
open_ports=ports,
scantime='3600',
model='Samsung SCX-6x55X Series',
serial='Z7EUBQBCB03539E',
description='Samsung SCX-6x55X Series; V2.00.03.01 03-23-2012;Engine 0.41.69;NIC V5.01.82(SCX-6x55X) 02-28-2012;S/N Z7EUBQBCB03539E',
counter=1280,
counter_time=time.time()
)
self.assertIsInstance(counter, PrinterCounter)
self.assertEqual(counter.counter, 1280)
self.assertEqual(counter.network_ip, hostname)
self.session.add(counter)
self.session.flush()
# Tenta ver se gravou
results = self.session.query(PrinterCounter).first()
self.assertIsNotNone(results)
def test_update_counter(self):
"""
Testa inserção dos parâmetros do contador em impressora existente
"""
hostname = '10.72.168.3'
nmap_xml = NmapXML(self.printer_file)
host = nmap_xml.parse_xml()
assert host
printer = nmap_xml.identify_host(hostname)
self.assertIsInstance(printer, Printer)
printer_counter = PrinterCounter(
ip_address=printer.ip_address,
mac_address=printer.mac_address,
hostname=printer.hostname,
inclusion_date=printer.inclusion_date,
open_ports=printer.open_ports,
scantime=printer.scantime,
model='Samsung SCX-6x55X Series',
serial='Z7EUBQBCB03539E',
description='Samsung SCX-6x55X Series; V2.00.03.01 03-23-2012;Engine 0.41.69;NIC V5.01.82(SCX-6x55X) 02-28-2012;S/N Z7EUBQBCB03539E',
counter=1280,
counter_time=time.time()
)
result = printer_counter.update_counter(self.session)
assert result
# Aqui não pode inserir de novo
result = printer_counter.update_counter(self.session)
self.assertFalse(result)
def test_export_printer(self):
"""
Exporta a impressora para a interface do Cocar
"""
hostname = '10.72.168.3'
nmap_xml = NmapXML(self.printer_file)
host = nmap_xml.parse_xml()
assert host
printer = nmap_xml.identify_host(hostname)
self.assertIsInstance(printer, Printer)
printer_counter = PrinterCounter(
ip_address=printer.ip_address,
mac_address=printer.mac_address,
hostname=printer.hostname,
inclusion_date=printer.inclusion_date,
open_ports=printer.open_ports,
scantime=printer.scantime,
model='Samsung SCX-6x55X Series',
serial='Z7EUBQBCB03539E',
description='Samsung SCX-6x55X Series; V2.00.03.01 03-23-2012;Engine 0.41.69;NIC V5.01.82(SCX-6x55X) 02-28-2012;S/N Z7EUBQBCB03539E',
counter=1280,
counter_time=time.time()
)
result = printer_counter.update_counter(self.session)
assert result
# Adiciona outro contador
printer_counter = PrinterCounter(
ip_address=printer.ip_address,
mac_address=printer.mac_address,
hostname=printer.hostname,
inclusion_date=printer.inclusion_date,
open_ports=printer.open_ports,
scantime=printer.scantime,
model='Samsung SCX-6x55X Series',
serial='Z7EUBQBCB03539E',
description='Samsung SCX-6x55X Series; V2.00.03.01 03-23-2012;Engine 0.41.69;NIC V5.01.82(SCX-6x55X) 02-28-2012;S/N Z7EUBQBCB03539E',
counter=1290,
counter_time=(time.time() + 1)
)
result = printer_counter.update_counter(self.session)
assert result
# Exportar a impressora deve retornar 200
result = printer.export_printer(cocar.tests.cocar.config.get('cocar', 'server_url'), self.session)
assert result
# Não deve ter mais nenhum impressora na tabela
result = self.session.query(PrinterCounter).all()
self.assertEqual(len(result), 0)
def test_import_printers(self):
"""
Testa importação da rede
"""
cocar_url = cocar.tests.cocar.config.get('cocar', 'server_url')
printers_url = cocar_url + '/api/printer'
result = requests.get(printers_url)
self.assertEqual(result.status_code, 200)
result_json = result.json()
self.assertGreater(len(result_json), 0)
for elm in result_json['printers']:
printer = Printer(
ip_address=elm['network_ip']
)
self.session.add(printer)
self.session.flush()
def tearDown(self):
"""
Remove dados
"""
self.patcher.stop()
self.session.close()
pass