blob: 6462f9af9869bd3ae47fa535f73f5283beeaedea [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())
33 self.zk_config = zuul.zk.ZooKeeperConnectionConfig(
34 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()
39 self.zk.connect([self.zk_config])
40
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
46 self.fake_nodepool = FakeNodepool(self.zk_config.host,
47 self.zk_config.port,
48 self.zk_config.chroot)
49
50 def waitForRequests(self):
51 # Wait until all requests are complete.
52 while self.nodepool.requests:
53 time.sleep(0.1)
54
55 def onNodesProvisioned(self, request):
56 # This is a scheduler method that the nodepool class calls
57 # back when a request is provisioned.
58 self.provisioned_requests.append(request)
59
60 def test_node_request(self):
61 # Test a simple node request
62
63 nodeset = model.NodeSet()
64 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
65 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
66 job = model.Job('testjob')
67 job.nodeset = nodeset
68 request = self.nodepool.requestNodes(None, job)
69 self.waitForRequests()
70 self.assertEqual(len(self.provisioned_requests), 1)
71 self.assertEqual(request.state, 'fulfilled')
James E. Blair15be0e12017-01-03 13:45:20 -080072
James E. Blaira38c28e2017-01-04 10:33:20 -080073 # Accept the nodes
74 self.nodepool.acceptNodes(request)
75 nodeset = request.nodeset
76
77 for node in nodeset.getNodes():
78 self.assertIsNotNone(node.lock)
79 self.assertEqual(node.state, 'ready')
80
James E. Blaircacdf2b2017-01-04 13:14:37 -080081 # Mark the nodes in use
James E. Blair1511bc32017-01-18 09:25:31 -080082 self.nodepool.useNodeSet(nodeset)
James E. Blaircacdf2b2017-01-04 13:14:37 -080083 for node in nodeset.getNodes():
84 self.assertEqual(node.state, 'in-use')
85
James E. Blair62295d32017-01-04 13:27:58 -080086 # Return the nodes
James E. Blair1511bc32017-01-18 09:25:31 -080087 self.nodepool.returnNodeSet(nodeset)
James E. Blair62295d32017-01-04 13:27:58 -080088 for node in nodeset.getNodes():
89 self.assertIsNone(node.lock)
90 self.assertEqual(node.state, 'used')
91
James E. Blair15be0e12017-01-03 13:45:20 -080092 def test_node_request_disconnect(self):
93 # Test that node requests are re-submitted after disconnect
94
95 nodeset = model.NodeSet()
96 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
97 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
98 job = model.Job('testjob')
99 job.nodeset = nodeset
100 self.fake_nodepool.paused = True
101 request = self.nodepool.requestNodes(None, job)
102 self.zk.client.stop()
103 self.zk.client.start()
104 self.fake_nodepool.paused = False
105 self.waitForRequests()
106 self.assertEqual(len(self.provisioned_requests), 1)
107 self.assertEqual(request.state, 'fulfilled')
James E. Blair01695c32017-01-04 17:29:25 -0800108
109 def test_node_request_canceled(self):
110 # Test that node requests can be canceled
111
112 nodeset = model.NodeSet()
113 nodeset.addNode(model.Node('controller', 'ubuntu-xenial'))
114 nodeset.addNode(model.Node('compute', 'ubuntu-xenial'))
115 job = model.Job('testjob')
116 job.nodeset = nodeset
117 self.fake_nodepool.paused = True
118 request = self.nodepool.requestNodes(None, job)
119 self.nodepool.cancelRequest(request)
120
121 self.waitForRequests()
122 self.assertEqual(len(self.provisioned_requests), 0)