/** tests mkDir function */ public function testMkDir() { $tmpDir = UtilityComponent::getTempDirectory(); $testDir = $tmpDir . '/' . 'KWUtilsTest'; $this->assertTrue(KWUtils::mkDir($testDir)); // now clean up KWUtils::recursiveRemoveDirectory($testDir); }
/** * exports a list of itemIds to a work dir. * * @param UserDao $userDao * @param Batchmake_TaskDao $taskDao * @param array $itemIds * @throws Zend_Exception */ public function exportItemsToWorkDataDir($userDao, $taskDao, $itemIds) { // export the items to the work dir data dir $datapath = $taskDao->getWorkDir() . '/' . 'data'; if (!KWUtils::mkDir($datapath)) { throw new Zend_Exception("couldn't create data export dir: " . $datapath); } /** @var ExportComponent $exportComponent */ $exportComponent = MidasLoader::loadComponent('Export'); $symlink = true; $exportComponent->exportBitstreams($userDao, $datapath, $itemIds, $symlink); }
/** * function will create a temporary batchmake config, copying over test data * to the the locations in that config needed for the tests, returning an * array of config property names to directory locations. * * @todo figure out a way to copy over Batchmake or else mock it * @return array * @throws Zend_Exception */ public function setupAndGetConfig() { // create a test batchmake setup in the temp dir // and initialize test data $tmpDir = $this->getTempDirectory(); $subDirs = array('batchmake', 'tests'); KWUtils::createSubDirectories($tmpDir . '/', $subDirs); $configProps = array(MIDAS_BATCHMAKE_TMP_DIR_PROPERTY => $tmpDir . '/batchmake/tests/tmp', MIDAS_BATCHMAKE_BIN_DIR_PROPERTY => $tmpDir . '/batchmake/tests/bin', MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => $tmpDir . '/batchmake/tests/script', MIDAS_BATCHMAKE_APP_DIR_PROPERTY => $tmpDir . '/batchmake/tests/bin', MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => $tmpDir . '/batchmake/tests/data', MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY => $tmpDir . '/batchmake/tests/condorbin'); // now make sure these dirs exist // later can actually add some stuff to these dirs foreach ($configProps as $dir) { if (!file_exists($dir) && !KWUtils::mkDir($dir)) { throw new Zend_Exception("couldn't create dir " . $dir); } } // now copy over the bms files $srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/script'; $targetDir = $configProps[MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY]; $extension = '.bms'; $this->symlinkFileset($srcDir, $targetDir, $extension); // and now the bmms $srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/bin'; $targetDir = $configProps[MIDAS_BATCHMAKE_APP_DIR_PROPERTY]; $extension = '.bmm'; $this->symlinkFileset($srcDir, $targetDir, $extension); // the mock object strategy requires both an interface and for // executable files to exist on disk in a particular location, // so here we will create symlinks to a known executable // ls // which should be on most systems $params = array('ls'); $cmd = KWUtils::prepareExeccommand('which', $params); // dir doesn't matter, just send in srcDir as it is convenient KWUtils::exec($cmd, $output, $srcDir, $returnVal); if ($returnVal !== 0 || !isset($output) || !isset($output[0])) { throw new Zend_Exception('Problem finding ls on your system, used for testing'); } $pathToLs = $output[0]; // get the applications and their path properties from the component that // expects them $applicationsPaths = Batchmake_KWBatchmakeComponent::getApplicationsPaths(); foreach ($applicationsPaths as $application => $pathProperty) { // now in the place of each executable, symlink the ls exe $link = $configProps[$pathProperty] . '/' . $application; if (!file_exists($link) && !symlink($pathToLs, $link)) { throw new Zend_Exception($pathToLs . ' could not be sym-linked to ' . $link); } } return $configProps; }
/** Test that TaskModel::createTask($userDao) works */ public function testCreateTask() { $usersFile = $this->loadData('User', 'default', '', 'batchmake'); /** @var Batchmake_TaskModel $taskModel */ $taskModel = MidasLoader::loadModel('Task', 'batchmake'); $user1Dao = $usersFile[0]; $tmpWorkDirRoot = $this->getTempDirectory() . '/' . 'test'; KWUtils::mkDir($tmpWorkDirRoot); $task1Dao = $taskModel->createTask($user1Dao, $tmpWorkDirRoot); $this->assertNotEmpty($task1Dao); $this->assertTrue($task1Dao instanceof Batchmake_TaskDao); $userId1 = $task1Dao->getUserId(); $this->assertTrue(is_numeric($userId1)); $this->assertFalse(is_object($userId1)); $this->assertEquals($userId1, $user1Dao->getUserId()); $taskId1 = $task1Dao->getKey(); $this->assertTrue(is_numeric($taskId1)); $this->assertFalse(is_object($taskId1)); // now try a different user $user2Dao = $usersFile[1]; $task2Dao = $taskModel->createTask($user2Dao, $tmpWorkDirRoot); $this->assertNotEmpty($task2Dao); $this->assertTrue($task2Dao instanceof Batchmake_TaskDao); $userId2 = $task2Dao->getUserId(); $this->assertTrue(is_numeric($userId2)); $this->assertFalse(is_object($userId2)); $this->assertEquals($userId2, $user2Dao->getUserId()); $taskId2 = $task2Dao->getKey(); $this->assertTrue(is_numeric($taskId2)); $this->assertFalse(is_object($taskId2)); // make sure each of the tasks got a different id $this->assertNotEquals($taskId1, $taskId2); // now try to retrieve it by key $task3Dao = $taskModel->load($taskId1); $this->assertTrue($taskModel->compareDao($task1Dao, $task3Dao)); $task4Dao = $taskModel->load($taskId2); $this->assertTrue($taskModel->compareDao($task2Dao, $task4Dao)); }
/** * Stop DICOM server. * * @path /dicomserver/server/stop * @http POST * @param storescp_cmd (Optional) The command to run storescp * @param dcmqrscp_cmd (Optional) The command to run dcmqrscp * @param incoming_dir (Optional) The incoming directory to receive and process DICOM files * @param get_command (Optional) If set, will not stop DICOM server, but only get command used to stop DICOM server in command line. * @return */ public function stop($args) { // Only administrator can call this api $userDao = Zend_Registry::get('userSession')->Dao; if (!$userDao || !$userDao->isAdmin()) { throw new Exception('Only administrator can stop DICOM server', MIDAS_INVALID_POLICY); } $ret = array(); $status_args = array(); if (!empty($args['storescp_cmd'])) { $status_args['storescp_cmd'] = $args['storescp_cmd']; } if (!empty($args['dcmqrscp_cmd'])) { $status_args['dcmqrscp_cmd'] = $args['dcmqrscp_cmd']; } $running_status = $this->status($status_args); if ($running_status['status'] == MIDAS_DICOMSERVER_SERVER_NOT_RUNNING && !array_key_exists('get_command', $args)) { $ret['message'] = 'DICOM server is not running now!'; return $ret; } $storescp_cmd = 'storescp'; if (!empty($args['storescp_cmd'])) { $storescp_cmd = $args['storescp_cmd']; } $dcmqrscp_cmd = 'dcmqrscp'; if (!empty($args['dcmqrscp_cmd'])) { $dcmqrscp_cmd = $args['dcmqrscp_cmd']; } if (!empty($args['incoming_dir'])) { $incoming_dir = $args['incoming_dir']; } else { /** @var Dicomserver_ServerComponent $serverComponent */ $serverComponent = MidasLoader::loadComponent('Server', 'dicomserver'); $incoming_dir = $serverComponent->getDefaultReceptionDir(); } $log_dir = $incoming_dir . MIDAS_DICOMSERVER_LOGS_DIRECTORY; if (!file_exists($log_dir)) { KWUtils::mkDir($log_dir, 0777); } $python_cmd = 'python'; $python_params = array(); $python_params[] = BASE_PATH . '/modules/dicomserver/library/serverWrapper.py'; $python_params[] = '--stop'; $python_params[] = '-i ' . $incoming_dir; $python_params[] = '-s ' . $storescp_cmd; $python_params[] = '-q ' . $dcmqrscp_cmd; $stop_server_command = KWUtils::prepareExeccommand($python_cmd, $python_params); if (array_key_exists('get_command', $args)) { $stop_server_command_string = str_replace("'", '', $stop_server_command); return escapeshellarg($stop_server_command_string); } KWUtils::exec($stop_server_command, $output, '', $returnVal); $ret['message'] = 'Succeeded to stop DICOM C-STORE receiver and Query-Retrieve services!'; if ($returnVal) { $exception_string = "Failed to stop DICOM server! \n Reason:" . implode("\n", $output); throw new Zend_Exception(htmlspecialchars($exception_string, ENT_QUOTES), MIDAS_INVALID_POLICY); } return $ret; }