/openstack_dashboard/dashboards/project/volumes/snapshots/tests.py

https://github.com/gabrielhurley/horizon · Python · 216 lines · 161 code · 36 blank · 19 comment · 1 complexity · 1d91e4e537dea9737d74edffdf26324f MD5 · raw file

  1. # Copyright 2011 United States Government as represented by the
  2. # Administrator of the National Aeronautics and Space Administration.
  3. # All Rights Reserved.
  4. #
  5. # Copyright 2011 Nebula, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License. You may obtain
  9. # a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. from django.core.urlresolvers import reverse
  19. from django import http
  20. from mox import IsA # noqa
  21. from openstack_dashboard import api
  22. from openstack_dashboard.api import cinder
  23. from openstack_dashboard.test import helpers as test
  24. from openstack_dashboard.usage import quotas
  25. INDEX_URL = reverse('horizon:project:volumes:index')
  26. VOLUME_SNAPSHOTS_TAB_URL = reverse('horizon:project:volumes:snapshots_tab')
  27. class VolumeSnapshotsViewTests(test.TestCase):
  28. @test.create_stubs({cinder: ('volume_get',),
  29. quotas: ('tenant_limit_usages',)})
  30. def test_create_snapshot_get(self):
  31. volume = self.cinder_volumes.first()
  32. cinder.volume_get(IsA(http.HttpRequest), volume.id) \
  33. .AndReturn(volume)
  34. snapshot_used = len(self.cinder_volume_snapshots.list())
  35. usage_limit = {'maxTotalVolumeGigabytes': 250,
  36. 'gigabytesUsed': 20,
  37. 'snapshotsUsed': snapshot_used,
  38. 'maxTotalSnapshots': 6}
  39. quotas.tenant_limit_usages(IsA(http.HttpRequest)).\
  40. AndReturn(usage_limit)
  41. self.mox.ReplayAll()
  42. url = reverse('horizon:project:volumes:'
  43. 'volumes:create_snapshot', args=[volume.id])
  44. res = self.client.get(url)
  45. self.assertTemplateUsed(res, 'project/volumes/volumes/'
  46. 'create_snapshot.html')
  47. @test.create_stubs({cinder: ('volume_get',
  48. 'volume_snapshot_create',)})
  49. def test_create_snapshot_post(self):
  50. volume = self.cinder_volumes.first()
  51. snapshot = self.cinder_volume_snapshots.first()
  52. cinder.volume_get(IsA(http.HttpRequest), volume.id) \
  53. .AndReturn(volume)
  54. cinder.volume_snapshot_create(IsA(http.HttpRequest),
  55. volume.id,
  56. snapshot.name,
  57. snapshot.description,
  58. force=False) \
  59. .AndReturn(snapshot)
  60. self.mox.ReplayAll()
  61. formData = {'method': 'CreateSnapshotForm',
  62. 'tenant_id': self.tenant.id,
  63. 'volume_id': volume.id,
  64. 'name': snapshot.name,
  65. 'description': snapshot.description}
  66. url = reverse('horizon:project:volumes:volumes:create_snapshot',
  67. args=[volume.id])
  68. res = self.client.post(url, formData)
  69. self.assertRedirectsNoFollow(res, VOLUME_SNAPSHOTS_TAB_URL)
  70. @test.create_stubs({cinder: ('volume_get',
  71. 'volume_snapshot_create',)})
  72. def test_force_create_snapshot(self):
  73. volume = self.cinder_volumes.get(name='my_volume')
  74. snapshot = self.cinder_volume_snapshots.first()
  75. cinder.volume_get(IsA(http.HttpRequest), volume.id) \
  76. .AndReturn(volume)
  77. cinder.volume_snapshot_create(IsA(http.HttpRequest),
  78. volume.id,
  79. snapshot.name,
  80. snapshot.description,
  81. force=True) \
  82. .AndReturn(snapshot)
  83. self.mox.ReplayAll()
  84. formData = {'method': 'CreateSnapshotForm',
  85. 'tenant_id': self.tenant.id,
  86. 'volume_id': volume.id,
  87. 'name': snapshot.name,
  88. 'description': snapshot.description}
  89. url = reverse('horizon:project:volumes:volumes:create_snapshot',
  90. args=[volume.id])
  91. res = self.client.post(url, formData)
  92. self.assertRedirectsNoFollow(res, VOLUME_SNAPSHOTS_TAB_URL)
  93. @test.create_stubs({api.cinder: ('volume_snapshot_list',
  94. 'volume_list',
  95. 'volume_backup_supported',
  96. 'volume_snapshot_delete')})
  97. def test_delete_volume_snapshot(self):
  98. vol_snapshots = self.cinder_volume_snapshots.list()
  99. volumes = self.cinder_volumes.list()
  100. snapshot = self.cinder_volume_snapshots.first()
  101. api.cinder.volume_backup_supported(IsA(http.HttpRequest)). \
  102. MultipleTimes().AndReturn(True)
  103. api.cinder.volume_snapshot_list(IsA(http.HttpRequest)). \
  104. AndReturn(vol_snapshots)
  105. api.cinder.volume_list(IsA(http.HttpRequest)). \
  106. AndReturn(volumes)
  107. api.cinder.volume_snapshot_delete(IsA(http.HttpRequest), snapshot.id)
  108. api.cinder.volume_snapshot_list(IsA(http.HttpRequest)). \
  109. AndReturn([])
  110. api.cinder.volume_list(IsA(http.HttpRequest)). \
  111. AndReturn(volumes)
  112. self.mox.ReplayAll()
  113. formData = {'action':
  114. 'volume_snapshots__delete__%s' % snapshot.id}
  115. res = self.client.post(VOLUME_SNAPSHOTS_TAB_URL, formData, follow=True)
  116. self.assertIn("Scheduled deletion of Volume Snapshot: test snapshot",
  117. [m.message for m in res.context['messages']])
  118. @test.create_stubs({api.cinder: ('volume_snapshot_get', 'volume_get')})
  119. def test_volume_snapshot_detail_get(self):
  120. volume = self.cinder_volumes.first()
  121. snapshot = self.cinder_volume_snapshots.first()
  122. api.cinder.volume_get(IsA(http.HttpRequest), volume.id). \
  123. AndReturn(volume)
  124. api.cinder.volume_snapshot_get(IsA(http.HttpRequest), snapshot.id). \
  125. AndReturn(snapshot)
  126. self.mox.ReplayAll()
  127. url = reverse('horizon:project:volumes:snapshots:detail',
  128. args=[snapshot.id])
  129. res = self.client.get(url)
  130. self.assertContains(res,
  131. "<h1>Volume Snapshot Details: %s</h1>" %
  132. snapshot.name,
  133. 1, 200)
  134. self.assertContains(res, "<dd>test snapshot</dd>", 1, 200)
  135. self.assertContains(res, "<dd>%s</dd>" % snapshot.id, 1, 200)
  136. self.assertContains(res, "<dd>Available</dd>", 1, 200)
  137. @test.create_stubs({api.cinder: ('volume_snapshot_get',)})
  138. def test_volume_snapshot_detail_get_with_exception(self):
  139. # Test to verify redirect if get volume snapshot fails
  140. snapshot = self.cinder_volume_snapshots.first()
  141. api.cinder.volume_snapshot_get(IsA(http.HttpRequest), snapshot.id).\
  142. AndRaise(self.exceptions.cinder)
  143. self.mox.ReplayAll()
  144. url = reverse('horizon:project:volumes:snapshots:detail',
  145. args=[snapshot.id])
  146. res = self.client.get(url)
  147. self.assertRedirectsNoFollow(res, INDEX_URL)
  148. @test.create_stubs({api.cinder: ('volume_snapshot_get', 'volume_get')})
  149. def test_volume_snapshot_detail_with_volume_get_exception(self):
  150. # Test to verify redirect if get volume fails
  151. volume = self.cinder_volumes.first()
  152. snapshot = self.cinder_volume_snapshots.first()
  153. api.cinder.volume_get(IsA(http.HttpRequest), volume.id). \
  154. AndRaise(self.exceptions.cinder)
  155. api.cinder.volume_snapshot_get(IsA(http.HttpRequest), snapshot.id). \
  156. AndReturn(snapshot)
  157. self.mox.ReplayAll()
  158. url = reverse('horizon:project:volumes:snapshots:detail',
  159. args=[snapshot.id])
  160. res = self.client.get(url)
  161. self.assertRedirectsNoFollow(res, INDEX_URL)
  162. @test.create_stubs({cinder: ('volume_snapshot_update',
  163. 'volume_snapshot_get')})
  164. def test_update_snapshot(self):
  165. snapshot = self.cinder_volume_snapshots.first()
  166. cinder.volume_snapshot_get(IsA(http.HttpRequest), snapshot.id) \
  167. .AndReturn(snapshot)
  168. cinder.volume_snapshot_update(IsA(http.HttpRequest),
  169. snapshot.id,
  170. snapshot.name,
  171. snapshot.description) \
  172. .AndReturn(snapshot)
  173. self.mox.ReplayAll()
  174. formData = {'method': 'UpdateSnapshotForm',
  175. 'name': snapshot.name,
  176. 'description': snapshot.description}
  177. url = reverse(('horizon:project:volumes:snapshots:update'),
  178. args=[snapshot.id])
  179. res = self.client.post(url, formData)
  180. self.assertRedirectsNoFollow(res, INDEX_URL)