forked from schen1628/ec2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestEc2Operator.py
146 lines (118 loc) · 6.37 KB
/
TestEc2Operator.py
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
from unittest import TestCase
import ec2_operator
import pytz
from dateutil import parser
class TestEc2Operator(TestCase):
# croniter requires a tzinfo object that supports localize so simple parsing of the timezone won't work
def get_pytz_utc_datetime(self, time_string):
return parser.parse(time_string).replace(tzinfo=pytz.utc)
def test_bad_cron(self):
try:
ec2_operator.time_to_stop("badcron",
self.get_pytz_utc_datetime("Jan 15 2015 00:00"),
self.get_pytz_utc_datetime("Jan 01 2015 00:00"))
except ValueError:
pass
except Exception as e:
self.fail("Unexpected exception thrown: " + e)
else:
self.fail("Expected exception not thrown")
try:
ec2_operator.time_to_stop("badcron 0 6 * * *",
self.get_pytz_utc_datetime("Jan 15 2015 00:00"),
self.get_pytz_utc_datetime("Jan 01 2015 00:00"))
except KeyError:
pass
except Exception as e:
self.fail("Unexpected exception thrown: " + e)
else:
self.fail("Expected exception not thrown")
# Check stop cron time: (window_start <= cron_time <= now)
def test_time_to_action_stop(self):
old_launch_time = self.get_pytz_utc_datetime("Jan 01 2012 00:00:00")
# Stop at 01:00 and it's 00:00 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:00"), old_launch_time),
"Stop well before window")
# Stop at 01:00 and it's 00:59 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:59"), old_launch_time),
"Stop one minute before window")
# Stop at 01:00 and it's 01:00 UTC
self.assertTrue(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:00"), old_launch_time),
"Stop at exact window start")
# Stop at 01:00 and it's 01:00 MST
self.assertTrue(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:00").replace(
tzinfo=pytz.timezone('MST')), old_launch_time), "Stop at exact window start MST")
# Stop at 01:00 and it's 01:01 UTC
self.assertTrue(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:01"), old_launch_time),
"Stop one minute inside window")
# Stop at 01:00 and it's 01:15 UTC
self.assertTrue(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:15"), old_launch_time),
"Stop inside window")
# Stop at 01:00 and it's 01:15 UTC, but the instance was restarted at 00:59 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:15"),
self.get_pytz_utc_datetime("Jan 15 2015 00:59")),
"Stop inside window with recent restart #1")
# Stop at 01:00 and it's 01:15 MST, but the instance was restarted at 00:59 MST
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:15").replace(
tzinfo=pytz.timezone('MST')),
self.get_pytz_utc_datetime("Jan 15 2015 00:59").replace(
tzinfo=pytz.timezone('MST'))), "Stop inside window with recent restart #2")
# Stop at 01:00 and it's 01:15 UTC, but the instance was restarted at 01:05 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:15"),
self.get_pytz_utc_datetime("Jan 15 2015 01:05")),
"Stop inside window with recent restart #3")
# Stop at 01:00 and it's 01:30 UTC
self.assertTrue(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:30"), old_launch_time),
"Stop at exact window end")
# Stop at 01:00 and it's 02:01 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 02:01"), old_launch_time),
"Stop one minute past window")
# Stop at 01:00 and it's 05:00 UTC
self.assertFalse(
ec2_operator.time_to_stop("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 05:00"), old_launch_time),
"Stop well past window")
# Check start cron time: (now <= cron_time <= window_end)
def test_time_to_action_start(self):
# Start at 01:00 and it's 00:00 UTC
self.assertFalse(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 15:00")),
"Start well before window")
# Start at 01:00 and it's 00:29 UTC
self.assertFalse(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:29")),
"Start one minute before window")
# Start at 01:00 and it's 00:50 UTC
self.assertTrue(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:50")),
"Start at exact window start")
# Start at 01:00 and it's 00:51 UTC
self.assertTrue(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:51")),
"Start one minute inside window")
# Start at 01:00 and it's 00:55 UTC
self.assertTrue(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 00:55")),
"Start inside window")
# Start at 01:00 and it's 01:00 UTC
self.assertTrue(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:00")),
"Start at exact window end")
# Start at 01:00 and it's 01:01 UTC
self.assertFalse(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 01:01")),
"Start one minute past window")
# Start at 01:00 and it's 05:00 UTC
self.assertFalse(
ec2_operator.time_to_start("0 1 * * *", self.get_pytz_utc_datetime("Jan 15 2015 05:00")),
"Start well past window")