Skip to content

Commit 8eb9065

Browse files
committed
Modified test files, added mock server.
Changed __srv_handler to _srv_handler for use in the test cases.
1 parent 5f26631 commit 8eb9065

7 files changed

Lines changed: 250 additions & 44 deletions

File tree

.coverage

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
�}q(U collectorqUcoverage v3.7.1qUlinesq}q(U1/home/one/dev/python-nut2/tests/test_nutclient.pyq]q(KKK eU!/home/one/dev/python-nut2/nut2.pyq]q (K'K)K*K,K-K.K/K0K1K2K4K5K8KRKYKpK�K�K�K�K�MMM#M-euu.
1+
�}q(U collectorqUcoverage v3.7.1qUlinesq}q(U-/home/one/dev/python-nut2/tests/mockserver.pyq]q(KKKKKKK K
2+
K K KKKKKKKKKKKKKKKKKKKK K!K"K#K$K%K&K'K(K)K*K+K,K-K.K0K2K4K5K7K9K;K=K?KBeU1/home/one/dev/python-nut2/tests/test_nutclient.pyq]q (KKK eU!/home/one/dev/python-nut2/nut2.pyq
3+
]q (J����K'K)K*K,K-K.K/K0K1K2K4K5K8KBKDKEKFKGKIKJKKKLKMKOKSKUKVKWKXKZKqKvKwKyKzK{K|K~KK�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�K�MMM M
4+
M MMMMMMMMMMMMMM!M"M$M(M)M+M,M.M3M4M6M7M9M:M;M<M=M>M?MAMBMCMDMEMFMGMHMIMJMKMLeU-/home/one/dev/python-nut2/tests/testclient.pyq ]q(KKKKKKK K
5+
K K KKKKKKKKKKKKKKKKKKKK K!K"K#K$K%K&K'K(K)K*K+K,K-K.K/K0K1K3K4euUarcsq}qU!/home/one/dev/python-nut2/nut2.pyq]q(J����K'�qJ����K)�qJ����KB�qJ����KU�qJ����Kv�qJ����K��qJ����K��qJ����K͆qJ����K�qJ����K��qJ����M �qJ����M�qJ����M(�qJ����M3�qK'K)�q K)J�����q!K)K*�q"K*K,�q#K,K-�q$K-K.�q%K.K/�q&K/K0�q'K0K1�q(K1K2�q)K2K4�q*K4K5�q+K5K8�q,K8KS�q-KBKD�q.KDKE�q/KDKI�q0KEKF�q1KFKG�q2KGKI�q3KIKJ�q4KJKK�q5KKKL�q6KLKM�q7KMKO�q8KOJ�����q9KSKZ�q:KUKV�q;KVKW�q<KWKX�q=KXJ�����q>KZKq�q?KqK��q@KvKw�qAKvKy�qBKwKy�qCKyKz�qDKzK{�qEK{K|�qFK{K~�qGK|J�����qHK~K�qIKK��qJK�K��qKK�K��qLK�K��qMK�K��qNK�K��qOK�K��qPK�J�����qQK�K��qRK�K��qSK�K��qTK�K��qUK�K��qVK�K��qWK�K��qXK�Jx����qYK�K��qZK�K��q[K�K��q\K�K��q]K�K��q^K�K��q_K�K��q`K�K��qaK�K��qbK�Jx����qcK�KȆqdK�K��qeK�K��qfK�K��qgK�K��qhK�K��qiK�K��qjK�J^����qkK�K��qlK�K��qmK�K��qnK�K��qoK�K��qpK�KƆqqK�K��qrK�K��qsK�K��qtK�K��quK�K��qvK�K��qwK�KĆqxK�K��qyK�J^����qzK�K�q{K�KΆq|K�KІq}K�Kцq~K�K҆qK�Kӆq�K�KՆq�K�J8����q�K�Kֆq�K�K׆q�K�K؆q�K�Kچq�K�Kۆq�K�K܆q�K�K�q�K�K݆q�K�Kކq�K�Kۆq�K�J8����q�K�K�q�K�K�q�K�K�q�K�K�q�K�K�q�K�J����q�K�J����q�K�M�q�K�K��q�K�K��q�K�K��q�K�K��q�K�K��q�K�M�q�K�J����q�MJ����q�MM�q�M M
6+
�q�M
7+
M �q�M M�q�MM�q�MM�q�MM�q�MJ�����q�MM�q�MM�q�MM�q�MM�q�MM�q�MM�q�MJ�����q�MJ�����q�MM$�q�MM�q�MM!�q�M!M"�q�M"J�����q�M$M.�q�M(M)�q�M)M+�q�M+M,�q�M,J�����q�M.J�����q�M3M4�q�M4M6�q�M6J�����q�M6M7�q�M6M9�q�M7J�����q�M7M:�q�M9M:�q�M9M<�q�M:M;�q�M:M=�q�M;M>�q�M<M=�q�M=M>�q�M>M?�q�M>MA�q�M?J�����q�M?MB�q�MAMB�q�MBMC�q�MBMD�q�MCMD�q�MCMK�q�MDJ�����q�MDMC�q�MDME�q�MDML�q�MEMD�q�MEMF�q�MFJ�����q�MFMG�q�MFMH�q�MGMH�q�MGMI�q�MHJ�����q�MHMI�q�MIJ�����q�MIMC�q�MIMJ�q�MJMD�q�MKJ�����q�MLJ�����q�esu.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ install:
77
- "pip install coveralls"
88
- "python setup.py install"
99
script:
10-
nosetests --with-coverage
10+
nosetests --with-coverage --cover-package=nut2
1111
after_success:
1212
coveralls

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ a Python library to allow communication with NUT (network UPS tools) servers.
99

