blob: 19c7e05bf49cc8b9ebed098c9802cd7e2f939e83 [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):
30 super(BaseTestCase, self).setUp()
31
32 self.zk_chroot_fixture = self.useFixture(ChrootedKazooFixture())
James E. Blair0d5a36e2017-02-21 10:53:44 -050033 self.zk_config = '%s:%s%s' % (
James E. Blair3897a132016-12-22 18:23:42 -080034 self.zk_chroot_fixture.zookeeper_host,
35 self.zk_chroot_fixture.zookeeper_port,
36 self.zk_chroot_fixture.zookeeper_chroot)
37
38 self.zk = zuul.zk.ZooKeeper()
James E. Blair0d5a36e2017-02-21 10:53:44 -050039 self.zk.connect(self.zk_config)
James E. Blair3897a132016-12-22 18:23:42 -080040
41 self.provisioned_requests = []
42 # This class implements the scheduler methods zuul.nodepool
43 # needs, so we pass 'self' as the scheduler.
44 self.nodepool = zuul.nodepool.Nodepool(self)
45
James E. Blair0d5a36e2017-02-21 10:53:44 -050046 self.fake_nodepool = FakeNodepool(
47 self.zk_chroot_fixture.zookeeper_host,
48 self.zk_chroot_fixture.zookeeper_port,
49 self.zk_chroot_fixture.zookeeper_chroot)
James E. Blair3897a132016-12-22 18:23:42 -080050
51 def waitForRequests(self):
52 # Wait until all requests are complete.
53 while self.nodepool.requests:
54 time.sleep(0.1)
55
56 def onNodesProvisioned(self, request):
57 # This is a scheduler method that the nodepool class calls
58 # back when a request is provisioned.
59 self.provisioned_requests.append(request)
60
61 def test_node_request(self):
62 # Test a simple node request
63
64 nodeset = model.NodeSet()
65 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
66 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
67 job = model.Job('testjob')
68 job.nodeset = nodeset
69 request = self.nodepool.requestNodes(None, job)
70 self.waitForRequests()
71 self.assertEqual(len(self.provisioned_requests), 1)
72 self.assertEqual(request.state, 'fulfilled')
James E. Blair15be0e12017-01-03 13:45:20 -080073
James E. Blaira38c28e2017-01-04 10:33:20 -080074 # Accept the nodes
75 self.nodepool.acceptNodes(request)
76 nodeset = request.nodeset
77
78 for node in nodeset.getNodes():
79 self.assertIsNotNone(node.lock)
80 self.assertEqual(node.state, 'ready')
81
James E. Blaircacdf2b2017-01-04 13:14:37 -080082 # Mark the nodes in use
James E. Blair1511bc32017-01-18 09:25:31 -080083 self.nodepool.useNodeSet(nodeset)
James E. Blaircacdf2b2017-01-04 13:14:37 -080084 for node in nodeset.getNodes():
85 self.assertEqual(node.state, 'in-use')
86
James E. Blair62295d32017-01-04 13:27:58 -080087 # Return the nodes
James E. Blair1511bc32017-01-18 09:25:31 -080088 self.nodepool.returnNodeSet(nodeset)
James E. Blair62295d32017-01-04 13:27:58 -080089 for node in nodeset.getNodes():
90 self.assertIsNone(node.lock)
91 self.assertEqual(node.state, 'used')
92
James E. Blair15be0e12017-01-03 13:45:20 -080093 def test_node_request_disconnect(self):
94 # Test that node requests are re-submitted after disconnect
95
96 nodeset = model.NodeSet()
97 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
98 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
99 job = model.Job('testjob')
100 job.nodeset = nodeset
101 self.fake_nodepool.paused = True
102 request = self.nodepool.requestNodes(None, job)
103 self.zk.client.stop()
104 self.zk.client.start()
105 self.fake_nodepool.paused = False
106 self.waitForRequests()
107 self.assertEqual(len(self.provisioned_requests), 1)
108 self.assertEqual(request.state, 'fulfilled')
James E. Blair01695c32017-01-04 17:29:25 -0800109
110 def test_node_request_canceled(self):
111 # Test that node requests can be canceled
112
113 nodeset = model.NodeSet()
114 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
115 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
116 job = model.Job('testjob')
117 job.nodeset = nodeset
118 self.fake_nodepool.paused = True
119 request = self.nodepool.requestNodes(None, job)
120 self.nodepool.cancelRequest(request)
121
122 self.waitForRequests()
123 self.assertEqual(len(self.provisioned_requests), 0)