Removed method isTestingOnOcisOrReva, and refactored its use

This commit is contained in:
Prarup Gurung
2023-03-09 14:25:15 +05:45
parent d9092d7e17
commit fab9bfc734
11 changed files with 147 additions and 1063 deletions
+3 -6
View File
@@ -76,12 +76,9 @@ class HttpRequestHelper {
* @return int
*/
public static function numRetriesOnHttpTooEarly():int {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently reva and oCIS may return HTTP_TOO_EARLY
// So try up to 10 times before giving up.
return 10;
}
return 0;
// Currently reva and oCIS may return HTTP_TOO_EARLY
// So try up to 10 times before giving up.
return 10;
}
/**
+20 -227
View File
@@ -53,32 +53,8 @@ class LoggingHelper {
public static function getLogFilePath(
?string $xRequestId = ''
):string {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
return "";
}
$result = SetupHelper::runOcc(
['log:owncloud'],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not get owncloud log file information" .
$result ["stdOut"] . " " . $result ["stdErr"]
);
}
\preg_match(
"/Log backend ownCloud: (\w+)\sLog file: (.*)/",
$result ['stdOut'],
$matches
);
if (!isset($matches[1]) || $matches[1] !== "enabled") {
throw new Exception("log backend is not set to 'owncloud'");
}
if (!isset($matches[2])) {
throw new Exception("could not get owncloud log file information");
}
return $matches[2];
// Currently we don't interact with the log file on reva or OCIS
return "";
}
/**
@@ -132,23 +108,7 @@ class LoggingHelper {
public static function getLogLevel(
?string $xRequestId = ''
):string {
if (OcisHelper::isTestingOnOcisOrReva()) {
return "debug";
}
$result = SetupHelper::runOcc(
["log:manage"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not get log level " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
if (!\preg_match("/Log level:\s(\w+)\s\(/", $result["stdOut"], $matches)) {
throw new Exception("could not get log level");
}
return \strtolower($matches[1]);
return "debug";
}
/**
@@ -163,23 +123,8 @@ class LoggingHelper {
?string $logLevel,
?string $xRequestId = ''
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we can't manage log file settings on reva or OCIS
return;
}
if (!\in_array($logLevel, self::LOG_LEVEL_ARRAY)) {
throw new InvalidArgumentException("invalid log level");
}
$result = SetupHelper::runOcc(
["log:manage", "--level=$logLevel"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not set log level " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
// Currently we can't manage log file settings on reva or OCIS
return;
}
/**
@@ -193,28 +138,7 @@ class LoggingHelper {
public static function getLogBackend(
?string $xRequestId = ''
):string {
if (OcisHelper::isTestingOnOcisOrReva()) {
return "errorlog";
}
$result = SetupHelper::runOcc(
["log:manage"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not get log backend " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
$pregResult = \preg_match(
"/Enabled logging backend:\s(\w+)\n/",
$result ["stdOut"],
$matches
);
if (!$pregResult) {
throw new Exception("could not get log backend");
}
return \strtolower($matches[1]);
return "errorlog";
}
/**
@@ -232,20 +156,8 @@ class LoggingHelper {
if (!\in_array($backend, ["owncloud", "syslog", "errorlog"])) {
throw new InvalidArgumentException("invalid log backend");
}
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we can't manage log file settings on reva or OCIS
return;
}
$result = SetupHelper::runOcc(
["log:manage", "--backend=$backend"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not set log backend " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
// Currently we can't manage log file settings on reva or OCIS
return;
}
/**
@@ -259,28 +171,7 @@ class LoggingHelper {
public static function getLogTimezone(
?string $xRequestId = ''
):string {
if (OcisHelper::isTestingOnOcisOrReva()) {
return "UTC";
}
$result = SetupHelper::runOcc(
["log:manage"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not get log timezone " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
$pregResult = \preg_match(
"/Log timezone:\s(\w+)/",
$result ["stdOut"],
$matches
);
if (!$pregResult) {
throw new Exception("could not get log timezone");
}
return $matches[1];
return "UTC";
}
/**
@@ -295,20 +186,8 @@ class LoggingHelper {
?string $timezone,
?string $xRequestId = ''
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we can't manage log file settings on reva or OCIS
return;
}
$result = SetupHelper::runOcc(
["log:manage", "--timezone=$timezone"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not set log timezone " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
// Currently we can't manage log file settings on reva or OCIS
return;
}
/**
@@ -327,21 +206,8 @@ class LoggingHelper {
?string $adminPassword,
?string $xRequestId = ''
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
return;
}
$result = OcsApiHelper::sendRequest(
$baseUrl,
$adminUsername,
$adminPassword,
"DELETE",
"/apps/testing/api/v1/logfile",
$xRequestId
);
if ($result->getStatusCode() !== 200) {
throw new Exception("could not clear logfile");
}
// Currently we don't interact with the log file on reva or OCIS
return;
}
/**
@@ -360,41 +226,8 @@ class LoggingHelper {
?string $timezone,
?string $xRequestId = ''
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
return;
}
if (!\in_array(\strtolower($logLevel), self::LOG_LEVEL_ARRAY)) {
throw new InvalidArgumentException("invalid log level");
}
if (!\in_array(\strtolower($backend), ["owncloud", "syslog", "errorlog"])) {
throw new InvalidArgumentException("invalid log backend");
}
$commands = ["log:manage"];
if ($timezone) {
\array_push($commands, "--timezone=$timezone");
}
if ($logLevel) {
\array_push($commands, "--backend=$backend");
}
if ($backend) {
\array_push($commands, "--level=$logLevel");
}
if (\count($commands) > 1) {
$result = SetupHelper::runOcc(
$commands,
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not restore log status " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
}
// Currently we don't interact with the log file on reva or OCIS
return;
}
/**
@@ -408,50 +241,10 @@ class LoggingHelper {
public static function getLogInfo(
?string $xRequestId = ''
):array {
if (OcisHelper::isTestingOnOcisOrReva()) {
return [
"level" => "debug",
"backend" => "errorlog",
"timezone" => "UTC"
];
}
$result = SetupHelper::runOcc(
["log:manage"],
$xRequestId
);
if ($result["code"] != 0) {
throw new Exception(
"could not get log level " . $result ["stdOut"] . " " .
$result ["stdErr"]
);
}
$logging = [];
if (!\preg_match("/Log level:\s(\w+)\s\(/", $result["stdOut"], $matches)) {
throw new Exception("could not get log level");
}
$logging["level"] = $matches[1];
$pregResult = \preg_match(
"/Log timezone:\s(\w+)/",
$result ["stdOut"],
$matches
);
if (!$pregResult) {
throw new Exception("could not get log timezone");
}
$logging["timezone"] = $matches[1];
$pregResult = \preg_match(
"/Enabled logging backend:\s(\w+)\n/",
$result ["stdOut"],
$matches
);
if (!$pregResult) {
throw new Exception("could not get log backend");
}
$logging["backend"] = $matches[1];
return $logging;
return [
"level" => "debug",
"backend" => "errorlog",
"timezone" => "UTC"
];
}
}
+2 -9
View File
@@ -47,18 +47,11 @@ class OcisHelper {
return (\getenv("TEST_REVA") === "true");
}
/**
* @return bool
*/
public static function isTestingOnOcisOrReva():bool {
return (self::isTestingOnOcis() || self::isTestingOnReva());
}
/**
* @return bool
*/
public static function isTestingOnOc10():bool {
return (!self::isTestingOnOcisOrReva());
return false;
}
/**
@@ -307,7 +300,7 @@ class OcisHelper {
*/
private static function getOcisRevaDataRoot():string {
$root = \getenv("OCIS_REVA_DATA_ROOT");
if (($root === false || $root === "") && self::isTestingOnOcisOrReva()) {
if ($root === false || $root === "") {
$root = "/var/tmp/ocis/owncloud/";
}
if (!\file_exists($root)) {
+2 -171
View File
@@ -789,54 +789,7 @@ class SetupHelper extends \PHPUnit\Framework\Assert {
?string $adminPassword = null,
?string $baseUrl = null
):array {
if (OcisHelper::isTestingOnOcisOrReva()) {
return [];
}
$baseUrl = self::checkBaseUrl($baseUrl, "runOcc");
$adminUsername = self::checkAdminUsername($adminUsername, "runOcc");
$adminPassword = self::checkAdminPassword($adminPassword, "runOcc");
if (!\is_array($commands)) {
throw new Exception("commands must be an array");
}
$isTestingAppEnabledText = "Is the testing app installed and enabled?\n";
$bodies = [];
foreach ($commands as $occ) {
if (!\array_key_exists('command', $occ)) {
throw new \InvalidArgumentException("command key is missing in array passed to runBulkOcc");
}
$body = [
'command' => \implode(' ', $occ['command'])
];
if (isset($occ['envVariables'])) {
$body['env_variables'] = $occ['envVariables'];
}
\array_push($bodies, $body);
}
try {
$result = OcsApiHelper::sendRequest(
$baseUrl,
$adminUsername,
$adminPassword,
"POST",
"/apps/testing/api/v1/occ/bulk?format=json",
$xRequestId,
\json_encode($bodies)
);
} catch (ServerException $e) {
throw new Exception(
"Could not execute 'occ'. " .
$isTestingAppEnabledText .
$e->getResponse()->getBody()
);
}
$result = \json_decode($result->getBody()->getContents());
return $result->ocs->data;
return [];
}
/**
@@ -864,129 +817,7 @@ class SetupHelper extends \PHPUnit\Framework\Assert {
?string $ocPath = null,
?array $envVariables = null
):array {
if (OcisHelper::isTestingOnOcisOrReva() && !OcisHelper::isTestingParallelDeployment()) {
return ['code' => '', 'stdOut' => '', 'stdErr' => '' ];
}
$baseUrl = self::checkBaseUrl($baseUrl, "runOcc");
$adminUsername = self::checkAdminUsername($adminUsername, "runOcc");
$adminPassword = self::checkAdminPassword($adminPassword, "runOcc");
if (self::$ocPath === null
&& $ocPath === null
) {
throw new Exception(
"runOcc called without ocPath - pass the ocPath or call SetupHelper::init"
);
}
if ($ocPath === null) {
$ocPath = self::$ocPath;
} else {
$ocPath = self::normaliseOcPath($ocPath);
}
$body = [];
$argsString = \implode(' ', $args);
$body['command'] = $argsString;
if ($envVariables !== null) {
$body['env_variables'] = $envVariables;
}
$isTestingAppEnabledText = "Is the testing app installed and enabled?\n";
try {
$result = OcsApiHelper::sendRequest(
$baseUrl,
$adminUsername,
$adminPassword,
"POST",
$ocPath,
$xRequestId,
$body
);
} catch (ServerException $e) {
throw new Exception(
"Could not execute 'occ'. " .
$isTestingAppEnabledText .
$e->getResponse()->getBody()
);
}
$return = [];
$contents = $result->getBody()->getContents();
$resultXml = \simplexml_load_string($contents);
if ($resultXml === false) {
$status = $result->getStatusCode();
throw new Exception(
"Response is not valid XML after executing 'occ $argsString'. " .
"HTTP status was $status. " .
$isTestingAppEnabledText .
"Response contents were '$contents'"
);
}
$return['code'] = $resultXml->xpath("//ocs/data/code");
$return['stdOut'] = $resultXml->xpath("//ocs/data/stdOut");
$return['stdErr'] = $resultXml->xpath("//ocs/data/stdErr");
if (!isset($return['code'][0])) {
throw new Exception(
"Return code not found after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
if (!isset($return['stdOut'][0])) {
throw new Exception(
"Return stdOut not found after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
if (!isset($return['stdErr'][0])) {
throw new Exception(
"Return stdErr not found after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
if (!\is_a($return['code'][0], "SimpleXMLElement")) {
throw new Exception(
"Return code is not a SimpleXMLElement after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
if (!\is_a($return['stdOut'][0], "SimpleXMLElement")) {
throw new Exception(
"Return stdOut is not a SimpleXMLElement after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
if (!\is_a($return['stdErr'][0], "SimpleXMLElement")) {
throw new Exception(
"Return stdErr is not a SimpleXMLElement after executing 'occ $argsString'. " .
$isTestingAppEnabledText .
$contents
);
}
$return['code'] = $return['code'][0]->__toString();
$return['stdOut'] = $return['stdOut'][0]->__toString();
$return['stdErr'] = $return['stdErr'][0]->__toString();
self::resetOpcache(
$baseUrl,
$adminUsername,
$adminPassword,
$xRequestId
);
return $return;
return ['code' => '', 'stdOut' => '', 'stdErr' => '' ];
}
/**