1010
## Installation
1111

12+
python setup.py install
1213

1314
## PyNUT
1415

nut2.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class PyNUTClient :
4747
__login = None
4848
__password = None
4949
__timeout = None
50-
__srv_handler = None
50+
_srv_handler = None
5151

5252
__version = "1.2.2"
5353
__release = "2012-02-07"
5454

5555

56-
def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debug=False, timeout=5 ) :
56+
def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debug=False, timeout=5, connect=True ) :
5757
""" Class initialization method
5858
5959
host : Host to connect (default to localhost)
@@ -76,13 +76,14 @@ def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debu
7676
self.__password = password
7777
self.__timeout = 5
7878

79-
self.__connect()
79+
if connect:
80+
self.__connect()
8081

8182
# Try to disconnect cleanly when class is deleted ;)
8283
def __del__( self ) :
8384
""" Class destructor method """
8485
try :
85-
self.__srv_handler.write( "LOGOUT\n" )
86+
self._srv_handler.write( "LOGOUT\n" )
8687
except :
8788
pass
8889

@@ -95,17 +96,17 @@ def __connect( self ) :
9596
if self.__debug :
9697
print( "[DEBUG] Connecting to host" )
9798

98-
self.__srv_handler = telnetlib.Telnet( self.__host, self.__port )
99+
self._srv_handler = telnetlib.Telnet( self.__host, self.__port )
99100

100101
if self.__login != None :
101-
self.__srv_handler.write( "USERNAME %s\n" % self.__login )
102-
result = self.__srv_handler.read_until( "\n", self.__timeout )
102+
self._srv_handler.write( "USERNAME %s\n" % self.__login )
103+
result = self._srv_handler.read_until( "\n", self.__timeout )
103104
if result[:2] != "OK" :
104105
raise Exception, result.replace( "\n", "" )
105106

106107
if self.__password != None :
107-
self.__srv_handler.write( "PASSWORD %s\n" % self.__password )
108-
result = self.__srv_handler.read_until( "\n", self.__timeout )
108+
self._srv_handler.write( "PASSWORD %s\n" % self.__password )
109+
result = self._srv_handler.read_until( "\n", self.__timeout )
109110
if result[:2] != "OK" :
110111
raise Exception, result.replace( "\n", "" )
111112

@@ -117,12 +118,12 @@ def GetUPSList( self ) :
117118
if self.__debug :
118119
print( "[DEBUG] GetUPSList from server" )
119120

120-
self.__srv_handler.write( "LIST UPS\n" )
121-
result = self.__srv_handler.read_until( "\n" )
121+
self._srv_handler.write( "LIST UPS\n" )
122+
result = self._srv_handler.read_until( "\n" )
122123
if result != "BEGIN LIST UPS\n" :
123124
raise Exception, result.replace( "\n", "" )
124125

125-
result = self.__srv_handler.read_until( "END LIST UPS\n" )
126+
result = self._srv_handler.read_until( "END LIST UPS\n" )
126127
ups_list = {}
127128

128129
for line in result.split( "\n" ) :
@@ -141,13 +142,13 @@ def GetUPSVars( self, ups="" ) :
141142
if self.__debug :
142143
print( "[DEBUG] GetUPSVars called..." )
143144

144-
self.__srv_handler.write( "LIST VAR %s\n" % ups )
145-
result = self.__srv_handler.read_until( "\n" )
145+
self._srv_handler.write( "LIST VAR %s\n" % ups )
146+
result = self._srv_handler.read_until( "\n" )
146147
if result != "BEGIN LIST VAR %s\n" % ups :
147148
raise Exception, result.replace( "\n", "" )
148149

149150
ups_vars = {}
150-
result = self.__srv_handler.read_until( "END LIST VAR %s\n" % ups )
151+
result = self._srv_handler.read_until( "END LIST VAR %s\n" % ups )
151152
offset = len( "VAR %s " % ups )
152153
end_offset = 0 - ( len( "END LIST VAR %s\n" % ups ) + 1 )
153154

@@ -167,13 +168,13 @@ def GetUPSCommands( self, ups="" ) :
167168
if self.__debug :
168169
print( "[DEBUG] GetUPSCommands called..." )
169170

170-
self.__srv_handler.write( "LIST CMD %s\n" % ups )
171-
result = self.__srv_handler.read_until( "\n" )
171+
self._srv_handler.write( "LIST CMD %s\n" % ups )
172+
result = self._srv_handler.read_until( "\n" )
172173
if result != "BEGIN LIST CMD %s\n" % ups :
173174
raise Exception, result.replace( "\n", "" )
174175

175176
ups_cmds = {}
176-
result = self.__srv_handler.read_until( "END LIST CMD %s\n" % ups )
177+
result = self._srv_handler.read_until( "END LIST CMD %s\n" % ups )
177178
offset = len( "CMD %s " % ups )
178179
end_offset = 0 - ( len( "END LIST CMD %s\n" % ups ) + 1 )
179180

@@ -182,8 +183,8 @@ def GetUPSCommands( self, ups="" ) :
182183

183184
# For each var we try to get the available description
184185
try :
185-
self.__srv_handler.write( "GET CMDDESC %s %s\n" % ( ups, var ) )
186-
temp = self.__srv_handler.read_until( "\n" )
186+
self._srv_handler.write( "GET CMDDESC %s %s\n" % ( ups, var ) )
187+
temp = self._srv_handler.read_until( "\n" )
187188
if temp[:7] != "CMDDESC" :
188189
raise
189190
else :
@@ -204,12 +205,12 @@ def GetRWVars( self, ups="" ) :
204205
if self.__debug :
205206
print( "[DEBUG] GetUPSVars from '%s'..." % ups )
206207

207-
self.__srv_handler.write( "LIST RW %s\n" % ups )
208-
result = self.__srv_handler.read_until( "\n" )
208+
self._srv_handler.write( "LIST RW %s\n" % ups )
209+
result = self._srv_handler.read_until( "\n" )
209210
if ( result != "BEGIN LIST RW %s\n" % ups ) :
210211
raise Exception, result.replace( "\n", "" )
211212

212-
result = self.__srv_handler.read_until( "END LIST RW %s\n" % ups )
213+
result = self._srv_handler.read_until( "END LIST RW %s\n" % ups )
213214
offset = len( "VAR %s" % ups )
214215
end_offset = 0 - ( len( "END LIST RW %s\n" % ups ) + 1 )
215216
rw_vars = {}
@@ -232,8 +233,8 @@ def SetRWVar( self, ups="", var="", value="" ):
232233
rights to set it (maybe login/password).
233234
"""
234235

