40 private $testInstaller;
49 private $uploadPermDao;
51 private $highlightDao;
53 protected function setUp()
55 $this->testDb =
new TestPgDb(
"monkSched");
56 $this->
dbManager = $this->testDb->getDbManager();
59 $logger =
new Logger(
"SchedulerTest");
60 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
65 $this->agentDir = dirname(dirname(__DIR__));
68 protected function tearDown()
70 $this->testDb->fullDestruct();
73 $this->licenseDao = null;
74 $this->highlightDao = null;
75 $this->clearingDao = null;
78 private function runMonk($uploadId, $userId=2, $groupId=2, $jobId=1, $args=
"")
80 $sysConf = $this->testDb->getFossSysConf();
85 $pipeFd = popen(
"echo $uploadId | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start $args",
"r");
86 $this->assertTrue($pipeFd !==
false,
'running monk failed');
89 while (($buffer = fgets($pipeFd, 4096)) !==
false) {
92 $retCode = pclose($pipeFd);
94 return array($output,$retCode);
97 private function setUpRepo()
99 $sysConf = $this->testDb->getFossSysConf();
101 $this->testInstaller->init();
102 $this->testInstaller->cpRepo();
103 $this->testInstaller->install($this->agentDir);
106 private function rmRepo()
108 $this->testInstaller->uninstall($this->agentDir);
109 $this->testInstaller->clear();
110 $this->testInstaller->rmRepo();
113 private function setUpTables()
115 $this->testDb->createPlainTables(array(
'upload',
'uploadtree',
'uploadtree_a',
'license_ref',
'license_ref_bulk',
'license_set_bulk',
116 'clearing_event',
'clearing_decision',
'clearing_decision_event',
'license_file',
'highlight',
'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users'),
false);
117 $this->testDb->createSequences(array(
'agent_agent_pk_seq',
'pfile_pfile_pk_seq',
'upload_upload_pk_seq',
'nomos_ars_ars_pk_seq',
'license_file_fl_pk_seq',
'license_ref_rf_pk_seq',
'license_ref_bulk_lrb_pk_seq',
'clearing_event_clearing_event_pk_seq',
'clearing_decision_clearing_decision_pk_seq'),
false);
118 $this->testDb->createViews(array(
'license_file_ref'),
false);
119 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
'upload_pkey_idx',
'FileLicense_pkey',
'clearing_event_pkey',
'clearing_decision_pkey'),
false);
120 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
'license_ref_bulk',
'license_set_bulk',
'clearing_event',
'license_file',
'highlight',
'clearing_decision'),
false);
121 $this->testDb->createInheritedTables();
122 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users'),
false);
123 $this->testDb->insertData_license_ref(200);
126 private function getHeartCount($output)
129 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches))
131 return intval($matches[1]);
137 public function testRunMonkScan()
139 $this->setUpTables();
142 list($output,$retCode) = $this->runMonk($uploadId=1);
146 $this->assertEquals($retCode, 0,
'monk failed: '.$output);
148 $this->assertEquals(6, $this->getHeartCount($output));
150 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
151 $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
153 $this->assertEquals($expected=2, count($matches));
156 $licenseMatch = $matches[0];
158 $this->assertEquals($expected=4, $licenseMatch->getFileId());
161 $matchedLicense = $licenseMatch->getLicenseRef();
162 $this->assertEquals($matchedLicense->getShortName(),
"GPL-3.0");
165 $agentRef = $licenseMatch->getAgentRef();
166 $this->assertEquals($agentRef->getAgentName(),
"monk");
168 $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(7));
170 $expectedHighlight =
new Highlight(18, 35825, Highlight::MATCH, 20, 35819);
171 $expectedHighlight->setLicenseId($matchedLicense->getId());
173 $this->assertEquals(array($expectedHighlight), $highlights);
175 $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(11));
177 $expectedHighlights = array();
178 $expectedHighlights[] =
new Highlight(18, 339, Highlight::MATCH, 20, 350);
179 $expectedHighlights[] =
new Highlight(340, 347, Highlight::CHANGED, 351, 357);
180 $expectedHighlights[] =
new Highlight(348, 35149, Highlight::MATCH, 358, 35819);
181 foreach($expectedHighlights as $expectedHighlight) {
182 $expectedHighlight->setLicenseId($matchedLicense->getId());
184 assertThat($highlights, containsInAnyOrder($expectedHighlights));
188 public function testRunMonkTwiceOnAScan()
190 $this->setUpTables();
193 list($output,$retCode) = $this->runMonk($uploadId=1);
194 list($output2,$retCode2) = $this->runMonk($uploadId);
196 $this->assertEquals($retCode, 0,
'monk failed: '.$output);
197 $this->assertEquals(6, $this->getHeartCount($output));
199 $this->assertEquals($retCode2, 0,
'monk failed: '.$output2);
200 $this->assertEquals(0, $this->getHeartCount($output2));
204 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
205 $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
206 $this->assertEquals($expected=2, count($matches));
209 $licenseMatch = $matches[0];
210 $this->assertEquals($expected=4, $licenseMatch->getFileId());
213 $matchedLicense = $licenseMatch->getLicenseRef();
214 $this->assertEquals($matchedLicense->getShortName(),
"GPL-3.0");
217 $agentRef = $licenseMatch->getAgentRef();
218 $this->assertEquals($agentRef->getAgentName(),
"monk");
fo_dbManager * dbManager
fo_dbManager object