/
ml_w_snmp_unittest.py
90 lines (84 loc) · 3.1 KB
/
ml_w_snmp_unittest.py
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
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
"""
unittest - SNMP
"""
import unittest
import os
import threading
import ml_w_snmp
import ml_system
import shutil
class test_get(unittest.TestCase):
""" Test SNMP """
def setUp(self):
""" setUp """
def test_snmp_g01(self):
""" snmp_g01 """
if "json" == ml_system.CFG_TYPE:
shutil.copyfile(os.path.join("unittest", "snmp-g01.json"), os.path.join("running", "snmp.txt"))
if "jcfg" == ml_system.CFG_TYPE:
shutil.copyfile(os.path.join("unittest", "snmp-g01.jcfg"), os.path.join("running", "snmp.txt"))
if "pickle" == ml_system.CFG_TYPE:
return
self.maxDiff = None
e = ml_w_snmp.get(None, threading.RLock())
self.assertEqual(e, (True, {
"enable": True,
"community": "public",
"system_name": "SLB",
"system_contact": "info <info@xtera.com>",
"system_location": "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan"
}))
class test_set(unittest.TestCase):
""" Test SNMP """
def setUp(self):
""" setUp """
def test_snmp_g02(self):
""" snmp_g02 """
snmp = {
"enable": True,
"community": "public",
"system_name": "SLB",
"system_contact": "info <info@xtera.com>",
"system_location": "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan"
}
self.maxDiff = None
e = ml_w_snmp.set(None, snmp)
self.assertTrue(e[0], e[1])
f = open(os.path.join("running", "snmp.txt"), "r")
e = f.readlines()
f.close()
if "json" == ml_system.CFG_TYPE:
self.assertEqual(e, ['{"system_name": "SLB", "system_contact": "info <info@xtera.com>", "enable": true, "system_location": "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan", "community": "public"}'])
if "jcfg" == ml_system.CFG_TYPE:
self.assertEqual(e, ['snmp {\n', ' enable True\n', ' community public\n', ' system_name SLB\n', ' system_contact "info <info@xtera.com>"\n', ' system_location "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan"\n', '}\n'])
def test_snmp_g03(self):
""" snmp_g03 """
snmp = {
"enable": True,
"community": "public",
"system_name": "SLB",
"system_contact": "info <info@xtera.com>",
"system_location": "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan"
}
class SetThread(threading.Thread):
def run(self):
e = ml_w_snmp.set(None, snmp, threading.RLock())
sl = {}
for i in range(100):
sl.update({i:SetThread()})
sl[i].setDaemon(True)
sl[i].start()
for i in range(100):
sl[i].join()
self.maxDiff = None
f = open(os.path.join("running", "snmp.txt"), "r")
e = f.readlines()
f.close()
if "json" == ml_system.CFG_TYPE:
self.assertEqual(e, ['{"system_name": "SLB", "system_contact": "info <info@xtera.com>", "enable": true, "system_location": "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan", "community": "public"}'])
if "jcfg" == ml_system.CFG_TYPE:
self.assertEqual(e, ['snmp {\n', ' enable True\n', ' community public\n', ' system_name SLB\n', ' system_contact "info <info@xtera.com>"\n', ' system_location "4F, 102 Guangfu S. Road, Daan District, Taipei 10612, Taiwan"\n', '}\n'])
if __name__ == "__main__":
unittest.main()