Skip to content

Commit

Permalink
TMP: Enable command logging for tests that sometimes fail on Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
jepio committed Dec 5, 2024
1 parent 5275302 commit 9735160
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/common/asb/Asb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2872,8 +2872,15 @@ static int RemediateEnsureDovecotCoreNotInstalled(char* value, void* log)
static int RemediateEnsureAuditdInstalled(char* value, void* log)
{
UNUSED(value);
return ((0 == InstallPackage(g_audit, log)) || (0 == InstallPackage(g_auditd, log)) ||
(0 == InstallPackage(g_auditLibs, log)) || (0 == InstallPackage(g_auditLibsDevel, log))) ? 0 : ENOENT;
int ret = 0;
bool state;
command_log_enable_save(state);
ret = ((0 == InstallPackage(g_audit, log)) || (0 == InstallPackage(g_auditd, log)) || (0 == InstallPackage(g_auditLibs, log)) ||
(0 == InstallPackage(g_auditLibsDevel, log))) ?
0 :
ENOENT;
command_log_disable_restore(state);
return ret;
}

static int RemediateEnsurePrelinkIsDisabled(char* value, void* log)
Expand All @@ -2891,15 +2898,23 @@ static int RemediateEnsureTalkClientIsNotInstalled(char* value, void* log)
static int RemediateEnsureCronServiceIsEnabled(char* value, void* log)
{
UNUSED(value);

return (((0 == InstallPackage(g_cron, log)) && EnableAndStartDaemon(g_cron, log)) ||
(((0 == InstallPackage(g_cronie, log)) && EnableAndStartDaemon(g_crond, log)))) ? 0 : ENOENT;
int ret = 0;
bool state;
command_log_enable_save(state);
ret = (((0 == InstallPackage(g_cron, log)) && EnableAndStartDaemon(g_cron, log)) ||
(((0 == InstallPackage(g_cronie, log)) && EnableAndStartDaemon(g_crond, log)))) ?
0 :
ENOENT;
command_log_disable_restore(state);
return ret;
}

static int RemediateEnsureAuditdServiceIsRunning(char* value, void* log)
{
int status = 0;
bool state;
UNUSED(value);
command_log_enable_save(state);
if ((0 != InstallPackage(g_audit, log)) && (0 != InstallPackage(g_auditd, log)) &&
(0 != InstallPackage(g_auditLibs, log)) && (0 != InstallPackage(g_auditLibsDevel, log)))
{
Expand All @@ -2911,6 +2926,7 @@ static int RemediateEnsureAuditdServiceIsRunning(char* value, void* log)
EnableAndStartDaemon(g_auditd, log);
status = CheckDaemonActive(g_auditd, NULL, log) ? 0 : ENOENT;
}
command_log_disable_restore(state);
return status;
}

Expand Down
15 changes: 13 additions & 2 deletions src/common/commonutils/CommonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ int RenameFile(const char* original, const char* target, void* log);

void SetCommandLogging(bool commandLogging);
bool IsCommandLoggingEnabled(void);

typedef int(*CommandCallback)(void* context);
#define command_log_enable_save(state) \
do \
{ \
state = IsCommandLoggingEnabled(); \
SetCommandLogging(true); \
} while (0)
#define command_log_disable_restore(state) \
do \
{ \
SetCommandLogging(state); \
} while (0)

typedef int (*CommandCallback)(void* context);

// If called from the main process thread the timeoutSeconds and callback arguments are ignored
int ExecuteCommand(void* context, const char* command, bool replaceEol, bool forJson, unsigned int maxTextResultBytes, unsigned int timeoutSeconds, char** textResult, CommandCallback callback, void* log);
Expand Down
4 changes: 3 additions & 1 deletion src/common/commonutils/DaemonUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool IsValidDaemonName(const char *name)
static int ExecuteSystemctlCommand(const char* command, const char* daemonName, void* log)
{
const char* commandTemplate = "systemctl %s %s";
char* textResult = NULL;
char* formattedCommand = NULL;
int result = 0;

Expand All @@ -56,7 +57,8 @@ static int ExecuteSystemctlCommand(const char* command, const char* daemonName,
return ENOMEM;
}

result = ExecuteCommand(NULL, formattedCommand, false, false, 0, 0, NULL, NULL, log);
result = ExecuteCommand(NULL, formattedCommand, false, false, 0, 0, &textResult, NULL, log);
FREE_MEMORY(textResult);
FREE_MEMORY(formattedCommand);
return result;
}
Expand Down
14 changes: 10 additions & 4 deletions src/common/commonutils/PackageUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static void CheckPackageManagersPresence(void* log)
static int CheckOrInstallPackage(const char* commandTemplate, const char* packageManager, const char* packageName, void* log)
{
char* command = NULL;
char* textResult = NULL;
int status = ENOENT;

if ((NULL == commandTemplate) || (NULL == packageManager) || (NULL == packageName) || ((0 == strlen(packageName))))
Expand All @@ -80,10 +81,11 @@ static int CheckOrInstallPackage(const char* commandTemplate, const char* packag
return ENOMEM;
}

status = ExecuteCommand(NULL, command, false, false, 0, 0, NULL, NULL, log);
status = ExecuteCommand(NULL, command, false, false, 0, 0, &textResult, NULL, log);

OsConfigLogInfo(log, "Package manager '%s' command '%s' complete with %d (errno: %d)", packageManager, command, status, errno);

FREE_MEMORY(textResult);
FREE_MEMORY(command);

return status;
Expand Down Expand Up @@ -182,13 +184,15 @@ int CheckPackageNotInstalled(const char* packageName, char** reason, void* log)
void AptGetUpdateOnce(void* log)
{
const char* command = "apt-get update";
char* textResult = NULL;
int status = 0;
bool state;
if (g_aptGetUpdateExecuted)
{
return;
}

if (0 == (status = ExecuteCommand(NULL, command, false, false, 0, 0, NULL, NULL, log)))
command_log_enable_save(state);
if (0 == (status = ExecuteCommand(NULL, command, false, false, 0, 0, &textResult, NULL, log)))
{
OsConfigLogInfo(log, "AptGetUpdateOnce: '%s' was successful", command);
g_aptGetUpdateExecuted = true;
Expand All @@ -197,6 +201,8 @@ void AptGetUpdateOnce(void* log)
{
OsConfigLogError(log, "AptGetUpdateOnce: '%s' failed with %d", command, status);
}
command_log_disable_restore(state);
FREE_MEMORY(textResult);
}

int InstallOrUpdatePackage(const char* packageName, void* log)
Expand Down

0 comments on commit 9735160

Please sign in to comment.