| 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 <MetricAlarms> |
| 38 <member> |
| 39 <StateUpdatedTimestamp>2011-11-18T23:43:59.111Z</StateUpdatedTimestamp> |
| 40 <InsufficientDataActions/> |
| 41 <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> |
| 42 <AlarmArn>arn:aws:cloudwatch:us-east-1:1234:alarm:FancyAlarm</AlarmArn> |
| 43 <AlarmConfigurationUpdatedTimestamp>2011-11-18T23:43:58.489Z</AlarmConfi
gurationUpdatedTimestamp> |
| 44 <AlarmName>FancyAlarm</AlarmName> |
| 45 <StateValue>OK</StateValue> |
| 46 <Period>60</Period> |
| 47 <OKActions/> |
| 48 <ActionsEnabled>true</ActionsEnabled> |
| 49 <Namespace>AcmeCo/Cronjobs</Namespace> |
| 50 <EvaluationPeriods>15</EvaluationPeriods> |
| 51 <Threshold>1.0</Threshold> |
| 52 <Statistic>Maximum</Statistic> |
| 53 <AlarmActions> |
| 54 <member>arn:aws:sns:us-east-1:1234:Alerts</member> |
| 55 </AlarmActions> |
| 56 <StateReason>Threshold Crossed: 2 datapoints were not less than the thre
shold (1.0). The most recent datapoints: [1.0, 1.0].</StateReason> |
| 57 <Dimensions> |
| 58 <member> |
| 59 <Name>Job</Name> |
| 60 <Value>ANiceCronJob</Value> |
| 61 </member> |
| 62 </Dimensions> |
| 63 <ComparisonOperator>LessThanThreshold</ComparisonOperator> |
| 64 <MetricName>Success</MetricName> |
| 65 </member> |
| 66 <member> |
| 67 <StateUpdatedTimestamp>2011-11-19T08:09:20.655Z</StateUpdatedTimestamp> |
| 68 <InsufficientDataActions/> |
| 69 <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> |
| 70 <AlarmArn>arn:aws:cloudwatch:us-east-1:1234:alarm:SuprtFancyAlarm</Alarm
Arn> |
| 71 <AlarmConfigurationUpdatedTimestamp>2011-11-19T16:20:19.687Z</AlarmConfi
gurationUpdatedTimestamp> |
| 72 <AlarmName>SuperFancyAlarm</AlarmName> |
| 73 <StateValue>OK</StateValue> |
| 74 <Period>60</Period> |
| 75 <OKActions/> |
| 76 <ActionsEnabled>true</ActionsEnabled> |
| 77 <Namespace>AcmeCo/CronJobs</Namespace> |
| 78 <EvaluationPeriods>60</EvaluationPeriods> |
| 79 <Threshold>1.0</Threshold> |
| 80 <Statistic>Maximum</Statistic> |
| 81 <AlarmActions> |
| 82 <member>arn:aws:sns:us-east-1:1234:alerts</member> |
| 83 </AlarmActions> |
| 84 <StateReason>Threshold Crossed: 1 datapoint (1.0) was not less than the
threshold (1.0).</StateReason> |
| 85 <Dimensions> |
| 86 <member> |
| 87 <Name>Job</Name> |
| 88 <Value>ABadCronJob</Value> |
| 89 </member> |
| 90 </Dimensions> |
| 91 <ComparisonOperator>GreaterThanThreshold</ComparisonOperator> |
| 92 <MetricName>Success</MetricName> |
| 93 </member> |
| 94 </MetricAlarms> |
| 95 </DescribeAlarmsResult> |
| 96 <ResponseMetadata> |
| 97 <RequestId>f621311-1463-11e1-95c3-312389123</RequestId> |
| 98 </ResponseMetadata> |
| 99 </DescribeAlarmsResponse>""" |
| 100 |
| 101 |
| 102 class CloudWatchConnectionTest(unittest.TestCase): |
| 103 |
| 104 def test_build_list_params(self): |
| 105 c = CloudWatchConnection() |
| 106 params = {} |
| 107 c.build_list_params( |
| 108 params, ['thing1', 'thing2', 'thing3'], 'ThingName%d') |
| 109 expected_params = { |
| 110 'ThingName1': 'thing1', |
| 111 'ThingName2': 'thing2', |
| 112 'ThingName3': 'thing3' |
| 113 } |
| 114 self.assertEqual(params, expected_params) |
| 115 |
| 116 def test_build_put_params_one(self): |
| 117 c = CloudWatchConnection() |
| 118 params = {} |
| 119 c.build_put_params(params, name="N", value=1, dimensions={"D": "V"}) |
| 120 expected_params = { |
| 121 'MetricData.member.1.MetricName': 'N', |
| 122 'MetricData.member.1.Value': 1, |
| 123 'MetricData.member.1.Dimensions.member.1.Name.1': 'D', |
| 124 'MetricData.member.1.Dimensions.member.1.Value.1': 'V', |
| 125 } |
| 126 self.assertEqual(params, expected_params) |
| 127 |
| 128 def test_build_put_params_multiple_metrics(self): |
| 129 c = CloudWatchConnection() |
| 130 params = {} |
| 131 c.build_put_params(params, name=["N", "M"], value=[1, 2], dimensions={"D
": "V"}) |
| 132 expected_params = { |
| 133 'MetricData.member.1.MetricName': 'N', |
| 134 'MetricData.member.1.Value': 1, |
| 135 'MetricData.member.1.Dimensions.member.1.Name.1': 'D', |
| 136 'MetricData.member.1.Dimensions.member.1.Value.1': 'V', |
| 137 'MetricData.member.2.MetricName': 'M', |
| 138 'MetricData.member.2.Value': 2, |
| 139 'MetricData.member.2.Dimensions.member.1.Name.1': 'D', |
| 140 'MetricData.member.2.Dimensions.member.1.Value.1': 'V', |
| 141 } |
| 142 self.assertEqual(params, expected_params) |
| 143 |
| 144 def test_build_put_params_multiple_dimensions(self): |
| 145 c = CloudWatchConnection() |
| 146 params = {} |
| 147 c.build_put_params(params, name="N", value=[1, 2], dimensions=[{"D": "V"
}, {"D": "W"}]) |
| 148 expected_params = { |
| 149 'MetricData.member.1.MetricName': 'N', |
| 150 'MetricData.member.1.Value': 1, |
| 151 'MetricData.member.1.Dimensions.member.1.Name.1': 'D', |
| 152 'MetricData.member.1.Dimensions.member.1.Value.1': 'V', |
| 153 'MetricData.member.2.MetricName': 'N', |
| 154 'MetricData.member.2.Value': 2, |
| 155 'MetricData.member.2.Dimensions.member.1.Name.1': 'D', |
| 156 'MetricData.member.2.Dimensions.member.1.Value.1': 'W', |
| 157 } |
| 158 self.assertEqual(params, expected_params) |
| 159 |
| 160 def test_build_put_params_multiple_parameter_dimension(self): |
| 161 from collections import OrderedDict |
| 162 self.maxDiff = None |
| 163 c = CloudWatchConnection() |
| 164 params = {} |
| 165 dimensions = [OrderedDict((("D1", "V"), ("D2", "W")))] |
| 166 c.build_put_params(params, |
| 167 name="N", |
| 168 value=[1], |
| 169 dimensions=dimensions) |
| 170 expected_params = { |
| 171 'MetricData.member.1.MetricName': 'N', |
| 172 'MetricData.member.1.Value': 1, |
| 173 'MetricData.member.1.Dimensions.member.1.Name.1': 'D1', |
| 174 'MetricData.member.1.Dimensions.member.1.Value.1': 'V', |
| 175 'MetricData.member.1.Dimensions.member.2.Name.1': 'D2', |
| 176 'MetricData.member.1.Dimensions.member.2.Value.1': 'W', |
| 177 } |
| 178 self.assertEqual(params, expected_params) |
| 179 |
| 180 def test_build_get_params_multiple_parameter_dimension(self): |
| 181 from collections import OrderedDict |
| 182 self.maxDiff = None |
| 183 c = CloudWatchConnection() |
| 184 params = {} |
| 185 dimensions = OrderedDict((("D1", "V"), ("D2", "W"))) |
| 186 c.build_dimension_param(dimensions, params) |
| 187 expected_params = { |
| 188 'Dimensions.member.1.Name.1': 'D1', |
| 189 'Dimensions.member.1.Value.1': 'V', |
| 190 'Dimensions.member.2.Name.1': 'D2', |
| 191 'Dimensions.member.2.Value.1': 'W', |
| 192 } |
| 193 self.assertEqual(params, expected_params) |
| 194 |
| 195 def test_build_put_params_invalid(self): |
| 196 c = CloudWatchConnection() |
| 197 params = {} |
| 198 try: |
| 199 c.build_put_params(params, name=["N", "M"], value=[1, 2, 3]) |
| 200 except: |
| 201 pass |
| 202 else: |
| 203 self.fail("Should not accept lists of different lengths.") |
| 204 |
| 205 def test_get_metric_statistics(self): |
| 206 c = CloudWatchConnection() |
| 207 m = c.list_metrics()[0] |
| 208 end = datetime.datetime.now() |
| 209 start = end - datetime.timedelta(hours=24*14) |
| 210 c.get_metric_statistics( |
| 211 3600*24, start, end, m.name, m.namespace, ['Average', 'Sum']) |
| 212 |
| 213 def test_put_metric_data(self): |
| 214 c = CloudWatchConnection() |
| 215 now = datetime.datetime.now() |
| 216 name, namespace = 'unit-test-metric', 'boto-unit-test' |
| 217 c.put_metric_data(namespace, name, 5, now, 'Bytes') |
| 218 |
| 219 # Uncomment the following lines for a slower but more thorough |
| 220 # test. (Hurrah for eventual consistency...) |
| 221 # |
| 222 # metric = Metric(connection=c) |
| 223 # metric.name = name |
| 224 # metric.namespace = namespace |
| 225 # time.sleep(60) |
| 226 # l = metric.query( |
| 227 # now - datetime.timedelta(seconds=60), |
| 228 # datetime.datetime.now(), |
| 229 # 'Average') |
| 230 # assert l |
| 231 # for row in l: |
| 232 # self.assertEqual(row['Unit'], 'Bytes') |
| 233 # self.assertEqual(row['Average'], 5.0) |
| 234 |
| 235 |
| 236 def test_describe_alarms(self): |
| 237 c = CloudWatchConnection() |
| 238 def make_request(*args, **kwargs): |
| 239 class Body(object): |
| 240 def __init__(self): |
| 241 self.status = 200 |
| 242 def read(self): |
| 243 return DESCRIBE_ALARMS_BODY |
| 244 return Body() |
| 245 |
| 246 c.make_request = make_request |
| 247 alarms = c.describe_alarms() |
| 248 self.assertEquals(alarms[0].name, 'FancyAlarm') |
| 249 self.assertEquals(alarms[0].comparison, '<') |
| 250 self.assertEquals(alarms[0].dimensions, {u'Job': [u'ANiceCronJob']}) |
| 251 self.assertEquals(alarms[1].name, 'SuperFancyAlarm') |
| 252 self.assertEquals(alarms[1].comparison, '>') |
| 253 self.assertEquals(alarms[1].dimensions, {u'Job': [u'ABadCronJob']}) |
| 254 |
| 255 if __name__ == '__main__': |
| 256 unittest.main() |
| OLD | NEW |