blob: 0f23c9e5948a81768523dd816b6fdb1c80a7ad6a [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
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()
James E. Blair0d5a36e2017-02-21 10:53:44 -050040 self.zk.connect(self.zk_config)
James E. Blair8b2a1472017-02-19 15:33:55 -080041 self.hostname = 'nodepool-test-hostname'
James E. Blair3897a132016-12-22 18:23:42 -080042
43 self.provisioned_requests = []
44 # This class implements the scheduler methods zuul.nodepool
45 # needs, so we pass 'self' as the scheduler.
46 self.nodepool = zuul.nodepool.Nodepool(self)
47
James E. Blair0d5a36e2017-02-21 10:53:44 -050048 self.fake_nodepool = FakeNodepool(
49 self.zk_chroot_fixture.zookeeper_host,
50 self.zk_chroot_fixture.zookeeper_port,
51 self.zk_chroot_fixture.zookeeper_chroot)
James E. Blair3897a132016-12-22 18:23:42 -080052
53 def waitForRequests(self):
54 # Wait until all requests are complete.
55 while self.nodepool.requests:
56 time.sleep(0.1)
57
58 def onNodesProvisioned(self, request):
59 # This is a scheduler method that the nodepool class calls
60 # back when a request is provisioned.
61 self.provisioned_requests.append(request)
62
63 def test_node_request(self):
64 # Test a simple node request
65
66 nodeset = model.NodeSet()
67 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
68 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
69 job = model.Job('testjob')
70 job.nodeset = nodeset
71 request = self.nodepool.requestNodes(None, job)
72 self.waitForRequests()
73 self.assertEqual(len(self.provisioned_requests), 1)
74 self.assertEqual(request.state, 'fulfilled')
James E. Blair15be0e12017-01-03 13:45:20 -080075
James E. Blaira38c28e2017-01-04 10:33:20 -080076 # Accept the nodes
77 self.nodepool.acceptNodes(request)
78 nodeset = request.nodeset
79
80 for node in nodeset.getNodes():
81 self.assertIsNotNone(node.lock)
82 self.assertEqual(node.state, 'ready')
83
James E. Blaircacdf2b2017-01-04 13:14:37 -080084 # Mark the nodes in use
James E. Blair1511bc32017-01-18 09:25:31 -080085 self.nodepool.useNodeSet(nodeset)
James E. Blaircacdf2b2017-01-04 13:14:37 -080086 for node in nodeset.getNodes():
87 self.assertEqual(node.state, 'in-use')
88
James E. Blair62295d32017-01-04 13:27:58 -080089 # Return the nodes
James E. Blair1511bc32017-01-18 09:25:31 -080090 self.nodepool.returnNodeSet(nodeset)
James E. Blair62295d32017-01-04 13:27:58 -080091 for node in nodeset.getNodes():
92 self.assertIsNone(node.lock)
93 self.assertEqual(node.state, 'used')
94
James E. Blair15be0e12017-01-03 13:45:20 -080095 def test_node_request_disconnect(self):
96 # Test that node requests are re-submitted after disconnect
97
98 nodeset = model.NodeSet()
99 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
100 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
101 job = model.Job('testjob')
102 job.nodeset = nodeset
103 self.fake_nodepool.paused = True
104 request = self.nodepool.requestNodes(None, job)
105 self.zk.client.stop()
106 self.zk.client.start()
107 self.fake_nodepool.paused = False
108 self.waitForRequests()
109 self.assertEqual(len(self.provisioned_requests), 1)
110 self.assertEqual(request.state, 'fulfilled')
James E. Blair01695c32017-01-04 17:29:25 -0800111
112 def test_node_request_canceled(self):
113 # Test that node requests can be canceled
114
115 nodeset = model.NodeSet()
116 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
117 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
118 job = model.Job('testjob')
119 job.nodeset = nodeset
120 self.fake_nodepool.paused = True
121 request = self.nodepool.requestNodes(None, job)
122 self.nodepool.cancelRequest(request)
123
124 self.waitForRequests()
125 self.assertEqual(len(self.provisioned_requests), 0)