test_network_service.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import test.helpers as helpers
  2. import uuid
  3. from test.helpers import ProviderTestBase
  4. class CloudNetworkServiceTestCase(ProviderTestBase):
  5. def __init__(self, methodName, provider):
  6. super(CloudNetworkServiceTestCase, self).__init__(
  7. methodName=methodName, provider=provider)
  8. def test_crud_network_service(self):
  9. name = 'cbtestnetworkservice-{0}'.format(uuid.uuid4())
  10. subnet_name = 'cbtestsubnetservice-{0}'.format(uuid.uuid4())
  11. net = self.provider.network.create(name=name)
  12. with helpers.cleanup_action(
  13. lambda:
  14. self.provider.network.delete(network_id=net.id)
  15. ):
  16. # test list method
  17. netl = self.provider.network.list()
  18. list_netl = [n for n in netl if n.name == name]
  19. self.assertTrue(
  20. len(list_netl) == 1,
  21. "List networks does not return the expected network %s" %
  22. name)
  23. # check get
  24. get_net = self.provider.network.get(network_id=net.id)
  25. self.assertTrue(
  26. get_net == net,
  27. "Get network did not return the expected network {0}."
  28. .format(name))
  29. # check subnet
  30. subnet = self.provider.network.subnets.create(
  31. network=net, cidr_block="10.0.0.1/24", name=subnet_name)
  32. with helpers.cleanup_action(
  33. lambda:
  34. self.provider.network.subnets.delete(subnet=subnet)
  35. ):
  36. # test list method
  37. subnetl = self.provider.network.subnets.list(network=net)
  38. list_subnetl = [n for n in subnetl if n.name == subnet_name]
  39. self.assertTrue(
  40. len(list_subnetl) == 1,
  41. "List subnets does not return the expected subnet %s" %
  42. subnet_name)
  43. subnetl = self.provider.network.subnets.list()
  44. found_subnet = [n for n in subnetl if n.name == subnet_name]
  45. self.assertTrue(
  46. len(found_subnet) == 0,
  47. "Subnet {0} should have been deleted but still exists."
  48. .format(subnet_name))
  49. # Check floating IP address
  50. ip = self.provider.network.create_floating_ip()
  51. ip_id = ip.id
  52. with helpers.cleanup_action(lambda: ip.delete()):
  53. ipl = self.provider.network.floating_ips()
  54. self.assertTrue(
  55. ip in ipl,
  56. "Floating IP address {0} should exist in the list {1}"
  57. .format(ip.id, ipl))
  58. self.assertIn(
  59. ip.public_ip, repr(ip),
  60. "repr(obj) should contain the address public IP value.")
  61. self.assertFalse(
  62. ip.private_ip,
  63. "Floating IP should not have a private IP value ({0})."
  64. .format(ip.private_ip))
  65. self.assertFalse(
  66. ip.in_use(),
  67. "Newly created floating IP address should not be in use.")
  68. ipl = self.provider.network.floating_ips()
  69. found_ip = [a for a in ipl if a.id == ip_id]
  70. self.assertTrue(
  71. len(found_ip) == 0,
  72. "Floating IP {0} should have been deleted but still exists."
  73. .format(ip_id))
  74. netl = self.provider.network.list()
  75. found_net = [n for n in netl if n.name == name]
  76. self.assertEqual(
  77. len(found_net), 0,
  78. "Network {0} should have been deleted but still exists."
  79. .format(name))
  80. def test_crud_network(self):
  81. name = 'cbtestnetwork-{0}'.format(uuid.uuid4())
  82. subnet_name = 'cbtestsubnet-{0}'.format(uuid.uuid4())
  83. net = self.provider.network.create(name=name)
  84. with helpers.cleanup_action(
  85. lambda: net.delete()
  86. ):
  87. net.wait_till_ready()
  88. self.assertEqual(
  89. net.refresh(), 'available',
  90. "Network in state %s , yet should be 'available'" % net.state)
  91. self.assertIn(
  92. net.id, repr(net),
  93. "repr(obj) should contain the object id so that the object"
  94. " can be reconstructed, but does not.")
  95. self.assertIn(
  96. net.cidr_block, ['', '10.0.0.0/16'],
  97. "Network CIDR %s does not contain the expected value."
  98. % net.cidr_block)
  99. cidr = '10.0.1.0/24'
  100. sn = net.create_subnet(cidr_block=cidr, name=subnet_name)
  101. with helpers.cleanup_action(lambda: sn.delete()):
  102. self.assertTrue(
  103. sn.id in [s.id for s in net.subnets()],
  104. "Subnet ID %s should be listed in network subnets %s."
  105. % (sn.id, net.subnets()))
  106. self.assertIn(
  107. net.id, sn.network_id,
  108. "Network ID %s should be specified in the subnet's network"
  109. " id %s." % (net.id, sn.network_id))
  110. self.assertEqual(
  111. cidr, sn.cidr_block,
  112. "Subnet's CIDR %s should match the specified one %s." % (
  113. sn.cidr_block, cidr))