OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # Copyright (c) 2012 X.commerce, a business unit of eBay Inc.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. # not use this file except in compliance with the License. You may obtain
  11. # a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. # License for the specific language governing permissions and limitations
  19. # under the License.
  20. from django import http
  21. from django.core.urlresolvers import reverse
  22. from mox import IsA
  23. from novaclient import exceptions as novaclient_exceptions
  24. from horizon import api
  25. from horizon import test
  26. INDEX_URL = reverse('horizon:nova:access_and_security:index')
  27. NAMESPACE = "horizon:nova:access_and_security:floating_ips"
  28. class FloatingIpViewTests(test.TestCase):
  29. def test_associate(self):
  30. floating_ip = self.floating_ips.first()
  31. self.mox.StubOutWithMock(api, 'server_list')
  32. self.mox.StubOutWithMock(api, 'tenant_floating_ip_get')
  33. api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
  34. api.tenant_floating_ip_get(IsA(http.HttpRequest),
  35. floating_ip.id).AndReturn(floating_ip)
  36. self.mox.ReplayAll()
  37. url = reverse('%s:associate' % NAMESPACE, args=[floating_ip.id])
  38. res = self.client.get(url)
  39. self.assertTemplateUsed(res,
  40. 'nova/access_and_security/floating_ips/associate.html')
  41. def test_associate_post(self):
  42. floating_ip = self.floating_ips.first()
  43. server = self.servers.first()
  44. self.mox.StubOutWithMock(api, 'server_list')
  45. self.mox.StubOutWithMock(api, 'tenant_floating_ip_list')
  46. self.mox.StubOutWithMock(api, 'server_add_floating_ip')
  47. self.mox.StubOutWithMock(api, 'tenant_floating_ip_get')
  48. api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
  49. api.tenant_floating_ip_list(IsA(http.HttpRequest)) \
  50. .AndReturn(self.floating_ips.list())
  51. api.server_add_floating_ip(IsA(http.HttpRequest),
  52. server.id,
  53. floating_ip.id)
  54. api.tenant_floating_ip_get(IsA(http.HttpRequest),
  55. floating_ip.id).AndReturn(floating_ip)
  56. self.mox.ReplayAll()
  57. form_data = {'instance_id': server.id,
  58. 'floating_ip_id': floating_ip.id,
  59. 'floating_ip': floating_ip.ip,
  60. 'method': 'FloatingIpAssociate'}
  61. url = reverse('%s:associate' % NAMESPACE, args=[floating_ip.id])
  62. res = self.client.post(url, form_data)
  63. self.assertRedirects(res, INDEX_URL)
  64. def test_associate_post_with_exception(self):
  65. floating_ip = self.floating_ips.first()
  66. server = self.servers.first()
  67. self.mox.StubOutWithMock(api, 'server_list')
  68. self.mox.StubOutWithMock(api, 'tenant_floating_ip_list')
  69. self.mox.StubOutWithMock(api, 'security_group_list')
  70. self.mox.StubOutWithMock(api.nova, 'keypair_list')
  71. self.mox.StubOutWithMock(api, 'server_add_floating_ip')
  72. self.mox.StubOutWithMock(api, 'tenant_floating_ip_get')
  73. api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
  74. api.tenant_floating_ip_list(IsA(http.HttpRequest)) \
  75. .AndReturn(self.floating_ips.list())
  76. api.security_group_list(IsA(http.HttpRequest)) \
  77. .AndReturn(self.security_groups.list())
  78. api.nova.keypair_list(IsA(http.HttpRequest)) \
  79. .AndReturn(self.keypairs.list())
  80. exc = novaclient_exceptions.ClientException('ClientException')
  81. api.server_add_floating_ip(IsA(http.HttpRequest),
  82. server.id,
  83. floating_ip.id).AndRaise(exc)
  84. api.tenant_floating_ip_get(IsA(http.HttpRequest),
  85. floating_ip.id).AndReturn(floating_ip)
  86. self.mox.ReplayAll()
  87. url = reverse('%s:associate' % NAMESPACE, args=[floating_ip.id])
  88. res = self.client.post(url,
  89. {'instance_id': 1,
  90. 'floating_ip_id': floating_ip.id,
  91. 'floating_ip': floating_ip.ip,
  92. 'method': 'FloatingIpAssociate'})
  93. self.assertRaises(novaclient_exceptions.ClientException)
  94. self.assertRedirects(res, INDEX_URL)
  95. def test_disassociate_post(self):
  96. floating_ip = self.floating_ips.first()
  97. server = self.servers.first()
  98. self.mox.StubOutWithMock(api.nova, 'keypair_list')
  99. self.mox.StubOutWithMock(api, 'security_group_list')
  100. self.mox.StubOutWithMock(api, 'tenant_floating_ip_list')
  101. self.mox.StubOutWithMock(api, 'tenant_floating_ip_get')
  102. self.mox.StubOutWithMock(api, 'server_remove_floating_ip')
  103. api.nova.keypair_list(IsA(http.HttpRequest)) \
  104. .AndReturn(self.keypairs.list())
  105. api.security_group_list(IsA(http.HttpRequest)) \
  106. .AndReturn(self.security_groups.list())
  107. api.tenant_floating_ip_list(IsA(http.HttpRequest)) \
  108. .AndReturn(self.floating_ips.list())
  109. api.server_remove_floating_ip(IsA(http.HttpRequest),
  110. server.id,
  111. floating_ip.id)
  112. self.mox.ReplayAll()
  113. action = "floating_ips__disassociate__%s" % floating_ip.id
  114. res = self.client.post(INDEX_URL, {"action": action})
  115. self.assertMessageCount(success=1)
  116. self.assertRedirectsNoFollow(res, INDEX_URL)
  117. def test_disassociate_post_with_exception(self):
  118. floating_ip = self.floating_ips.first()
  119. server = self.servers.first()
  120. self.mox.StubOutWithMock(api.nova, 'keypair_list')
  121. self.mox.StubOutWithMock(api, 'security_group_list')
  122. self.mox.StubOutWithMock(api, 'tenant_floating_ip_list')
  123. self.mox.StubOutWithMock(api, 'tenant_floating_ip_get')
  124. self.mox.StubOutWithMock(api, 'server_remove_floating_ip')
  125. api.nova.keypair_list(IsA(http.HttpRequest)) \
  126. .AndReturn(self.keypairs.list())
  127. api.security_group_list(IsA(http.HttpRequest)) \
  128. .AndReturn(self.security_groups.list())
  129. api.tenant_floating_ip_list(IsA(http.HttpRequest)) \
  130. .AndReturn(self.floating_ips.list())
  131. exc = novaclient_exceptions.ClientException('ClientException')
  132. api.server_remove_floating_ip(IsA(http.HttpRequest),
  133. server.id,
  134. floating_ip.id).AndRaise(exc)
  135. self.mox.ReplayAll()
  136. action = "floating_ips__disassociate__%s" % floating_ip.id
  137. res = self.client.post(INDEX_URL, {"action": action})
  138. self.assertRaises(novaclient_exceptions.ClientException)
  139. self.assertRedirectsNoFollow(res, INDEX_URL)