From b48435cd25070d6d56987503908d3cb6bc03b9d5 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Thu, 24 Oct 2013 12:23:39 -0400 Subject: [PATCH] Fix UnboundLocalError on account PUT Fixes bug-1243973 Change-Id: I67143535c0f7a0c6b53f67329a0bb128a355a4de Signed-off-by: Peter Portante --- swift/account/server.py | 2 +- test/unit/account/test_server.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/swift/account/server.py b/swift/account/server.py index 53770c44ed..7a41564fca 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -142,7 +142,7 @@ class AccountController(object): broker.initialize(timestamp) created = True except DatabaseAlreadyExists: - pass + created = False elif broker.is_status_deleted(): return self._deleted_response(broker, req, HTTPForbidden, body='Recently deleted') diff --git a/test/unit/account/test_server.py b/test/unit/account/test_server.py index d224e1b8e6..81bf6c15a7 100644 --- a/test/unit/account/test_server.py +++ b/test/unit/account/test_server.py @@ -259,6 +259,44 @@ class TestAccountController(unittest.TestCase): resp = req.get_response(self.controller) self.assertEqual(resp.status_int, 202) + def test_PUT_simulated_create_race(self): + state = ['initial'] + + from swift.account.backend import AccountBroker as OrigAcBr + + class InterceptedAcBr(OrigAcBr): + + def __init__(self, *args, **kwargs): + super(InterceptedAcBr, 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(InterceptedAcBr, self).initialize(*args, **kwargs) + + with mock.patch("swift.account.server.AccountBroker", InterceptedAcBr): + req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '0'}) + resp = req.get_response(self.controller) + self.assertEqual(resp.status_int, 201) + state[0] = "race" + req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '1'}) + resp = req.get_response(self.controller) + self.assertEqual(resp.status_int, 202) + def test_PUT_after_DELETE(self): req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT'}, headers={'X-Timestamp': normalize_timestamp(1)})