235-
self.__srv_handler.write( "SET VAR %s %s %s\n" % ( ups, var, value ) )
236-
result = self.__srv_handler.read_until( "\n" )
236+
self._srv_handler.write( "SET VAR %s %s %s\n" % ( ups, var, value ) )
237+
result = self._srv_handler.read_until( "\n" )
237238
if ( result == "OK\n" ) :
238239
return( "OK" )
239240
else :
@@ -248,8 +249,8 @@ def RunUPSCommand( self, ups="", command="" ) :
248249
if self.__debug :
249250
print( "[DEBUG] RunUPSCommand called..." )
250251

251-
self.__srv_handler.write( "INSTCMD %s %s\n" % ( ups, command ) )
252-
result = self.__srv_handler.read_until( "\n" )
252+
self._srv_handler.write( "INSTCMD %s %s\n" % ( ups, command ) )
253+
result = self._srv_handler.read_until( "\n" )
253254
if ( result == "OK\n" ) :
254255
return( "OK" )
255256
else :
@@ -264,15 +265,15 @@ def FSD( self, ups="") :
264265
if self.__debug :
265266
print( "[DEBUG] MASTER called..." )
266267

267-
self.__srv_handler.write( "MASTER %s\n" % ups )
268-
result = self.__srv_handler.read_until( "\n" )
268+
self._srv_handler.write( "MASTER %s\n" % ups )
269+
result = self._srv_handler.read_until( "\n" )
269270
if ( result != "OK MASTER-GRANTED\n" ) :
270271
raise Exception, ( "Master level function are not available", "" )
271272

