Coverage for tests/test_cmCampaignToBps.py: 96%

76 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 10:15 +0000

1# This file is part of ap_pipe. 

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 

22import os 

23import tempfile 

24import unittest 

25 

26import yaml 

27 

28import lsst.utils.tests 

29from lsst.ap.pipe.cmCampaignToBps import makeBpsConfig, readCampaign, writeBpsConfig 

30 

31CAMPAIGN = """ 

32kind: campaign 

33metadata: 

34 name: test_${{ yesterday | as_day_obs }} 

35--- 

36kind: node 

37metadata: 

38 kind: step 

39 name: STEP 

40spec: 

41 bps: 

42 pipeline_yaml: "${AP_PIPE_DIR}/pipelines/step.yaml" 

43--- 

44kind: butler 

45metadata: 

46 name: repo 

47spec: 

48 repo: embargo 

49 collections: 

50 campaign_input: 

51 - LSSTCam/defaults 

52 - LSSTCam/runs/prompt-${{ yesterday | as_day_obs }} 

53 campaign_public_output: "u/test/${{yesterday|as_day_obs}}" 

54 include_files: 

55 - "${AP_PIPE_DIR}/a.yaml" 

56 predicates: 

57 - instrument='LSSTCam' 

58 - day_obs=${{ yesterday | as_day_obs }} 

59--- 

60kind: bps 

61metadata: 

62 name: bps 

63spec: 

64 pipeline_yaml: "${AP_PIPE_DIR}/pipelines/campaign.yaml" 

65 extra_qgraph_options: 

66 - "--skip-existing-in LSSTCam/runs/prompt-${{ yesterday | as_day_obs }}" 

67 - "--dataset-query-constraint off" 

68 include_files: 

69 - "${AP_PIPE_DIR}/a.yaml" 

70 - "${AP_PIPE_DIR}/b.yaml" 

71 literals: 

72 numberOfRetries: 3 

73--- 

74kind: lsst 

75metadata: 

76 name: lsst 

77spec: 

78 campaign: Test 

79 environment: 

80 SOME_VAR: value 

81--- 

82kind: wms 

83metadata: 

84 name: wms 

85spec: 

86 auto_provision: true 

87 provisioned_extra_arguments: 

88 - "--exclusive" 

89--- 

90kind: site 

91metadata: 

92 name: site 

93spec: 

94 provisioned_account_group: rubin:production 

95--- 

96kind: schedule 

97spec: 

98 expressions: 

99 yesterday: "datetime.now(timezone.utc) - timedelta(days=1)" 

100""" 

101 

102 

103class CmCampaignToBpsTestCase(lsst.utils.tests.TestCase): 

104 

105 def setUp(self): 

106 self.documents = list(yaml.safe_load_all(CAMPAIGN)) 

107 

108 def testPayload(self): 

109 config = makeBpsConfig(self.documents) 

110 self.assertEqual(config["payload"], { 

111 "payloadName": "test_{dayObs}", 

112 "butlerConfig": "embargo", 

113 "inCollection": "LSSTCam/defaults,LSSTCam/runs/prompt-{dayObs}", 

114 "output": "u/test/{dayObs}", 

115 "dataQuery": "instrument='LSSTCam' AND day_obs={dayObs}", 

116 }) 

117 self.assertEqual(config["extraQgraphOptions"], 

118 "--skip-existing-in LSSTCam/runs/prompt-{dayObs} --dataset-query-constraint off") 

119 

120 def testMerging(self): 

121 config = makeBpsConfig(self.documents) 

122 # The step's pipeline overrides the campaign BPS manifest. 

123 self.assertEqual(config["pipelineYaml"], "${AP_PIPE_DIR}/pipelines/step.yaml") 

124 self.assertEqual(config["includeConfigs"], ["${AP_PIPE_DIR}/a.yaml", "${AP_PIPE_DIR}/b.yaml"]) 

125 self.assertEqual(config["environment"], {"SOME_VAR": "value"}) 

126 self.assertEqual(config["numberOfRetries"], 3) 

