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

Commit c1c0afa

Browse files
Michael GraueryuzhengZ
authored andcommitted
BUG: Refs #212. Fixed bug in chmod warning, and rewrite of tests.
There was a bug when a file was tested to see if it could be chmod-ed, a warning was issued if the file could not be chmoded and this broke json parsing. Now I've changed this to first check the UID of the process, and see if that is the same as the UID of the file, if not, then don't perform the chmod test and hence don't generate the warning. Also in this commit is the creation of the BatchmakeControllerTest superclass and changes in the batchmake tests to use this superclass. This superclass helps streamline batchmake testing, so the user doesn't have to set as many properties manually to test the batchmake module.
1 parent 97cb7b3 commit c1c0afa

File tree

4 files changed

+205
-78
lines changed

4 files changed

+205
-78
lines changed

modules/batchmake/controllers/components/KWBatchmakeComponent.php

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,11 @@ public function __construct($alternateConfig = null)
6767

6868
/**
6969
* helper function to load the correct config file
70-
* @param string $alternateConfig path to alternative config ini file
7170
* @return config array with config properties
7271
*/
73-
protected function loadConfig($alternateConfig = null)
72+
protected function loadConfig()
7473
{
75-
if($alternateConfig)
76-
{
77-
$config = parse_ini_file($alternateConfig, false);
78-
}
79-
elseif(file_exists(MIDAS_BATCHMAKE_MODULE_LOCAL_CONFIG))
74+
if(file_exists(MIDAS_BATCHMAKE_MODULE_LOCAL_CONFIG))
8075
{
8176
$config = parse_ini_file(MIDAS_BATCHMAKE_MODULE_LOCAL_CONFIG, false);
8277
}
@@ -92,25 +87,31 @@ protected function loadConfig($alternateConfig = null)
9287
* will load the configuration property values for this module, and filter
9388
* out only those properties that are in the 'batchmake.' config namespace,
9489
* removing the 'batchmake.' from the key name.
95-
* @param string $alternateConfig a path to an alternate config ini file
90+
* @param string $alternateConfig an array of alternate config props
9691
* @return array of batchmake module specific config properties
9792
*/
9893
public function loadConfigProperties($alternateConfig = null)
9994
{
100-
$configPropertiesParamVals = array();
101-
$rawConfig = $this->loadConfig($alternateConfig);
102-
103-
$modulePropertyNamespace = MIDAS_BATCHMAKE_MODULE . '.';
104-
foreach($rawConfig as $configProperty => $configPropertyVal)
95+
if(!isset($alternateConfig))
10596
{
106-
$ind = strpos($configProperty, $modulePropertyNamespace);
107-
if($ind !== false && $ind == 0)
97+
$configPropertiesParamVals = array();
98+
$rawConfig = $this->loadConfig();
99+
100+
$modulePropertyNamespace = MIDAS_BATCHMAKE_MODULE . '.';
101+
foreach($rawConfig as $configProperty => $configPropertyVal)
108102
{
109-
$reducedKey = substr($configProperty, strpos($configProperty, '.') + 1);
110-
$configPropertiesParamVals[$reducedKey] = $configPropertyVal;
103+
$ind = strpos($configProperty, $modulePropertyNamespace);
104+
if($ind !== false && $ind == 0)
105+
{
106+
$reducedKey = substr($configProperty, strpos($configProperty, '.') + 1);
107+
$configPropertiesParamVals[$reducedKey] = $configPropertyVal;
108+
}
111109
}
112110
}
113-
111+
else
112+
{
113+
$configPropertiesParamVals = $alternateConfig;
114+
}
114115
$this->componentConfig = $configPropertiesParamVals;
115116
$this->configScriptDir = $this->componentConfig[MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY];
116117
$this->configAppDir = $this->componentConfig[MIDAS_BATCHMAKE_APP_DIR_PROPERTY];
@@ -129,7 +130,7 @@ public function loadConfigProperties($alternateConfig = null)
129130
* @TODO from KWUtils, may need to be moved, but first tested
130131
* checks whether the file at the passed in path has the passed in options.
131132
*/
132-
protected function checkFileFlag($file, $options = 0x0)
133+
protected function checkFileFlag($file, $processUserUid, $options = 0x0)
133134
{
134135
$exist = file_exists($file);
135136
Zend_Loader::loadClass("InternationalizationComponent", BASE_PATH.'/core/controllers/components');
@@ -157,7 +158,7 @@ protected function checkFileFlag($file, $options = 0x0)
157158
}
158159
if(!KWUtils::isWindows() && $exist && ($options & MIDAS_BATCHMAKE_CHECK_IF_CHMODABLE))
159160
{
160-
$chmodable = $this->IsChmodable($file);
161+
$chmodable = $this->IsChmodable($file, $processUserUid);
161162
$status .= $chmodable ? " / Chmodable" : " / NotChmodable";
162163
$ret = $ret && $chmodable;
163164
}
@@ -172,7 +173,7 @@ protected function checkFileFlag($file, $options = 0x0)
172173
* Note: If return true, the mode of the file will be MIDAS_BATCHMAKE_DEFAULT_MKDIR_MODE
173174
* On windows, return always True
174175
*/
175-
protected function isChmodable($fileOrDirectory)
176+
protected function isChmodable($fileOrDirectory, $processUserUid)
176177
{
177178
if(KWUtils::isWindows())
178179
{
@@ -196,8 +197,20 @@ protected function isChmodable($fileOrDirectory)
196197

197198
if(is_writable($fileOrDirectory))
198199
{
199-
// Try to re-apply them
200-
$return = chmod($fileOrDirectory, $current_perms);
200+
// first check on the uid of the fileOrDirectory
201+
// if that is different than the processUserUid, then chmod
202+
// will return false and add a warning, so let's prevent this beforehand
203+
$fileStat = stat($fileOrDirectory);
204+
$fileUid = $fileStat['uid'];
205+
if($fileUid !== $processUserUid)
206+
{
207+
return false;
208+
}
209+
else
210+
{
211+
// Try to re-apply them
212+
$return = chmod($fileOrDirectory, $current_perms);
213+
}
201214
}
202215
else
203216
{
@@ -227,13 +240,48 @@ public function testconfig($alternateConfigValues = null)
227240
$configToTest = $this->componentConfig;
228241
}
229242

243+
Zend_Loader::loadClass("InternationalizationComponent", BASE_PATH.'/core/controllers/components');
244+
245+
// Process web server user information
246+
247+
// TODO what should be done if there are warnings??
248+
$processUser = posix_getpwuid(posix_geteuid());
249+
$processUserUid = $processUser['uid'];
250+
$processGroup = posix_getgrgid(posix_geteuid());
251+
252+
$phpProcessString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_STRING);
253+
$phpProcessUserString = $phpProcessString . ' ' . InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_USER_STRING);
254+
$phpProcessNameString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING);
255+
$phpProcessGroupString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_GROUP_STRING);
256+
$phpProcessHomeString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_HOME_STRING);
257+
$phpProcessShellString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING);
258+
$unknownString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_UNKNOWN_STRING);
259+
260+
$phpProcessUserNameString = $phpProcessUserString . '[' . $phpProcessNameString . ']';
261+
$phpProcessUserGroupString = $phpProcessUserString . '[' . $phpProcessGroupString . ']';
262+
$phpProcessUserHomeString = $phpProcessUserString . '[' . $phpProcessHomeString . ']';
263+
$phpProcessUserShellString = $phpProcessUserString . '[' . $phpProcessShellString . ']';
264+
265+
$processProperties = array($phpProcessUserNameString => !empty($processUser[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING]) ? $processUser[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING] : "",
266+
$phpProcessUserGroupString => !empty($processGroup[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING]) ? $processGroup[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING] : "",
267+
$phpProcessUserHomeString => !empty($processUser[MIDAS_BATCHMAKE_DIR_KEY]) ? $processUser[MIDAS_BATCHMAKE_DIR_KEY] : "",
268+
$phpProcessUserShellString => !empty($processUser[MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING]) ? $processUser[MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING] : "");
269+
270+
foreach($processProperties as $property => $value)
271+
{
272+
$status = !empty($value);
273+
$configStatus[] = array(MIDAS_BATCHMAKE_PROPERTY_KEY => $property,
274+
MIDAS_BATCHMAKE_STATUS_KEY => $status ? $value : $unknownString,
275+
MIDAS_BATCHMAKE_TYPE_KEY => $status ? MIDAS_BATCHMAKE_STATUS_TYPE_INFO : MIDAS_BATCHMAKE_STATUS_TYPE_WARNING);
276+
}
277+
230278
foreach(self::$configPropertiesRequirements as $configProperty => $configPropertyRequirement)
231279
{
232280
$configPropertyVal = $configToTest[$configProperty];
233281
if($configPropertyVal)
234282
{
235283
// if the property exists, check its configuration
236-
list($result, $status) = $this->checkFileFlag($configPropertyVal, $configPropertyRequirement);
284+
list($result, $status) = $this->checkFileFlag($configPropertyVal, $processUserUid, $configPropertyRequirement);
237285
$configStatus[] = array(MIDAS_BATCHMAKE_PROPERTY_KEY => $configProperty, MIDAS_BATCHMAKE_STATUS_KEY => $status, MIDAS_BATCHMAKE_TYPE_KEY => $result ? MIDAS_BATCHMAKE_STATUS_TYPE_INFO : MIDAS_BATCHMAKE_STATUS_TYPE_ERROR);
238286
// the property is in error, therefore so is the global config
239287
if(!$result)
@@ -255,8 +303,6 @@ public function testconfig($alternateConfigValues = null)
255303
{
256304
$appPath = $configToTest[$pathProperty] ."/" . KWUtils::formatAppName($app);
257305
list($result, $status) = $this->checkFileFlag($appPath, MIDAS_BATCHMAKE_CHECK_IF_EXECUTABLE);
258-
Zend_Loader::loadClass("InternationalizationComponent", BASE_PATH.'/core/controllers/components');
259-
260306
$applicationString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_APPLICATION_STRING);
261307
$configStatus[] = array(MIDAS_BATCHMAKE_PROPERTY_KEY => $applicationString . ' ' .$appPath, MIDAS_BATCHMAKE_STATUS_KEY => $status, MIDAS_BATCHMAKE_TYPE_KEY => $result ? MIDAS_BATCHMAKE_STATUS_TYPE_INFO : MIDAS_BATCHMAKE_STATUS_TYPE_ERROR);
262308
// the property is in error, therefore so is the global config
@@ -266,37 +312,6 @@ public function testconfig($alternateConfigValues = null)
266312
}
267313
}
268314

