Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit f305733

Browse files
Michael GraueryuzhengZ
authored andcommitted
BUG: Refs #212. Additional testing fixes, including move to Mock Objects.
Removed many redundant and unneeded testing files. Changed the batchmake module tests around to use Mock Objects that simulate BatchMake executable and condor_submit_dag executable, so these tests can be run on systems without BatchMake and condor installed. Adding in the Mock Objects also allowed more complete testing of the KWBatchmakeComponent.
1 parent ca564ba commit f305733

33 files changed

+527
-298
lines changed

modules/batchmake/controllers/components/KWBatchmakeComponent.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Batchmake_KWBatchmakeComponent extends AppComponent
3030
* accessor functin to return the names of the config propeties, and
3131
* their filesystem requirements;
3232
*/
33-
public function getConfigPropertiesRequirements()
33+
public static function getConfigPropertiesRequirements()
3434
{
3535
return self::$configPropertiesRequirements;
3636
}
@@ -42,6 +42,14 @@ public function getConfigPropertiesRequirements()
4242
MIDAS_BATCHMAKE_CONDOR_SUBMIT_DAG => MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY,
4343
MIDAS_BATCHMAKE_EXE => MIDAS_BATCHMAKE_BIN_DIR_PROPERTY);
4444

45+
/**
46+
* accessor functin to return the application names, along with their
47+
* expected location property.
48+
*/
49+
public static function getApplicationsPaths()
50+
{
51+
return self::$applicationsPaths;
52+
}
4553

4654

4755
// component configuration settings
@@ -54,14 +62,26 @@ public function getConfigPropertiesRequirements()
5462
protected $configDataDir;
5563
protected $configCondorBinDir;
5664

65+
66+
protected $executor;
67+
5768
/**
5869
* Constructor, loads ini from standard config location, unless a
5970
* supplied alternateConfig.
6071
* @param string $alternateConfig path to alternative config ini file
6172
*/
62-
public function __construct($alternateConfig = null)
73+
public function __construct($alternateConfig = null, $executor = null)
6374
{
6475
$this->loadConfigProperties($alternateConfig);
76+
if(!isset($executor))
77+
{
78+
require_once BASE_PATH.'/modules/batchmake/library/Executor.php';
79+
$this->executor = new Batchmake_Executor();
80+
}
81+
else
82+
{
83+
$this->executor = $executor;
84+
}
6585
} // end __construct($alternateConfig)
6686

6787

@@ -521,11 +541,11 @@ public function compileBatchMakeScript($workDir, $bmScript)
521541
}
522542

523543
// Run command
524-
KWUtils::exec($cmd, $output, $workDir, $returnVal);
544+
$this->executor->exec($cmd, $output, $workDir, $returnVal);
525545

526546
if($returnVal !== 0)
527547
{
528-
throw new Zend_Exception("Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
548+
throw new Zend_Exception("compileBatchMakeScript: Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
529549
}
530550

531551
// if BatchMake reports errors, throw an exception
@@ -541,12 +561,12 @@ public function compileBatchMakeScript($workDir, $bmScript)
541561
}
542562
else
543563
{
544-
throw new Zend_Exception("Compiling script [".$bmScript."] yielded output: [".implode(",", $output )."]");
564+
throw new Zend_Exception("compileBatchMakeScript: Compiling script [".$bmScript."] yielded output: [".implode(",", $output )."]");
545565
}
546566
}
547567
}
548568

549-
throw new Zend_Exception("Error in BatchMake script, the compile step didn't report errors, output: [".implode(",", $output )."]");
569+
throw new Zend_Exception("compileBatchMakeScript: Error in BatchMake script, the compile step didn't report errors, output: [".implode(",", $output )."]");
550570
}
551571

552572

