TestFileCache.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  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. import ConfigParser
  18. import os
  19. import pprint
  20. from unittest import TestCase
  21. import threading
  22. import tempfile
  23. import time
  24. from threading import Thread
  25. from PythonExecutor import PythonExecutor
  26. from CustomServiceOrchestrator import CustomServiceOrchestrator
  27. from FileCache import FileCache
  28. from AmbariConfig import AmbariConfig
  29. from mock.mock import MagicMock, patch
  30. import StringIO
  31. import sys
  32. from ambari_agent import AgentException
  33. from AgentException import AgentException
  34. class TestFileCache(TestCase):
  35. def setUp(self):
  36. # disable stdout
  37. out = StringIO.StringIO()
  38. sys.stdout = out
  39. # generate sample config
  40. tmpdir = tempfile.gettempdir()
  41. self.config = ConfigParser.RawConfigParser()
  42. self.config.add_section('agent')
  43. self.config.set('agent', 'prefix', tmpdir)
  44. self.config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  45. @patch("os.path.isdir")
  46. def test_get_service_base_dir(self, isdir_mock):
  47. fileCache = FileCache(self.config)
  48. # Check existing dir case
  49. isdir_mock.return_value = True
  50. base = fileCache.get_service_base_dir("HDP", "2.0.7",
  51. "HBASE", "REGION_SERVER")
  52. self.assertEqual(base, "/var/lib/ambari-agent/cache/stacks/HDP/2.0.7/"
  53. "services/HBASE/package")
  54. # Check absent dir case
  55. isdir_mock.return_value = False
  56. try:
  57. fileCache.get_service_base_dir("HDP", "2.0.7",
  58. "HBASE", "REGION_SERVER")
  59. self.fail("Should throw an exception")
  60. except AgentException:
  61. pass # Expected
  62. @patch("os.path.isdir")
  63. def test_get_hook_base_dir(self, isdir_mock):
  64. fileCache = FileCache(self.config)
  65. # Check existing dir case
  66. isdir_mock.return_value = True
  67. base = fileCache.get_hook_base_dir("HDP", "2.0.7")
  68. self.assertEqual(base, "/var/lib/ambari-agent/cache/stacks/HDP/2.0.7/hooks")
  69. # Check absent dir case
  70. isdir_mock.return_value = False
  71. try:
  72. fileCache.get_hook_base_dir("HDP", "2.0.7")
  73. self.fail("Should throw an exception")
  74. except AgentException:
  75. pass # Expected
  76. def tearDown(self):
  77. # enable stdout
  78. sys.stdout = sys.__stdout__