Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

import os 

import unittest 

import shutil 

import yaml 

import tempfile 

import lsst.utils 

 

from lsst.daf.butler import StorageClassFactory, StorageClass 

from lsst.daf.butler import DatastoreConfig, DatasetTypeNotSupportedError, DatastoreValidationError 

 

from lsst.utils import doImport 

 

from datasetsHelper import DatasetTestHelper, DatastoreTestHelper 

from examplePythonTypes import MetricsExample 

 

from dummyRegistry import DummyRegistry 

 

 

TESTDIR = os.path.dirname(__file__) 

 

 

def makeExampleMetrics(): 

return MetricsExample({"AM1": 5.2, "AM2": 30.6}, 

{"a": [1, 2, 3], 

"b": {"blue": 5, "red": "green"}}, 

[563, 234, 456.7] 

) 

 

 

class TransactionTestError(Exception): 

"""Specific error for transactions, to prevent misdiagnosing 

that might otherwise occur when a standard exception is used. 

""" 

pass 

 

 

class DatastoreTestsBase(DatasetTestHelper, DatastoreTestHelper): 

"""Support routines for datastore testing""" 

root = None 

 

@classmethod 

def setUpClass(cls): 

# Storage Classes are fixed for all datastores in these tests 

scConfigFile = os.path.join(TESTDIR, "config/basic/storageClasses.yaml") 

cls.storageClassFactory = StorageClassFactory() 

cls.storageClassFactory.addFromConfig(scConfigFile) 

 

# Read the Datastore config so we can get the class 

# information (since we should not assume the constructor 

# name here, but rely on the configuration file itself) 

datastoreConfig = DatastoreConfig(cls.configFile) 

cls.datastoreType = doImport(datastoreConfig["cls"]) 

 

def setUp(self): 

self.setUpDatastoreTests(DummyRegistry, DatastoreConfig) 

 

def tearDown(self): 

if self.root is not None and os.path.exists(self.root): 

shutil.rmtree(self.root, ignore_errors=True) 

 

 

class DatastoreTests(DatastoreTestsBase): 

"""Some basic tests of a simple datastore.""" 

 

hasUnsupportedPut = True 

 

def testConfigRoot(self): 

full = DatastoreConfig(self.configFile) 

config = DatastoreConfig(self.configFile, mergeDefaults=False) 

newroot = "/random/location" 

self.datastoreType.setConfigRoot(newroot, config, full) 

if self.rootKeys: 

for k in self.rootKeys: 

self.assertIn(newroot, config[k]) 

 

def testConstructor(self): 

datastore = self.makeDatastore() 

self.assertIsNotNone(datastore) 

self.assertIs(datastore.isEphemeral, self.isEphemeral) 

 

def testConfigurationValidation(self): 

datastore = self.makeDatastore() 

sc = self.storageClassFactory.getStorageClass("ThingOne") 

datastore.validateConfiguration([sc]) 

 

sc2 = self.storageClassFactory.getStorageClass("ThingTwo") 

if self.validationCanFail: 

with self.assertRaises(DatastoreValidationError): 

datastore.validateConfiguration([sc2], logFailures=True) 

 

# Without a universe testing is limited but create a 

# ref that has valid dataId and one that does not 

dimensions = frozenset(("visit", "physical_filter")) 

dataId = {"visit": 52, "physical_filter": "V"} 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

datastore.validateConfiguration([ref]) 

 

dataId = {"visit": 52, "physical_filter": "V", "foo": "bar"} 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

if self.validationCanFail: 

with self.assertRaises(DatastoreValidationError): 

datastore.validateConfiguration([ref]) 

 

def testParameterValidation(self): 

"""Check that parameters are validated""" 

sc = self.storageClassFactory.getStorageClass("ThingOne") 

dimensions = frozenset(("visit", "physical_filter")) 

dataId = {"visit": 52, "physical_filter": "V"} 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

datastore = self.makeDatastore() 

data = {1: 2, 3: 4} 

