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

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

# 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/>. 

 

"""Tests for Butler. 

""" 

 

import os 

import posixpath 

import unittest 

import tempfile 

import shutil 

import pickle 

import string 

import random 

 

try: 

import boto3 

import botocore 

from moto import mock_s3 

except ImportError: 

boto3 = None 

 

def mock_s3(cls): 

"""A no-op decorator in case moto mock_s3 can not be imported. 

""" 

return cls 

 

from lsst.daf.butler.core.safeFileIo import safeMakeDir 

from lsst.daf.butler import Butler, Config, ButlerConfig 

from lsst.daf.butler import StorageClassFactory 

from lsst.daf.butler import DatasetType, DatasetRef 

from lsst.daf.butler import FileTemplateValidationError, ValidationError 

from examplePythonTypes import MetricsExample 

from lsst.daf.butler.core.repoRelocation import BUTLER_ROOT_TAG 

from lsst.daf.butler.core.location import ButlerURI 

from lsst.daf.butler.core.s3utils import (s3CheckFileExists, setAwsEnvCredentials, 

unsetAwsEnvCredentials) 

 

 

TESTDIR = os.path.abspath(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, 752, 8, 9, 27] 

) 

 

 

class TransactionTestError(Exception): 

"""Specific error for testing transactions, to prevent misdiagnosing 

that might otherwise occur when a standard exception is used. 

""" 

pass 

 

 

class ButlerConfigTests(unittest.TestCase): 

"""Simple tests for ButlerConfig that are not tested in other test cases. 

""" 

 

def testSearchPath(self): 

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

with self.assertLogs("lsst.daf.butler", level="DEBUG") as cm: 

config1 = ButlerConfig(configFile) 

self.assertNotIn("testConfigs", "\n".join(cm.output)) 

 

overrideDirectory = os.path.join(TESTDIR, "config", "testConfigs") 

with self.assertLogs("lsst.daf.butler", level="DEBUG") as cm: 

config2 = ButlerConfig(configFile, searchPaths=[overrideDirectory]) 

self.assertIn("testConfigs", "\n".join(cm.output)) 

 

key = ("datastore", "records", "table") 

self.assertNotEqual(config1[key], config2[key]) 

self.assertEqual(config2[key], "override_record") 

 

 

class ButlerTests: 

"""Tests for Butler. 

""" 

useTempRoot = True 

 

@staticmethod 

def addDatasetType(datasetTypeName, dimensions, storageClass, registry): 

"""Create a DatasetType and register it 

""" 

datasetType = DatasetType(datasetTypeName, dimensions, storageClass) 

registry.registerDatasetType(datasetType) 

return datasetType 

 

@classmethod 

def setUpClass(cls): 

cls.storageClassFactory = StorageClassFactory() 

cls.storageClassFactory.addFromConfig(cls.configFile) 

 

def assertGetComponents(self, butler, datasetRef, components, reference): 

datasetTypeName = datasetRef.datasetType.name 

dataId = datasetRef.dataId 

for component in components: 

compTypeName = DatasetType.nameWithComponent(datasetTypeName, component) 

result = butler.get(compTypeName, dataId) 

self.assertEqual(result, getattr(reference, component)) 

 

def setUp(self): 

"""Create a new butler root for each test.""" 

if self.useTempRoot: 

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

Butler.makeRepo(self.root, config=Config(self.configFile)) 

self.tmpConfigFile = os.path.join(self.root, "butler.yaml") 

else: 

self.root = None 

self.tmpConfigFile = self.configFile 

 

def tearDown(self): 

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

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

 

def testConstructor(self): 

"""Independent test of constructor. 

""" 

butler = Butler(self.tmpConfigFile) 

self.assertIsInstance(butler, Butler) 

 

collections = butler.registry.getAllCollections() 

self.assertEqual(collections, set()) 

 

def testBasicPutGet(self): 

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

self.runPutGetTest(storageClass, "test_metric") 

 

def testCompositePutGetConcrete(self): 

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

self.runPutGetTest(storageClass, "test_metric") 

 

def testCompositePutGetVirtual(self): 

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

self.runPutGetTest(storageClass, "test_metric_comp") 

 

def runPutGetTest(self, storageClass, datasetTypeName): 

