Skip to content

Commit 19f1f3d

Browse files
committed
MQE-813: Assist DevOps with creation of flag or command for returning test metadata to build
- impelemnt new factory pattern for manifest file - add parallel option and logic to split tests - update Test Generator to use new Manifest
1 parent bf041ff commit 19f1f3d

File tree

9 files changed

+338
-105
lines changed

9 files changed

+338
-105
lines changed

dev/tests/verification/Tests/SuiteGenerationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testSuiteGeneration1()
7979
DIRECTORY_SEPARATOR;
8080

8181
// Validate test manifest contents
82-
$actualManifest = $suiteResultBaseDir . TestManifest::TEST_MANIFEST_FILENAME;
82+
$actualManifest = $suiteResultBaseDir . 'testManifest.txt';
8383
$actualTestReferences = explode(PHP_EOL, file_get_contents($actualManifest));
8484

8585
for ($i = 0; $i < count($actualTestReferences); $i++) {

src/Magento/FunctionalTestingFramework/Test/Objects/TestObject.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ public function getHooks()
103103
return $this->hooks;
104104
}
105105

106+
public function getTestActionCount()
107+
{
108+
$hookActions = 0;
109+
if (array_key_exists('before', $this->hooks)) {
110+
$hookActions += count($this->hooks['before']->getActions());
111+
}
112+
113+
if (array_key_exists('after', $this->hooks)) {
114+
$hookActions += count($this->hooks['after']->getActions());
115+
}
116+
117+
$testActions = count($this->getOrderedActions());
118+
119+
return $hookActions + $testActions;
120+
}
121+
106122
/**
107123
* Method to return the value(s) of a corresponding annotation such as group.
108124
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util\Manifest;
8+
9+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
10+
11+
abstract class BaseTestManifest
12+
{
13+
/**
14+
* Type of manifest to generate. (Currently describes whether to path to a dir or for each test).
15+
*
16+
* @var string
17+
*/
18+
protected $runTypeConfig;
19+
20+
/**
21+
* Relative dir path from functional yml file. For devOps execution flexibility.
22+
*
23+
* @var string
24+
*/
25+
protected $relativeDirPath;
26+
27+
/**
28+
* TestManifest constructor.
29+
*
30+
* @param string $path
31+
* @param string $runConfig
32+
*/
33+
public function __construct($path, $runConfig)
34+
{
35+
$this->runTypeConfig = $runConfig;
36+
$this->relativeDirPath = substr($path, strlen(dirname(dirname(TESTS_BP))) + 1);
37+
}
38+
39+
/**
40+
* Returns a string indicating the generation config (e.g. singleRun).
41+
*
42+
* @return string
43+
*/
44+
public function getManifestConfig()
45+
{
46+
return $this->runTypeConfig;
47+
}
48+
49+
/**
50+
* Takes a test name and set of tests, records the names in a file for codeception to consume.
51+
*
52+
* @param TestObject $testObject
53+
* @return void
54+
*/
55+
abstract public function addTest($testObject);
56+
57+
/**
58+
* Function which generates the actual manifest(s) once the relevant tests have been added to the array.
59+
*
60+
* @param int|null $nodes
61+
* @return void
62+
*/
63+
abstract public function generate($nodes = null);
64+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util\Manifest;
8+
9+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
10+
11+
class DefaultTestManifest extends BaseTestManifest
12+
{
13+
const DEFAULT_CONFIG = 'default';
14+
15+
/**
16+
* Path to the test manifest file.
17+
*
18+
* @var string
19+
*/
20+
protected $manifestPath;
21+
22+
/**
23+
* An array containing all test names for output.
24+
*
25+
* @var string[]
26+
*/
27+
protected $testNames = [];
28+
29+
/**
30+
* DefaultTestManifest constructor.
31+
* @param string $path
32+
*/
33+
public function __construct($path)
34+
{
35+
$this->manifestPath = $path . DIRECTORY_SEPARATOR . 'testManifest.txt';
36+
parent::__construct($path, self::DEFAULT_CONFIG);
37+
38+
$fileResource = fopen($this->manifestPath, 'w');
39+
fclose($fileResource);
40+
}
41+
42+
/**
43+
* Takes a test name and set of tests, records the names in a file for codeception to consume.
44+
*
45+
* @param TestObject $testObject
46+
* @return void
47+
*/
48+
public function addTest($testObject)
49+
{
50+
$this->testNames[] = $testObject->getCodeceptionName();
51+
}
52+
53+
/**
54+
* Function which outputs a list of all test files to the defined testManifest.txt file.
55+
*
56+
* @param int|null $nodes
57+
* @return void
58+
*/
59+
public function generate($nodes = null)
60+
{
61+
$fileResource = fopen($this->manifestPath, 'a');
62+
63+
foreach ($this->testNames as $testName) {
64+
$line = $this->relativeDirPath . DIRECTORY_SEPARATOR . $testName . '.php';
65+
fwrite($fileResource, $line . PHP_EOL);
66+
}
67+
68+
fclose($fileResource);
69+
}
70+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util\Manifest;
8+
9+
use Magento\Framework\Exception\RuntimeException;
10+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
11+
use Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil;
12+
13+
class ParallelTestManifest extends BaseTestManifest
14+
{
15+
const PARALLEL_CONFIG = 'parallel';
16+
17+
/**
18+
* An associate array of test name to size of test.
19+
*
20+
* @var string[]
21+
*/
22+
private $testNameToSize = [];
23+
24+
/**
25+
* Path to the directory that will contain all test group files
26+
*
27+
* @var string
28+
*/
29+
private $dirPath;
30+
31+
/**
32+
* TestManifest constructor.
33+
*
34+
* @param string $path
35+
*/
36+
public function __construct($path)
37+
{
38+
$this->dirPath = $path . DIRECTORY_SEPARATOR . 'groups';
39+
parent::__construct($path, self::PARALLEL_CONFIG);
40+
}
41+
42+
/**
43+
* Takes a test name and set of tests, records the names in a file for codeception to consume.
44+
*
45+
* @param TestObject $testObject
46+
* @return void
47+
*/
48+
public function addTest($testObject)
49+
{
50+
$this->testNameToSize[$testObject->getCodeceptionName()] = $testObject->getTestActionCount();
51+
}
52+
53+
/**
54+
* Function which generates the actual manifest once the relevant tests have been added to the array.
55+
*
56+
* @param int $nodes
57+
* @return void
58+
*/
59+
public function generate($nodes = null)
60+
{
61+
if ($nodes == null) {
62+
$nodes = 2;
63+
}
64+
65+
DirSetupUtil::createGroupDir($this->dirPath);
66+
arsort($this->testNameToSize);
67+
$node = $nodes;
68+
69+
foreach (array_keys($this->testNameToSize) as $testName) {
70+
$node = $this->getNodeOrder($node, $nodes);
71+
$nodeString = strval($node);
72+
$fileResource = fopen($this->dirPath . DIRECTORY_SEPARATOR . "group{$nodeString}.txt", 'a');
73+
$line = $this->relativeDirPath . DIRECTORY_SEPARATOR . $testName . '.php';
74+
fwrite($fileResource, $line . PHP_EOL);
75+
fclose($fileResource);
76+
}
77+
}
78+
79+
/**
80+
* Function which independently iterates node position based on number of desired nodes.
81+
*
82+
* @param int $currentNode
83+
* @param int $nodes
84+
* @return int
85+
*/
86+
private function getNodeOrder($currentNode, $nodes)
87+
{
88+
$adjustedRef = $currentNode + 1;
89+
if ($adjustedRef <= $nodes) {
90+
return $currentNode + 1;
91+
}
92+
93+
return 1;
94+
}
95+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util\Manifest;
8+
9+
class SingleRunTestManifest extends DefaultTestManifest
10+
{
11+
const SINGLE_RUN_CONFIG = 'singleRun';
12+
13+
/**
14+
* SingleRunTestManifest constructor.
15+
* @param string $path
16+
*/
17+
public function __construct($path)
18+
{
19+
$this->manifestPath = $path . DIRECTORY_SEPARATOR . 'testManifest.txt';
20+
parent::__construct($path, self::SINGLE_RUN_CONFIG);
21+
22+
$fileResource = fopen($this->manifestPath, 'w');
23+
fclose($fileResource);
24+
}
25+
26+
/**
27+
* Function which generates the actual manifest once the relevant tests have been added to the array.
28+
*
29+
* @param int|null $nodes
30+
* @return void
31+
*/
32+
public function generate($nodes = null)
33+
{
34+
$fileResource = fopen($this->manifestPath, 'a');
35+
$line = $this->relativeDirPath . DIRECTORY_SEPARATOR;
36+
fwrite($fileResource, $line . PHP_EOL);
37+
fclose($fileResource);
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util\Manifest;
8+
9+
class TestManifestFactory
10+
{
11+
/**
12+
* TestManifestFactory constructor.
13+
*/
14+
private function __construct()
15+
{
16+
// private constructor
17+
}
18+
19+
/**
20+
* Static function which takes path and config to return the appropriate manifest output type.
21+
*
22+
* @param String $path
23+
* @param String $runConfig
24+
* @return BaseTestManifest
25+
*/
26+
public static function makeManifest($path, $runConfig)
27+
{
28+
switch ($runConfig) {
29+
case 'singleRun':
30+
return new SingleRunTestManifest($path);
31+
32+
case 'parallel':
33+
return new ParallelTestManifest($path);
34+
35+
default:
36+
return new DefaultTestManifest($path);
37+
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)