blob: d51898b733661e9cb57db7a56a9080d1f59e2b8f [file] [log] [blame]
James E. Blair15d91cc2017-01-18 09:05:17 -08001# Copyright 2017 Red Hat, Inc.
James E. Blair3897a132016-12-22 18:23:42 -08002#
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
16import time
17
18import zuul.zk
19import zuul.nodepool
20from zuul import model
21
22from tests.base import BaseTestCase, ChrootedKazooFixture, FakeNodepool
23
24
25class TestNodepool(BaseTestCase):
26 # Tests the Nodepool interface class using a fake nodepool and
27 # scheduler.
28
29 def setUp(self):
Clark Boylan24620182017-04-24 10:11:51 -070030 super(TestNodepool, self).setUp()
James E. Blair3897a132016-12-22 18:23:42 -080031
Clark Boylan621ec9a2017-04-07 17:41:33 -070032 self.zk_chroot_fixture = self.useFixture(
33 ChrootedKazooFixture(self.id()))
James E. Blair0d5a36e2017-02-21 10:53:44 -050034 self.zk_config = '%s:%s%s' % (
James E. Blair3897a132016-12-22 18:23:42 -080035 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 Boylanffe8f8b2017-04-24 17:36:11 -070040 self.addCleanup(self.zk.disconnect)
James E. Blair0d5a36e2017-02-21 10:53:44 -050041 self.zk.connect(self.zk_config)
James E. Blair8b2a1472017-02-19 15:33:55 -080042 self.hostname = 'nodepool-test-hostname'
James E. Blair3897a132016-12-22 18:23:42 -080043
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. Blair0d5a36e2017-02-21 10:53:44 -050049 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 Boylanffe8f8b2017-04-24 17:36:11 -070053 self.addCleanup(self.fake_nodepool.stop)
James E. Blair3897a132016-12-22 18:23:42 -080054
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. Blair15be0e12017-01-03 13:45:20 -080077
James E. Blaira38c28e2017-01-04 10:33:20 -080078 # Accept the nodes
David Shrewsbury94e95882017-10-04 15:26:04 -040079 self.nodepool.acceptNodes(request, request.id)
James E. Blaira38c28e2017-01-04 10:33:20 -080080 nodeset = request.nodeset
81
82 for node in nodeset.getNodes():
83 self.assertIsNotNone(node.lock)
84 self.assertEqual(node.state, 'ready')
85
James E. Blaircacdf2b2017-01-04 13:14:37 -080086 # Mark the nodes in use
James E. Blair1511bc32017-01-18 09:25:31 -080087 self.nodepool.useNodeSet(nodeset)
James E. Blaircacdf2b2017-01-04 13:14:37 -080088 for node in nodeset.getNodes():
89 self.assertEqual(node.state, 'in-use')
90
James E. Blair62295d32017-01-04 13:27:58 -080091 # Return the nodes
James E. Blair1511bc32017-01-18 09:25:31 -080092 self.nodepool.returnNodeSet(nodeset)
James E. Blair62295d32017-01-04 13:27:58 -080093 for node in nodeset.getNodes():
94 self.assertIsNone(node.lock)
95 self.assertEqual(node.state, 'used')
96
James E. Blair15be0e12017-01-03 13:45:20 -080097 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. Blair01695c32017-01-04 17:29:25 -0800113
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)
David Shrewsbury94e95882017-10-04 15:26:04 -0400128
129 def test_accept_nodes_resubmitted(self):
130 # Test that a resubmitted request would not lock nodes
131
132 nodeset = model.NodeSet()
133 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
134 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
135 job = model.Job('testjob')
136 job.nodeset = nodeset
137 request = self.nodepool.requestNodes(None, job)
138 self.waitForRequests()
139 self.assertEqual(len(self.provisioned_requests), 1)
140 self.assertEqual(request.state, 'fulfilled')
141
142 # Accept the nodes, passing a different ID
143 self.nodepool.acceptNodes(request, "invalid")
144 nodeset = request.nodeset
145
146 for node in nodeset.getNodes():
147 self.assertIsNone(node.lock)
148 self.assertEqual(node.state, 'ready')
149
150 def test_accept_nodes_lost_request(self):
151 # Test that a lost request would not lock nodes
152
153 nodeset = model.NodeSet()
154 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
155 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
156 job = model.Job('testjob')
157 job.nodeset = nodeset
158 request = self.nodepool.requestNodes(None, job)
159 self.waitForRequests()
160 self.assertEqual(len(self.provisioned_requests), 1)
161 self.assertEqual(request.state, 'fulfilled')
162
163 self.zk.deleteNodeRequest(request)
164
165 # Accept the nodes
166 self.nodepool.acceptNodes(request, request.id)
167 nodeset = request.nodeset
168
169 for node in nodeset.getNodes():
170 self.assertIsNone(node.lock)
171 self.assertEqual(node.state, 'ready')