TestHostname.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import ambari_agent.hostname as hostname
  19. import ambari_agent.AmbariConfig as AmbariConfig
  20. import socket
  21. import tempfile
  22. import shutil
  23. import os, pprint, json,stat
  24. class TestHostname(TestCase):
  25. def test_hostname(self):
  26. self.assertEquals(hostname.hostname(), socket.gethostname(), "hostname should equal the socket-based hostname")
  27. pass
  28. def test_hostname_override(self):
  29. fd = tempfile.mkstemp(text=True)
  30. tmpname = fd[1]
  31. os.close(fd[0])
  32. os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
  33. tmpfile = file(tmpname, "w+")
  34. config = AmbariConfig.config
  35. try:
  36. tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
  37. tmpfile.close()
  38. config.set('agent', 'hostname_script', tmpname)
  39. self.assertEquals(hostname.hostname(), 'test.example.com', "expected hostname 'test.example.com'")
  40. finally:
  41. os.remove(tmpname)
  42. config.remove_option('agent', 'hostname_script')
  43. pass