butler = Butler(self.tmpConfigFile) 

 

# There will not be a collection yet 

collections = butler.registry.getAllCollections() 

self.assertEqual(collections, set()) 

 

# Create and register a DatasetType 

dimensions = butler.registry.dimensions.extract(["instrument", "visit"]) 

 

datasetType = self.addDatasetType(datasetTypeName, dimensions, storageClass, butler.registry) 

 

# Add needed Dimensions 

butler.registry.addDimensionEntry("instrument", {"instrument": "DummyCamComp"}) 

butler.registry.addDimensionEntry("physical_filter", {"instrument": "DummyCamComp", 

"physical_filter": "d-r"}) 

butler.registry.addDimensionEntry("visit", {"instrument": "DummyCamComp", "visit": 423, 

"physical_filter": "d-r"}) 

 

# Create and store a dataset 

metric = makeExampleMetrics() 

dataId = {"instrument": "DummyCamComp", "visit": 423} 

 

# Create a DatasetRef for put 

refIn = DatasetRef(datasetType, dataId, id=None) 

 

# Put with a preexisting id should fail 

with self.assertRaises(ValueError): 

butler.put(metric, DatasetRef(datasetType, dataId, id=100)) 

 

# Put and remove the dataset once as a DatasetRef, once as a dataId, 

# and once with a DatasetType 

for args in ((refIn,), (datasetTypeName, dataId), (datasetType, dataId)): 

with self.subTest(args=args): 

ref = butler.put(metric, *args) 

self.assertIsInstance(ref, DatasetRef) 

 

# Test getDirect 

metricOut = butler.getDirect(ref) 

self.assertEqual(metric, metricOut) 

# Test get 

metricOut = butler.get(ref.datasetType.name, dataId) 

self.assertEqual(metric, metricOut) 

# Test get with a datasetRef 

metricOut = butler.get(ref) 

self.assertEqual(metric, metricOut) 

 

# Check we can get components 

if storageClass.isComposite(): 

self.assertGetComponents(butler, ref, 

("summary", "data", "output"), metric) 

 

# Remove from collection only; after that we shouldn't be able 

# to find it unless we use the dataset_id. 

butler.remove(*args, delete=False) 

with self.assertRaises(LookupError): 

butler.datasetExists(*args) 

# If we use the output ref with the dataset_id, we should 

# still be able to load it with getDirect(). 

self.assertEqual(metric, butler.getDirect(ref)) 

 

# Reinsert into collection, then delete from Datastore *and* 

# remove from collection. 

butler.registry.associate(butler.collection, [ref]) 

butler.remove(*args) 

# Lookup with original args should still fail. 

with self.assertRaises(LookupError): 

butler.datasetExists(*args) 

# Now getDirect() should fail, too. 

with self.assertRaises(FileNotFoundError): 

butler.getDirect(ref) 

# Registry still knows about it, if we use the dataset_id. 

self.assertEqual(butler.registry.getDataset(ref.id), ref) 

 

# Put again, then remove completely (this generates a new 

# dataset record in registry, with a new ID - the old one 

# still exists but it is not in any collection so we don't 

# care). 

ref = butler.put(metric, *args) 

butler.remove(*args, remember=False) 

# Lookup with original args should still fail. 

with self.assertRaises(LookupError): 

butler.datasetExists(*args) 

# getDirect() should still fail. 

with self.assertRaises(FileNotFoundError): 

butler.getDirect(ref) 

# Registry shouldn't be able to find it by dataset_id anymore. 

self.assertIsNone(butler.registry.getDataset(ref.id)) 

 

# Put the dataset again, since the last thing we did was remove it. 

ref = butler.put(metric, refIn) 

 

# Get with parameters 

stop = 4 

sliced = butler.get(ref, parameters={"slice": slice(stop)}) 

self.assertNotEqual(metric, sliced) 

self.assertEqual(metric.summary, sliced.summary) 

self.assertEqual(metric.output, sliced.output) 

self.assertEqual(metric.data[:stop], sliced.data) 

 

# Combining a DatasetRef with a dataId should fail 

with self.assertRaises(ValueError): 

butler.get(ref, dataId) 

# Getting with an explicit ref should fail if the id doesn't match 

