Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Clark Boylan | 5ac7638 | 2018-02-06 15:18:45 -0800 | [diff] [blame] | 13 | import fixtures |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 14 | import os |
| 15 | import tempfile |
| 16 | import time |
| 17 | |
| 18 | from tests.base import BaseTestCase |
| 19 | |
| 20 | from zuul.executor.server import DiskAccountant |
| 21 | |
| 22 | |
| 23 | class FakeExecutor(object): |
| 24 | def __init__(self): |
| 25 | self.stopped_jobs = set() |
| 26 | self.used = {} |
| 27 | |
| 28 | def stopJobByJobDir(self, jobdir): |
| 29 | self.stopped_jobs.add(jobdir) |
| 30 | |
| 31 | def usage(self, dirname, used): |
| 32 | self.used[dirname] = used |
| 33 | |
| 34 | |
| 35 | class TestDiskAccountant(BaseTestCase): |
Clark Boylan | 5ac7638 | 2018-02-06 15:18:45 -0800 | [diff] [blame] | 36 | def setUp(self): |
| 37 | super(TestDiskAccountant, self).setUp() |
| 38 | self.useFixture(fixtures.NestedTempfile()) |
| 39 | |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 40 | def test_disk_accountant(self): |
Tobias Henkel | 683f10c | 2017-07-31 22:13:29 +0200 | [diff] [blame] | 41 | jobs_dir = tempfile.mkdtemp( |
| 42 | dir=os.environ.get("ZUUL_TEST_ROOT", None)) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 43 | cache_dir = tempfile.mkdtemp() |
| 44 | executor_server = FakeExecutor() |
| 45 | da = DiskAccountant(jobs_dir, 1, executor_server.stopJobByJobDir, |
| 46 | cache_dir) |
| 47 | da.start() |
| 48 | |
Tobias Henkel | 227c551 | 2017-07-31 21:44:40 +0200 | [diff] [blame] | 49 | try: |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 50 | jobdir = os.path.join(jobs_dir, '012345') |
| 51 | os.mkdir(jobdir) |
| 52 | testfile = os.path.join(jobdir, 'tfile') |
| 53 | with open(testfile, 'w') as tf: |
| 54 | tf.write(2 * 1024 * 1024 * '.') |
Clark Boylan | d688a62 | 2018-02-06 15:02:16 -0800 | [diff] [blame] | 55 | tf.flush() |
| 56 | os.fsync(tf.fileno()) |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 57 | |
| 58 | # da should catch over-limit dir within 5 seconds |
| 59 | for i in range(0, 50): |
| 60 | if jobdir in executor_server.stopped_jobs: |
| 61 | break |
| 62 | time.sleep(0.1) |
Tobias Henkel | 227c551 | 2017-07-31 21:44:40 +0200 | [diff] [blame] | 63 | self.assertEqual(set([jobdir]), executor_server.stopped_jobs) |
| 64 | finally: |
| 65 | da.stop() |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 66 | self.assertFalse(da.thread.is_alive()) |
| 67 | |
| 68 | def test_cache_hard_links(self): |
Tobias Henkel | 683f10c | 2017-07-31 22:13:29 +0200 | [diff] [blame] | 69 | root_dir = tempfile.mkdtemp( |
| 70 | dir=os.environ.get("ZUUL_TEST_ROOT", None)) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 71 | jobs_dir = os.path.join(root_dir, 'jobs') |
| 72 | os.mkdir(jobs_dir) |
| 73 | cache_dir = os.path.join(root_dir, 'cache') |
| 74 | os.mkdir(cache_dir) |
| 75 | |
| 76 | executor_server = FakeExecutor() |
| 77 | da = DiskAccountant(jobs_dir, 1, executor_server.stopJobByJobDir, |
| 78 | cache_dir, executor_server.usage) |
| 79 | da.start() |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 80 | self.addCleanup(da.stop) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 81 | |
| 82 | jobdir = os.path.join(jobs_dir, '012345') |
| 83 | os.mkdir(jobdir) |
| 84 | |
| 85 | repo_dir = os.path.join(cache_dir, 'a.repo') |
| 86 | os.mkdir(repo_dir) |
| 87 | source_file = os.path.join(repo_dir, 'big_file') |
| 88 | with open(source_file, 'w') as tf: |
| 89 | tf.write(2 * 1024 * 1024 * '.') |
| 90 | dest_link = os.path.join(jobdir, 'big_file') |
| 91 | os.link(source_file, dest_link) |
| 92 | |
| 93 | # da should _not_ count this file. Wait for 5s to get noticed |
| 94 | for i in range(0, 50): |
| 95 | if jobdir in executor_server.used: |
| 96 | break |
| 97 | time.sleep(0.1) |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 98 | self.assertEqual(set(), executor_server.stopped_jobs) |
| 99 | self.assertIn(jobdir, executor_server.used) |
| 100 | self.assertTrue(executor_server.used[jobdir] <= 1) |