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

# This file is part of ctrl_mpexec. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

 

"""Simple unit test for parser module. 

""" 

 

from argparse import ArgumentParser 

import unittest 

 

import lsst.utils.tests 

import lsst.ctrl.mpexec.cmdLineParser as parser_mod 

 

 

class _Error(Exception): 

pass 

 

 

class _NoExitParser(ArgumentParser): 

"""Special parser subclass which does not exit on errors or help. 

""" 

 

def exit(self, status=0, message=None): 

pass 

 

def error(self, message): 

raise _Error(message) 

 

 

class CmdLineParserTestCase(unittest.TestCase): 

"""A test case for parser module 

""" 

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

def testPipelineAction(self): 

"""Test for a _PipelineAction and _pipe_action 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-t", dest="pipeline_actions", action='append', 

type=parser_mod._ACTION_ADD_TASK) 

parser.add_argument("-m", dest="pipeline_actions", action='append', 

type=parser_mod._ACTION_MOVE_TASK) 

 

PipelineAction = parser_mod._PipelineAction 

args = parser.parse_args("-t task".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

args = parser.parse_args("-t task:label -m label:1".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", "label", "task"), 

PipelineAction("move_task", "label", 1)]) 

 

args = parser.parse_args("-t task".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

with self.assertRaises(_Error): 

parser.parse_args("-m label".split()) 

 

with self.assertRaises(_Error): 

parser.parse_args("-m label:notANumber".split()) 

 

# something that is not syntaxically correct (not a literal) 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:CannotEval".split()) 

 

# Literal but of the wrong type 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:[1,2]".split()) 

 

# dictionary but not all strings 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:{'x':1}".split()) 

 

def testInputCollectionType(self): 

"""Test for a _inputCollectionType 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-i", dest="input", type=parser_mod._inputCollectionType, 

default={}) 

 

args = parser.parse_args("".split()) 

self.assertEqual(args.input, {}) 

 

args = parser.parse_args("-i coll".split()) 

self.assertEqual(args.input, {"": ["coll"]}) 

 

args = parser.parse_args("-i coll1,coll2,coll3".split()) 

self.assertEqual(args.input, {"": ["coll1", "coll2", "coll3"]}) 

 

args = parser.parse_args("-i ds:coll".split()) 

self.assertEqual(args.input, {"ds": ["coll"]}) 

 

args = parser.parse_args("-i ds1:coll1,ds2:coll2,ds2:coll3".split()) 

self.assertEqual(args.input, {"ds1": ["coll1"], 

"ds2": ["coll2", "coll3"]}) 

 

args = parser.parse_args("-i coll1,ds1:coll1,coll2,ds2:coll2,ds2:coll3,coll3".split()) 

self.assertEqual(args.input, {"": ["coll1", "coll2", "coll3"], 

"ds1": ["coll1"], 

"ds2": ["coll2", "coll3"]}) 

 

def testOutputCollectionType(self): 

"""Test for a _outputCollectionType 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-o", dest="output", type=parser_mod._outputCollectionType, 

default={}) 

 

args = parser.parse_args("".split()) 

self.assertEqual(args.output, {}) 

 

args = parser.parse_args("-o coll".split()) 

self.assertEqual(args.output, {"": "coll"}) 

 

with self.assertRaises(_Error): 

args = parser.parse_args("-o coll1,coll2,coll3".split()) 

 

args = parser.parse_args("-o ds:coll".split()) 

self.assertEqual(args.output, {"ds": "coll"}) 

 

args = parser.parse_args("-o ds1:coll1,ds2:coll2,ds3:coll3".split()) 

self.assertEqual(args.output, {"ds1": "coll1", 

"ds2": "coll2", 

"ds3": "coll3"}) 

 

args = parser.parse_args("-o coll1,ds2:coll2".split()) 

self.assertEqual(args.output, {"": "coll1", 

"ds2": "coll2"}) 

 

with self.assertRaises(_Error): 

args = parser.parse_args("-o coll1,ds2:coll2,coll3".split()) 

 

def testCmdLineParser(self): 

"""Test for parser_mod.CmdLineParser 

""" 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

# this should result in error 

self.assertRaises(_Error, parser.parse_args) 

 

# know attributes to appear in parser output 

global_options = """ 

