test_network_service.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. # test get method
  44. sn = self.provider.network.subnets.get(subnet.id)
  45. self.assertTrue(
  46. subnet.id == sn.id,
  47. "GETting subnet should return the same subnet")
  48. subnetl = self.provider.network.subnets.list()
  49. found_subnet = [n for n in subnetl if n.name == subnet_name]
  50. self.assertTrue(
  51. len(found_subnet) == 0,
  52. "Subnet {0} should have been deleted but still exists."
  53. .format(subnet_name))
  54. # Check floating IP address
  55. ip = self.provider.network.create_floating_ip()
  56. ip_id = ip.id
  57. with helpers.cleanup_action(lambda: ip.delete()):
  58. ipl = self.provider.network.floating_ips()
  59. self.assertTrue(
  60. ip in ipl,
  61. "Floating IP address {0} should exist in the list {1}"
  62. .format(ip.id, ipl))
  63. # 2016-08: address filtering not implemented in moto
  64. # empty_ipl = self.provider.network.floating_ips('dummy-net')
  65. # self.assertFalse(
  66. # empty_ipl,
  67. # "Bogus network should not have any floating IPs: {0}"
  68. # .format(empty_ipl))
  69. self.assertIn(
  70. ip.public_ip, repr(ip),
  71. "repr(obj) should contain the address public IP value.")
  72. self.assertFalse(
  73. ip.private_ip,
  74. "Floating IP should not have a private IP value ({0})."
  75. .format(ip.private_ip))
  76. self.assertFalse(
  77. ip.in_use(),
  78. "Newly created floating IP address should not be in use.")
  79. ipl = self.provider.network.floating_ips()
  80. found_ip = [a for a in ipl if a.id == ip_id]
  81. self.assertTrue(
  82. len(found_ip) == 0,
  83. "Floating IP {0} should have been deleted but still exists."
  84. .format(ip_id))
  85. netl = self.provider.network.list()
  86. found_net = [n for n in netl if n.name == name]
  87. self.assertEqual(
  88. len(found_net), 0,
  89. "Network {0} should have been deleted but still exists."
  90. .format(name))
  91. def test_crud_network(self):
  92. name = 'cbtestnetwork-{0}'.format(uuid.uuid4())
  93. subnet_name = 'cbtestsubnet-{0}'.format(uuid.uuid4())
  94. net = self.provider.network.create(name=name)
  95. with helpers.cleanup_action(
  96. lambda: net.delete()
  97. ):
  98. net.wait_till_ready()
  99. self.assertEqual(
  100. net.refresh(), 'available',
  101. "Network in state %s , yet should be 'available'" % net.state)
  102. self.assertIn(
  103. net.id, repr(net),
  104. "repr(obj) should contain the object id so that the object"
  105. " can be reconstructed, but does not.")
  106. self.assertIn(
  107. net.cidr_block, ['', '10.0.0.0/16'],
  108. "Network CIDR %s does not contain the expected value."
  109. % net.cidr_block)
  110. cidr = '10.0.1.0/24'
  111. sn = net.create_subnet(cidr_block=cidr, name=subnet_name)
  112. with helpers.cleanup_action(lambda: sn.delete()):
  113. self.assertTrue(
  114. sn.id in [s.id for s in net.subnets()],
  115. "Subnet ID %s should be listed in network subnets %s."
  116. % (sn.id, net.subnets()))
  117. self.assertIn(
  118. net.id, sn.network_id,
  119. "Network ID %s should be specified in the subnet's network"
  120. " id %s." % (net.id, sn.network_id))
  121. self.assertEqual(
  122. cidr, sn.cidr_block,
  123. "Subnet's CIDR %s should match the specified one %s." % (
  124. sn.cidr_block, cidr))