James E. Blair | 15d91cc | 2017-01-18 09:05:17 -0800 | [diff] [blame] | 1 | # Copyright 2017 Red Hat, Inc. |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | |
| 16 | import time |
| 17 | |
| 18 | import zuul.zk |
| 19 | import zuul.nodepool |
| 20 | from zuul import model |
| 21 | |
| 22 | from tests.base import BaseTestCase, ChrootedKazooFixture, FakeNodepool |
| 23 | |
| 24 | |
| 25 | class TestNodepool(BaseTestCase): |
| 26 | # Tests the Nodepool interface class using a fake nodepool and |
| 27 | # scheduler. |
| 28 | |
| 29 | def setUp(self): |
Clark Boylan | 2462018 | 2017-04-24 10:11:51 -0700 | [diff] [blame] | 30 | super(TestNodepool, self).setUp() |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 31 | |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 32 | self.zk_chroot_fixture = self.useFixture( |
| 33 | ChrootedKazooFixture(self.id())) |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 34 | self.zk_config = '%s:%s%s' % ( |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 35 | self.zk_chroot_fixture.zookeeper_host, |
| 36 | self.zk_chroot_fixture.zookeeper_port, |
| 37 | self.zk_chroot_fixture.zookeeper_chroot) |
| 38 | |
| 39 | self.zk = zuul.zk.ZooKeeper() |
Clark Boylan | ffe8f8b | 2017-04-24 17:36:11 -0700 | [diff] [blame] | 40 | self.addCleanup(self.zk.disconnect) |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 41 | self.zk.connect(self.zk_config) |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 42 | self.hostname = 'nodepool-test-hostname' |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 43 | |
| 44 | self.provisioned_requests = [] |
| 45 | # This class implements the scheduler methods zuul.nodepool |
| 46 | # needs, so we pass 'self' as the scheduler. |
| 47 | self.nodepool = zuul.nodepool.Nodepool(self) |
| 48 | |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 49 | self.fake_nodepool = FakeNodepool( |
| 50 | self.zk_chroot_fixture.zookeeper_host, |
| 51 | self.zk_chroot_fixture.zookeeper_port, |
| 52 | self.zk_chroot_fixture.zookeeper_chroot) |
Clark Boylan | ffe8f8b | 2017-04-24 17:36:11 -0700 | [diff] [blame] | 53 | self.addCleanup(self.fake_nodepool.stop) |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 54 | |
| 55 | def waitForRequests(self): |
| 56 | # Wait until all requests are complete. |
| 57 | while self.nodepool.requests: |
| 58 | time.sleep(0.1) |
| 59 | |
| 60 | def onNodesProvisioned(self, request): |
| 61 | # This is a scheduler method that the nodepool class calls |
| 62 | # back when a request is provisioned. |
| 63 | self.provisioned_requests.append(request) |
| 64 | |
| 65 | def test_node_request(self): |
| 66 | # Test a simple node request |
| 67 | |
| 68 | nodeset = model.NodeSet() |
| 69 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 70 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 71 | job = model.Job('testjob') |
| 72 | job.nodeset = nodeset |
| 73 | request = self.nodepool.requestNodes(None, job) |
| 74 | self.waitForRequests() |
| 75 | self.assertEqual(len(self.provisioned_requests), 1) |
| 76 | self.assertEqual(request.state, 'fulfilled') |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 77 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 78 | # Accept the nodes |
| 79 | self.nodepool.acceptNodes(request) |
| 80 | nodeset = request.nodeset |
| 81 | |
| 82 | for node in nodeset.getNodes(): |
| 83 | self.assertIsNotNone(node.lock) |
| 84 | self.assertEqual(node.state, 'ready') |
| 85 | |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 86 | # Mark the nodes in use |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 87 | self.nodepool.useNodeSet(nodeset) |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 88 | for node in nodeset.getNodes(): |
| 89 | self.assertEqual(node.state, 'in-use') |
| 90 | |
James E. Blair | 62295d3 | 2017-01-04 13:27:58 -0800 | [diff] [blame] | 91 | # Return the nodes |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 92 | self.nodepool.returnNodeSet(nodeset) |
James E. Blair | 62295d3 | 2017-01-04 13:27:58 -0800 | [diff] [blame] | 93 | for node in nodeset.getNodes(): |
| 94 | self.assertIsNone(node.lock) |
| 95 | self.assertEqual(node.state, 'used') |
| 96 | |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 97 | def test_node_request_disconnect(self): |
| 98 | # Test that node requests are re-submitted after disconnect |
| 99 | |
| 100 | nodeset = model.NodeSet() |
| 101 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 102 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 103 | job = model.Job('testjob') |
| 104 | job.nodeset = nodeset |
| 105 | self.fake_nodepool.paused = True |
| 106 | request = self.nodepool.requestNodes(None, job) |
| 107 | self.zk.client.stop() |
| 108 | self.zk.client.start() |
| 109 | self.fake_nodepool.paused = False |
| 110 | self.waitForRequests() |
| 111 | self.assertEqual(len(self.provisioned_requests), 1) |
| 112 | self.assertEqual(request.state, 'fulfilled') |
James E. Blair | 01695c3 | 2017-01-04 17:29:25 -0800 | [diff] [blame] | 113 | |
| 114 | def test_node_request_canceled(self): |
| 115 | # Test that node requests can be canceled |
| 116 | |
| 117 | nodeset = model.NodeSet() |
| 118 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 119 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 120 | job = model.Job('testjob') |
| 121 | job.nodeset = nodeset |
| 122 | self.fake_nodepool.paused = True |
| 123 | request = self.nodepool.requestNodes(None, job) |
| 124 | self.nodepool.cancelRequest(request) |
| 125 | |
| 126 | self.waitForRequests() |
| 127 | self.assertEqual(len(self.provisioned_requests), 0) |