data_query butler_config clobberConfig clobberVersions debug 

doraise input loglevel longlog noBackupConfig noVersions 

output packages processes profile subcommand timeout 

""".split() 

 

# test for the set of options defined in each command 

args = parser.parse_args( 

""" 

list 

""".split()) 

list_options = ['show', 'show_headers', 'subparser'] 

self.assertEqual(set(vars(args).keys()), set(global_options + list_options)) 

self.assertEqual(args.subcommand, 'list') 

 

args = parser.parse_args( 

""" 

build -t cmd 

""".split()) 

show_options = ['pipeline_actions', 'show', 'subparser', 'pipeline', 

'order_pipeline', 'save_pipeline', 'pipeline_dot', 

'save_single_quanta'] 

self.assertEqual(set(vars(args).keys()), set(global_options + show_options)) 

self.assertEqual(args.subcommand, 'build') 

 

args = parser.parse_args( 

""" 

qgraph -t cmd 

""".split()) 

qgraph_options = ['pipeline_actions', 'show', 'subparser', 'pipeline', 

'order_pipeline', 'save_pipeline', 'pipeline_dot', 

'qgraph_dot', 'qgraph', 'save_qgraph', 'skip_existing', 

'clobber_output', 'save_single_quanta'] 

self.assertEqual(set(vars(args).keys()), set(global_options + qgraph_options)) 

self.assertEqual(args.subcommand, 'qgraph') 

 

args = parser.parse_args( 

""" 

run -t taskname 

""".split()) 

run_options = ['pipeline_actions', 'show', 'subparser', 'pipeline', 

'order_pipeline', 'save_pipeline', 'pipeline_dot', 

'qgraph_dot', 'qgraph', 'save_qgraph', 'skip_existing', 

'clobber_output', 'register_dataset_types', 'skip_init_writes', 

'init_only', 'save_single_quanta'] 

self.assertEqual(set(vars(args).keys()), set(global_options + run_options)) 

self.assertEqual(args.subcommand, 'run') 

 

def testCmdLineList(self): 

 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

# check list subcommand with options 

args = parser.parse_args("list".split()) 

self.assertIsNone(args.show) 

 

args = parser.parse_args("list -p".split()) 

self.assertEqual(args.show, ["packages"]) 

 

args = parser.parse_args("list -m".split()) 

self.assertEqual(args.show, ["modules"]) 

 

args = parser.parse_args("list -t".split()) 

self.assertEqual(args.show, ["tasks"]) 

 

args = parser.parse_args("list -l".split()) 

self.assertEqual(args.show, ["pipeline-tasks"]) 

 

args = parser.parse_args("list -l --pipeline-tasks".split()) 

self.assertEqual(args.show, ["pipeline-tasks", "pipeline-tasks"]) 

 

args = parser.parse_args("list --packages --modules".split()) 

self.assertEqual(args.show, ["packages", "modules"]) 

 

args = parser.parse_args("list --pipeline-tasks --tasks".split()) 

self.assertEqual(args.show, ["pipeline-tasks", "tasks"]) 

 

def testCmdLineTasks(self): 

 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

PipelineAction = parser_mod._PipelineAction 

 

# default options 

args = parser.parse_args( 

""" 

run -t taskname 

""".split()) 

self.assertFalse(args.clobberConfig) 

self.assertFalse(args.clobberVersions) 

self.assertFalse(args.debug) 

self.assertFalse(args.doraise) 

self.assertEqual(args.input, {}) 

self.assertEqual(args.loglevel, []) 

self.assertFalse(args.longlog) 

self.assertFalse(args.noBackupConfig) 

self.assertFalse(args.noVersions) 

self.assertEqual(args.output, {}) 

self.assertEqual(args.processes, 1) 

self.assertIsNone(args.profile) 

self.assertIsNone(args.timeout) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "taskname")]) 

self.assertEqual(args.show, []) 

self.assertIsNotNone(args.subparser) 

self.assertIsNone(args.pipeline) 

 

# bunch of random options 

args = parser.parse_args( 

""" 

--clobber-config 

--clobber-versions 

--debug 

--doraise 

--input inputColl 

--loglevel DEBUG -L component=trace 

--longlog 

--no-backup-config 

--no-versions 

--output outputColl 

-j 66 

