|
5 | 5 | from croniter import croniter
|
6 | 6 | from validate import validate_event_webhook,validate_event_headers
|
7 | 7 | from queue import Empty
|
| 8 | +import json |
8 | 9 | import time
|
9 | 10 |
|
10 | 11 | # The create and delete tests should ideally go in setup and teardown YAML files,
|
@@ -145,7 +146,7 @@ def test_create_cron_schedule_triggers(self,hge_ctx):
|
145 | 146 | TestCronTrigger.init_time = datetime.utcnow()
|
146 | 147 | # the cron events will be generated based on the current time, they
|
147 | 148 | # will not be exactly the same though(the server now and now here)
|
148 |
| - assert cron_st_code == 200,resp |
| 149 | + assert cron_st_code == 200,cron_st_resp |
149 | 150 | assert cron_st_resp['message'] == 'success'
|
150 | 151 |
|
151 | 152 | def test_check_generated_cron_scheduled_events(self,hge_ctx):
|
@@ -177,6 +178,73 @@ def test_check_generated_cron_scheduled_events(self,hge_ctx):
|
177 | 178 | actual_schedule_timestamps.append(datetime_ts)
|
178 | 179 | assert actual_schedule_timestamps == expected_schedule_timestamps
|
179 | 180 |
|
| 181 | + def test_update_existing_cron_trigger(self,hge_ctx): |
| 182 | + expected_schedule_timestamps = [] |
| 183 | + iter = croniter(self.cron_schedule,datetime.utcnow()) |
| 184 | + for i in range(100): |
| 185 | + expected_schedule_timestamps.append(iter.next(datetime)) |
| 186 | + q = { |
| 187 | + "type":"create_cron_trigger", |
| 188 | + "args":{ |
| 189 | + "name":self.cron_trigger_name, |
| 190 | + "webhook":"{{SCHEDULED_TRIGGERS_WEBHOOK_DOMAIN}}" + "/foo", |
| 191 | + "schedule":self.cron_schedule, |
| 192 | + "headers":[ |
| 193 | + { |
| 194 | + "name":"header-name", |
| 195 | + "value":"header-value" |
| 196 | + } |
| 197 | + ], |
| 198 | + "payload":{"foo":"baz"}, |
| 199 | + "include_in_metadata":False, |
| 200 | + "replace":True |
| 201 | + } |
| 202 | + } |
| 203 | + st,resp = hge_ctx.v1q(q) |
| 204 | + assert st == 200, resp |
| 205 | + |
| 206 | + sql = ''' |
| 207 | + select header_conf::json |
| 208 | + from hdb_catalog.hdb_cron_triggers where |
| 209 | + name = '{}' ''' |
| 210 | + q = { |
| 211 | + "type":"run_sql", |
| 212 | + "args":{ |
| 213 | + "sql":sql.format(self.cron_trigger_name) |
| 214 | + } |
| 215 | + } |
| 216 | + st,resp = hge_ctx.v1q(q) |
| 217 | + assert st == 200,resp |
| 218 | + print ("resp",resp['result'][1][0]) |
| 219 | + assert json.loads(resp['result'][1][0]) == [{ |
| 220 | + "name":"header-name", |
| 221 | + "value":"header-value" |
| 222 | + }] |
| 223 | + |
| 224 | + # Get timestamps in UTC from the db to compare it with |
| 225 | + # the croniter generated timestamps |
| 226 | + # After updating the cron trigger, the future events should |
| 227 | + # have been created |
| 228 | + sql = ''' |
| 229 | + select timezone('utc',scheduled_time) as scheduled_time |
| 230 | + from hdb_catalog.hdb_cron_events where |
| 231 | + trigger_name = '{}' order by scheduled_time asc;''' |
| 232 | + q = { |
| 233 | + "type":"run_sql", |
| 234 | + "args":{ |
| 235 | + "sql":sql.format(self.cron_trigger_name) |
| 236 | + } |
| 237 | + } |
| 238 | + st,resp = hge_ctx.v1q(q) |
| 239 | + assert st == 200,resp |
| 240 | + ts_resp = resp['result'][1:] |
| 241 | + assert len(ts_resp) == 100 |
| 242 | + actual_schedule_timestamps = [] |
| 243 | + for ts in ts_resp: |
| 244 | + datetime_ts = datetime.strptime(ts[0],"%Y-%m-%d %H:%M:%S") |
| 245 | + actual_schedule_timestamps.append(datetime_ts) |
| 246 | + assert actual_schedule_timestamps == expected_schedule_timestamps |
| 247 | + |
180 | 248 | def test_delete_cron_scheduled_trigger(self,hge_ctx):
|
181 | 249 | q = {
|
182 | 250 | "type":"delete_cron_trigger",
|
|
0 commit comments