datastore.put(data, ref) 

newdata = datastore.get(ref) 

self.assertEqual(data, newdata) 

with self.assertRaises(KeyError): 

newdata = datastore.get(ref, parameters={"missing": 5}) 

 

def testBasicPutGet(self): 

metrics = makeExampleMetrics() 

datastore = self.makeDatastore() 

 

# Create multiple storage classes for testing different formulations 

storageClasses = [self.storageClassFactory.getStorageClass(sc) 

for sc in ("StructuredData", 

"StructuredDataJson", 

"StructuredDataPickle")] 

 

dimensions = frozenset(("visit", "physical_filter")) 

dataId = {"visit": 52, "physical_filter": "V"} 

 

for sc in storageClasses: 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

print("Using storageClass: {}".format(sc.name)) 

datastore.put(metrics, ref) 

 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

 

# Get 

metricsOut = datastore.get(ref, parameters=None) 

self.assertEqual(metrics, metricsOut) 

 

uri = datastore.getUri(ref) 

self.assertEqual(uri[:len(self.uriScheme)], self.uriScheme) 

 

# Get a component -- we need to construct new refs for them 

# with derived storage classes but with parent ID 

comp = "output" 

compRef = self.makeDatasetRef(ref.datasetType.componentTypeName(comp), dimensions, 

sc.components[comp], dataId, id=ref.id) 

output = datastore.get(compRef) 

self.assertEqual(output, metricsOut.output) 

 

uri = datastore.getUri(compRef) 

self.assertEqual(uri[:len(self.uriScheme)], self.uriScheme) 

 

storageClass = sc 

 

# Check that a put fails if the dataset type is not supported 

if self.hasUnsupportedPut: 

sc = StorageClass("UnsupportedSC", pytype=type(metrics)) 

ref = self.makeDatasetRef("unsupportedType", dimensions, sc, dataId) 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.put(metrics, ref) 

 

# These should raise 

ref = self.makeDatasetRef("metrics", dimensions, storageClass, dataId, id=10000) 

with self.assertRaises(FileNotFoundError): 

# non-existing file 

datastore.get(ref) 

 

# Get a URI from it 

uri = datastore.getUri(ref, predict=True) 

self.assertEqual(uri[:len(self.uriScheme)], self.uriScheme) 

 

with self.assertRaises(FileNotFoundError): 

datastore.getUri(ref) 

 

def testCompositePutGet(self): 

metrics = makeExampleMetrics() 

datastore = self.makeDatastore() 

 

# Create multiple storage classes for testing different formulations 

# of composites 

storageClasses = [self.storageClassFactory.getStorageClass(sc) 

for sc in ("StructuredComposite", 

"StructuredCompositeTestA", 

"StructuredCompositeTestB")] 

 

dimensions = frozenset(("visit", "Filter")) 

dataId = {"visit": 428, "physical_filter": "R"} 

 

for sc in storageClasses: 

print("Using storageClass: {}".format(sc.name)) 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

 

components = sc.assembler().disassemble(metrics) 

self.assertTrue(components) 

 

compsRead = {} 

for compName, compInfo in components.items(): 

compRef = self.makeDatasetRef(ref.datasetType.componentTypeName(compName), dimensions, 

components[compName].storageClass, dataId) 

 

print("Writing component {} with {}".format(compName, compRef.datasetType.storageClass.name)) 

datastore.put(compInfo.component, compRef) 

 

uri = datastore.getUri(compRef) 

self.assertEqual(uri[:len(self.uriScheme)], self.uriScheme) 

 

compsRead[compName] = datastore.get(compRef) 

 

# We can generate identical files for each storage class 

# so remove the component here 

datastore.remove(compRef) 

 

# combine all the components we read back into a new composite 

metricsOut = sc.assembler().assemble(compsRead) 

self.assertEqual(metrics, metricsOut) 

 

def testRemove(self): 

metrics = makeExampleMetrics() 

datastore = self.makeDatastore() 