--profile profile.out 

--timeout 10.10 

run -t taskname:label 

--show config 

--show config=Task.* 

-c label:a=b 

-C label:filename1 

-c label:c=d -c label:e=f 

-C label:filename2 -C label:filename3 

--skip-existing 

""".split()) 

self.assertTrue(args.clobberConfig) 

self.assertTrue(args.clobberVersions) 

self.assertTrue(args.debug) 

self.assertTrue(args.doraise) 

self.assertEqual(args.input, {"": ["inputColl"]}) 

self.assertEqual(args.loglevel, [(None, 'DEBUG'), ('component', 'TRACE')]) 

self.assertTrue(args.longlog) 

self.assertTrue(args.noBackupConfig) 

self.assertTrue(args.noVersions) 

self.assertEqual(args.output, {"": "outputColl"}) 

self.assertEqual(args.processes, 66) 

self.assertEqual(args.profile, 'profile.out') 

self.assertEqual(args.timeout, 10.10) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", "label", "taskname"), 

PipelineAction("config", "label", "a=b"), 

PipelineAction("configfile", "label", "filename1"), 

PipelineAction("config", "label", "c=d"), 

PipelineAction("config", "label", "e=f"), 

PipelineAction("configfile", "label", "filename2"), 

PipelineAction("configfile", "label", "filename3")]) 

self.assertIsNone(args.pipeline) 

self.assertIsNone(args.qgraph) 

self.assertFalse(args.order_pipeline) 

self.assertIsNone(args.save_pipeline) 

self.assertIsNone(args.save_qgraph) 

self.assertIsNone(args.pipeline_dot) 

self.assertIsNone(args.qgraph_dot) 

self.assertTrue(args.skip_existing) 

 

# multiple tasks plus more options (-q should be exclusive with 

# some other options but we do not check it during parsing (yet)) 

args = parser.parse_args( 

""" 

run 

-p pipeline.pickle 

-g qgraph.pickle 

-t task1 

-t task2:label2 

-t task3 

-t task4 

--show config 

-m task1:2 

-c task1:a=b 

-C task1:filename1 

-c label2:c=d -c label2:e=f 

-C task3:filename2 -C task3:filename3 

--show config=Task.* 

-C task4:filename4 -c task4:x=y 

--order-pipeline 

--save-pipeline=newpipe.pickle 

--save-qgraph=newqgraph.pickle 

--pipeline-dot pipe.dot 

--qgraph-dot qgraph.dot 

""".split()) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task1"), 

PipelineAction("new_task", "label2", "task2"), 

PipelineAction("new_task", None, "task3"), 

PipelineAction("new_task", None, "task4"), 

PipelineAction("move_task", "task1", 2), 

PipelineAction("config", "task1", "a=b"), 

PipelineAction("configfile", "task1", "filename1"), 

PipelineAction("config", "label2", "c=d"), 

PipelineAction("config", "label2", "e=f"), 

PipelineAction("configfile", "task3", "filename2"), 

PipelineAction("configfile", "task3", "filename3"), 

PipelineAction("configfile", "task4", "filename4"), 

PipelineAction("config", "task4", "x=y")]) 

self.assertEqual(args.pipeline, "pipeline.pickle") 

self.assertEqual(args.qgraph, "qgraph.pickle") 

self.assertTrue(args.order_pipeline) 

self.assertEqual(args.save_pipeline, "newpipe.pickle") 

self.assertEqual(args.save_qgraph, "newqgraph.pickle") 

self.assertEqual(args.pipeline_dot, "pipe.dot") 

self.assertEqual(args.qgraph_dot, "qgraph.dot") 

 

# check that exclusive options generate error 

with self.assertRaises(_Error): 

parser.parse_args("qgraph -p pipeline.pickle --skip-existing --clobber-output".split()) 

 

def testCmdLinePipeline(self): 

 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

PipelineAction = parser_mod._PipelineAction 

 

args = parser.parse_args( 

""" 

-p package 

run -p pipeline 

--show config 

--show config=Task.* 

""".split()) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline, 'pipeline') 

self.assertEqual(args.pipeline_actions, []) 

 

args = parser.parse_args("run -p pipeline -t task".split()) 

self.assertEqual(args.pipeline, 'pipeline') 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

 

class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()