with self.assertRaises(ValueError): 

butler.get(DatasetRef(ref.datasetType, ref.dataId, id=101)) 

 

# Getting a dataset with unknown parameters should fail 

with self.assertRaises(KeyError): 

butler.get(ref, parameters={"unsupported": True}) 

 

# Check we have a collection 

collections = butler.registry.getAllCollections() 

self.assertEqual(collections, {"ingest", }) 

 

def testPickle(self): 

"""Test pickle support. 

""" 

butler = Butler(self.tmpConfigFile) 

butlerOut = pickle.loads(pickle.dumps(butler)) 

self.assertIsInstance(butlerOut, Butler) 

self.assertEqual(butlerOut.config, butler.config) 

self.assertEqual(butlerOut.collection, butler.collection) 

self.assertEqual(butlerOut.run, butler.run) 

 

def testGetDatasetTypes(self): 

butler = Butler(self.tmpConfigFile) 

dimensions = butler.registry.dimensions.extract(["instrument", "visit", "physical_filter"]) 

dimensionEntries = (("instrument", {"instrument": "DummyCam"}), 

("instrument", {"instrument": "DummyHSC"}), 

("instrument", {"instrument": "DummyCamComp"}), 

("physical_filter", {"instrument": "DummyCam", "physical_filter": "d-r"}), 

("visit", {"instrument": "DummyCam", "visit": 42, "physical_filter": "d-r"})) 

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

# Add needed Dimensions 

for name, value in dimensionEntries: 

butler.registry.addDimensionEntry(name, value) 

 

# When a DatasetType is added to the registry entries are created 

# for each component. Need entries for each component in the test 

# configuration otherwise validation won't work. The ones that 

# are deliberately broken will be ignored later. 

datasetTypeNames = {"metric", "metric2", "metric4", "metric33", "pvi"} 

components = set() 

for datasetTypeName in datasetTypeNames: 

# Create and register a DatasetType 

self.addDatasetType(datasetTypeName, dimensions, storageClass, butler.registry) 

 

for componentName in storageClass.components: 

components.add(DatasetType.nameWithComponent(datasetTypeName, componentName)) 

 

fromRegistry = butler.registry.getAllDatasetTypes() 

self.assertEqual({d.name for d in fromRegistry}, datasetTypeNames | components) 

 

# Now that we have some dataset types registered, validate them 

butler.validateConfiguration(ignore=["test_metric_comp", "metric3", "calexp", "DummySC", 

"datasetType.component"]) 

 

# Add a new datasetType that will fail template validation 

self.addDatasetType("test_metric_comp", dimensions, storageClass, butler.registry) 

if self.validationCanFail: 

with self.assertRaises(ValidationError): 

butler.validateConfiguration() 

 

# Rerun validation but with a subset of dataset type names 

butler.validateConfiguration(datasetTypeNames=["metric4"]) 

 

# Rerun validation but ignore the bad datasetType 

butler.validateConfiguration(ignore=["test_metric_comp", "metric3", "calexp", "DummySC", 

"datasetType.component"]) 

 

def testTransaction(self): 

butler = Butler(self.tmpConfigFile) 

datasetTypeName = "test_metric" 

dimensions = butler.registry.dimensions.extract(["instrument", "visit"]) 

dimensionEntries = (("instrument", {"instrument": "DummyCam"}), 

("physical_filter", {"instrument": "DummyCam", "physical_filter": "d-r"}), 

("visit", {"instrument": "DummyCam", "visit": 42, "physical_filter": "d-r"})) 

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

metric = makeExampleMetrics() 

dataId = {"instrument": "DummyCam", "visit": 42} 

with self.assertRaises(TransactionTestError): 

with butler.transaction(): 

# Create and register a DatasetType 

datasetType = self.addDatasetType(datasetTypeName, dimensions, storageClass, butler.registry) 

# Add needed Dimensions 

for name, value in dimensionEntries: 

butler.registry.addDimensionEntry(name, value) 

# Store a dataset 

ref = butler.put(metric, datasetTypeName, dataId) 

self.assertIsInstance(ref, DatasetRef) 

# Test getDirect 

metricOut = butler.getDirect(ref) 