269-
// Process web server user information
270-
271-
// TODO what should be done if there are warnings??
272-
$processUser = posix_getpwuid(posix_geteuid());
273-
$processGroup = posix_getgrgid(posix_geteuid());
274-
275-
$phpProcessString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_STRING);
276-
$phpProcessUserString = $phpProcessString . ' ' . InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_USER_STRING);
277-
$phpProcessNameString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING);
278-
$phpProcessGroupString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_GROUP_STRING);
279-
$phpProcessHomeString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_HOME_STRING);
280-
$phpProcessShellString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING);
281-
$unknownString = InternationalizationComponent::translate(MIDAS_BATCHMAKE_UNKNOWN_STRING);
282-
283-
$phpProcessUserNameString = $phpProcessUserString . '[' . $phpProcessNameString . ']';
284-
$phpProcessUserGroupString = $phpProcessUserString . '[' . $phpProcessGroupString . ']';
285-
$phpProcessUserHomeString = $phpProcessUserString . '[' . $phpProcessHomeString . ']';
286-
$phpProcessUserShellString = $phpProcessUserString . '[' . $phpProcessShellString . ']';
287-
288-
$processProperties = array($phpProcessUserNameString => !empty($processUser[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING]) ? $processUser[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING] : "",
289-
$phpProcessUserGroupString => !empty($processGroup[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING]) ? $processGroup[MIDAS_BATCHMAKE_PHP_PROCESS_NAME_STRING] : "",
290-
$phpProcessUserHomeString => !empty($processUser[MIDAS_BATCHMAKE_DIR_KEY]) ? $processUser[MIDAS_BATCHMAKE_DIR_KEY] : "",
291-
$phpProcessUserShellString => !empty($processUser[MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING]) ? $processUser[MIDAS_BATCHMAKE_PHP_PROCESS_SHELL_STRING] : "");
292-
293-
foreach($processProperties as $property => $value)
294-
{
295-
$status = !empty($value);
296-
$configStatus[] = array(MIDAS_BATCHMAKE_PROPERTY_KEY => $property,
297-
MIDAS_BATCHMAKE_STATUS_KEY => $status ? $value : $unknownString,
298-
MIDAS_BATCHMAKE_TYPE_KEY => $status ? MIDAS_BATCHMAKE_STATUS_TYPE_INFO : MIDAS_BATCHMAKE_STATUS_TYPE_WARNING);
299-
}
300315

