OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 Hunter Blanks http://artifex.org/~hblanks/ |
| 2 # All rights reserved. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 """ |
| 24 Initial, and very limited, unit tests for CloudWatchConnection. |
| 25 """ |
| 26 |
| 27 import datetime |
| 28 import time |
| 29 import unittest |
| 30 |
| 31 from boto.ec2.cloudwatch import CloudWatchConnection |
| 32 from boto.ec2.cloudwatch.metric import Metric |
| 33 |
| 34 # HTTP response body for CloudWatchConnection.describe_alarms |
| 35 DESCRIBE_ALARMS_BODY = """<DescribeAlarmsResponse xmlns="http://monitoring.amazo
naws.com/doc/2010-08-01/"> |
| 36 <DescribeAlarmsResult> |
| 37 <NextToken>mynexttoken</NextToken> |
| 38 <MetricAlarms> |
| 39 <member> |
| 40 <StateUpdatedTimestamp>2011-11-18T23:43:59.111Z</StateUpdatedTimestamp> |
| 41 <InsufficientDataActions/> |
| 42 <StateReasonData>{"version":"1.0","queryDate&qu
ot;:"2011-11-18T23:43:59.089+0000","startDate":"2011-11
-18T23:30:00.000+0000","statistic":"Maximum","peri
od":60,"recentDatapoints":[1.0,null,null,null,null,null,null,null
,null,null,1.0],"threshold":1.0}</StateReasonData> |
| 43 <AlarmArn>arn:aws:cloudwatch:us-east-1:1234:alarm:FancyAlarm</AlarmArn> |
| 44 <AlarmConfigurationUpdatedTimestamp>2011-11-18T23:43:58.489Z</AlarmConfi
gurationUpdatedTimestamp> |
| 45 <AlarmName>FancyAlarm</AlarmName> |
| 46 <StateValue>OK</StateValue> |
| 47 <Period>60</Period> |
| 48 <OKActions/> |
| 49 <ActionsEnabled>true</ActionsEnabled> |
| 50 <Namespace>AcmeCo/Cronjobs</Namespace> |
| 51 <EvaluationPeriods>15</EvaluationPeriods> |
| 52 <Threshold>1.0</Threshold> |
| 53 <Statistic>Maximum</Statistic> |
| 54 <AlarmActions> |
| 55 <member>arn:aws:sns:us-east-1:1234:Alerts</member> |
| 56 </AlarmActions> |
| 57 <StateReason>Threshold Crossed: 2 datapoints were not less than the thre
shold (1.0). The most recent datapoints: [1.0, 1.0].</StateReason> |
| 58 <Dimensions> |
| 59 <member> |
| 60 <Name>Job</Name> |
| 61 <Value>ANiceCronJob</Value> |
| 62 </member> |
| 63 </Dimensions> |
| 64 <ComparisonOperator>LessThanThreshold</ComparisonOperator> |
| 65 <MetricName>Success</MetricName> |
| 66 </member> |
| 67 <member> |
| 68 <StateUpdatedTimestamp>2011-11-19T08:09:20.655Z</StateUpdatedTimestamp> |
| 69 <InsufficientDataActions/> |
| 70 <StateReasonData>{"version":"1.0","queryDate&qu
ot;:"2011-11-19T08:09:20.633+0000","startDate":"2011-11
-19T08:07:00.000+0000","statistic":"Maximum","peri
od":60,"recentDatapoints":[1.0],"threshold":1.0}</State
ReasonData> |
| 71 <AlarmArn>arn:aws:cloudwatch:us-east-1:1234:alarm:SuprtFancyAlarm</Alarm
Arn> |
| 72 <AlarmConfigurationUpdatedTimestamp>2011-11-19T16:20:19.687Z</AlarmConfi
gurationUpdatedTimestamp> |
| 73 <AlarmName>SuperFancyAlarm</AlarmName> |
| 74 <StateValue>OK</StateValue> |
| 75 <Period>60</Period> |
| 76 <OKActions/> |
| 77 <ActionsEnabled>true</ActionsEnabled> |
| 78 <Namespace>AcmeCo/CronJobs</Namespace> |
| 79 <EvaluationPeriods>60</EvaluationPeriods> |
| 80 <Threshold>1.0</Threshold> |
| 81 <Statistic>Maximum</Statistic> |
| 82 <AlarmActions> |
| 83 <member>arn:aws:sns:us-east-1:1234:alerts</member> |
| 84 </AlarmActions> |
| 85 <StateReason>Threshold Crossed: 1 datapoint (1.0) was not less than the
threshold (1.0).</StateReason> |
| 86 <Dimensions> |
| 87 <member> |
| 88 <Name>Job</Name> |
| 89 <Value>ABadCronJob</Value> |
| 90 </member> |
| 91 </Dimensions> |
| 92 <ComparisonOperator>GreaterThanThreshold</ComparisonOperator> |
| 93 <MetricName>Success</MetricName> |
| 94 </member> |
| 95 </MetricAlarms> |
| 96 </DescribeAlarmsResult> |
| 97 <ResponseMetadata> |
| 98 <RequestId>f621311-1463-11e1-95c3-312389123</RequestId> |
| 99 </ResponseMetadata> |
| 100 </DescribeAlarmsResponse>""" |
| 101 |
| 102 |
| 103 class CloudWatchConnectionTest(unittest.TestCase): |
| 104 ec2 = True |
| 105 |
| 106 def test_build_list_params(self): |
| 107 c = CloudWatchConnection() |
| 108 params = {} |
| 109 c.build_list_params( |
| 110 params, ['thing1', 'thing2', 'thing3'], 'ThingName%d') |
| 111 expected_params = { |
| 112 'ThingName1': 'thing1', |
| 113 'ThingName2': 'thing2', |
| 114 'ThingName3': 'thing3' |
| 115 } |
| 116 self.assertEqual(params, expected_params) |
| 117 |
| 118 def test_build_put_params_one(self): |
| 119 c = CloudWatchConnection() |
| 120 params = {} |
| 121 c.build_put_params(params, name="N", value=1, dimensions={"D": "V"}) |
| 122 expected_params = { |
| 123 'MetricData.member.1.MetricName': 'N', |
| 124 'MetricData.member.1.Value': 1, |
| 125 'MetricData.member.1.Dimensions.member.1.Name': 'D', |
| 126 'MetricData.member.1.Dimensions.member.1.Value': 'V', |
| 127 } |
| 128 self.assertEqual(params, expected_params) |
| 129 |
| 130 def test_build_put_params_multiple_metrics(self): |
| 131 c = CloudWatchConnection() |
| 132 params = {} |
| 133 c.build_put_params(params, name=["N", "M"], value=[1, 2], dimensions={"D
": "V"}) |
| 134 expected_params = { |
| 135 'MetricData.member.1.MetricName': 'N', |
| 136 'MetricData.member.1.Value': 1, |
| 137 'MetricData.member.1.Dimensions.member.1.Name': 'D', |
| 138 'MetricData.member.1.Dimensions.member.1.Value': 'V', |
| 139 'MetricData.member.2.MetricName': 'M', |
| 140 'MetricData.member.2.Value': 2, |
| 141 'MetricData.member.2.Dimensions.member.1.Name': 'D', |
| 142 'MetricData.member.2.Dimensions.member.1.Value': 'V', |
| 143 } |
| 144 self.assertEqual(params, expected_params) |
| 145 |
| 146 def test_build_put_params_multiple_dimensions(self): |
| 147 c = CloudWatchConnection() |
| 148 params = {} |
| 149 c.build_put_params(params, name="N", value=[1, 2], dimensions=[{"D": "V"
}, {"D": "W"}]) |
| 150 expected_params = { |
| 151 'MetricData.member.1.MetricName': 'N', |
| 152 'MetricData.member.1.Value': 1, |
| 153 'MetricData.member.1.Dimensions.member.1.Name': 'D', |
| 154 'MetricData.member.1.Dimensions.member.1.Value': 'V', |
| 155 'MetricData.member.2.MetricName': 'N', |
| 156 'MetricData.member.2.Value': 2, |
| 157 'MetricData.member.2.Dimensions.member.1.Name': 'D', |
| 158 'MetricData.member.2.Dimensions.member.1.Value': 'W', |
| 159 } |
| 160 self.assertEqual(params, expected_params) |
| 161 |
| 162 def test_build_put_params_multiple_parameter_dimension(self): |
| 163 from collections import OrderedDict |
| 164 self.maxDiff = None |
| 165 c = CloudWatchConnection() |
| 166 params = {} |
| 167 dimensions = [OrderedDict((("D1", "V"), ("D2", "W")))] |
| 168 c.build_put_params(params, |
| 169 name="N", |
| 170 value=[1], |
| 171 dimensions=dimensions) |
| 172 expected_params = { |
| 173 'MetricData.member.1.MetricName': 'N', |
| 174 'MetricData.member.1.Value': 1, |
| 175 'MetricData.member.1.Dimensions.member.1.Name': 'D1', |
| 176 'MetricData.member.1.Dimensions.member.1.Value': 'V', |
| 177 'MetricData.member.1.Dimensions.member.2.Name': 'D2', |
| 178 'MetricData.member.1.Dimensions.member.2.Value': 'W', |
| 179 } |
| 180 self.assertEqual(params, expected_params) |
| 181 |
| 182 def test_build_get_params_multiple_parameter_dimension1(self): |
| 183 from collections import OrderedDict |
| 184 self.maxDiff = None |
| 185 c = CloudWatchConnection() |
| 186 params = {} |
| 187 dimensions = OrderedDict((("D1", "V"), ("D2", "W"))) |
| 188 c.build_dimension_param(dimensions, params) |
| 189 expected_params = { |
| 190 'Dimensions.member.1.Name': 'D1', |
| 191 'Dimensions.member.1.Value': 'V', |
| 192 'Dimensions.member.2.Name': 'D2', |
| 193 'Dimensions.member.2.Value': 'W', |
| 194 } |
| 195 self.assertEqual(params, expected_params) |
| 196 |
| 197 def test_build_get_params_multiple_parameter_dimension2(self): |
| 198 from collections import OrderedDict |
| 199 self.maxDiff = None |
| 200 c = CloudWatchConnection() |
| 201 params = {} |
| 202 dimensions = OrderedDict((("D1", ["V1", "V2"]), ("D2", "W"), ("D3", None
))) |
| 203 c.build_dimension_param(dimensions, params) |
| 204 expected_params = { |
| 205 'Dimensions.member.1.Name': 'D1', |
| 206 'Dimensions.member.1.Value': 'V1', |
| 207 'Dimensions.member.2.Name': 'D1', |
| 208 'Dimensions.member.2.Value': 'V2', |
| 209 'Dimensions.member.3.Name': 'D2', |
| 210 'Dimensions.member.3.Value': 'W', |
| 211 'Dimensions.member.4.Name': 'D3', |
| 212 } |
| 213 self.assertEqual(params, expected_params) |
| 214 |
| 215 def test_build_put_params_invalid(self): |
| 216 c = CloudWatchConnection() |
| 217 params = {} |
| 218 try: |
| 219 c.build_put_params(params, name=["N", "M"], value=[1, 2, 3]) |
| 220 except: |
| 221 pass |
| 222 else: |
| 223 self.fail("Should not accept lists of different lengths.") |
| 224 |
| 225 def test_get_metric_statistics(self): |
| 226 c = CloudWatchConnection() |
| 227 m = c.list_metrics()[0] |
| 228 end = datetime.datetime.now() |
| 229 start = end - datetime.timedelta(hours=24*14) |
| 230 c.get_metric_statistics( |
| 231 3600*24, start, end, m.name, m.namespace, ['Average', 'Sum']) |
| 232 |
| 233 def test_put_metric_data(self): |
| 234 c = CloudWatchConnection() |
| 235 now = datetime.datetime.now() |
| 236 name, namespace = 'unit-test-metric', 'boto-unit-test' |
| 237 c.put_metric_data(namespace, name, 5, now, 'Bytes') |
| 238 |
| 239 # Uncomment the following lines for a slower but more thorough |
| 240 # test. (Hurrah for eventual consistency...) |
| 241 # |
| 242 # metric = Metric(connection=c) |
| 243 # metric.name = name |
| 244 # metric.namespace = namespace |
| 245 # time.sleep(60) |
| 246 # l = metric.query( |
| 247 # now - datetime.timedelta(seconds=60), |
| 248 # datetime.datetime.now(), |
| 249 # 'Average') |
| 250 # assert l |
| 251 # for row in l: |
| 252 # self.assertEqual(row['Unit'], 'Bytes') |
| 253 # self.assertEqual(row['Average'], 5.0) |
| 254 |
| 255 |
| 256 def test_describe_alarms(self): |
| 257 c = CloudWatchConnection() |
| 258 def make_request(*args, **kwargs): |
| 259 class Body(object): |
| 260 def __init__(self): |
| 261 self.status = 200 |
| 262 def read(self): |
| 263 return DESCRIBE_ALARMS_BODY |
| 264 return Body() |
| 265 |
| 266 c.make_request = make_request |
| 267 alarms = c.describe_alarms() |
| 268 self.assertEquals(alarms.next_token, 'mynexttoken') |
| 269 self.assertEquals(alarms[0].name, 'FancyAlarm') |
| 270 self.assertEquals(alarms[0].comparison, '<') |
| 271 self.assertEquals(alarms[0].dimensions, {u'Job': [u'ANiceCronJob']}) |
| 272 self.assertEquals(alarms[1].name, 'SuperFancyAlarm') |
| 273 self.assertEquals(alarms[1].comparison, '>') |
| 274 self.assertEquals(alarms[1].dimensions, {u'Job': [u'ABadCronJob']}) |
| 275 |
| 276 if __name__ == '__main__': |
| 277 unittest.main() |
OLD | NEW |