test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python
  2. #-*-coding=utf-8
  3. import unittest
  4. from suds import WebFault
  5. from onvif import ONVIFCamera, ONVIFError
  6. CAM_HOST = '192.168.0.112'
  7. CAM_PORT = 80
  8. CAM_USER = 'admin'
  9. CAM_PASS = '12345'
  10. DEBUG = False
  11. def log(ret):
  12. if DEBUG:
  13. print ret
  14. class TestDevice(unittest.TestCase):
  15. # Class level cam. Run this test more efficiently..
  16. cam = ONVIFCamera(CAM_HOST, CAM_PORT, CAM_USER, CAM_PASS)
  17. # ***************** Test Capabilities ***************************
  18. def test_GetWsdlUrl(self):
  19. ret = self.cam.devicemgmt.GetWsdlUrl()
  20. def test_GetServices(self):
  21. '''
  22. Returns a cllection of the devices
  23. services and possibly their available capabilities
  24. '''
  25. params = {'IncludeCapability': True }
  26. ret = self.cam.devicemgmt.GetServices(params)
  27. params = self.cam.devicemgmt.create_type('GetServices')
  28. params.IncludeCapability=False
  29. ret = self.cam.devicemgmt.GetServices(params)
  30. def test_GetServiceCapabilities(self):
  31. '''Returns the capabilities of the devce service.'''
  32. ret = self.cam.devicemgmt.GetServiceCapabilities()
  33. ret.Network._IPFilter
  34. def test_GetCapabilities(self):
  35. '''
  36. Probides a backward compatible interface for the base capabilities.
  37. '''
  38. categorys = ['PTZ', 'Media', 'Imaging',
  39. 'Device', 'Analytics', 'Events']
  40. ret = self.cam.devicemgmt.GetCapabilities()
  41. for category in categorys:
  42. ret = self.cam.devicemgmt.GetCapabilities({'Category': category})
  43. with self.assertRaises(ONVIFError):
  44. self.cam.devicemgmt.GetCapabilities({'Category': 'unknown'})
  45. # *************** Test Network *********************************
  46. def test_GetHostname(self):
  47. ''' Get the hostname from a device '''
  48. self.cam.devicemgmt.GetHostname()
  49. def test_SetHostname(self):
  50. '''
  51. Set the hostname on a device
  52. A device shall accept strings formated according to
  53. RFC 1123 section 2.1 or alternatively to RFC 952,
  54. other string shall be considered as invalid strings
  55. '''
  56. pre_host_name = self.cam.devicemgmt.GetHostname()
  57. self.cam.devicemgmt.SetHostname({'Name':'testHostName'})
  58. self.assertEqual(self.cam.devicemgmt.GetHostname().Name, 'testHostName')
  59. self.cam.devicemgmt.SetHostname(pre_host_name)
  60. def test_SetHostnameFromDHCP(self):
  61. ''' Controls whether the hostname shall be retrieved from DHCP '''
  62. ret = self.cam.devicemgmt.SetHostnameFromDHCP(dict(FromDHCP=False))
  63. self.assertTrue(isinstance(ret, bool))
  64. def test_GetDNS(self):
  65. ''' Gets the DNS setting from a device '''
  66. ret = self.cam.devicemgmt.GetDNS()
  67. self.assertTrue(hasattr(ret, 'FromDHCP'))
  68. if ret.FromDHCP == False:
  69. log(ret.DNSManual[0].Type)
  70. log(ret.DNSManual[0].IPv4Address)
  71. def test_SetDNS(self):
  72. ''' Set the DNS settings on a device '''
  73. ret = self.cam.devicemgmt.SetDNS(dict(FromDHCP=False))
  74. def test_GetNTP(self):
  75. ''' Get the NTP settings from a device '''
  76. ret = self.cam.devicemgmt.GetNTP()
  77. if ret.FromDHCP == False:
  78. self.assertTrue(hasattr(ret, 'NTPManual'))
  79. log(ret.NTPManual)
  80. def test_SetNTP(self):
  81. '''Set the NTP setting'''
  82. ret = self.cam.devicemgmt.SetNTP(dict(FromDHCP=False))
  83. def test_GetDynamicDNS(self):
  84. '''Get the dynamic DNS setting'''
  85. ret = self.cam.devicemgmt.GetDynamicDNS()
  86. log(ret)
  87. def test_SetDynamicDNS(self):
  88. ''' Set the dynamic DNS settings on a device '''
  89. ret = self.cam.devicemgmt.GetDynamicDNS()
  90. ret = self.cam.devicemgmt.SetDynamicDNS(dict(Type=ret.Type, Name="random"))
  91. if __name__ == '__main__':
  92. unittest.main()