/horizon/dashboards/nova/instances_and_volumes/volumes/tests.py

https://github.com/StackOps/horizon
Python | 110 lines | 74 code | 13 blank | 23 comment | 0 complexity | d57af7b224c75d504420e886d5f2c603 MD5 | raw file
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2012 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Copyright 2012 Nebula, Inc.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. from copy import deepcopy
  20. from django import http
  21. from django.core.urlresolvers import reverse
  22. from mox import IsA
  23. from horizon import api
  24. from horizon import test
  25. class VolumeViewTests(test.TestCase):
  26. def test_edit_attachments(self):
  27. volume = self.volumes.first()
  28. servers = self.servers.list()
  29. self.mox.StubOutWithMock(api, 'volume_get')
  30. self.mox.StubOutWithMock(api.nova, 'server_list')
  31. api.volume_get(IsA(http.HttpRequest), volume.id) \
  32. .AndReturn(volume)
  33. api.nova.server_list(IsA(http.HttpRequest)).AndReturn(servers)
  34. self.mox.ReplayAll()
  35. url = reverse('horizon:nova:instances_and_volumes:volumes:attach',
  36. args=[volume.id])
  37. res = self.client.get(url)
  38. # Asserting length of 2 accounts for the one instance option,
  39. # and the one 'Choose Instance' option.
  40. self.assertEqual(len(res.context['form'].fields['instance']._choices),
  41. 2)
  42. self.assertEqual(res.status_code, 200)
  43. def test_edit_attachments_attached_volume(self):
  44. servers = deepcopy(self.servers)
  45. active_server = deepcopy(self.servers.first())
  46. active_server.status = 'ACTIVE'
  47. active_server.id = "3"
  48. servers.add(active_server)
  49. volumes = deepcopy(self.volumes)
  50. volume = deepcopy(self.volumes.first())
  51. volume.id = "2"
  52. volume.status = "in-use"
  53. volume.attachments = [{"id": "1", "server_id": "3",
  54. "device": "/dev/hdn"}]
  55. volumes.add(volume)
  56. self.mox.StubOutWithMock(api, 'volume_get')
  57. self.mox.StubOutWithMock(api.nova, 'server_list')
  58. api.volume_get(IsA(http.HttpRequest), volume.id) \
  59. .AndReturn(volume)
  60. api.nova.server_list(IsA(http.HttpRequest)).AndReturn(servers.list())
  61. self.mox.ReplayAll()
  62. url = reverse('horizon:nova:instances_and_volumes:volumes:attach',
  63. args=[volume.id])
  64. res = self.client.get(url)
  65. # Asserting length of 1 instance (plus 'Select ..' item).
  66. # The other instance is already attached to this volume
  67. self.assertEqual(len(res.context['form'].fields['instance']._choices),
  68. 2)
  69. self.assertEqual(res.context['form'].\
  70. fields['instance']._choices[0][1],
  71. "Select an instance")
  72. # The instance choice should not be server_id = 3
  73. self.assertNotEqual(res.context['form'].\
  74. fields['instance']._choices[1][0],
  75. volume.attachments[0]['server_id'])
  76. self.assertEqual(res.status_code, 200)
  77. def test_detail_view(self):
  78. volume = self.volumes.first()
  79. server = self.servers.first()
  80. volume.attachments = [{"server_id": server.id}]
  81. self.mox.StubOutWithMock(api.nova, 'volume_get')
  82. self.mox.StubOutWithMock(api.nova, 'server_get')
  83. api.nova.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
  84. api.nova.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
  85. self.mox.ReplayAll()
  86. url = reverse('horizon:nova:instances_and_volumes:volumes:detail',
  87. args=[volume.id])
  88. res = self.client.get(url)
  89. self.assertContains(res, "<dd>Volume name</dd>", 1, 200)
  90. self.assertContains(res, "<dd>1</dd>", 1, 200)
  91. self.assertContains(res, "<dd>Available</dd>", 1, 200)
  92. self.assertContains(res, "<dd>40 GB</dd>", 1, 200)
  93. self.assertContains(res, "<dd>04/01/12 at 10:30:00</dd>", 1, 200)
  94. self.assertContains(res, "<a href=\"/nova/instances_and_volumes/"
  95. "instances/1/detail\">Instance server_1 "
  96. "(1)</a>", 1, 200)
  97. self.assertNoMessages()