TestNetUtil.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python2.6
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. from unittest import TestCase
  18. from ambari_agent.ServerStatus import ServerStatus
  19. from ambari_agent.NetUtil import NetUtil
  20. import ambari_agent.main
  21. from threading import Thread
  22. import time
  23. from ambari_agent.Heartbeat import Heartbeat
  24. from ambari_agent.ActionQueue import ActionQueue
  25. from ambari_agent import AmbariConfig
  26. import socket
  27. import os
  28. import logging
  29. from ambari_agent.Controller import Controller
  30. import socket
  31. NON_EXISTING_DOMAIN = 'non-existing-domain43342432.com'
  32. BAD_URL = 'http://localhost:54222/badurl/'
  33. class TestNetUtil(TestCase):
  34. logger = logging.getLogger()
  35. def setUp(self):
  36. self.logger.info("Starting TestConnectionRetries test")
  37. self.logger.disabled = True
  38. self.defaulttimeout = -1.0
  39. if hasattr(socket, 'getdefaulttimeout'):
  40. # get the default timeout on sockets
  41. self.defaulttimeout = socket.getdefaulttimeout()
  42. def test_url_checks(self):
  43. netutil = NetUtil()
  44. if hasattr(socket, 'setdefaulttimeout'):
  45. # Set the default timeout on sockets
  46. socket.setdefaulttimeout(1)
  47. self.assertEquals(netutil.checkURL('http://' + NON_EXISTING_DOMAIN), False, "Not existing domain")
  48. self.assertEquals(netutil.checkURL(BAD_URL), False, "Bad url")
  49. self.assertEquals(netutil.checkURL('http://192.168.253.177'), False, "Not reachable IP")
  50. if hasattr(socket, 'setdefaulttimeout'):
  51. # Set the default timeout on sockets
  52. socket.setdefaulttimeout(20)
  53. self.assertEquals(netutil.checkURL('http://www.iana.org/domains/example/'), True, "Good url - HTTP code 200")
  54. self.assertEquals(netutil.checkURL('https://www.iana.org/domains/example/'), True, "Good HTTPS url - HTTP code 200")
  55. def test_registration_retries(self):
  56. netutil = NetUtil()
  57. netutil.CONNECT_SERVER_RETRY_INTERVAL_SEC=0.05
  58. retries = netutil.try_to_connect(BAD_URL, 3)
  59. self.assertEquals(retries, 3)
  60. def test_infinit_registration_retries(self):
  61. netutil = NetUtil()
  62. netutil.CONNECT_SERVER_RETRY_INTERVAL_SEC=0.05
  63. thread = Thread(target = netutil.try_to_connect, args = (BAD_URL, -1))
  64. thread.start()
  65. time.sleep(0.25)
  66. # I have to stop the thread anyway, so I'll check results later
  67. threadWasAlive = thread.isAlive()
  68. netutil.DEBUG_STOP_RETRIES_FLAG = True
  69. time.sleep(0.5)
  70. # Checking results before thread stop
  71. self.assertEquals(threadWasAlive, True, "Thread should still be retrying to connect")
  72. # Checking results after thread stop
  73. self.assertEquals(thread.isAlive(), False, "Thread should stop now")
  74. def tearDown(self):
  75. if self.defaulttimeout is not None and self.defaulttimeout > 0 and hasattr(socket, 'setdefaulttimeout'):
  76. # Set the default timeout on sockets
  77. socket.setdefaulttimeout(self.defaulttimeout)
  78. self.logger.disabled = False
  79. self.logger.info("Finished TestConnectionRetries test")