301316
return array($total_config_correct, $configStatus);
302317

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
13+
// need to include the module constant for this test
14+
require_once BASE_PATH.'/modules/batchmake/constant/module.php';
15+
require_once BASE_PATH.'/library/KWUtils.php';
16+
17+
/** helper class used for testing batchmake module */
18+
class BatchmakeControllerTest extends ControllerTestCase
19+
{
20+
21+
/**
22+
* helper function to return Midas configured temp directory
23+
* @return midas temp dir
24+
*/
25+
protected function getTempDirectory()
26+
{
27+
include_once BASE_PATH.'/core/GlobalController.php';
28+
$controller = new MIDAS_GlobalController($this->request, $this->response);
29+
return $controller->getTempDirectory();
30+
}
31+
32+
/**
33+
* function will create a temporary batchmake config, copying over test data
34+
* to the the locations in that config needed for the tests, returning an
35+
* array of config property names to directory locations.
36+
* @TODO figure out a way to copy over Batchmake or else mock it
37+
* @return string
38+
*/
39+
public function setupAndGetConfig()
40+
{
41+
// create a test batchmake setup in the temp dir
42+
// and initialize test data
43+
$tmpDir = $this->getTempDirectory() .'/';
44+
$subDirs = array("batchmake", "tests");
45+
$testDir = KWUtils::createSubDirectories($tmpDir, $subDirs);
46+
$configProps = array(MIDAS_BATCHMAKE_TMP_DIR_PROPERTY => $tmpDir.'/batchmake/tests/tmp',
47+
MIDAS_BATCHMAKE_BIN_DIR_PROPERTY => $tmpDir.'/batchmake/tests/bin',
48+
MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => $tmpDir.'/batchmake/tests/script',
49+
MIDAS_BATCHMAKE_APP_DIR_PROPERTY => $tmpDir.'/batchmake/tests/bin',
50+
MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => $tmpDir.'/batchmake/tests/data',
51+
MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY => '/home/condor/bin');//change this with mocks??
52+
// now make sure these dirs exist
53+
// later can actually add some stuff to these dirs
54+
foreach($configProps as $prop => $dir)
55+
{
56+
if(!file_exists($dir) && !KWUtils::mkDir($dir))
57+
{
58+
throw new Zend_Exception("couldn't create dir ".$dir);
59+
}
60+
}
61+
62+
// now copy over the bms files
63+
$srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/script';
64+
$targetDir = $configProps[MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY];
65+
$extension = '.bms';
66+
$this->symlinkFileset($srcDir, $targetDir, $extension);
67+
68+
// and now the bmms
69+
$srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/bin';
70+
$targetDir = $configProps[MIDAS_BATCHMAKE_APP_DIR_PROPERTY];
71+
$extension = '.bmm';
72+
$this->symlinkFileset($srcDir, $targetDir, $extension);
73+
74+
return $configProps;
75+
}
76+
77+
/**
78+
* looks in the srcDir, finds all files ending with $extension, and
79+
* symlinks them to targetDir if there isn't already a file there
80+
* by that name
81+
* @param type $srcDir
82+
* @param type $targetDir
83+
* @param type $extension
84+
*/
85+
protected function symlinkFileset($srcDir, $targetDir, $extension)
86+
{
87+
// open the directory
88+
$handle = opendir($srcDir);
89+
if(!is_readable($srcDir))
90+
{
91+
throw new Zend_Exception("can't read ".$srcDir);
92+
}
93+
// and scan through the items inside
94+
while(false !== ($item = readdir($handle)))
95+
{
96+
// make sure item matches extendsion
97+
if((strpos($item, $extension) == strlen($item) - 4))
98+
{
99+
// link the file if it isn't already there
100+
$scriptTarget = $srcDir . '/' . $item;
101+
$scriptLink = $targetDir . '/' . $item;
102+
if(!file_exists($scriptLink) && !symlink($scriptTarget, $scriptLink))
103+
{
104+
throw new Zend_Exception($scriptTarget . ' could not be sym-linked to ' . $scriptLink);
105+
}
106+
}
107+
}
108+
closedir($handle);
109+
}
110+
111+
}