127 self.assertEqual(config["campaign"], "Test") 

128 self.assertNotIn("project", config) 

129 

130 def testProvisioning(self): 

131 config = makeBpsConfig(self.documents) 

132 self.assertTrue(config["provisionResources"]) 

133 self.assertEqual(config["provisioning"]["provisioningAccountingUser"], "rubin:production") 

134 self.assertEqual(config["provisioning"]["provisioningQueue"], "milano") 

135 self.assertEqual(config["provisioning"]["provisioningExtraOptions"], "--cpu 16 --exclusive") 

136 self.assertEqual(config["releaseExpr"], "HoldReasonCode >= 6") 

137 

138 def testDefaultManifest(self): 

139 extra = {"kind": "lsst", "metadata": {"name": "other"}, "spec": {"campaign": "Other"}} 

140 with self.assertRaises(ValueError): 

141 makeBpsConfig(self.documents + [extra]) 

142 extra["metadata"]["default"] = True 

143 self.assertEqual(makeBpsConfig(self.documents + [extra])["campaign"], "Other") 

144 

145 def _addSecondStep(self): 

146 second = { 

147 "kind": "node", 

148 "metadata": {"kind": "step", "name": "SECOND"}, 

149 "spec": {"bps": { 

150 "pipeline_yaml": "${AP_PIPE_DIR}/pipelines/second.yaml", 

151 "extra_qgraph_options": ["--skip-existing-in {{ butler.collections.step_input }}"], 

152 }}, 

153 } 

154 self.documents.append(second) 

155 

156 def testStepSelection(self): 

157 self._addSecondStep() 

158 # With several steps, one must be named. 

159 with self.assertRaises(ValueError): 

160 makeBpsConfig(self.documents) 

161 with self.assertRaises(ValueError): 

162 makeBpsConfig(self.documents, "MISSING") 

163 self.assertEqual(makeBpsConfig(self.documents, "STEP")["pipelineYaml"], 

164 "${AP_PIPE_DIR}/pipelines/step.yaml") 

165 second = makeBpsConfig(self.documents, "SECOND") 

166 self.assertEqual(second["pipelineYaml"], "${AP_PIPE_DIR}/pipelines/second.yaml") 

167 # The step's options replace the campaign list, and the step input is 

168 # the shared output collection. 

169 self.assertEqual(second["extraQgraphOptions"], "--skip-existing-in u/test/{dayObs}") 

170 

171 def testUnsupportedManifestVariable(self): 

172 bps = next(doc for doc in self.documents if doc["kind"] == "bps") 

173 bps["spec"]["extra_qgraph_options"].append("--skip-existing-in {{ butler.collections.run }}") 

174 with self.assertRaises(ValueError): 

175 makeBpsConfig(self.documents) 

176 

177 def testUnsupportedTemplate(self): 

178 self.documents[0]["metadata"]["name"] = "test_${{ day_obs | random_n(8) }}" 

179 with self.assertRaises(ValueError): 

180 makeBpsConfig(self.documents) 

181 

182 def testRoundTrip(self): 

183 with tempfile.TemporaryDirectory() as tempDir: 

184 campaignPath = os.path.join(tempDir, "campaign.yaml") 

185 bpsPath = os.path.join(tempDir, "bps", "LSSTCam", "bps.yaml") 

186 with open(campaignPath, "w") as stream: 

187 stream.write(CAMPAIGN) 

188 config = makeBpsConfig(readCampaign(campaignPath)) 

189 writeBpsConfig(config, bpsPath, campaignPath, "STEP") 

190 with open(bpsPath) as stream: 

191 text = stream.read() 

192 self.assertIn("of the STEP step", text) 

193 self.assertEqual(yaml.safe_load(text), config) 

194 

195 

196class MemoryTester(lsst.utils.tests.MemoryTestCase): 

197 pass 

198 

199 

200def setup_module(module): 

201 lsst.utils.tests.init() 

202 

203 

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

205 lsst.utils.tests.init() 

206 unittest.main()