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 | |
James E. Blair | 4f1731b | 2017-10-10 18:11:42 -0700 | [diff] [blame] | 32 | self.statsd = None |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 33 | self.zk_chroot_fixture = self.useFixture( |
| 34 | ChrootedKazooFixture(self.id())) |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 35 | self.zk_config = '%s:%s%s' % ( |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 36 | self.zk_chroot_fixture.zookeeper_host, |
| 37 | self.zk_chroot_fixture.zookeeper_port, |
| 38 | self.zk_chroot_fixture.zookeeper_chroot) |
| 39 | |
| 40 | self.zk = zuul.zk.ZooKeeper() |
Clark Boylan | ffe8f8b | 2017-04-24 17:36:11 -0700 | [diff] [blame] | 41 | self.addCleanup(self.zk.disconnect) |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 42 | self.zk.connect(self.zk_config) |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 43 | self.hostname = 'nodepool-test-hostname' |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 44 | |
| 45 | self.provisioned_requests = [] |
| 46 | # This class implements the scheduler methods zuul.nodepool |
| 47 | # needs, so we pass 'self' as the scheduler. |
| 48 | self.nodepool = zuul.nodepool.Nodepool(self) |
| 49 | |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 50 | self.fake_nodepool = FakeNodepool( |
| 51 | self.zk_chroot_fixture.zookeeper_host, |
| 52 | self.zk_chroot_fixture.zookeeper_port, |
| 53 | self.zk_chroot_fixture.zookeeper_chroot) |
Clark Boylan | ffe8f8b | 2017-04-24 17:36:11 -0700 | [diff] [blame] | 54 | self.addCleanup(self.fake_nodepool.stop) |
James E. Blair | 3897a13 | 2016-12-22 18:23:42 -0800 | [diff] [blame] | 55 | |
| 56 | def waitForRequests(self): |
| 57 | # Wait until all requests are complete. |
| 58 | while self.nodepool.requests: |
| 59 | time.sleep(0.1) |
| 60 | |
| 61 | def onNodesProvisioned(self, request): |
| 62 | # This is a scheduler method that the nodepool class calls |
| 63 | # back when a request is provisioned. |
| 64 | self.provisioned_requests.append(request) |
| 65 | |
| 66 | def test_node_request(self): |
| 67 | # Test a simple node request |
| 68 | |
| 69 | nodeset = model.NodeSet() |
| 70 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 71 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 72 | job = model.Job('testjob') |
| 73 | job.nodeset = nodeset |
| 74 | request = self.nodepool.requestNodes(None, job) |
| 75 | self.waitForRequests() |
| 76 | self.assertEqual(len(self.provisioned_requests), 1) |
| 77 | self.assertEqual(request.state, 'fulfilled') |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 78 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 79 | # Accept the nodes |
David Shrewsbury | 94e9588 | 2017-10-04 15:26:04 -0400 | [diff] [blame] | 80 | self.nodepool.acceptNodes(request, request.id) |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 81 | nodeset = request.nodeset |
| 82 | |
| 83 | for node in nodeset.getNodes(): |
| 84 | self.assertIsNotNone(node.lock) |
| 85 | self.assertEqual(node.state, 'ready') |
| 86 | |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 87 | # Mark the nodes in use |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 88 | self.nodepool.useNodeSet(nodeset) |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 89 | for node in nodeset.getNodes(): |
| 90 | self.assertEqual(node.state, 'in-use') |
| 91 | |
James E. Blair | 62295d3 | 2017-01-04 13:27:58 -0800 | [diff] [blame] | 92 | # Return the nodes |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 93 | self.nodepool.returnNodeSet(nodeset) |
James E. Blair | 62295d3 | 2017-01-04 13:27:58 -0800 | [diff] [blame] | 94 | for node in nodeset.getNodes(): |
| 95 | self.assertIsNone(node.lock) |
| 96 | self.assertEqual(node.state, 'used') |
| 97 | |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 98 | def test_node_request_disconnect(self): |
| 99 | # Test that node requests are re-submitted after disconnect |
| 100 | |
| 101 | nodeset = model.NodeSet() |
| 102 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 103 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 104 | job = model.Job('testjob') |
| 105 | job.nodeset = nodeset |
| 106 | self.fake_nodepool.paused = True |
| 107 | request = self.nodepool.requestNodes(None, job) |
| 108 | self.zk.client.stop() |
| 109 | self.zk.client.start() |
| 110 | self.fake_nodepool.paused = False |
| 111 | self.waitForRequests() |
| 112 | self.assertEqual(len(self.provisioned_requests), 1) |
| 113 | self.assertEqual(request.state, 'fulfilled') |
James E. Blair | 01695c3 | 2017-01-04 17:29:25 -0800 | [diff] [blame] | 114 | |
| 115 | def test_node_request_canceled(self): |
| 116 | # Test that node requests can be canceled |
| 117 | |
| 118 | nodeset = model.NodeSet() |
| 119 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 120 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 121 | job = model.Job('testjob') |
| 122 | job.nodeset = nodeset |
| 123 | self.fake_nodepool.paused = True |
| 124 | request = self.nodepool.requestNodes(None, job) |
| 125 | self.nodepool.cancelRequest(request) |
| 126 | |
| 127 | self.waitForRequests() |
| 128 | self.assertEqual(len(self.provisioned_requests), 0) |
David Shrewsbury | 94e9588 | 2017-10-04 15:26:04 -0400 | [diff] [blame] | 129 | |
| 130 | def test_accept_nodes_resubmitted(self): |
| 131 | # Test that a resubmitted request would not lock nodes |
| 132 | |
| 133 | nodeset = model.NodeSet() |
| 134 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 135 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 136 | job = model.Job('testjob') |
| 137 | job.nodeset = nodeset |
| 138 | request = self.nodepool.requestNodes(None, job) |
| 139 | self.waitForRequests() |
| 140 | self.assertEqual(len(self.provisioned_requests), 1) |
| 141 | self.assertEqual(request.state, 'fulfilled') |
| 142 | |
| 143 | # Accept the nodes, passing a different ID |
| 144 | self.nodepool.acceptNodes(request, "invalid") |
| 145 | nodeset = request.nodeset |
| 146 | |
| 147 | for node in nodeset.getNodes(): |
| 148 | self.assertIsNone(node.lock) |
| 149 | self.assertEqual(node.state, 'ready') |
| 150 | |
| 151 | def test_accept_nodes_lost_request(self): |
| 152 | # Test that a lost request would not lock nodes |
| 153 | |
| 154 | nodeset = model.NodeSet() |
| 155 | nodeset.addNode(model.Node('controller', 'ubuntu-xenial')) |
| 156 | nodeset.addNode(model.Node('compute', 'ubuntu-xenial')) |
| 157 | job = model.Job('testjob') |
| 158 | job.nodeset = nodeset |
| 159 | request = self.nodepool.requestNodes(None, job) |
| 160 | self.waitForRequests() |
| 161 | self.assertEqual(len(self.provisioned_requests), 1) |
| 162 | self.assertEqual(request.state, 'fulfilled') |
| 163 | |
| 164 | self.zk.deleteNodeRequest(request) |
| 165 | |
| 166 | # Accept the nodes |
| 167 | self.nodepool.acceptNodes(request, request.id) |
| 168 | nodeset = request.nodeset |
| 169 | |
| 170 | for node in nodeset.getNodes(): |
| 171 | self.assertIsNone(node.lock) |
| 172 | self.assertEqual(node.state, 'ready') |