Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Fix prepared agents / tasks when no actor and/or no target. fixes #2953 #2956

Open
wants to merge 1 commit into
base: glpi9.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ function prepareTaskjobs($methods = [], $tasks_id = false) {
foreach ($results as $result) {

$actors = importArrayFromDB($result['job']['actors']);
$targets = importArrayFromDB($result['job']['targets']);
// Get agents linked to the actors
$agent_ids = [];
foreach ($this->getAgentsFromActors($actors) as $agent_id) {
Expand All @@ -583,11 +584,25 @@ function prepareTaskjobs($methods = [], $tasks_id = false) {
//agent's ids generated by actors like array('actors_type-id' => array( 'agent_0',...).
//Then the following could be put in the targets foreach loop before looping through
//agents.
if (count($agent_ids) == 0) {
if (count($agent_ids) == 0
|| count($targets) == 0) {
// If have no actors, cancel jobstates if in prepared
$jobstates_tocancel = $jobstate->find(
['plugin_fusioninventory_taskjobs_id' => $result['job']['id'],
'NOT' => [
'state' => [
PluginFusioninventoryTaskjobstate::FINISHED,
PluginFusioninventoryTaskjobstate::IN_ERROR,
PluginFusioninventoryTaskjobstate::CANCELLED,
]],
]);
foreach ($jobstates_tocancel as $jobstate_tocancel) {
$jobstate->getFromDB($jobstate_tocancel['id']);
$jobstate->cancel(__('Device no longer defined in definition of job', 'fusioninventory'));
}
continue;
}
$saved_agent_ids = $agent_ids;
$targets = importArrayFromDB($result['job']['targets']);
if ($result['job']['method'] == 'networkinventory') {
$newtargets = [];
$pfNetworkinventory = new PluginFusioninventoryNetworkinventory();
Expand Down
256 changes: 256 additions & 0 deletions phpunit/2_Integration/Deploy/TaskDeployEmptyTaskjobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php

/*
------------------------------------------------------------------------
FusionInventory
Copyright (C) 2010-2016 by the FusionInventory Development Team.

http://www.fusioninventory.org/ http://forge.fusioninventory.org/
------------------------------------------------------------------------

LICENSE

This file is part of FusionInventory project.

FusionInventory is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FusionInventory is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.

------------------------------------------------------------------------

@package FusionInventory
@author David Durieux
@co-author
@copyright Copyright (c) 2010-2016 FusionInventory team
@license AGPL License 3.0 or (at your option) any later version
http://www.gnu.org/licenses/agpl-3.0-standalone.html
@link http://www.fusioninventory.org/
@link http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
@since 2013

------------------------------------------------------------------------
*/

class TaskDeployEmptyTaskjob extends Common_TestCase {

protected function setUp() {
parent::setUp();
self::restore_database();
}

/**
* @test
*/
public function TaskWithoutTarget() {
global $DB;

$_SESSION['glpiactiveentities_string'] = 0;

$pfDeployGroup = new PluginFusioninventoryDeployGroup();
$pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
$pfDeployPackage = new PluginFusioninventoryDeployPackage();
$pfTask = new PluginFusioninventoryTask();
$pfTaskJob = new PluginFusioninventoryTaskjob();
$computer = new Computer();
$pfAgent = new PluginFusioninventoryAgent();

$computers_id = $computer->add(['name' => 'pc01', 'entities_id' => 0]);
$this->assertNotFalse($computers_id);
$agents_id = $pfAgent->add(['computers_id'=> $computers_id, 'entities_id' => 0]);
$this->assertNotFalse($agents_id);

$input = [
'name' => 'ls',
'entities_id' => 0,
'json' => '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls","name":"","logLineLimit":"-1","retChecks":[{"type":"okCode","values":["0"]}]}}],"userinteractions":[]},"associatedFiles":[]}'
];
$packages_id = $pfDeployPackage->add($input);
$this->assertNotFalse($packages_id);

$input = [
'name' => 'deploy',
'entities_id' => 0,
'is_active' => 1
];
$tasks_id = $pfTask->add($input);
$this->assertNotFalse($tasks_id);

$input = [
'plugin_fusioninventory_tasks_id' => $tasks_id,
'name' =>'deploy',
'entities_id' => 0,
'method' => 'deployinstall',
'actors' => '[{"Computer":"'.$computers_id.'"}]',
'targets' => '[{"PluginFusioninventoryDeployPackage":"'.$packages_id.'"}]'
];
$taskjobs_id = $pfTaskJob->add($input);
$this->assertNotFalse($taskjobs_id);

// Force task prepation
$pfTask->forceRunning();

$a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
$this->assertEquals(1, count($a_jobstates));
foreach ($a_jobstates as $num=>$data) {
unset($data['uniqid']);
$a_jobstates[$num] = $data;
}

$a_reference = [
1 => [
'id' => '1',
'plugin_fusioninventory_taskjobs_id' => $taskjobs_id,
'items_id' => $packages_id,
'itemtype' => 'PluginFusioninventoryDeployPackage',
'state' => PluginFusioninventoryTaskjobstate::PREPARED,
'plugin_fusioninventory_agents_id' => $agents_id,
'specificity' => null,
'date_start' => null,
'nb_retry' => '0',
'max_retry' => '1'
],
];
$this->assertEquals($a_reference, $a_jobstates);

$pfTaskJob->update([
'id' => $taskjobs_id,
'targets' => ''
]);

// Force task prepation
$pfTask->forceRunning();

$a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
foreach ($a_jobstates as $num=>$data) {
unset($data['uniqid']);
$a_jobstates[$num] = $data;
}

$a_reference = [
1 => [
'id' => '1',
'plugin_fusioninventory_taskjobs_id' => $taskjobs_id,
'items_id' => $packages_id,
'itemtype' => 'PluginFusioninventoryDeployPackage',
'state' => PluginFusioninventoryTaskjobstate::CANCELLED,
'plugin_fusioninventory_agents_id' => $agents_id,
'specificity' => null,
'date_start' => null,
'nb_retry' => '0',
'max_retry' => '1'
],
];
$this->assertEquals($a_reference, $a_jobstates);
}

/**
* @test
*/
public function TaskWithoutTargetActor() {
global $DB;

$_SESSION['glpiactiveentities_string'] = 0;

$pfDeployGroup = new PluginFusioninventoryDeployGroup();
$pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
$pfDeployPackage = new PluginFusioninventoryDeployPackage();
$pfTask = new PluginFusioninventoryTask();
$pfTaskJob = new PluginFusioninventoryTaskjob();
$computer = new Computer();
$pfAgent = new PluginFusioninventoryAgent();

$computers_id = $computer->add(['name' => 'pc01', 'entities_id' => 0]);
$agents_id = $pfAgent->add(['computers_id'=> $computers_id, 'entities_id' => 0]);


$input = [
'name' => 'ls',
'entities_id' => 0
];
$packages_id = $pfDeployPackage->add($input);

$input = [
'name' => 'deploy',
'is_active' => 1,
'communication' => 'pull'
];
$tasks_id = $pfTask->add($input);

$a_plugins = current(getAllDatasFromTable('glpi_plugins', ['directory' => 'fusioninventory']));

$input = [
'plugin_fusioninventory_tasks_id' => $tasks_id,
'name' =>'deploy',
'is_active' => 1,
'method' => 'deployinstall',
'actors' => '[{"Computer":"'.$computers_id.'"}]',
'targets' => '[{"PluginFusioninventoryDeployPackage":"'.$packages_id.'"}]'
];
$taskjobs_id = $pfTaskJob->add($input);

// Force task prepation
$pfTask->forceRunning();

$a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
foreach ($a_jobstates as $num=>$data) {
unset($data['uniqid']);
$a_jobstates[$num] = $data;
}

$a_reference = [
1 => [
'id' => '1',
'plugin_fusioninventory_taskjobs_id' => $taskjobs_id,
'items_id' => $packages_id,
'itemtype' => 'PluginFusioninventoryDeployPackage',
'state' => PluginFusioninventoryTaskjobstate::PREPARED,
'plugin_fusioninventory_agents_id' => $agents_id,
'specificity' => null,
'date_start' => null,
'nb_retry' => '0',
'max_retry' => '1'
],
];
$this->assertEquals($a_reference, $a_jobstates);

$pfTaskJob->update([
'id' => $taskjobs_id,
'actors' => ''
]);

// Force task prepation
$pfTask->forceRunning();

$a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
foreach ($a_jobstates as $num=>$data) {
unset($data['uniqid']);
$a_jobstates[$num] = $data;
}

$a_reference = [
1 => [
'id' => '1',
'plugin_fusioninventory_taskjobs_id' => $taskjobs_id,
'items_id' => $packages_id,
'itemtype' => 'PluginFusioninventoryDeployPackage',
'state' => PluginFusioninventoryTaskjobstate::CANCELLED,
'plugin_fusioninventory_agents_id' => $agents_id,
'specificity' => null,
'date_start' => null,
'nb_retry' => '0',
'max_retry' => '1'
],
];
$this->assertEquals($a_reference, $a_jobstates);
}

}