test_network_service.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import uuid
  2. from test.helpers import ProviderTestBase
  3. import test.helpers as helpers
  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. netl = self.provider.network.list()
  50. found_net = [n for n in netl if n.name == name]
  51. self.assertEqual(
  52. len(found_net), 0,
  53. "Network {0} should have been deleted but still exists."
  54. .format(name))
  55. def test_crud_network(self):
  56. name = 'cbtestnetwork-{0}'.format(uuid.uuid4())
  57. subnet_name = 'cbtestsubnet-{0}'.format(uuid.uuid4())
  58. net = self.provider.network.create(name=name)
  59. with helpers.cleanup_action(
  60. lambda: net.delete()
  61. ):
  62. net.wait_till_ready()
  63. self.assertEqual(
  64. net.refresh(), 'available',
  65. "Network in state %s , yet should be 'available'" % net.state)
  66. self.assertIn(
  67. net.id, repr(net),
  68. "repr(obj) should contain the object id so that the object"
  69. " can be reconstructed, but does not.")
  70. self.assertIn(
  71. net.cidr_block, ['', '10.0.0.0/16'],
  72. "Network CIDR %s does not contain the expected value."
  73. % net.cidr_block)
  74. cidr = '10.0.1.0/24'
  75. sn = net.create_subnet(cidr_block=cidr, name=subnet_name)
  76. with helpers.cleanup_action(lambda: sn.delete()):
  77. self.assertTrue(
  78. sn.id in [s.id for s in net.subnets()],
  79. "Subnet ID %s should be listed in network subnets %s."
  80. % (sn.id, net.subnets()))
  81. self.assertIn(
  82. net.id, sn.network_id,
  83. "Network ID %s should be specified in the subnet's network"
  84. " id %s." % (net.id, sn.network_id))
  85. self.assertEqual(
  86. cidr, sn.cidr_block,
  87. "Subnet's CIDR %s should match the specified one %s." % (
  88. sn.cidr_block, cidr))