Manage a pool of nodes for a distributed test infrastructure
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.

test_driver_openshift.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (C) 2018 Red Hat
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import fixtures
  16. import logging
  17. from nodepool import tests
  18. from nodepool import zk
  19. from nodepool.driver.openshift import provider
  20. class FakeOpenshiftClient(object):
  21. def __init__(self):
  22. self.projects = []
  23. class FakeApi:
  24. class configuration:
  25. host = "http://localhost:8080"
  26. verify_ssl = False
  27. self.api_client = FakeApi()
  28. def list_project(self):
  29. class FakeProjects:
  30. items = self.projects
  31. return FakeProjects
  32. def create_project_request(self, proj_body):
  33. class FakeProject:
  34. class metadata:
  35. name = proj_body['metadata']['name']
  36. self.projects.append(FakeProject)
  37. return FakeProject
  38. def delete_project(self, name):
  39. to_delete = None
  40. for project in self.projects:
  41. if project.metadata.name == name:
  42. to_delete = project
  43. break
  44. if not to_delete:
  45. raise RuntimeError("Unknown project %s" % name)
  46. self.projects.remove(to_delete)
  47. def create_namespaced_role_binding(self, ns, role_binding_body):
  48. return
  49. class FakeCoreClient(object):
  50. def create_namespaced_service_account(self, ns, sa_body):
  51. return
  52. def read_namespaced_service_account(self, user, ns):
  53. class FakeSA:
  54. class secret:
  55. name = "fake"
  56. FakeSA.secrets = [FakeSA.secret]
  57. return FakeSA
  58. def read_namespaced_secret(self, name, ns):
  59. class FakeSecret:
  60. class metadata:
  61. annotations = {'openshift.io/token-secret.value': 'fake-token'}
  62. return FakeSecret
  63. def create_namespaced_pod(self, ns, pod_body):
  64. return
  65. def read_namespaced_pod(self, name, ns):
  66. class FakePod:
  67. class status:
  68. phase = "Running"
  69. return FakePod
  70. class TestDriverOpenshift(tests.DBTestCase):
  71. log = logging.getLogger("nodepool.TestDriverOpenshift")
  72. def setUp(self):
  73. super().setUp()
  74. self.fake_os_client = FakeOpenshiftClient()
  75. self.fake_k8s_client = FakeCoreClient()
  76. def fake_get_client(*args):
  77. return self.fake_os_client, self.fake_k8s_client
  78. self.useFixture(fixtures.MockPatchObject(
  79. provider.OpenshiftProvider, '_get_client',
  80. fake_get_client
  81. ))
  82. def test_openshift_machine(self):
  83. configfile = self.setup_config('openshift.yaml')
  84. pool = self.useNodepool(configfile, watermark_sleep=1)
  85. pool.start()
  86. req = zk.NodeRequest()
  87. req.state = zk.REQUESTED
  88. req.node_types.append('pod-fedora')
  89. self.zk.storeNodeRequest(req)
  90. self.log.debug("Waiting for request %s", req.id)
  91. req = self.waitForNodeRequest(req)
  92. self.assertEqual(req.state, zk.FULFILLED)
  93. self.assertNotEqual(req.nodes, [])
  94. node = self.zk.getNode(req.nodes[0])
  95. self.assertEqual(node.allocated_to, req.id)
  96. self.assertEqual(node.state, zk.READY)
  97. self.assertIsNotNone(node.launcher)
  98. self.assertEqual(node.connection_type, 'kubectl')
  99. self.assertEqual(node.connection_port.get('token'), 'fake-token')
  100. node.state = zk.DELETING
  101. self.zk.storeNode(node)
  102. self.waitForNodeDeletion(node)
  103. def test_openshift_native(self):
  104. configfile = self.setup_config('openshift.yaml')
  105. pool = self.useNodepool(configfile, watermark_sleep=1)
  106. pool.start()
  107. req = zk.NodeRequest()
  108. req.state = zk.REQUESTED
  109. req.node_types.append('openshift-project')
  110. self.zk.storeNodeRequest(req)
  111. self.log.debug("Waiting for request %s", req.id)
  112. req = self.waitForNodeRequest(req)
  113. self.assertEqual(req.state, zk.FULFILLED)
  114. self.assertNotEqual(req.nodes, [])
  115. node = self.zk.getNode(req.nodes[0])
  116. self.assertEqual(node.allocated_to, req.id)
  117. self.assertEqual(node.state, zk.READY)
  118. self.assertIsNotNone(node.launcher)
  119. self.assertEqual(node.connection_type, 'project')
  120. self.assertEqual(node.connection_port.get('token'), 'fake-token')
  121. node.state = zk.DELETING
  122. self.zk.storeNode(node)
  123. self.waitForNodeDeletion(node)