# Put 

dimensions = frozenset(("visit", "physical_filter")) 

dataId = {"visit": 638, "physical_filter": "U"} 

 

sc = self.storageClassFactory.getStorageClass("StructuredData") 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

datastore.put(metrics, ref) 

 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

 

# Get 

metricsOut = datastore.get(ref) 

self.assertEqual(metrics, metricsOut) 

# Remove 

datastore.remove(ref) 

 

# Does it exist? 

self.assertFalse(datastore.exists(ref)) 

 

# Do we now get a predicted URI? 

uri = datastore.getUri(ref, predict=True) 

self.assertTrue(uri.endswith("#predicted")) 

 

# Get should now fail 

with self.assertRaises(FileNotFoundError): 

datastore.get(ref) 

# Can only delete once 

with self.assertRaises(FileNotFoundError): 

datastore.remove(ref) 

 

def testTransfer(self): 

metrics = makeExampleMetrics() 

 

dimensions = frozenset(("visit", "physical_filter")) 

dataId = {"visit": 2048, "physical_filter": "Uprime"} 

 

sc = self.storageClassFactory.getStorageClass("StructuredData") 

ref = self.makeDatasetRef("metric", dimensions, sc, dataId) 

 

inputDatastore = self.makeDatastore("test_input_datastore") 

outputDatastore = self.makeDatastore("test_output_datastore") 

 

inputDatastore.put(metrics, ref) 

outputDatastore.transfer(inputDatastore, ref) 

 

metricsOut = outputDatastore.get(ref) 

self.assertEqual(metrics, metricsOut) 

 

def testBasicTransaction(self): 

datastore = self.makeDatastore() 

storageClass = self.storageClassFactory.getStorageClass("StructuredData") 

dimensions = frozenset(("visit", "physical_filter")) 

nDatasets = 6 

dataIds = [{"visit": i, "physical_filter": "V"} for i in range(nDatasets)] 

data = [(self.makeDatasetRef("metric", dimensions, storageClass, dataId), makeExampleMetrics()) 

for dataId in dataIds] 

