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# This file is part of ctrl_mpexec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

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

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

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

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

21 

22"""Simple unit test for PreExecInit class. 

23""" 

24 

25import contextlib 

26import shutil 

27import tempfile 

28import unittest 

29 

30from lsst.ctrl.mpexec import PreExecInit 

31from testUtil import makeSimpleQGraph, AddTaskFactoryMock 

32 

33 

34@contextlib.contextmanager 

35def temporaryDirectory(): 

36 """Context manager that creates and destroys temporary directory. 

37 

38 Difference from `tempfile.TemporaryDirectory` is that it ignores errors 

39 when deleting a directory, which may happen with some filesystems. 

40 """ 

41 tmpdir = tempfile.mkdtemp() 

42 yield tmpdir 

43 shutil.rmtree(tmpdir, ignore_errors=True) 

44 

45 

46class PreExecInitTestCase(unittest.TestCase): 

47 """A test case for PreExecInit 

48 """ 

49 

50 def test_saveInitOutputs(self): 

51 taskFactory = AddTaskFactoryMock() 

52 for skipExisting in (False, True): 

53 with self.subTest(skipExisting=skipExisting): 

54 with temporaryDirectory() as tmpdir: 

55 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

56 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, 

57 skipExisting=skipExisting) 

58 preExecInit.saveInitOutputs(qgraph) 

59 

60 def test_saveInitOutputs_twice(self): 

61 taskFactory = AddTaskFactoryMock() 

62 for skipExisting in (False, True): 

63 with self.subTest(skipExisting=skipExisting): 

64 with temporaryDirectory() as tmpdir: 

65 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

66 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, 

67 skipExisting=skipExisting) 

68 preExecInit.saveInitOutputs(qgraph) 

69 if skipExisting: 

70 # will ignore this 

71 preExecInit.saveInitOutputs(qgraph) 

72 else: 

73 # Second time it will fail 

74 with self.assertRaises(Exception): 

75 preExecInit.saveInitOutputs(qgraph) 

76 

77 def test_saveConfigs(self): 

78 for skipExisting in (False, True): 

79 with self.subTest(skipExisting=skipExisting): 

80 with temporaryDirectory() as tmpdir: 

81 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

82 preExecInit = PreExecInit(butler=butler, taskFactory=None, skipExisting=skipExisting) 

83 preExecInit.saveConfigs(qgraph) 

84 

85 def test_saveConfigs_twice(self): 

86 for skipExisting in (False, True): 

87 with self.subTest(skipExisting=skipExisting): 

88 with temporaryDirectory() as tmpdir: 

89 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

90 preExecInit = PreExecInit(butler=butler, taskFactory=None, skipExisting=skipExisting) 

91 preExecInit.saveConfigs(qgraph) 

92 if skipExisting: 

93 # will ignore this 

94 preExecInit.saveConfigs(qgraph) 

95 else: 

96 # Second time it will fail 

97 with self.assertRaises(Exception): 

98 preExecInit.saveConfigs(qgraph) 

99 

100 def test_savePackageVersions(self): 

101 for skipExisting in (False, True): 

102 with self.subTest(skipExisting=skipExisting): 

103 with temporaryDirectory() as tmpdir: 

104 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

105 preExecInit = PreExecInit(butler=butler, taskFactory=None, skipExisting=skipExisting) 

106 preExecInit.savePackageVersions(qgraph) 

107 

108 def test_savePackageVersions_twice(self): 

109 for skipExisting in (False, True): 

110 with self.subTest(skipExisting=skipExisting): 

111 with temporaryDirectory() as tmpdir: 

112 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

113 preExecInit = PreExecInit(butler=butler, taskFactory=None, skipExisting=skipExisting) 

114 preExecInit.savePackageVersions(qgraph) 

115 if skipExisting: 

116 # if this is the same packages then it should not attempt to save 

117 preExecInit.savePackageVersions(qgraph) 

118 else: 

119 # second time it will fail 

120 with self.assertRaises(Exception): 

121 preExecInit.savePackageVersions(qgraph) 

122 

123 

124if __name__ == "__main__": 124 ↛ 125line 124 didn't jump to line 125, because the condition on line 124 was never true

125 unittest.main()