From 304fb34153b0761f45136b304756c463fd158e70 Mon Sep 17 00:00:00 2001 From: Steven Lang Date: Thu, 24 Oct 2013 11:46:49 -0700 Subject: [PATCH] Fix UnboundLocalError on container PUT Fixes bug-1243973 Change-Id: If165fdcccb5d4712570b1cdabcc89e618f539849 --- swift/container/server.py | 2 +- test/unit/container/test_server.py | 41 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/swift/container/server.py b/swift/container/server.py index 4810b9bfaf..3f85544350 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -257,7 +257,7 @@ class ContainerController(object): broker.initialize(timestamp) created = True except DatabaseAlreadyExists: - pass + created = False else: created = broker.is_deleted() broker.update_put_timestamp(timestamp) diff --git a/test/unit/container/test_server.py b/test/unit/container/test_server.py index 170aad471b..7662006fec 100644 --- a/test/unit/container/test_server.py +++ b/test/unit/container/test_server.py @@ -183,6 +183,47 @@ class TestContainerController(unittest.TestCase): resp = req.get_response(self.controller) self.assertEquals(resp.status_int, 202) + def test_PUT_simulated_create_race(self): + state = ['initial'] + + from swift.container.backend import ContainerBroker as OrigCoBr + + class InterceptedCoBr(OrigCoBr): + + def __init__(self, *args, **kwargs): + super(InterceptedCoBr, self).__init__(*args, **kwargs) + if state[0] == 'initial': + # Do nothing initially + pass + elif state[0] == 'race': + # Save the original db_file attribute value + self._saved_db_file = self.db_file + self.db_file += '.doesnotexist' + + def initialize(self, *args, **kwargs): + if state[0] == 'initial': + # Do nothing initially + pass + elif state[0] == 'race': + # Restore the original db_file attribute to get the race + # behavior + self.db_file = self._saved_db_file + return super(InterceptedCoBr, self).initialize(*args, **kwargs) + + with mock.patch("swift.container.server.ContainerBroker", + InterceptedCoBr): + req = Request.blank( + '/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '1'}) + resp = req.get_response(self.controller) + self.assertEqual(resp.status_int, 201) + state[0] = "race" + req = Request.blank( + '/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '1'}) + resp = req.get_response(self.controller) + self.assertEqual(resp.status_int, 202) + def test_PUT_obj_not_found(self): req = Request.blank( '/sda1/p/a/c/o', environ={'REQUEST_METHOD': 'PUT'},