succeed = data[:nDatasets//2] 

fail = data[nDatasets//2:] 

# All datasets added in this transaction should continue to exist 

with datastore.transaction(): 

for ref, metrics in succeed: 

datastore.put(metrics, ref) 

# Whereas datasets added in this transaction should not 

with self.assertRaises(TransactionTestError): 

with datastore.transaction(): 

for ref, metrics in fail: 

datastore.put(metrics, ref) 

raise TransactionTestError("This should propagate out of the context manager") 

# Check for datasets that should exist 

for ref, metrics in succeed: 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

# Get 

metricsOut = datastore.get(ref, parameters=None) 

self.assertEqual(metrics, metricsOut) 

# URI 

uri = datastore.getUri(ref) 

self.assertEqual(uri[:len(self.uriScheme)], self.uriScheme) 

# Check for datasets that should not exist 

for ref, _ in fail: 

# These should raise 

with self.assertRaises(FileNotFoundError): 

# non-existing file 

datastore.get(ref) 

with self.assertRaises(FileNotFoundError): 

datastore.getUri(ref) 

 

def testNestedTransaction(self): 

datastore = self.makeDatastore() 

storageClass = self.storageClassFactory.getStorageClass("StructuredData") 

dimensions = frozenset(("visit", "physical_filter")) 

metrics = makeExampleMetrics() 

 

dataId = {"visit": 0, "physical_filter": "V"} 

refBefore = self.makeDatasetRef("metric", dimensions, storageClass, dataId) 

datastore.put(metrics, refBefore) 

with self.assertRaises(TransactionTestError): 

with datastore.transaction(): 

dataId = {"visit": 1, "physical_filter": "V"} 

refOuter = self.makeDatasetRef("metric", dimensions, storageClass, dataId) 

datastore.put(metrics, refOuter) 

with datastore.transaction(): 

dataId = {"visit": 2, "physical_filter": "V"} 

refInner = self.makeDatasetRef("metric", dimensions, storageClass, dataId) 

datastore.put(metrics, refInner) 

# All datasets should exist 

for ref in (refBefore, refOuter, refInner): 

metricsOut = datastore.get(ref, parameters=None) 

self.assertEqual(metrics, metricsOut) 

raise TransactionTestError("This should roll back the transaction") 

# Dataset(s) inserted before the transaction should still exist 

metricsOut = datastore.get(refBefore, parameters=None) 

self.assertEqual(metrics, metricsOut) 

# But all datasets inserted during the (rolled back) transaction 

# should be gone 

with self.assertRaises(FileNotFoundError): 

datastore.get(refOuter) 

with self.assertRaises(FileNotFoundError): 

datastore.get(refInner) 

 

def runIngestTest(self, func, expectOutput=True): 

storageClass = self.storageClassFactory.getStorageClass("StructuredData") 

dimensions = frozenset(("visit", "physical_filter")) 

metrics = makeExampleMetrics() 

dataId = {"visit": 0, "physical_filter": "V"} 

ref = self.makeDatasetRef("metric", dimensions, storageClass, dataId) 

with lsst.utils.tests.getTempFilePath(".yaml", expectOutput=expectOutput) as path: 

with open(path, 'w') as fd: 

yaml.dump(metrics._asdict(), stream=fd) 

func(metrics, path, ref) 

 

def testIngestNoTransfer(self): 

"""Test ingesting existing files with no transfer. 

""" 

datastore = self.makeDatastore() 

 

def succeed(obj, path, ref): 

"""Ingest a file already in the datastore root.""" 

# first move it into the root, and adjust the path accordingly 

path = shutil.copy(path, datastore.root) 

path = os.path.relpath(path, start=datastore.root) 

datastore.ingest(path, ref, transfer=None) 

self.assertEqual(obj, datastore.get(ref)) 

 

def failInputDoesNotExist(obj, path, ref): 

"""Can't ingest files if we're given a bad path.""" 

with self.assertRaises(FileNotFoundError): 

datastore.ingest("this-file-does-not-exist.yaml", ref, transfer=None) 

self.assertFalse(datastore.exists(ref)) 

 

def failOutsideRoot(obj, path, ref): 

"""Can't ingest files outside of datastore root.""" 

with self.assertRaises(RuntimeError): 

datastore.ingest(os.path.abspath(path), ref, transfer=None) 

self.assertFalse(datastore.exists(ref)) 

 

def failNotImplemented(obj, path, ref): 

with self.assertRaises(NotImplementedError): 

datastore.ingest(path, ref, transfer=None) 

 

if None in self.ingestTransferModes: 

self.runIngestTest(failOutsideRoot) 

self.runIngestTest(failInputDoesNotExist) 

self.runIngestTest(succeed) 

else: 

self.runIngestTest(failNotImplemented) 

 

def testIngestTransfer(self): 

"""Test ingesting existing files after transferring them. 

""" 

 

for mode in ("copy", "move", "hardlink", "symlink"): 

with self.subTest(mode=mode): 

datastore = self.makeDatastore(mode) 

 

def succeed(obj, path, ref): 

"""Ingest a file by transferring it to the template 

location.""" 

datastore.ingest(os.path.abspath(path), ref, transfer=mode) 

self.assertEqual(obj, datastore.get(ref)) 

 

def failInputDoesNotExist(obj, path, ref): 

"""Can't ingest files if we're given a bad path.""" 

with self.assertRaises(FileNotFoundError): 

datastore.ingest("this-file-does-not-exist.yaml", ref, transfer=mode) 

self.assertFalse(datastore.exists(ref)) 

 

def failOutputExists(obj, path, ref): 

"""Can't ingest files if transfer destination already 

exists.""" 

with self.assertRaises(FileExistsError): 

datastore.ingest(os.path.abspath(path), ref, transfer=mode) 

self.assertFalse(datastore.exists(ref)) 

 

def failNotImplemented(obj, path, ref): 

with self.assertRaises(NotImplementedError): 

datastore.ingest(os.path.abspath(path), ref, transfer=mode) 

 

if mode in self.ingestTransferModes: 

self.runIngestTest(failInputDoesNotExist) 

self.runIngestTest(succeed, expectOutput=(mode != "move")) 

self.runIngestTest(failOutputExists) 

else: 

self.runIngestTest(failNotImplemented) 

 

 

class PosixDatastoreTestCase(DatastoreTests, unittest.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

uriScheme = "file:" 

ingestTransferModes = (None, "copy", "move", "hardlink", "symlink") 

isEphemeral = False 

rootKeys = ("root",) 

validationCanFail = True 

 

def setUp(self): 

# Override the working directory before calling the base class 

self.root = tempfile.mkdtemp(dir=TESTDIR) 

super().setUp() 

 

 

class InMemoryDatastoreTestCase(DatastoreTests, unittest.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/inMemoryDatastore.yaml") 

uriScheme = "mem:" 

hasUnsupportedPut = False 

ingestTransferModes = () 

isEphemeral = True 

rootKeys = None 

validationCanFail = False 

 

 

class ChainedDatastoreTestCase(PosixDatastoreTestCase): 

"""ChainedDatastore specialization using a POSIXDatastore""" 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastore.yaml") 

hasUnsupportedPut = False 

ingestTransferModes = ("copy", "move", "hardlink", "symlink") 

isEphemeral = False 

rootKeys = (".datastores.1.root", ".datastores.2.root") 

validationCanFail = True 

 

 

class ChainedDatastoreMemoryTestCase(InMemoryDatastoreTestCase): 

"""ChainedDatastore specialization using all InMemoryDatastore""" 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastore2.yaml") 

validationCanFail = False 

 

 

class DatastoreConstraintsTests(DatastoreTestsBase): 

"""Basic tests of constraints model of Datastores.""" 

 

def testConstraints(self): 

"""Test constraints model. Assumes that each test class has the 

same constraints.""" 

metrics = makeExampleMetrics() 

datastore = self.makeDatastore() 

 

sc1 = self.storageClassFactory.getStorageClass("StructuredData") 

sc2 = self.storageClassFactory.getStorageClass("StructuredDataJson") 

dimensions = frozenset(("visit", "physical_filter", "Instrument")) 

dataId = {"visit": 52, "physical_filter": "V", "instrument": "DummyCamComp"} 

 

# Write empty file suitable for ingest check 

testfile = tempfile.NamedTemporaryFile() 

 

for datasetTypeName, sc, accepted in (("metric", sc1, True), ("metric2", sc1, False), 

("metric33", sc1, True), ("metric2", sc2, True)): 

with self.subTest(datasetTypeName=datasetTypeName): 

ref = self.makeDatasetRef(datasetTypeName, dimensions, sc, dataId) 

if accepted: 

datastore.put(metrics, ref) 

self.assertTrue(datastore.exists(ref)) 

datastore.remove(ref) 

 

# Try ingest 

if self.canIngest: 

datastore.ingest(testfile.name, ref, transfer="symlink") 

self.assertTrue(datastore.exists(ref)) 

datastore.remove(ref) 

else: 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.put(metrics, ref) 

self.assertFalse(datastore.exists(ref)) 

 

# Again with ingest 

if self.canIngest: 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.ingest(testfile.name, ref, transfer="symlink") 

self.assertFalse(datastore.exists(ref)) 

 

 

class PosixDatastoreConstraintsTestCase(DatastoreConstraintsTests, unittest.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/posixDatastoreP.yaml") 

canIngest = True 

 

def setUp(self): 

# Override the working directory before calling the base class 

self.root = tempfile.mkdtemp(dir=TESTDIR) 

super().setUp() 

 

 

class InMemoryDatastoreConstraintsTestCase(DatastoreConstraintsTests, unittest.TestCase): 

"""InMemoryDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/inMemoryDatastoreP.yaml") 

canIngest = False 

 

 

class ChainedDatastoreConstraintsNativeTestCase(PosixDatastoreConstraintsTestCase): 

"""ChainedDatastore specialization using a POSIXDatastore and constraints 

at the ChainedDatstore """ 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastorePa.yaml") 

 

 

class ChainedDatastoreConstraintsTestCase(PosixDatastoreConstraintsTestCase): 

"""ChainedDatastore specialization using a POSIXDatastore""" 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastoreP.yaml") 

 

 

class ChainedDatastoreMemoryConstraintsTestCase(InMemoryDatastoreConstraintsTestCase): 

"""ChainedDatastore specialization using all InMemoryDatastore""" 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastore2P.yaml") 

canIngest = False 

 

 

class ChainedDatastorePerStoreConstraintsTests(DatastoreTestsBase, unittest.TestCase): 

"""Test that a chained datastore can control constraints per-datastore 

even if child datastore would accept.""" 

 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastorePb.yaml") 

 

def setUp(self): 

# Override the working directory before calling the base class 

self.root = tempfile.mkdtemp(dir=TESTDIR) 

super().setUp() 

 

def testConstraints(self): 

"""Test chained datastore constraints model.""" 

metrics = makeExampleMetrics() 

datastore = self.makeDatastore() 

 

sc1 = self.storageClassFactory.getStorageClass("StructuredData") 

sc2 = self.storageClassFactory.getStorageClass("StructuredDataJson") 

dimensions = frozenset(("visit", "physical_filter", "instrument")) 

dataId1 = {"visit": 52, "physical_filter": "V", "instrument": "DummyCamComp"} 

dataId2 = {"visit": 52, "physical_filter": "V", "instrument": "HSC"} 

 

# Write empty file suitable for ingest check 

testfile = tempfile.NamedTemporaryFile() 

 

for typeName, dataId, sc, accept, ingest in (("metric", dataId1, sc1, (False, True, False), True), 

("metric2", dataId1, sc1, (False, False, False), False), 

("metric2", dataId2, sc1, (True, False, False), False), 

("metric33", dataId2, sc2, (True, True, False), True), 

("metric2", dataId1, sc2, (False, True, False), True)): 

with self.subTest(datasetTypeName=typeName, dataId=dataId, sc=sc.name): 

ref = self.makeDatasetRef(typeName, dimensions, sc, dataId) 

if any(accept): 

datastore.put(metrics, ref) 

self.assertTrue(datastore.exists(ref)) 

 

# Check each datastore inside the chained datastore 

for childDatastore, expected in zip(datastore.datastores, accept): 

self.assertEqual(childDatastore.exists(ref), expected, 

f"Testing presence of {ref} in datastore {childDatastore.name}") 

 

datastore.remove(ref) 

 

# Check that ingest works 

if ingest: 

datastore.ingest(testfile.name, ref, transfer="symlink") 

self.assertTrue(datastore.exists(ref)) 

 

# Check each datastore inside the chained datastore 

for childDatastore, expected in zip(datastore.datastores, accept): 

# Ephemeral datastores means InMemory at the moment 

# and that does not accept ingest of files. 

if childDatastore.isEphemeral: 

expected = False 

self.assertEqual(childDatastore.exists(ref), expected, 

f"Testing presence of ingested {ref} in datastore" 

f" {childDatastore.name}") 

 

datastore.remove(ref) 

else: 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.ingest(testfile.name, ref, transfer="symlink") 

 

else: 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.put(metrics, ref) 

self.assertFalse(datastore.exists(ref)) 

 

# Again with ingest 

with self.assertRaises(DatasetTypeNotSupportedError): 

datastore.ingest(testfile.name, ref, transfer="symlink") 

self.assertFalse(datastore.exists(ref)) 

 

 

645 ↛ 646line 645 didn't jump to line 646, because the condition on line 645 was never trueif __name__ == "__main__": 

unittest.main()