OLD | NEW |
(Empty) | |
| 1 import mock |
| 2 from tests.unit import unittest |
| 3 |
| 4 from boto.ec2.snapshot import Snapshot |
| 5 from boto.ec2.tag import Tag, TagSet |
| 6 from boto.ec2.volume import Volume, AttachmentSet, VolumeAttribute |
| 7 |
| 8 |
| 9 class VolumeTests(unittest.TestCase): |
| 10 def setUp(self): |
| 11 self.attach_data = AttachmentSet() |
| 12 self.attach_data.id = 1 |
| 13 self.attach_data.instance_id = 2 |
| 14 self.attach_data.status = "some status" |
| 15 self.attach_data.attach_time = 5 |
| 16 self.attach_data.device = "/dev/null" |
| 17 |
| 18 self.volume_one = Volume() |
| 19 self.volume_one.id = 1 |
| 20 self.volume_one.create_time = 5 |
| 21 self.volume_one.status = "one_status" |
| 22 self.volume_one.size = "one_size" |
| 23 self.volume_one.snapshot_id = 1 |
| 24 self.volume_one.attach_data = self.attach_data |
| 25 self.volume_one.zone = "one_zone" |
| 26 |
| 27 self.volume_two = Volume() |
| 28 self.volume_two.connection = mock.Mock() |
| 29 self.volume_two.id = 1 |
| 30 self.volume_two.create_time = 6 |
| 31 self.volume_two.status = "two_status" |
| 32 self.volume_two.size = "two_size" |
| 33 self.volume_two.snapshot_id = 2 |
| 34 self.volume_two.attach_data = None |
| 35 self.volume_two.zone = "two_zone" |
| 36 |
| 37 @mock.patch("boto.ec2.volume.TaggedEC2Object.startElement") |
| 38 def test_startElement_calls_TaggedEC2Object_startElement_with_correct_args(s
elf, startElement): |
| 39 volume = Volume() |
| 40 volume.startElement("some name", "some attrs", None) |
| 41 startElement.assert_called_with(volume, "some name", "some attrs", None) |
| 42 |
| 43 @mock.patch("boto.ec2.volume.TaggedEC2Object.startElement") |
| 44 def test_startElement_retval_not_None_returns_correct_thing(self, startEleme
nt): |
| 45 tag_set = mock.Mock(TagSet) |
| 46 startElement.return_value = tag_set |
| 47 volume = Volume() |
| 48 retval = volume.startElement(None, None, None) |
| 49 self.assertEqual(retval, tag_set) |
| 50 |
| 51 @mock.patch("boto.ec2.volume.TaggedEC2Object.startElement") |
| 52 @mock.patch("boto.resultset.ResultSet") |
| 53 def test_startElement_with_name_tagSet_calls_ResultSet(self, ResultSet, star
tElement): |
| 54 startElement.return_value = None |
| 55 result_set = mock.Mock(ResultSet([("item", Tag)])) |
| 56 volume = Volume() |
| 57 volume.tags = result_set |
| 58 retval = volume.startElement("tagSet", None, None) |
| 59 self.assertEqual(retval, volume.tags) |
| 60 |
| 61 @mock.patch("boto.ec2.volume.TaggedEC2Object.startElement") |
| 62 def test_startElement_with_name_attachmentSet_returns_AttachmentSet(self, st
artElement): |
| 63 startElement.return_value = None |
| 64 attach_data = AttachmentSet() |
| 65 volume = Volume() |
| 66 volume.attach_data = attach_data |
| 67 retval = volume.startElement("attachmentSet", None, None) |
| 68 self.assertEqual(retval, volume.attach_data) |
| 69 |
| 70 @mock.patch("boto.ec2.volume.TaggedEC2Object.startElement") |
| 71 def test_startElement_else_returns_None(self, startElement): |
| 72 startElement.return_value = None |
| 73 volume = Volume() |
| 74 retval = volume.startElement("not tagSet or attachmentSet", None, None) |
| 75 self.assertEqual(retval, None) |
| 76 |
| 77 def check_that_attribute_has_been_set(self, name, value, attribute): |
| 78 volume = Volume() |
| 79 volume.endElement(name, value, None) |
| 80 self.assertEqual(getattr(volume, attribute), value) |
| 81 |
| 82 def test_endElement_sets_correct_attributes_with_values(self): |
| 83 for arguments in [("volumeId", "some value", "id"), |
| 84 ("createTime", "some time", "create_time"), |
| 85 ("status", "some status", "status"), |
| 86 ("size", 5, "size"), |
| 87 ("snapshotId", 1, "snapshot_id"), |
| 88 ("availabilityZone", "some zone", "zone"), |
| 89 ("someName", "some value", "someName")]: |
| 90 self.check_that_attribute_has_been_set(arguments[0], arguments[1], a
rguments[2]) |
| 91 |
| 92 def test_endElement_with_name_status_and_empty_string_value_doesnt_set_statu
s(self): |
| 93 volume = Volume() |
| 94 volume.endElement("status", "", None) |
| 95 self.assertNotEqual(volume.status, "") |
| 96 |
| 97 def test_update_with_result_set_greater_than_0_updates_dict(self): |
| 98 self.volume_two.connection.get_all_volumes.return_value = [self.volume_o
ne] |
| 99 self.volume_two.update() |
| 100 |
| 101 assert all([self.volume_two.create_time == 5, |
| 102 self.volume_two.status == "one_status", |
| 103 self.volume_two.size == "one_size", |
| 104 self.volume_two.snapshot_id == 1, |
| 105 self.volume_two.attach_data == self.attach_data, |
| 106 self.volume_two.zone == "one_zone"]) |
| 107 |
| 108 def test_update_with_validate_true_raises_value_error(self): |
| 109 self.volume_one.connection = mock.Mock() |
| 110 self.volume_one.connection.get_all_volumes.return_value = [] |
| 111 with self.assertRaisesRegexp(ValueError, "^1 is not a valid Volume ID$")
: |
| 112 self.volume_one.update(True) |
| 113 |
| 114 def test_update_returns_status(self): |
| 115 self.volume_one.connection = mock.Mock() |
| 116 self.volume_one.connection.get_all_volumes.return_value = [self.volume_t
wo] |
| 117 retval = self.volume_one.update() |
| 118 self.assertEqual(retval, "two_status") |
| 119 |
| 120 def test_delete_calls_delete_volume(self): |
| 121 self.volume_one.connection = mock.Mock() |
| 122 self.volume_one.delete() |
| 123 self.volume_one.connection.delete_volume.assert_called_with(1) |
| 124 |
| 125 def test_attach_calls_attach_volume(self): |
| 126 self.volume_one.connection = mock.Mock() |
| 127 self.volume_one.attach("instance_id", "/dev/null") |
| 128 self.volume_one.connection.attach_volume.assert_called_with(1, "instance
_id", "/dev/null") |
| 129 |
| 130 def test_detach_calls_detach_volume(self): |
| 131 self.volume_one.connection = mock.Mock() |
| 132 self.volume_one.detach() |
| 133 self.volume_one.connection.detach_volume.assert_called_with( |
| 134 1, 2, "/dev/null", False) |
| 135 |
| 136 def test_detach_with_no_attach_data(self): |
| 137 self.volume_two.connection = mock.Mock() |
| 138 self.volume_two.detach() |
| 139 self.volume_two.connection.detach_volume.assert_called_with( |
| 140 1, None, None, False) |
| 141 |
| 142 def test_detach_with_force_calls_detach_volume_with_force(self): |
| 143 self.volume_one.connection = mock.Mock() |
| 144 self.volume_one.detach(True) |
| 145 self.volume_one.connection.detach_volume.assert_called_with( |
| 146 1, 2, "/dev/null", True) |
| 147 |
| 148 |
| 149 def test_create_snapshot_calls_connection_create_snapshot(self): |
| 150 self.volume_one.connection = mock.Mock() |
| 151 self.volume_one.create_snapshot() |
| 152 self.volume_one.connection.create_snapshot.assert_called_with( |
| 153 1, None) |
| 154 |
| 155 def test_create_snapshot_with_description(self): |
| 156 self.volume_one.connection = mock.Mock() |
| 157 self.volume_one.create_snapshot("some description") |
| 158 self.volume_one.connection.create_snapshot.assert_called_with( |
| 159 1, "some description") |
| 160 |
| 161 def test_volume_state_returns_status(self): |
| 162 retval = self.volume_one.volume_state() |
| 163 self.assertEqual(retval, "one_status") |
| 164 |
| 165 def test_attachment_state_returns_state(self): |
| 166 retval = self.volume_one.attachment_state() |
| 167 self.assertEqual(retval, "some status") |
| 168 |
| 169 def test_attachment_state_no_attach_data_returns_None(self): |
| 170 retval = self.volume_two.attachment_state() |
| 171 self.assertEqual(retval, None) |
| 172 |
| 173 def test_snapshots_returns_snapshots(self): |
| 174 snapshot_one = Snapshot() |
| 175 snapshot_one.volume_id = 1 |
| 176 snapshot_two = Snapshot() |
| 177 snapshot_two.volume_id = 2 |
| 178 |
| 179 self.volume_one.connection = mock.Mock() |
| 180 self.volume_one.connection.get_all_snapshots.return_value = [snapshot_on
e, snapshot_two] |
| 181 retval = self.volume_one.snapshots() |
| 182 self.assertEqual(retval, [snapshot_one]) |
| 183 |
| 184 def test_snapshots__with_owner_and_restorable_by(self): |
| 185 self.volume_one.connection = mock.Mock() |
| 186 self.volume_one.connection.get_all_snapshots.return_value = [] |
| 187 self.volume_one.snapshots("owner", "restorable_by") |
| 188 self.volume_one.connection.get_all_snapshots.assert_called_with( |
| 189 owner="owner", restorable_by="restorable_by") |
| 190 |
| 191 class AttachmentSetTests(unittest.TestCase): |
| 192 def check_that_attribute_has_been_set(self, name, value, attribute): |
| 193 attachment_set = AttachmentSet() |
| 194 attachment_set.endElement(name, value, None) |
| 195 self.assertEqual(getattr(attachment_set, attribute), value) |
| 196 |
| 197 def test_endElement_with_name_volumeId_sets_id(self): |
| 198 return self.check_that_attribute_has_been_set("volumeId", "some value",
"id") |
| 199 |
| 200 def test_endElement_with_name_instanceId_sets_instance_id(self): |
| 201 return self.check_that_attribute_has_been_set("instanceId", 1, "instance
_id") |
| 202 |
| 203 def test_endElement_with_name_status_sets_status(self): |
| 204 return self.check_that_attribute_has_been_set("status", "some value", "s
tatus") |
| 205 |
| 206 def test_endElement_with_name_attachTime_sets_attach_time(self): |
| 207 return self.check_that_attribute_has_been_set("attachTime", 5, "attach_t
ime") |
| 208 |
| 209 def test_endElement_with_name_device_sets_device(self): |
| 210 return self.check_that_attribute_has_been_set("device", "/dev/null", "de
vice") |
| 211 |
| 212 def test_endElement_with_other_name_sets_other_name_attribute(self): |
| 213 return self.check_that_attribute_has_been_set("someName", "some value",
"someName") |
| 214 |
| 215 class VolumeAttributeTests(unittest.TestCase): |
| 216 def setUp(self): |
| 217 self.volume_attribute = VolumeAttribute() |
| 218 self.volume_attribute._key_name = "key_name" |
| 219 self.volume_attribute.attrs = {"key_name": False} |
| 220 |
| 221 def test_startElement_with_name_autoEnableIO_sets_key_name(self): |
| 222 self.volume_attribute.startElement("autoEnableIO", None, None) |
| 223 self.assertEqual(self.volume_attribute._key_name, "autoEnableIO") |
| 224 |
| 225 def test_startElement_without_name_autoEnableIO_returns_None(self): |
| 226 retval = self.volume_attribute.startElement("some name", None, None) |
| 227 self.assertEqual(retval, None) |
| 228 |
| 229 def test_endElement_with_name_value_and_value_true_sets_attrs_key_name_True(
self): |
| 230 self.volume_attribute.endElement("value", "true", None) |
| 231 self.assertEqual(self.volume_attribute.attrs['key_name'], True) |
| 232 |
| 233 def test_endElement_with_name_value_and_value_false_sets_attrs_key_name_Fals
e(self): |
| 234 self.volume_attribute._key_name = "other_key_name" |
| 235 self.volume_attribute.endElement("value", "false", None) |
| 236 self.assertEqual(self.volume_attribute.attrs['other_key_name'], False) |
| 237 |
| 238 def test_endElement_with_name_volumeId_sets_id(self): |
| 239 self.volume_attribute.endElement("volumeId", "some_value", None) |
| 240 self.assertEqual(self.volume_attribute.id, "some_value") |
| 241 |
| 242 def test_endElement_with_other_name_sets_other_name_attribute(self): |
| 243 self.volume_attribute.endElement("someName", "some value", None) |
| 244 self.assertEqual(self.volume_attribute.someName, "some value") |
| 245 |
| 246 |
| 247 if __name__ == "__main__": |
| 248 unittest.main() |
OLD | NEW |