def test_ringmaster_validate_locked_dir(self): self._setup_builder_rings() rma = RingMasterApp({'swiftdir': self.testdir, 'log_path': self.test_log_path, 'locktimeout': "0.1"}) for i in rma.current_md5: self.assertEquals(rma._changed(i), False) self._setup_builder_rings(count=5) for i in rma.current_md5: t = time.time() - 300 os.utime(i, (t, t)) with lock_parent_directory(self.testdir): for i in rma.current_md5: self.assertRaises(LockTimeout, rma._validate_file, i)
def test_ringmaster_validate_locked_dir(self): self._setup_builder_rings() rma = RingMasterApp({ 'swiftdir': self.testdir, 'log_path': self.test_log_path, 'locktimeout': "0.1" }) for i in rma.current_md5: self.assertEquals(rma._changed(i), False) self._setup_builder_rings(count=5) for i in rma.current_md5: t = time.time() - 300 os.utime(i, (t, t)) with lock_parent_directory(self.testdir): for i in rma.current_md5: self.assertRaises(LockTimeout, rma._validate_file, i)
def test_handle_request(self): self._setup_builder_rings() start_response = MagicMock() rma = RingMasterApp({'swiftdir': self.testdir, 'log_path': self.test_log_path}) # test bad path req = Request.blank('/invalidrandomness', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_request(req.environ, start_response) start_response.assert_called_with( '404 Not Found', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Found\r\n']) # test legit path req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'HEAD'}) resp = rma.handle_request(req.environ, start_response) account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) start_response.assert_called_with('200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) self.assertEquals(resp, [])
def test_handle_request(self): self._setup_builder_rings() start_response = MagicMock() rma = RingMasterApp({ 'swiftdir': self.testdir, 'log_path': self.test_log_path }) # test bad path req = Request.blank('/invalidrandomness', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_request(req.environ, start_response) start_response.assert_called_with('404 Not Found', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Found\r\n']) # test legit path req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'HEAD'}) resp = rma.handle_request(req.environ, start_response) account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) start_response.assert_called_with( '200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) self.assertEquals(resp, [])
def test_ringmasterapp_methods(self): self._setup_builder_rings() rma = RingMasterApp({ 'swiftdir': self.testdir, 'log_path': self.test_log_path }) for i in rma.current_md5: self.assertEquals(rma._changed(i), False) self._setup_builder_rings(count=5) for i in rma.current_md5: t = time.time() - 300 os.utime(i, (t, t)) for i in rma.current_md5: self.assertTrue(rma._changed(i)) rma._validate_file(i) self.assertFalse(rma._changed(i))
def test_ringmasterapp_methods(self): self._setup_builder_rings() rma = RingMasterApp({'swiftdir': self.testdir, 'log_path': self.test_log_path}) for i in rma.current_md5: self.assertEquals(rma._changed(i), False) self._setup_builder_rings(count=5) for i in rma.current_md5: t = time.time() - 300 os.utime(i, (t, t)) for i in rma.current_md5: self.assertTrue(rma._changed(i)) rma._validate_file(i) self.assertFalse(rma._changed(i))
def test_handle_ring(self): self._setup_builder_rings() start_response = MagicMock() rma = RingMasterApp({'swiftdir': self.testdir, 'log_path': self.test_log_path}) # test bad path req = Request.blank('/ring/not_a_valid_ring.gz', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with( '404 Not Found', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Found\r\n']) # test bad method start_response.reset_mock() req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'DELETE'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with( '501 Not Implemented', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Implemented\r\n']) # test HEAD start_response.reset_mock() req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'HEAD'}) resp = rma.handle_ring(req.environ, start_response) account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) start_response.assert_called_with('200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) self.assertEquals(resp, []) # test GET w/ current If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'GET', 'HTTP_IF_NONE_MATCH': account_md5}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with('304 Not Modified', [( 'Content-Type', 'application/octet-stream')]) self.assertEquals(resp, ['Not Modified\r\n']) # test GET w/ outdated If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'GET', 'HTTP_IF_NONE_MATCH': 'ihazaring'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with('200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) testfile1 = os.path.join(self.testdir, 'gettest1.file') with open(testfile1, 'w') as f: for i in resp: f.write(i) self.assertTrue(account_md5, get_md5sum(testfile1)) # test GET without If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with('200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) testfile2 = os.path.join(self.testdir, 'gettest2.file') with open(testfile2, 'w') as f: for i in resp: f.write(i) self.assertTrue(account_md5, get_md5sum(testfile2))
def test_handle_ring(self): self._setup_builder_rings() start_response = MagicMock() rma = RingMasterApp({ 'swiftdir': self.testdir, 'log_path': self.test_log_path }) # test bad path req = Request.blank('/ring/not_a_valid_ring.gz', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with('404 Not Found', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Found\r\n']) # test bad method start_response.reset_mock() req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'DELETE'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with('501 Not Implemented', [('Content-Type', 'text/plain')]) self.assertEquals(resp, ['Not Implemented\r\n']) # test HEAD start_response.reset_mock() req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'HEAD'}) resp = rma.handle_ring(req.environ, start_response) account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) start_response.assert_called_with( '200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) self.assertEquals(resp, []) # test GET w/ current If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={ 'REQUEST_METHOD': 'GET', 'HTTP_IF_NONE_MATCH': account_md5 }) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with( '304 Not Modified', [('Content-Type', 'application/octet-stream')]) self.assertEquals(resp, ['Not Modified\r\n']) # test GET w/ outdated If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={ 'REQUEST_METHOD': 'GET', 'HTTP_IF_NONE_MATCH': 'ihazaring' }) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with( '200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) testfile1 = os.path.join(self.testdir, 'gettest1.file') with open(testfile1, 'w') as f: for i in resp: f.write(i) self.assertTrue(account_md5, get_md5sum(testfile1)) # test GET without If-None-Match start_response.reset_mock() account_md5 = get_md5sum(os.path.join(self.testdir, 'account.ring.gz')) req = Request.blank('/ring/account.ring.gz', environ={'REQUEST_METHOD': 'GET'}) resp = rma.handle_ring(req.environ, start_response) start_response.assert_called_with( '200 OK', [('Content-Type', 'application/octet-stream'), ('Etag', account_md5)]) testfile2 = os.path.join(self.testdir, 'gettest2.file') with open(testfile2, 'w') as f: for i in resp: f.write(i) self.assertTrue(account_md5, get_md5sum(testfile2))