self.assertEqual(metric, metricOut) 

# Test get 

metricOut = butler.get(datasetTypeName, dataId) 

self.assertEqual(metric, metricOut) 

# Check we can get components 

self.assertGetComponents(butler, ref, 

("summary", "data", "output"), metric) 

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

 

with self.assertRaises(KeyError): 

butler.registry.getDatasetType(datasetTypeName) 

for name, value in dimensionEntries: 

self.assertIsNone(butler.registry.findDimensionEntry(name, value)) 

# Should raise KeyError for missing DatasetType 

with self.assertRaises(KeyError): 

butler.get(datasetTypeName, dataId) 

# Also check explicitly if Dataset entry is missing 

self.assertIsNone(butler.registry.find(butler.collection, datasetType, dataId)) 

# Direct retrieval should not find the file in the Datastore 

with self.assertRaises(FileNotFoundError): 

butler.getDirect(ref) 

 

def testMakeRepo(self): 

"""Test that we can write butler configuration to a new repository via 

the Butler.makeRepo interface and then instantiate a butler from the 

repo root. 

""" 

# Do not run the test if we know this datastore configuration does 

# not support a file system root 

if self.fullConfigKey is None: 

return 

 

# Remove the file created in setUp 

os.unlink(self.tmpConfigFile) 

 

Butler.makeRepo(self.root, config=Config(self.configFile)) 

limited = Config(self.configFile) 

butler1 = Butler(self.root, collection="ingest") 

Butler.makeRepo(self.root, standalone=True, createRegistry=False, 

config=Config(self.configFile)) 

full = Config(self.tmpConfigFile) 

butler2 = Butler(self.root, collection="ingest") 

# Butlers should have the same configuration regardless of whether 

# defaults were expanded. 

self.assertEqual(butler1.config, butler2.config) 

# Config files loaded directly should not be the same. 

self.assertNotEqual(limited, full) 

# Make sure "limited" doesn't have a few keys we know it should be 

# inheriting from defaults. 

self.assertIn(self.fullConfigKey, full) 

self.assertNotIn(self.fullConfigKey, limited) 

 

# Collections don't appear until something is put in them 

collections1 = butler1.registry.getAllCollections() 

self.assertEqual(collections1, set()) 

self.assertEqual(butler2.registry.getAllCollections(), collections1) 

 

def testStringification(self): 

butler = Butler(self.tmpConfigFile) 

butlerStr = str(butler) 

 

if self.datastoreStr is not None: 

for testStr in self.datastoreStr: 

self.assertIn(testStr, butlerStr) 

if self.registryStr is not None: 

self.assertIn(self.registryStr, butlerStr) 

 

datastoreName = butler.datastore.name 

if self.datastoreName is not None: 

for testStr in self.datastoreName: 

self.assertIn(testStr, datastoreName) 

 

 

class PosixDatastoreButlerTestCase(ButlerTests, unittest.TestCase): 

"""PosixDatastore specialization of a butler""" 

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

fullConfigKey = ".datastore.formatters" 

validationCanFail = True 

 

datastoreStr = ["/tmp"] 

datastoreName = [f"POSIXDatastore@{BUTLER_ROOT_TAG}"] 

registryStr = "/gen3.sqlite3" 

 

def checkFileExists(self, root, path): 

"""Checks if file exists at a given path (relative to root). 

 

Test testPutTemplates verifies actual physical existance of the files 

in the requested location. For POSIXDatastore this test is equivalent 

to `os.path.exists` call. 

""" 

return os.path.exists(os.path.join(root, path)) 

 

def testPutTemplates(self): 

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

butler = Butler(self.tmpConfigFile) 

 

# Add needed Dimensions 

butler.registry.addDimensionEntry("instrument", {"instrument": "DummyCamComp"}) 

butler.registry.addDimensionEntry("physical_filter", {"instrument": "DummyCamComp", 

"physical_filter": "d-r"}) 

butler.registry.addDimensionEntry("visit", {"instrument": "DummyCamComp", "visit": 423, 

"physical_filter": "d-r"}) 

butler.registry.addDimensionEntry("visit", {"instrument": "DummyCamComp", "visit": 425, 

"physical_filter": "d-r"}) 

 

# Create and store a dataset 