@@ -559,7 +579,15 @@ public function compileBatchMakeScript($workDir, $bmScript)
559579
*/
560580
public function generateCondorDag($workDir, $bmScript)
561581
{
562-
$dagName = $bmScript.'.dagjob';
582+
// remove .bms if it exists, for the creation of the dagjob
583+
if(substr($bmScript, strlen($bmScript) - strlen('.bms') === '.bms'))
584+
{
585+
$dagName = substr($bmScript, 0, strlen($bmScript)-4) . '.dagjob';
586+
}
587+
else
588+
{
589+
$dagName = $bmScript.'.dagjob';
590+
}
563591

564592
// Prepare command
565593
$params = array(
@@ -571,11 +599,11 @@ public function generateCondorDag($workDir, $bmScript)
571599
$cmd = KWUtils::prepareExecCommand($this->configBinDir . '/'. MIDAS_BATCHMAKE_EXE, $params);
572600

573601
// Run command
574-
KWUtils::exec($cmd, $output, $workDir, $returnVal);
602+
$this->executor->exec($cmd, $output, $workDir, $returnVal);
575603

576604
if($returnVal !== 0)
577605
{
578-
throw new Zend_Exception("Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
606+
throw new Zend_Exception("generateCondorDag: Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
579607
}
580608
return $dagName;
581609
}
@@ -594,11 +622,11 @@ public function condorSubmitDag($workDir, $dagScript)
594622
$cmd = KWUtils::prepareExecCommand($this->configCondorBinDir . '/'. MIDAS_BATCHMAKE_CONDOR_SUBMIT_DAG, $params);
595623

596624
// Run command
597-
KWUtils::exec($cmd, $output, $workDir, $returnVal);
625+
$this->executor->exec($cmd, $output, $workDir, $returnVal);
598626

599627
if($returnVal !== 0)
600628
{
601-
throw new Zend_Exception("Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
629+
throw new Zend_Exception("condorSubmitDag: Failed to run: [".$cmd."], output: [".implode(",", $output )."]");
602630
}
603631
}
604632

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
include_once BASE_PATH . '/library/KWUtils.php';
13+
class Batchmake_Executor
14+
{
15+
public function exec($command, &$output = null, $chdir = "", &$return_val = null)
16+
{
17+
KWUtils::exec($command, $output, $chdir, $return_val);
18+
}
19+
} // end class
20+
21+
22+

modules/batchmake/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ option( MIDAS_RUN_BATCHMAKE_TESTS "Should MIDAS run BatchMake tests? Specific c
22
if(MIDAS_RUN_BATCHMAKE_TESTS)
33
add_subdirectory( controllers )
44
add_midas_style_test( StyleTestsBatchmakeControllers ${CMAKE_SOURCE_DIR}/modules/batchmake/tests/controllers )
5+
add_midas_style_test( StyleTestsBatchmakeLibrary ${CMAKE_SOURCE_DIR}/modules/batchmake/tests/library )
56
endif()
67

modules/batchmake/tests/controllers/BatchmakeControllerTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
// need to include the module constant for this test
1414
require_once BASE_PATH.'/modules/batchmake/constant/module.php';
15+
require_once BASE_PATH.'/modules/batchmake/controllers/components/KWBatchmakeComponent.php';
1516
require_once BASE_PATH.'/library/KWUtils.php';
1617

1718
/** helper class used for testing batchmake module */
@@ -48,7 +49,7 @@ public function setupAndGetConfig()
4849
MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => $tmpDir.'/batchmake/tests/script',
4950
MIDAS_BATCHMAKE_APP_DIR_PROPERTY => $tmpDir.'/batchmake/tests/bin',
5051
MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => $tmpDir.'/batchmake/tests/data',
51-
MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY => '/home/condor/bin');//change this with mocks??
52+
MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY => $tmpDir.'/batchmake/tests/condorbin');//change this with mocks??
5253
// now make sure these dirs exist
5354
// later can actually add some stuff to these dirs
5455
foreach($configProps as $prop => $dir)
@@ -71,6 +72,36 @@ public function setupAndGetConfig()
7172
$extension = '.bmm';
7273
$this->symlinkFileset($srcDir, $targetDir, $extension);
7374

75+
76+
// the mock object strategy requires both an interface and for
77+
// executable files to exist on disk in a particular location,
78+
// so here we will create symlinks to a known executable
79+
// ls
80+
// which should be on most systems
81+
82+
$params = array('ls');
83+
$cmd = KWUtils::prepareExeccommand('which', $params);
84+
// dir doesn't matter, just send in srcDir as it is convenient
85+
KWUtils::exec($cmd, $output, $srcDir, $returnVal);
86+
if($returnVal !== 0 || !isset($output) || !isset($output[0]))
87+
{
88+
throw new Zend_Exception('Problem finding ls on your system, used for testing');
89+
}
90+
$pathToLs = $output[0];
91+
92+
// get the applications and their path properties from the component that
93+
// expects them
94+
$applicationsPaths = Batchmake_KWBatchmakeComponent::getApplicationsPaths();
95+
foreach($applicationsPaths as $application => $pathProperty)
96+
{
97+
// now in the place of each executable, symlink the ls exe
98+
$link = $configProps[$pathProperty] . '/' . $application;
99+
if(!file_exists($link) && !symlink($pathToLs, $link))
100+
{
101+
throw new Zend_Exception($pathToLs . ' could not be sym-linked to ' . $link);
102+
}
103+
}
104+
74105
return $configProps;
75106
}
76107

modules/batchmake/tests/controllers/components/KWBatchmakeComponentTest.php

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function setUp()
3434
if(!isset($this->kwBatchmakeComponent))
3535
{
3636
require_once BASE_PATH.'/modules/batchmake/controllers/components/KWBatchmakeComponent.php';
37-
$this->kwBatchmakeComponent = new Batchmake_KWBatchmakeComponent($this->setupAndGetConfig());
37+
require_once BASE_PATH.'/modules/batchmake/tests/library/ExecutorMock.php';
38+
$executor = new Batchmake_ExecutorMock();
39+
$this->kwBatchmakeComponent = new Batchmake_KWBatchmakeComponent($this->setupAndGetConfig(), $executor);
40+
// $this->kwBatchmakeComponent = new Batchmake_KWBatchmakeComponent($this->setupAndGetConfig());
3841
}
3942
}
4043

