29 private $testInstaller;
38 private $uploadPermDao;
40 private $highlightDao;
42 protected function setUp() :
void
44 $this->testDb =
new TestPgDb(
"monkSched");
45 $this->
dbManager = $this->testDb->getDbManager();
48 $logger =
new Logger(
"SchedulerTest");
49 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
54 $this->agentDir = dirname(__DIR__, 4).
'/build/src/monk';
57 protected function tearDown() :
void
59 $this->testDb->fullDestruct();
62 $this->licenseDao =
null;
63 $this->highlightDao =
null;
64 $this->clearingDao =
null;
67 private function runMonk($uploadId, $userId=2, $groupId=2, $jobId=1, $args=
"")
69 $sysConf = $this->testDb->getFossSysConf();
72 $execDir = dirname(__DIR__,4).
'/build/src/monk/agent';
74 $pipeFd = popen(
"echo $uploadId | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start $args",
"r");
75 $this->assertTrue($pipeFd !==
false,
'running monk failed');
78 while (($buffer = fgets($pipeFd, 4096)) !==
false) {
81 $retCode = pclose($pipeFd);
83 return array($output,$retCode);
86 private function setUpRepo()
88 $sysConf = $this->testDb->getFossSysConf();
90 $this->testInstaller->init();
91 $this->testInstaller->cpRepo();
92 $this->testInstaller->install($this->agentDir);
95 private function rmRepo()
97 $this->testInstaller->uninstall($this->agentDir);
98 $this->testInstaller->clear();
99 $this->testInstaller->rmRepo();
102 private function setUpTables()
104 $this->testDb->createPlainTables(array(
'upload',
'uploadtree',
'uploadtree_a',
'license_ref',
'license_ref_bulk',
'license_set_bulk',
105 'clearing_event',
'clearing_decision',
'clearing_decision_event',
'license_file',
'highlight',
'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users'),
false);
106 $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);
107 $this->testDb->createViews(array(
'license_file_ref'),
false);
108 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
'upload_pkey_idx',
'FileLicense_pkey',
'clearing_event_pkey',
'clearing_decision_pkey'),
false);
109 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
'license_ref_bulk',
'license_ref',
'license_set_bulk',
'clearing_event',
'license_file',
'highlight',
'clearing_decision'),
false);
110 $this->testDb->createInheritedTables();
111 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users'),
false);
112 $this->testDb->insertData_license_ref(200);
115 private function getHeartCount($output)
118 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches))
120 return intval($matches[1]);
126 public function testRunMonkScan()
128 $this->setUpTables();
131 list($output,$retCode) = $this->runMonk($uploadId=1);
135 $this->assertEquals($retCode, 0,
'monk failed: '.$output);
137 $this->assertEquals(6, $this->getHeartCount($output));
139 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
140 $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
142 $this->assertEquals($expected=2, count($matches));
145 $licenseMatch = $matches[0];
147 $this->assertEquals($expected=4, $licenseMatch->getFileId());
150 $matchedLicense = $licenseMatch->getLicenseRef();
151 $this->assertEquals($matchedLicense->getShortName(),
"GPL-3.0-only");
154 $agentRef = $licenseMatch->getAgentRef();
155 $this->assertEquals($agentRef->getAgentName(),
"monk");
157 $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(7));
159 $expectedHighlight =
new Highlight(18, 35824, Highlight::MATCH, 0, 34505);
160 $expectedHighlight->setLicenseId($matchedLicense->getId());
162 $this->assertEquals(array($expectedHighlight), $highlights);
164 $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(11));
166 $expectedHighlights = array();
167 $expectedHighlights[] =
new Highlight(18, 338, Highlight::MATCH, 0, 265);
168 $expectedHighlights[] =
new Highlight(339, 346, Highlight::CHANGED, 266, 272);
169 $expectedHighlights[] =
new Highlight(347, 35148, Highlight::MATCH, 273, 34505);
170 foreach($expectedHighlights as $expectedHighlight) {
171 $expectedHighlight->setLicenseId($matchedLicense->getId());
173 assertThat($highlights, containsInAnyOrder($expectedHighlights));
177 public function testRunMonkTwiceOnAScan()
179 $this->setUpTables();
182 list($output,$retCode) = $this->runMonk($uploadId=1);
183 list($output2,$retCode2) = $this->runMonk($uploadId);
185 $this->assertEquals($retCode, 0,
'monk failed: '.$output);
186 $this->assertEquals(6, $this->getHeartCount($output));
188 $this->assertEquals($retCode2, 0,
'monk failed: '.$output2);
189 $this->assertEquals(0, $this->getHeartCount($output2));
193 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
194 $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
195 $this->assertEquals($expected=2, count($matches));
198 $licenseMatch = $matches[0];
199 $this->assertEquals($expected=4, $licenseMatch->getFileId());
202 $matchedLicense = $licenseMatch->getLicenseRef();
203 $this->assertEquals($matchedLicense->getShortName(),
"GPL-3.0-only");
206 $agentRef = $licenseMatch->getAgentRef();
207 $this->assertEquals($agentRef->getAgentName(),
"monk");
fo_dbManager * dbManager
fo_dbManager object