metric = makeExampleMetrics() 

 

# Create two almost-identical DatasetTypes (both will use default 

# template) 

dimensions = butler.registry.dimensions.extract(["instrument", "visit"]) 

butler.registry.registerDatasetType(DatasetType("metric1", dimensions, storageClass)) 

butler.registry.registerDatasetType(DatasetType("metric2", dimensions, storageClass)) 

butler.registry.registerDatasetType(DatasetType("metric3", dimensions, storageClass)) 

 

dataId1 = {"instrument": "DummyCamComp", "visit": 423} 

dataId2 = {"instrument": "DummyCamComp", "visit": 423, "physical_filter": "d-r"} 

dataId3 = {"instrument": "DummyCamComp", "visit": 425} 

 

# Put with exactly the data ID keys needed 

ref = butler.put(metric, "metric1", dataId1) 

self.assertTrue(self.checkFileExists(butler.datastore.root, 

"ingest/metric1/DummyCamComp_423.pickle")) 

 

# Check the template based on dimensions 

butler.datastore.templates.validateTemplates([ref]) 

 

# Put with extra data ID keys (physical_filter is an optional 

# dependency); should not change template (at least the way we're 

# defining them to behave now; the important thing is that they 

# must be consistent). 

ref = butler.put(metric, "metric2", dataId2) 

self.assertTrue(self.checkFileExists(butler.datastore.root, 

"ingest/metric2/DummyCamComp_423.pickle")) 

 

# Check the template based on dimensions 

butler.datastore.templates.validateTemplates([ref]) 

 

# Now use a file template that will not result in unique filenames 

ref = butler.put(metric, "metric3", dataId1) 

 

# Check the template based on dimensions. This one is a bad template 

with self.assertRaises(FileTemplateValidationError): 

butler.datastore.templates.validateTemplates([ref]) 

 

with self.assertRaises(FileExistsError): 

butler.put(metric, "metric3", dataId3) 

 

 

class InMemoryDatastoreButlerTestCase(ButlerTests, unittest.TestCase): 

"""InMemoryDatastore specialization of a butler""" 

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

fullConfigKey = None 

useTempRoot = False 

validationCanFail = False 

datastoreStr = ["datastore='InMemory'"] 

datastoreName = ["InMemoryDatastore@"] 

registryStr = "registry='sqlite:///:memory:'" 

 

 

class ChainedDatastoreButlerTestCase(ButlerTests, unittest.TestCase): 

"""PosixDatastore specialization""" 

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

fullConfigKey = ".datastore.datastores.1.formatters" 

validationCanFail = True 

datastoreStr = ["datastore='InMemory", "/PosixDatastore_1,", "/PosixDatastore_2'"] 

datastoreName = ["InMemoryDatastore@", f"POSIXDatastore@{BUTLER_ROOT_TAG}/PosixDatastore_1", 

"SecondDatastore"] 

registryStr = "/gen3.sqlite3'" 

 

 

class ButlerExplicitRootTestCase(PosixDatastoreButlerTestCase): 

"""Test that a yaml file in one location can refer to a root in another.""" 

 

datastoreStr = ["dir1"] 

# Disable the makeRepo test since we are deliberately not using 

# butler.yaml as the config name. 

fullConfigKey = None 

 

def setUp(self): 

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

 

# Make a new repository in one place 

self.dir1 = os.path.join(self.root, "dir1") 

Butler.makeRepo(self.dir1, config=Config(self.configFile)) 

 

# Move the yaml file to a different place and add a "root" 

self.dir2 = os.path.join(self.root, "dir2") 

safeMakeDir(self.dir2) 

configFile1 = os.path.join(self.dir1, "butler.yaml") 

config = Config(configFile1) 

config["root"] = self.dir1 

configFile2 = os.path.join(self.dir2, "butler2.yaml") 

config.dumpToFile(configFile2) 

os.remove(configFile1) 

self.tmpConfigFile = configFile2 

 

def testFileLocations(self): 

self.assertNotEqual(self.dir1, self.dir2) 

self.assertTrue(os.path.exists(os.path.join(self.dir2, "butler2.yaml"))) 

self.assertFalse(os.path.exists(os.path.join(self.dir1, "butler.yaml"))) 