@@ -61,7 +64,7 @@ public function testGetBatchmakeScripts()
6164
{
6265
$foundTestScripts = $this->kwBatchmakeComponent->getBatchmakeScripts();
6366
sort($foundTestScripts);
64-
$expectedTestScripts = array("Compiles.bms", "Myscript2.bms", "noscripts.bms",
67+
$expectedTestScripts = array("CompileEmptyOutput.bms", "CompileReturnNonzero.bms", "Compiles.bms", "Myscript2.bms", "noscripts.bms",
6568
"anotherscript.bms", "anotherscriptwitherrors.bms", "bmmswitherrors.bms",
6669
"cycle1.bms", "cycle31.bms", "cycle32.bms", "cycle33.bms", "nocycle1.bms",
6770
"nocycle2.bms", "nocycle3.bms", "myscript.bms", "PixelCounter.bms",
@@ -232,9 +235,6 @@ public function testPreparePipelineBmms()
232235

233236

234237

235-
236-
237-
238238
/** tests testCompileBatchMakeScript */
239239
public function testCompileBatchMakeScript()
240240
{
@@ -250,7 +250,7 @@ public function testCompileBatchMakeScript()
250250
// this one should work
251251
$this->kwBatchmakeComponent->compileBatchMakeScript($workDir, $scriptName);
252252

253-
// now try a script that doesn't compile
253+
// now try a script that doesn't compile but returns errors
254254
$scriptName = 'CompileErrors.bms';
255255
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
256256
$bmms = $this->kwBatchmakeComponent->preparePipelineBmms($workDir, $bmScripts);
@@ -262,7 +262,41 @@ public function testCompileBatchMakeScript()
262262
catch(Zend_Exception $ze)
263263
{
264264
// if we got here, this is the correct behavior
265-
$this->assertTrue(true);
265+
// check that the exception came from the right place
266+
$this->assertEquals(1, preg_match('/compileBatchMakeScript/', $ze->getMessage()));
267+
}
268+
269+
// now try a script that returns a non-zero value from the compile step
270+
$scriptName = 'CompileReturnNonzero.bms';
271+
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
272+
$bmms = $this->kwBatchmakeComponent->preparePipelineBmms($workDir, $bmScripts);
273+
try
274+
{
275+
$this->kwBatchmakeComponent->compileBatchMakeScript($workDir, $scriptName);
276+
$this->fail('Should have had a compile error but did not, testCompileBatchMakeScript');
277+
}
278+
catch(Zend_Exception $ze)
279+
{
280+
// if we got here, this is the correct behavior
281+
// check that the exception came from the right place
282+
$this->assertEquals(1, preg_match('/compileBatchMakeScript/', $ze->getMessage()));
283+
}
284+
285+
286+
//a script that returns a zero value but an empty output from the compile step
287+
$scriptName = 'CompileEmptyOutput.bms';
288+
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
289+
$bmms = $this->kwBatchmakeComponent->preparePipelineBmms($workDir, $bmScripts);
290+
try
291+
{
292+
$this->kwBatchmakeComponent->compileBatchMakeScript($workDir, $scriptName);
293+
$this->fail('Should have had a compile error but did not, testCompileBatchMakeScript');
294+
}
295+
catch(Zend_Exception $ze)
296+
{
297+
// if we got here, this is the correct behavior
298+
// check that the exception came from the right place
299+
$this->assertEquals(1, preg_match('/compileBatchMakeScript/', $ze->getMessage()));
266300
}
267301

268302
}
@@ -274,27 +308,36 @@ public function testGenerateCondorDag()
274308
$userDao = $this->User->load($usersFile[0]->getKey());
275309
$workDir = $this->kwBatchmakeComponent->createTask($userDao);
276310

311+
// try a script that returns a non-zero value
312+
$scriptName = 'CompileReturnNonzero.bms';
313+
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
314+
$bmms = $this->kwBatchmakeComponent->preparePipelineBmms($workDir, $bmScripts);
315+
try
316+
{
317+
$dagJobFile = $this->kwBatchmakeComponent->generateCondorDag($workDir, $scriptName);
318+
$this->fail('Should have had an error but did not, testGenerateCondorDag');
319+
}
320+
catch(Zend_Exception $ze)
321+
{
322+
// if we got here, this is the correct behavior
323+
// check that the exception came from the right place
324+
$this->assertEquals(1, preg_match('/generateCondorDag/', $ze->getMessage()));
325+
}
326+
277327
// a script that compiles
278328
$scriptName = 'Compiles.bms';
279329
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
280330
$bmms = $this->kwBatchmakeComponent->preparePipelineBmms($workDir, $bmScripts);
281331

282332
// try to generate the Condor script
283333
$dagJobFile = $this->kwBatchmakeComponent->generateCondorDag($workDir, $scriptName);
284-
$this->assertEquals($dagJobFile, 'Compiles.bms.dagjob');
334+
$this->assertEquals($dagJobFile, 'Compiles.dagjob');
285335
// check that dag files and condor job files were created
286-
$condorFiles = array($dagJobFile, 'Compiles.1.bms.dagjob', 'Compiles.3.bms.dagjob', 'Compiles.5.bms.dagjob');
336+
$condorFiles = array($dagJobFile, 'Compiles.1.dagjob', 'Compiles.3.dagjob', 'Compiles.5.dagjob');
287337
foreach($condorFiles as $condorFile)
288338
{
289339
$this->assertFileExists($workDir.'/'.$condorFile);
290340
}
291-
// now look for some specific strings
292-
$contents = file_get_contents($workDir.'/'. 'Compiles.bms.dagjob');
293-
$dagjobStrings = array('Job job3', 'Job job5', 'PARENT job1 CHILD job3', 'PARENT job3 CHILD job5');
294-
foreach($dagjobStrings as $string)
295-
{
296-
$this->assertTrue(preg_match("/".$string."/", $contents, $matches) === 1);
297-
}
298341
}
299342

300343

@@ -305,6 +348,20 @@ public function testCondorSubmitDag()
305348
$userDao = $this->User->load($usersFile[0]->getKey());
306349
$workDir = $this->kwBatchmakeComponent->createTask($userDao);
307350

351+
// try a script that returns a non-zero value
352+
$scriptName = 'CompileReturnNonzero.dagjob';
353+
try
354+
{
355+
$this->kwBatchmakeComponent->condorSubmitDag($workDir, $scriptName);
356+
$this->fail('Should have had an error but did not, testCondorSubmitDag');
357+
}
358+
catch(Zend_Exception $ze)
359+
{
360+
// if we got here, this is the correct behavior
361+
// check that the exception came from the right place
362+
$this->assertEquals(1, preg_match('/condorSubmitDag/', $ze->getMessage()));
363+
}
364+
308365
// a script that compiles
309366
$scriptName = 'Compiles.bms';
310367
$bmScripts = $this->kwBatchmakeComponent->preparePipelineScripts($workDir, $scriptName);
@@ -313,6 +370,9 @@ public function testCondorSubmitDag()
313370
$dagScript = $this->kwBatchmakeComponent->generateCondorDag($workDir, $scriptName);
314371
$this->kwBatchmakeComponent->condorSubmitDag($workDir, $dagScript);
315372
// how to check this now?
373+
// TODO think about this some more
374+
// perhaps send something back through the web-api
375+
// what would that be testing exactly?
316376
}
317377

318378

0 commit comments

Comments
 (0)