272273
if self.__debug :
273274
print( "[DEBUG] FSD called..." )
274-
self.__srv_handler.write( "FSD %s\n" % ups )
275-
result = self.__srv_handler.read_until( "\n" )
275+
self._srv_handler.write( "FSD %s\n" % ups )
276+
result = self._srv_handler.read_until( "\n" )
276277
if ( result == "OK FSD-SET\n" ) :
277278
return( "OK" )
278279
else :
@@ -285,8 +286,8 @@ def help(self) :
285286
if self.__debug :
286287
print( "[DEBUG] HELP called..." )
287288

288-
self.__srv_handler.write( "HELP\n")
289-
return self.__srv_handler.read_until( "\n" )
289+
self._srv_handler.write( "HELP\n")
290+
return self._srv_handler.read_until( "\n" )
290291

291292
def ver(self) :
292293
""" Send VER command
@@ -295,8 +296,8 @@ def ver(self) :
295296
if self.__debug :
296297
print( "[DEBUG] VER called..." )
297298

298-
self.__srv_handler.write( "VER\n")
299-
return self.__srv_handler.read_until( "\n" )
299+
self._srv_handler.write( "VER\n")
300+
return self._srv_handler.read_until( "\n" )
300301

301302
def ListClients( self, ups = None ) :
302303
""" Returns the list of connected clients from the NUT server
@@ -310,14 +311,16 @@ def ListClients( self, ups = None ) :
310311
raise Exception, "%s is not a valid UPS" % ups
311312

312313
if ups:
313-
self.__srv_handler.write( "LIST CLIENTS %s\n" % ups)
314+
self._srv_handler.write( "LIST CLIENTS %s\n" % ups)
314315
else:
315-
self.__srv_handler.write( "LIST CLIENTS\n" )
316-
result = self.__srv_handler.read_until( "\n" )
316+
self._srv_handler.write( "LIST CLIENTS\n" )
317+
result = self._srv_handler.read_until( "\n" )
317318
if result != "BEGIN LIST CLIENTS\n" :
318319
raise Exception, result.replace( "\n", "" )
319320

320-
result = self.__srv_handler.read_until( "END LIST CLIENTS\n" )
321+
result = self._srv_handler.read_until( "END LIST CLIENTS\n" )
322+
ups_list = {}
323+
321324
for line in result.split( "\n" ):
322325
if line[:6] == "CLIENT" :
323326
host, ups = line[7:].split(' ')

requirements-testing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nose
22
coverage
3+
mock