self.assertTrue(os.path.exists(os.path.join(self.dir1, "gen3.sqlite3"))) 

 

 

class ButlerConfigNoRunTestCase(unittest.TestCase): 

"""Test case for butler config which does not have ``run``. 

""" 

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

 

def testPickle(self): 

"""Test pickle support. 

""" 

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

Butler.makeRepo(self.root, config=Config(self.configFile)) 

self.tmpConfigFile = os.path.join(self.root, "butler.yaml") 

butler = Butler(self.tmpConfigFile, run="ingest") 

butlerOut = pickle.loads(pickle.dumps(butler)) 

self.assertIsInstance(butlerOut, Butler) 

self.assertEqual(butlerOut.config, butler.config) 

 

def tearDown(self): 

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

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

 

 

@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!") 

@mock_s3 

class S3DatastoreButlerTestCase(PosixDatastoreButlerTestCase): 

"""S3Datastore specialization of a butler; an S3 storage Datastore + 

a local in-memory SqlRegistry. 

""" 

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

fullConfigKey = None 

validationCanFail = True 

 

bucketName = "anybucketname" 

"""Name of the Bucket that will be used in the tests. The name is read from 

the config file used with the tests during set-up. 

""" 

 

root = "butlerRoot/" 

"""Root repository directory expected to be used in case useTempRoot=False. 

Otherwise the root is set to a 20 characters long randomly generated string 

during set-up. 

""" 

 

datastoreStr = [f"datastore={root}"] 

"""Contains all expected root locations in a format expected to be 

returned by Butler stringification. 

""" 

 

datastoreName = ["S3Datastore@s3://{bucketName}/{root}"] 

"""The expected format of the S3Datastore string.""" 

 

registryStr = f"registry='sqlite:///:memory:'" 

"""Expected format of the Registry string.""" 

 

def genRoot(self): 

"""Returns a random string of len 20 to serve as a root 

name for the temporary bucket repo. 

 

This is equivalent to tempfile.mkdtemp as this is what self.root 

becomes when useTempRoot is True. 

""" 

rndstr = "".join( 

random.choice(string.ascii_uppercase + string.digits) for _ in range(20) 

) 

return rndstr + "/" 

 

def setUp(self): 

config = Config(self.configFile) 

uri = ButlerURI(config[".datastore.datastore.root"]) 

self.bucketName = uri.netloc 

 

# set up some fake credentials if they do not exist 

self.usingDummyCredentials = setAwsEnvCredentials() 

 

if self.useTempRoot: 

self.root = self.genRoot() 

rooturi = f"s3://{self.bucketName}/{self.root}" 

config.update({"datastore": {"datastore": {"root": rooturi}}}) 

 

# MOTO needs to know that we expect Bucket bucketname to exist 

# (this used to be the class attribute bucketName) 

s3 = boto3.resource("s3") 

s3.create_bucket(Bucket=self.bucketName) 

 

self.datastoreStr = f"datastore={self.root}" 

self.datastoreName = [f"S3Datastore@{rooturi}"] 

Butler.makeRepo(rooturi, config=config, forceConfigRoot=False) 

self.tmpConfigFile = posixpath.join(rooturi, "butler.yaml") 

 

def tearDown(self): 

s3 = boto3.resource("s3") 

bucket = s3.Bucket(self.bucketName) 

try: 

bucket.objects.all().delete() 

except botocore.exceptions.ClientError as e: 

if e.response["Error"]["Code"] == "404": 

# the key was not reachable - pass 

pass 

else: 

raise 

 

bucket = s3.Bucket(self.bucketName) 

bucket.delete() 

 

# unset any potentially set dummy credentials 

if self.usingDummyCredentials: 

unsetAwsEnvCredentials() 

 

def checkFileExists(self, root, relpath): 

"""Checks if file exists at a given path (relative to root). 

 

Test testPutTemplates verifies actual physical existance of the files 

in the requested location. For S3Datastore this test is equivalent to 

`lsst.daf.butler.core.s3utils.s3checkFileExists` call. 

""" 

uri = ButlerURI(root) 

client = boto3.client("s3") 

return s3CheckFileExists(uri, client=client)[0] 

 

 

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

unittest.main()