def test_get_shard_ranges_for_object_put(self): ts_iter = make_timestamp_iter() shard_ranges = [dict(ShardRange( '.sharded_a/sr%d' % i, next(ts_iter), '%d_lower' % i, '%d_upper' % i, object_count=i, bytes_used=1024 * i, meta_timestamp=next(ts_iter))) for i in range(3)] base = Controller(self.app) req = Request.blank('/v1/a/c/o', method='PUT') resp_headers = {'X-Backend-Record-Type': 'shard'} with mocked_http_conn( 200, 200, body_iter=iter([b'', json.dumps(shard_ranges[1:2]).encode('ascii')]), headers=resp_headers ) as fake_conn: actual = base._get_shard_ranges(req, 'a', 'c', '1_test') # account info captured = fake_conn.requests self.assertEqual('HEAD', captured[0]['method']) self.assertEqual('a', captured[0]['path'][7:]) # container GET self.assertEqual('GET', captured[1]['method']) self.assertEqual('a/c', captured[1]['path'][7:]) params = sorted(captured[1]['qs'].split('&')) self.assertEqual( ['format=json', 'includes=1_test'], params) self.assertEqual( 'shard', captured[1]['headers'].get('X-Backend-Record-Type')) self.assertEqual(shard_ranges[1:2], [dict(pr) for pr in actual]) self.assertFalse(self.app.logger.get_lines_for_level('error'))
def test_get_shard_ranges_request_failed(self): base = Controller(self.app) req = Request.blank('/v1/a/c/o', method='PUT') with mocked_http_conn(200, 404, 404, 404): actual = base._get_shard_ranges(req, 'a', 'c', '1_test') self.assertIsNone(actual) self.assertFalse(self.app.logger.get_lines_for_level('error')) warning_lines = self.app.logger.get_lines_for_level('warning') self.assertIn('Failed to get container listing', warning_lines[0]) self.assertIn('/a/c', warning_lines[0]) self.assertFalse(warning_lines[1:])
def _check_get_shard_ranges_bad_data(self, body): base = Controller(self.app) req = Request.blank('/v1/a/c/o', method='PUT') # empty response headers = {'X-Backend-Record-Type': 'shard'} with mocked_http_conn(200, 200, body_iter=iter([b'', body]), headers=headers): actual = base._get_shard_ranges(req, 'a', 'c', '1_test') self.assertIsNone(actual) lines = self.app.logger.get_lines_for_level('error') return lines
def test_get_shard_ranges_missing_record_type(self): base = Controller(self.app) req = Request.blank('/v1/a/c/o', method='PUT') sr = ShardRange('a/c', Timestamp.now()) body = json.dumps([dict(sr)]).encode('ascii') with mocked_http_conn( 200, 200, body_iter=iter([b'', body])): actual = base._get_shard_ranges(req, 'a', 'c', '1_test') self.assertIsNone(actual) error_lines = self.app.logger.get_lines_for_level('error') self.assertIn('Failed to get shard ranges', error_lines[0]) self.assertIn('unexpected record type', error_lines[0]) self.assertIn('/a/c', error_lines[0]) self.assertFalse(error_lines[1:])