modules/batchmake/tests/controllers/ConfigControllerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
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/tests/controllers/BatchmakeControllerTest.php';
1516

1617
/** config controller tests*/
17-
class ConfigControllerTest extends ControllerTestCase
18+
class ConfigControllerTest extends BatchmakeControllerTest
1819
{
1920

2021
protected $kwBatchmakeComponent;
@@ -25,12 +26,13 @@ public function setUp()
2526
{
2627
$this->setupDatabase(array('default'));
2728
$this->_models = array('User');
28-
//$this->_daos = array('User');//
29-
//$this->_moduleModels = array('Task');//
3029
$this->enabledModules = array('batchmake');
31-
require_once BASE_PATH.'/modules/batchmake/controllers/components/KWBatchmakeComponent.php';
32-
$this->kwBatchmakeComponent = new Batchmake_KWBatchmakeComponent(BASE_PATH.'/modules/batchmake/tests/configs/module.local.ini');
3330
parent::setUp();
31+
if(!isset($this->kwBatchmakeComponent))
32+
{
33+
require_once BASE_PATH.'/modules/batchmake/controllers/components/KWBatchmakeComponent.php';
34+
$this->kwBatchmakeComponent = new Batchmake_KWBatchmakeComponent($this->setupAndGetConfig());
35+
}
3436
}
3537

3638

@@ -49,10 +51,8 @@ public function testIndexAction()
4951
$this->fail('Unable to find body element');
5052
}
5153

52-
5354
$this->assertQuery("form#configForm");
54-
$applicationConfig = $this->kwBatchmakeComponent->loadConfigProperties(BASE_PATH.'/modules/batchmake/tests/configs/module.local.ini');
55-
// change a value to something bad
55+
$applicationConfig = $this->setupAndGetConfig();
5656
$this->params = array();
5757
$this->params[MIDAS_BATCHMAKE_TMP_DIR_PROPERTY] = $applicationConfig[MIDAS_BATCHMAKE_TMP_DIR_PROPERTY];
5858
$this->params[MIDAS_BATCHMAKE_BIN_DIR_PROPERTY] = $applicationConfig[MIDAS_BATCHMAKE_BIN_DIR_PROPERTY];

0 commit comments

Comments
 (0)