Repository: ambari Updated Branches: refs/heads/branch-2.5 240775107 -> 3f5ea585f
AMBARI-20924 - Old Status Command Structured Output is Returned on Every Status Command Causing Upgrades to Fail (jonathanhurley) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3f5ea585 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3f5ea585 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3f5ea585 Branch: refs/heads/branch-2.5 Commit: 3f5ea585f5c150428a0ee80f95558c3668d4a75c Parents: 2407751 Author: Jonathan Hurley <[email protected]> Authored: Wed May 3 13:58:30 2017 -0400 Committer: Jonathan Hurley <[email protected]> Committed: Wed May 3 17:49:13 2017 -0400 ---------------------------------------------------------------------- .../python/resource_management/TestScript.py | 56 ++++++++++++++------ .../libraries/script/script.py | 7 +++ 2 files changed, 46 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/3f5ea585/ambari-agent/src/test/python/resource_management/TestScript.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/resource_management/TestScript.py b/ambari-agent/src/test/python/resource_management/TestScript.py index 65f8c2d..d531314 100644 --- a/ambari-agent/src/test/python/resource_management/TestScript.py +++ b/ambari-agent/src/test/python/resource_management/TestScript.py @@ -17,27 +17,15 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ''' -import ConfigParser -import os - -import pprint - -from unittest import TestCase -import threading -import tempfile -import time -from threading import Thread - - import StringIO -import sys, logging, pprint -from ambari_agent import AgentException +import sys, pprint from resource_management.libraries.script import Script from resource_management.core.environment import Environment -from mock.mock import MagicMock, patch - +from mock.mock import patch +from stacks.utils.RMFTestCase import * +import logging -class TestScript(TestCase): +class TestScript(RMFTestCase): def setUp(self): # disable stdout @@ -116,6 +104,40 @@ class TestScript(TestCase): self.assertEqual(open_mock.call_count, 3) self.assertEqual(Script.structuredOut, {"1": "3", "2": "2"}) + @patch("__builtin__.open") + def test_status_commands_clear_structured_out(self, open_mock): + """ + Tests that status commands will clear and stored structured output from prior status commands. + :param open_mock: + :return: + """ + class MagicFile(object): + def read(self): + return "{}" + + def write(self, data): + pass + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + + def __enter__(self): + return self + + sys.argv = ["", "status", "foo.py", "", "", "INFO", ""] + open_mock.side_effect = [MagicFile()] + + try: + with Environment(".", test_mode=True) as env: + script = Script() + Script.structuredOut = { "version" : "old_version" } + script.execute() + except: + pass + + self.assertTrue(open_mock.called) + self.assertEquals({}, Script.structuredOut) + def tearDown(self): # enable stdout sys.stdout = sys.__stdout__ http://git-wip-us.apache.org/repos/asf/ambari/blob/3f5ea585/ambari-common/src/main/python/resource_management/libraries/script/script.py ---------------------------------------------------------------------- diff --git a/ambari-common/src/main/python/resource_management/libraries/script/script.py b/ambari-common/src/main/python/resource_management/libraries/script/script.py index b1fbc50..bafb8e2 100644 --- a/ambari-common/src/main/python/resource_management/libraries/script/script.py +++ b/ambari-common/src/main/python/resource_management/libraries/script/script.py @@ -289,6 +289,13 @@ class Script(object): if OSCheck.is_windows_family(): reload_windows_env() + # !!! status commands re-use structured output files; if the status command doesn't update the + # the file (because it doesn't have to) then we must ensure that the file is reset to prevent + # old, stale structured output from a prior status command from being used + if self.command_name == "status": + Script.structuredOut = {} + self.put_structured_out({}) + try: with open(self.command_data_file) as f: pass
