FOSSology  4.7.1
Open Source License Compliance by Open Source Software
AgentAdderTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
11 use Mockery as M;
12 
13 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/Test/Reflectory.php');
14 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/Plugin/FO_Plugin.php');
15 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/common-plugin.php');
16 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/common-menu.php');
17 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/common-job.php');
18 
36 class AgentAdderTest extends \PHPUnit\Framework\TestCase
37 {
39  private $assertCountBefore;
41  private $agentAdder;
43  private $callLog;
44 
45  protected function setUp(): void
46  {
47  // Loaded per-test, not at file scope: setUpBeforeClass() runs in the
48  // parent process, before the isolation above forks a child.
49  if (!class_exists('AgentAdder', false)) {
50  $GLOBALS['SysConf'] = [];
51  $GLOBALS['container'] = new class {
52  public function get($name)
53  {
54  return new \stdClass();
55  }
56  };
57  require_once(__DIR__ . '/../../ui/agent-add.php');
58  }
59 
60  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
61 
62  global $MenuList, $Plugins;
63  $MenuList = array();
64  $Plugins = array();
65  $this->callLog = array();
66 
67  $GLOBALS['SysConf']['auth']['UserId'] = 1;
68  $GLOBALS['SysConf']['auth']['GroupId'] = 1;
69 
70  // Bypass DefaultPlugin::__construct(): agentsAdd() only touches globals,
71  // so the bare object is enough to test it.
72  $this->agentAdder = (new \ReflectionClass('AgentAdder'))->newInstanceWithoutConstructor();
73  }
74 
75  protected function tearDown(): void
76  {
77  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
78  M::close();
79  }
80 
85  private function registerLoggingPlugin($menu, $pluginName)
86  {
87  $log = &$this->callLog;
88  $plugin = new class($pluginName, $log) {
89  public $Name;
90  public $State;
91  private $log;
92  public function __construct($name, &$log)
93  {
94  $this->Name = $name;
95  $this->State = PLUGIN_STATE_READY;
96  $this->log = &$log;
97  }
98  public function AgentAdd(...$args)
99  {
100  $this->log[] = "$this->Name:AgentAdd";
101  return 1;
102  }
103  public function scheduleAgent(...$args)
104  {
105  $this->log[] = "$this->Name:scheduleAgent";
106  return 1;
107  }
108  };
109 
110  global $Plugins;
111  $Plugins[$pluginName] = $plugin;
112  menu_insert("$menu::$pluginName", 0, $pluginName);
113  }
114 
115  private function mockContainer($uploadFilename = 'test.tar')
116  {
117  $dbManager = M::mock(DbManager::class);
118  $dbManager->shouldReceive('getSingleRow')->andReturn(['job_pk' => 1]);
119 
120  $upload = M::mock(Upload::class);
121  $upload->shouldReceive('getFilename')->andReturn($uploadFilename);
122  $uploadDao = M::mock('stdClass');
123  $uploadDao->shouldReceive('getUpload')->andReturn($upload);
124 
125  $container = M::mock('ContainerBuilder');
126  $container->shouldReceive('get')->with('db.manager')->andReturn($dbManager);
127  $container->shouldReceive('get')->with('dao.upload')->andReturn($uploadDao);
128 
129  $GLOBALS['container'] = $container;
130  }
131 
141  {
142  $this->mockContainer();
143  $this->registerLoggingPlugin('Agents', 'agent_nomos');
144  $this->registerLoggingPlugin('ParmAgents', 'agent_decider');
145  $this->registerLoggingPlugin('ParmAgents', 'agent_scancode');
146 
147  $request = new \Symfony\Component\HttpFoundation\Request(
148  [], ['agents' => ['agent_nomos', 'agent_decider', 'agent_scancode']]
149  );
150 
151  $result = Reflectory::invokeObjectsMethodnameWith(
152  $this->agentAdder, 'agentsAdd', [42, ['agent_nomos', 'agent_decider', 'agent_scancode'], $request]
153  );
154 
155  $this->assertSame(1, $result);
156 
157  $nomosIdx = array_search('agent_nomos:AgentAdd', $this->callLog);
158  $scancodeIdx = array_search('agent_scancode:scheduleAgent', $this->callLog);
159  $deciderIdx = array_search('agent_decider:scheduleAgent', $this->callLog);
160 
161  $this->assertNotFalse($nomosIdx, 'nomos was never scheduled');
162  $this->assertNotFalse($scancodeIdx, 'scancode was never scheduled');
163  $this->assertNotFalse($deciderIdx, 'decider was never scheduled');
164  $this->assertLessThan($deciderIdx, $nomosIdx, 'nomos must be queued before decider');
165  $this->assertLessThan($deciderIdx, $scancodeIdx, 'scancode must be queued before decider');
166  }
167 
178  {
179  $this->mockContainer();
180  $this->registerLoggingPlugin('ParmAgents', 'agent_decider');
181  $this->registerLoggingPlugin('ParmAgents', 'agent_scancode');
182 
183  $request = new \Symfony\Component\HttpFoundation\Request(
184  [], ['agents' => ['agent_decider', 'agent_scancode']]
185  );
186 
187  Reflectory::invokeObjectsMethodnameWith(
188  $this->agentAdder, 'agentsAdd', [42, ['agent_decider', 'agent_scancode'], $request]
189  );
190 
191  $scheduleIdx = array_search('agent_scancode:scheduleAgent', $this->callLog);
192  $agentAddIdx = array_search('agent_scancode:AgentAdd', $this->callLog);
193 
194  $this->assertNotFalse($scheduleIdx);
195  $this->assertNotFalse($agentAddIdx);
196  $this->assertLessThan($agentAddIdx, $scheduleIdx);
197  }
198 
205  public function testRejectsNonArrayAgentsToStart(): void
206  {
207  $request = new \Symfony\Component\HttpFoundation\Request();
208 
209  $result = Reflectory::invokeObjectsMethodnameWith(
210  $this->agentAdder, 'agentsAdd', [42, 'not-an-array', $request]
211  );
212 
213  $this->assertSame('bad parameters', $result);
214  }
215 }
Test for AgentAdder::agentsAdd(), covering the scheduling-order fix @runTestsInSeparateProcesses @pre...
testPlainAgentsAndReorderedParmAgentsRunBeforeDecider()
testRejectsNonArrayAgentsToStart()
registerLoggingPlugin($menu, $pluginName)
testScancodeScheduleAgentRunsBeforeItsOwnLeftoverAgentAddPass()
Definition: state.hpp:16
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
$MenuList
Global menu list array.
Definition: common-menu.php:48