Skip to content
Merged
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
63 changes: 44 additions & 19 deletions capone/src/Util/CaponeTasking.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ class CaponeTasking extends \FOG\Base\FOGBase
3 => 'mpa',
4 => 'dd'
];
/**
* Builds the unencoded line FOS reads for one Capone image.
*
* Fields: path|osid|type|parttype|format|storage|storageip|osfamily.
* Fields 1-7 keep their order because an old FOS reads them by position
* and ignores a field it does not know. The eighth is the OS family, so
* FOS can place an osid it has no table entry for. It is empty when core
* has no Image::getOSFamily() (before fogproject#2085), which FOS reads
* as "not sent". This lives in its own static method because the
* constructor reads filter_input() and builds its own Image, so a test
* cannot drive it (FOGProject/fog-plugins#43).
*
* @param object $Image the image
* @param object $StorageNode the storage node the image comes from
* @param array $imgTypes image type id => FOS type name
*
* @return string
*/
public static function imageLine($Image, $StorageNode, array $imgTypes)
{
$strSetup = '%s|%s|%s|%s|%s|%s|%s|%s';
$family = method_exists($Image, 'getOSFamily') ?
$Image->getOSFamily() :
'';
return sprintf(
$strSetup,
$Image->get('path'),
$Image->get('osID'),
$imgTypes[$Image->get('imageTypeID')],
$Image->getPartitionType(),
(int)$Image->get('format'),
sprintf(
'%s:%s',
$StorageNode->get('ip'),
$StorageNode->get('path')
),
$StorageNode->get('ip'),
$family
);
}
/**
* Initializes the Capone tasking class.
*
Expand All @@ -63,7 +103,6 @@ public function __construct()
break;
}
try {
$strSetup = "%s|%s|%s|%s|%s|%s|%s";
ob_start();
$capones = \FOG\Router\Route::getList(
'capone',
Expand All @@ -81,27 +120,13 @@ public function __construct()
) {
continue;
}
$path = $Image->get('path');
$osid = $Image->get('osID');
$itid = $Image->get('imageTypeID');
$ptid = $Image->getPartitionType();
$format = $Image->get('format');
printf(
"%s\n",
base64_encode(
sprintf(
$strSetup,
$path,
$osid,
$this->imgTypes[$itid],
$ptid,
(int)$format,
sprintf(
'%s:%s',
$StorageNode->get('ip'),
$StorageNode->get('path')
),
$StorageNode->get('ip')
self::imageLine(
$Image,
$StorageNode,
$this->imgTypes
)
)
);
Expand Down
179 changes: 179 additions & 0 deletions tests/capone-image-line.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php
/**
* The Capone image line carries the OS family as its eighth field.
*
* FOS reads each Capone image as a base64 line
* path|osid|type|parttype|format|storage|storageip. It carried no OS family,
* so a Capone image whose osid FOS does not know stopped the listing. The line
* now ends with the family (FOGProject/fog-plugins#43), the way fog.checkin
* already sends it for a normal task.
*
* Pinned here:
* - fields 1-7 keep their order, because an old FOS reads them by position;
* - the eighth is Image::getOSFamily() when the core has it;
* - the eighth is empty on a core without that method (FOS reads empty as
* "not sent"), so the plugin keeps working on any fogproject 1.6.
*
* CaponeTasking::__construct() reads filter_input(), which is null under CLI,
* so the test drives the static imageLine() the constructor calls instead.
*
* Usage: php tests/capone-image-line.test.php
* Exit status 0 = pass, 1 = fail.
*/

$root = dirname(__DIR__);

require $root . '/tests/stubs/fog-stubs.php';
require $root . '/capone/src/Util/CaponeTasking.php';

/**
* Image with the data accessors Capone reads, plus getOSFamily().
*/
class CaponeLineImageWithFamily
{
/**
* Read a field.
*
* @param string $key the field
*
* @return mixed
*/
public function get($key)
{
$data = [
'path' => 'win11',
'osID' => 10,
'imageTypeID' => 1,
'format' => 1,
];
return $data[$key];
}
/**
* The partition type id.
*
* @return int
*/
public function getPartitionType()
{
return 7;
}
/**
* The OS family, as core has had it since fogproject#2085.
*
* @return string
*/
public function getOSFamily()
{
return 'windows';
}
}

/**
* Image on an older core: identical data, and no getOSFamily() at all.
*
* Not a subclass of the class above: method_exists() would find the
* inherited method.
*/
class CaponeLineImageOldCore
{
/**
* Read a field.
*
* @param string $key the field
*
* @return mixed
*/
public function get($key)
{
$data = [
'path' => 'win11',
'osID' => 10,
'imageTypeID' => 1,
'format' => 1,
];
return $data[$key];
}
/**
* The partition type id.
*
* @return int
*/
public function getPartitionType()
{
return 7;
}
}

/**
* Storage node with the two fields Capone reads.
*/
class CaponeLineNode
{
/**
* Read a field.
*
* @param string $key the field
*
* @return string
*/
public function get($key)
{
$data = ['ip' => '10.0.0.5', 'path' => '/images'];
return $data[$key];
}
}

$types = [1 => 'n', 2 => 'mps', 3 => 'mpa', 4 => 'dd'];
$node = new CaponeLineNode();
$failures = [];
$checks = 0;

/**
* Records one assertion.
*
* @param bool $ok whether it held
* @param string $what what was asserted
*
* @return void
*/
function check($ok, $what)
{
global $failures, $checks;
$checks++;
if (!$ok) {
$failures[] = $what;
}
}

$with = \FOG\Plugins\Capone\Util\CaponeTasking::imageLine(
new CaponeLineImageWithFamily(),
$node,
$types
);
check(
$with === 'win11|10|n|7|1|10.0.0.5:/images|10.0.0.5|windows',
'core with getOSFamily(): got ' . var_export($with, true)
);

$without = \FOG\Plugins\Capone\Util\CaponeTasking::imageLine(
new CaponeLineImageOldCore(),
$node,
$types
);
check(
$without === 'win11|10|n|7|1|10.0.0.5:/images|10.0.0.5|',
'core without getOSFamily(): got ' . var_export($without, true)
);
check(
count(explode('|', $without)) === 8,
'the line has eight fields even when the family is empty'
);

if ($failures) {
foreach ($failures as $f) {
fwrite(STDERR, "FAIL: $f\n");
}
exit(1);
}
echo "capone-image-line: $checks checks passed\n";
exit(0);
Loading