tests/mockserver.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""A simple mock NUT server for testing the Python client."""
2+
3+
class MockServer(object):
4+
def __init__(self, host=None, port=None, broken=False, ok=True):
5+
self.valid = "test"
6+
self.broken = broken
7+
self.ok = ok
8+
9+
def write(self, text):
10+
self.command = text
11+
self.first = True
12+
13+
def read_until(self, text=None, timeout=None):
14+
return self.run_command().split(text)[0] + text
15+
16+
def run_command(self):
17+
if self.broken == True:
18+
return 'ERR\n'
19+
elif self.command == "HELP\n":
20+
return 'Commands: HELP VER GET LIST SET INSTCMD LOGIN LOGOUT USERNAME PASSWORD STARTTLS\n'
21+
elif self.command == "VER\n":
22+
return 'Network UPS Tools upsd 2.7.1 - http://www.networkupstools.org/\n'
23+
elif self.command == "GET CMDDESC %s %s\n" % (self.valid, self.valid):
24+
return 'CMDDESC '+self.valid+' '+self.valid+' "Test description"\n'
25+
elif self.command == "LIST UPS\n" and self.first:
26+
self.first = False
27+
return 'BEGIN LIST UPS\n'
28+
elif self.command == "LIST UPS\n":
29+
return 'UPS '+self.valid+'"Test UPS 1"\nUPS Test_UPS2 "Test UPS 2"\nEND LIST UPS\n'
30+
elif self.command == "LIST VAR %s\n" % self.valid and self.first:
31+
self.first = False
32+
return 'BEGIN LIST VAR '+self.valid+'\n'
33+
elif self.command == "LIST VAR %s\n" % self.valid:
34+
return 'VAR '+self.valid+' battery.charge "100"\nVAR '+self.valid+' battery.voltage "14.44"\nEND LIST VAR '+self.valid+'\n'
35+
elif self.command.startswith("LIST VAR"):
36+
return 'ERR INVALID-ARGUMENT\n'
37+
elif self.command == "LIST CMD %s\n" % self.valid and self.first:
38+
self.first = False
39+
return 'BEGIN LIST CMD '+self.valid+'\n'
40+
elif self.command == "LIST CMD %s\n" % self.valid:
41+
return 'CMD '+self.valid+' '+self.valid+'\nEND LIST CMD '+self.valid+'\n'
42+
elif self.command.startswith("LIST CMD"):
43+
return 'ERR INVALID-ARGUMENT\n'
44+
elif self.command == "LIST RW %s\n" % self.valid and self.first:
45+
self.first = False
46+
return 'BEGIN LIST RW '+self.valid+'\n'
47+
elif self.command == "LIST RW %s\n" % self.valid:
48+
return 'RW '+self.valid+' '+self.valid+' "test"\nEND LIST RW '+self.valid+'\n'
49+
elif self.command.startswith("LIST RW"):
50+
return 'ERR INVALID-ARGUMENT\n'
51+
elif self.command == "LIST CLIENTS %s\n" % self.valid and self.first:
52+
self.first = False
53+
return 'BEGIN LIST CLIENTS\n'
54+
elif self.command == "LIST CLIENTS %s\n" % self.valid:
55+
return 'CLIENT '+self.valid+' '+self.valid+'\nEND LIST CLIENTS\n'
56+
elif self.command.startswith("LIST CLIENTS"):
57+
return 'ERR INVALID-ARGUMENT\n'
58+
# TODO: SET commands
59+
elif self.command == "SET VAR %s %s %s\n" % (self.valid, self.valid, self.valid):
60+
return 'OK\n'
61+
elif self.command.startswith("SET"):
62+
return 'ERR ACCESS-DENIED\n'
63+
elif self.command == "INSTCMD %s %s\n"% (self.valid, self.valid):
64+
return 'OK\n'
65+
elif self.command.startswith("INSTCMD"):
66+
return 'ERR CMD-NOT-SUPPORTED\n'
67+
# TODO: LOGIN/LOGOUT commands
68+
elif self.command == "USERNAME %s\n" % self.valid:
69+
return 'OK\n'
70+
elif self.command.startswith("USERNAME"):
71+
return 'OK\n' # FIXME
72+
elif self.command == "PASSWORD %s\n" % self.valid:
73+
return 'OK\n'
74+
elif self.command.startswith("PASSWORD"):
75+
return 'OK\n' # FIXME
76+
elif self.command == "STARTTLS\n":
77+
return 'ERR FEATURE-NOT-CONFIGURED\n'
78+
elif self.command == "MASTER %s\n" % self.valid:
79+
return 'OK MASTER-GRANTED\n'
80+
elif self.command == "FSD %s\n" % self.valid and self.ok:
81+
return 'OK FSD-SET\n'
82+
elif self.command == "FSD %s\n" % self.valid:
83+
return 'ERR\n'
84+
else:
85+
return 'ERR UNKNOWN-COMMAND\n'

0 commit comments

Comments
 (0)