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 | |
| 13 | import os |
| 14 | import tempfile |
| 15 | import time |
| 16 | |
| 17 | from tests.base import BaseTestCase |
| 18 | |
| 19 | from zuul.executor.server import DiskAccountant |
| 20 | |
| 21 | |
| 22 | class FakeExecutor(object): |
| 23 | def __init__(self): |
| 24 | self.stopped_jobs = set() |
| 25 | self.used = {} |
| 26 | |
| 27 | def stopJobByJobDir(self, jobdir): |
| 28 | self.stopped_jobs.add(jobdir) |
| 29 | |
| 30 | def usage(self, dirname, used): |
| 31 | self.used[dirname] = used |
| 32 | |
| 33 | |
| 34 | class TestDiskAccountant(BaseTestCase): |
| 35 | def test_disk_accountant(self): |
Tobias Henkel | 683f10c | 2017-07-31 22:13:29 +0200 | [diff] [blame] | 36 | jobs_dir = tempfile.mkdtemp( |
| 37 | dir=os.environ.get("ZUUL_TEST_ROOT", None)) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 38 | cache_dir = tempfile.mkdtemp() |
| 39 | executor_server = FakeExecutor() |
| 40 | da = DiskAccountant(jobs_dir, 1, executor_server.stopJobByJobDir, |
| 41 | cache_dir) |
| 42 | da.start() |
| 43 | |
Tobias Henkel | 227c551 | 2017-07-31 21:44:40 +0200 | [diff] [blame] | 44 | try: |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 45 | jobdir = os.path.join(jobs_dir, '012345') |
| 46 | os.mkdir(jobdir) |
| 47 | testfile = os.path.join(jobdir, 'tfile') |
| 48 | with open(testfile, 'w') as tf: |
| 49 | tf.write(2 * 1024 * 1024 * '.') |
Clark Boylan | d688a62 | 2018-02-06 15:02:16 -0800 | [diff] [blame^] | 50 | tf.flush() |
| 51 | os.fsync(tf.fileno()) |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 52 | |
| 53 | # da should catch over-limit dir within 5 seconds |
| 54 | for i in range(0, 50): |
| 55 | if jobdir in executor_server.stopped_jobs: |
| 56 | break |
| 57 | time.sleep(0.1) |
Tobias Henkel | 227c551 | 2017-07-31 21:44:40 +0200 | [diff] [blame] | 58 | self.assertEqual(set([jobdir]), executor_server.stopped_jobs) |
| 59 | finally: |
| 60 | da.stop() |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 61 | self.assertFalse(da.thread.is_alive()) |
| 62 | |
| 63 | def test_cache_hard_links(self): |
Tobias Henkel | 683f10c | 2017-07-31 22:13:29 +0200 | [diff] [blame] | 64 | root_dir = tempfile.mkdtemp( |
| 65 | dir=os.environ.get("ZUUL_TEST_ROOT", None)) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 66 | jobs_dir = os.path.join(root_dir, 'jobs') |
| 67 | os.mkdir(jobs_dir) |
| 68 | cache_dir = os.path.join(root_dir, 'cache') |
| 69 | os.mkdir(cache_dir) |
| 70 | |
| 71 | executor_server = FakeExecutor() |
| 72 | da = DiskAccountant(jobs_dir, 1, executor_server.stopJobByJobDir, |
| 73 | cache_dir, executor_server.usage) |
| 74 | da.start() |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 75 | self.addCleanup(da.stop) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 76 | |
| 77 | jobdir = os.path.join(jobs_dir, '012345') |
| 78 | os.mkdir(jobdir) |
| 79 | |
| 80 | repo_dir = os.path.join(cache_dir, 'a.repo') |
| 81 | os.mkdir(repo_dir) |
| 82 | source_file = os.path.join(repo_dir, 'big_file') |
| 83 | with open(source_file, 'w') as tf: |
| 84 | tf.write(2 * 1024 * 1024 * '.') |
| 85 | dest_link = os.path.join(jobdir, 'big_file') |
| 86 | os.link(source_file, dest_link) |
| 87 | |
| 88 | # da should _not_ count this file. Wait for 5s to get noticed |
| 89 | for i in range(0, 50): |
| 90 | if jobdir in executor_server.used: |
| 91 | break |
| 92 | time.sleep(0.1) |
Tobias Henkel | 8f01653 | 2017-08-01 07:42:44 +0200 | [diff] [blame] | 93 | self.assertEqual(set(), executor_server.stopped_jobs) |
| 94 | self.assertIn(jobdir, executor_server.used) |
| 95 | self.assertTrue(executor_server.used[jobdir] <= 1) |