| Total Complexity | 10 |
| Total Lines | 79 |
| Duplicated Lines | 18.99 % |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | from nose.tools import assert_true, assert_false, assert_equal, assert_raises |
||
| 12 | class TestJobGenerator(object): |
||
| 13 | |||
| 14 | def setUp(self): |
||
| 15 | self.testing_dir = tempfile.mkdtemp() |
||
| 16 | self.cluster_name = "skynet" |
||
| 17 | self.name = "9000@hal" |
||
| 18 | self.walltime = "10:00" |
||
| 19 | self.cores = 42 |
||
| 20 | self.gpus = 42 |
||
| 21 | self.mem_per_node = 32 |
||
| 22 | self.modules = ["cuda", "python"] |
||
| 23 | |||
| 24 | self.queue = Queue(self.name, self.cluster_name, self.walltime, self.cores, 0, self.mem_per_node, self.modules) |
||
| 25 | self.queue_gpu = Queue(self.name, self.cluster_name, self.walltime, self.cores, self.gpus, self.mem_per_node, self.modules) |
||
| 26 | |||
| 27 | self.commands = ["echo 1", "echo 2", "echo 3", "echo 4"] |
||
| 28 | |||
| 29 | def tearDown(self): |
||
| 30 | shutil.rmtree(self.testing_dir) |
||
| 31 | |||
| 32 | def test_generate_pbs(self): |
||
| 33 | job_generator = JobGenerator(self.queue, self.commands) |
||
| 34 | |||
| 35 | # Test nb_cores_per_command argument |
||
| 36 | # Should needs one PBS file |
||
| 37 | assert_equal(len(job_generator.pbs_list), 1) |
||
| 38 | assert_equal(job_generator.pbs_list[0].commands, self.commands) |
||
| 39 | |||
| 40 | View Code Duplication | def test_generate_pbs2_cpu(self): |
|
|
|
|||
| 41 | # Should needs two PBS file |
||
| 42 | command_params = {'nb_cores_per_command': self.cores // 2} |
||
| 43 | job_generator = JobGenerator(self.queue, self.commands, command_params) |
||
| 44 | assert_equal(len(job_generator.pbs_list), 2) |
||
| 45 | assert_equal(job_generator.pbs_list[0].commands, self.commands[:2]) |
||
| 46 | assert_equal(job_generator.pbs_list[1].commands, self.commands[2:]) |
||
| 47 | |||
| 48 | def test_generate_pbs4_cpu(self): |
||
| 49 | # Should needs four PBS file |
||
| 50 | command_params = {'nb_cores_per_command': self.cores} |
||
| 51 | job_generator = JobGenerator(self.queue, self.commands, command_params) |
||
| 52 | assert_equal(len(job_generator.pbs_list), 4) |
||
| 53 | assert_equal([pbs.commands[0] for pbs in job_generator.pbs_list], self.commands) |
||
| 54 | |||
| 55 | # Since queue has no gpus it should not be specified in PBS resource `nodes` |
||
| 56 | assert_true('gpus' not in job_generator.pbs_list[0].resources['nodes']) |
||
| 57 | |||
| 58 | # Test modules to load |
||
| 59 | # Check if needed modules for this queue are included in the PBS file |
||
| 60 | assert_equal(job_generator.pbs_list[0].modules, self.modules) |
||
| 61 | |||
| 62 | View Code Duplication | def test_generate_pbs2_gpu(self): |
|
| 63 | # Test nb_gpus_per_command argument |
||
| 64 | # Should needs two PBS file |
||
| 65 | command_params = {'nb_gpus_per_command': self.gpus // 2} |
||
| 66 | job_generator = JobGenerator(self.queue_gpu, self.commands, command_params) |
||
| 67 | assert_equal(len(job_generator.pbs_list), 2) |
||
| 68 | assert_equal(job_generator.pbs_list[0].commands, self.commands[:2]) |
||
| 69 | assert_equal(job_generator.pbs_list[1].commands, self.commands[2:]) |
||
| 70 | |||
| 71 | def test_generate_pbs4_gpu(self): |
||
| 72 | # Should needs four PBS files |
||
| 73 | command_params = {'nb_gpus_per_command': self.gpus} |
||
| 74 | job_generator = JobGenerator(self.queue_gpu, self.commands, command_params) |
||
| 75 | assert_equal(len(job_generator.pbs_list), 4) |
||
| 76 | assert_equal([pbs.commands[0] for pbs in job_generator.pbs_list], self.commands) |
||
| 77 | |||
| 78 | # Since queue has gpus it should be specified in PBS resource `nodes` |
||
| 79 | assert_true('gpus' in job_generator.pbs_list[0].resources['nodes']) |
||
| 80 | |||
| 81 | # Test modules to load |
||
| 82 | # Check if needed modules for this queue are included in the PBS file |
||
| 83 | assert_equal(job_generator.pbs_list[0].modules, self.modules) |
||
| 84 | |||
| 85 | def test_write_pbs_files(self): |
||
| 86 | commands = ["echo 1", "echo 2", "echo 3", "echo 4"] |
||
| 87 | command_params = {'nb_cores_per_command': self.cores} |
||
| 88 | job_generator = JobGenerator(self.queue, commands, command_params) |
||
| 89 | filenames = job_generator.write_pbs_files(self.testing_dir) |
||
| 90 | assert_equal(len(filenames), 4) |
||
| 91 | |||
| 241 |