Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Logging for Flakey Workspace Cleanup Test and removal of Sync over Async while deleting a workspace #740

Merged
merged 1 commit into from
Dec 14, 2023
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Halibut;
using Octopus.Tentacle.Client;
Expand Down Expand Up @@ -53,10 +54,16 @@ public async ValueTask DisposeAsync()
{
SafelyMoveTentacleLogFileToSharedLocation();

logger.Information("****** ****** ****** ****** ****** ****** ******");
logger.Information("****** CLIENT AND TENTACLE DISPOSE CALLED *****");
logger.Information("* Subsequent errors should be ignored *");
logger.Information("****** ****** ****** ****** ****** ****** ******");
var banner = new StringBuilder();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly easier to read log
image

banner.AppendLine("");
banner.AppendLine("");

banner.AppendLine("****** ****** ****** ****** ****** ****** ******");
banner.AppendLine("****** CLIENT AND TENTACLE DISPOSE CALLED *****");
banner.AppendLine("* Subsequent errors should be ignored *");
banner.AppendLine("****** ****** ****** ****** ****** ****** ******");

logger.Information(banner.ToString());

logger.Information("Starting DisposeAsync");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Text;
using Octopus.Tentacle.Client.Scripts;
using Octopus.Tentacle.Contracts;
using Serilog;

namespace Octopus.Tentacle.Tests.Integration.Support.ExtensionMethods
{
public static class ScriptExecutionResultExtensionMethods
{
public static void LogExecuteScriptOutput(this (ScriptExecutionResult ScriptExecutionResult, List<ProcessOutput> ProcessOutput) result, ILogger logger)
Copy link
Contributor Author

@nathanwoctopusdeploy nathanwoctopusdeploy Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

{
var scriptOutput = new StringBuilder();

scriptOutput.AppendLine("");
scriptOutput.AppendLine("");
scriptOutput.AppendLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
scriptOutput.AppendLine("~~ Start Execute Script Output ~~");
scriptOutput.AppendLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

scriptOutput.AppendLine($"ScriptExecutionResult.State: {result.ScriptExecutionResult.State}");
scriptOutput.AppendLine($"ScriptExecutionResult.ExitCode: {result.ScriptExecutionResult.ExitCode}");
scriptOutput.AppendLine("");

result.ProcessOutput.ForEach(x => scriptOutput.AppendLine($"{x.Source} {x.Occurred} {x.Text}"));

scriptOutput.AppendLine($@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
scriptOutput.AppendLine($@"~~~ End Execute Script Output ~~~");
scriptOutput.AppendLine($@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

logger.Information(scriptOutput.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Halibut.Exceptions;
Expand All @@ -10,6 +9,7 @@
using Octopus.Tentacle.CommonTestUtils.Builders;
using Octopus.Tentacle.Contracts;
using Octopus.Tentacle.Tests.Integration.Support;
using Octopus.Tentacle.Tests.Integration.Support.ExtensionMethods;
using Octopus.Tentacle.Tests.Integration.Support.TestAttributes;
using Octopus.Tentacle.Tests.Integration.Util.Builders;

Expand Down Expand Up @@ -53,11 +53,7 @@ public async Task WhenRunningTentacleAsAServiceItShouldBeAbleToRestartItself(Ten
result = await clientAndTentacle.TentacleClient.ExecuteScript(startScriptCommand, CancellationToken);
}

var scriptOutput = new StringBuilder();
result.ProcessOutput.ForEach(x => scriptOutput.AppendLine($"{x.Source} {x.Occurred} {x.Text}"));

Logger.Information($@"Script Output:
{scriptOutput}");
result.LogExecuteScriptOutput(Logger);

result.ProcessOutput.Any(x => x.Text.Contains("Stopping service")).Should().BeTrue("Stopping service should be logged");
result.ScriptExecutionResult.State.Should().Be(ProcessState.Complete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using FluentAssertions;
using NUnit.Framework;
using Octopus.Tentacle.CommonTestUtils.Builders;
using Octopus.Tentacle.Contracts;
using Octopus.Tentacle.Contracts.ClientServices;
using Octopus.Tentacle.Scripts;
using Octopus.Tentacle.Tests.Integration.Support;
using Octopus.Tentacle.Tests.Integration.Support.ExtensionMethods;
using Octopus.Tentacle.Tests.Integration.Util;
using Octopus.Tentacle.Tests.Integration.Util.Builders;
using Octopus.Tentacle.Tests.Integration.Util.Builders.Decorators;
Expand Down Expand Up @@ -49,9 +51,13 @@ public async Task WhenScriptServiceIsRunning_ThenWorkspaceIsNotDeleted(TentacleC
Directory.Exists(startScriptWorkspaceDirectory).Should().BeTrue("Workspace should not have been cleaned up");

File.WriteAllText(waitBeforeCompletingScriptFile, "Write file that makes script continue executing");
await runningScriptTask;
var runningScriptResult = await runningScriptTask;

Directory.Exists(startScriptWorkspaceDirectory).Should().BeFalse("Workspace should be naturally cleaned up after completion");
runningScriptResult.LogExecuteScriptOutput(Logger);

runningScriptResult.ScriptExecutionResult.ExitCode.Should().Be(0, "Script should have completed successfully");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to ensure everything works in the test and there is not another side effect causing the test to fail

runningScriptResult.ScriptExecutionResult.State.Should().Be(ProcessState.Complete, "Script should have completed successfully");
Directory.Exists(startScriptWorkspaceDirectory).Should().BeFalse($"Workspace {startScriptWorkspaceDirectory} should have been cleaned up when CompleteScript was called");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public async Task GetStatusShouldReturnAnExitCodeOf46ForAScriptWithAnUnknownResu

response.ExitCode.Should().Be(-46);

CleanupWorkspace(ticket);
await CleanupWorkspace(ticket, CancellationToken.None);
}

[Test]
Expand All @@ -380,7 +380,7 @@ public async Task CancelScriptShouldReturnAnExitCodeOf46ForAScriptWithAnUnknownR

response.ExitCode.Should().Be(-46);

CleanupWorkspace(ticket);
await CleanupWorkspace(ticket, CancellationToken.None);
}

[Test]
Expand Down Expand Up @@ -472,10 +472,10 @@ private ScriptStateStore SetupScriptStateStore(ScriptTicket ticket)
return stateWorkspace;
}

private void CleanupWorkspace(ScriptTicket ticket)
private async Task CleanupWorkspace(ScriptTicket ticket, CancellationToken cancellationToken)
{
var workspace = workspaceFactory.GetWorkspace(ticket);
workspace.Delete();
await workspace.Delete(cancellationToken);
}

(StartScriptCommandV2 StartScriptCommand, FileInfo FileScriptWillCreate) GetStartScriptCommandForScriptThatCreatesAFile(ScriptTicket scriptTicket, int? scriptDelayInSeconds = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public async Task GetStatusShouldReturnAnExitCodeOf46ForAScriptWithAnUnknownResu

response.ExitCode.Should().Be(-46);

CleanupWorkspace(ticket);
await CleanupWorkspace(ticket, CancellationToken.None);
}

[Test]
Expand All @@ -376,7 +376,7 @@ public async Task CancelScriptShouldReturnAnExitCodeOf46ForAScriptWithAnUnknownR

response.ExitCode.Should().Be(-46);

CleanupWorkspace(ticket);
await CleanupWorkspace(ticket, CancellationToken.None);
}

[Test]
Expand Down Expand Up @@ -468,10 +468,10 @@ private ScriptStateStore SetupScriptStateStore(ScriptTicket ticket)
return stateWorkspace;
}

private void CleanupWorkspace(ScriptTicket ticket)
private async Task CleanupWorkspace(ScriptTicket ticket, CancellationToken cancellationToken)
{
var workspace = workspaceFactory.GetWorkspace(ticket);
workspace.Delete();
await workspace.Delete(cancellationToken);
}

(StartScriptCommandV3Alpha StartScriptCommand, FileInfo FileScriptWillCreate) GetStartScriptCommandForScriptThatCreatesAFile(ScriptTicket scriptTicket, int? scriptDelayInSeconds = null)
Expand Down
1 change: 0 additions & 1 deletion source/Octopus.Tentacle/Scripts/IScriptWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public interface IScriptWorkspace
string? ScriptMutexName { get; set; }
void BootstrapScript(string scriptBody);
string ResolvePath(string fileName);
void Delete();
Task Delete(CancellationToken cancellationToken);
IScriptLog CreateLog();
string LogFilePath { get; }
Expand Down
5 changes: 0 additions & 5 deletions source/Octopus.Tentacle/Scripts/ScriptWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ public string ResolvePath(string fileName)
return path;
}

public void Delete()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing Sync code path that does a Wait on an Async call as we can be fully async now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving FileSystem.DeleteDirectory sync method as it is used in other places in the tests. Future cleanup task.

{
FileSystem.DeleteDirectory(WorkingDirectory, DeletionOptions.TryThreeTimesIgnoreFailure);
}

public async Task Delete(CancellationToken cancellationToken)
{
await FileSystem.DeleteDirectory(WorkingDirectory, cancellationToken, DeletionOptions.TryThreeTimesIgnoreFailure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<ScriptStatusResponse> CompleteScriptAsync(CompleteScriptComman
cancellationTokens.TryRemove(command.Ticket.TaskId, out _);
var response = GetResponse(command.Ticket, script, command.LastLogSequence);
var workspace = workspaceFactory.GetWorkspace(command.Ticket);
workspace.Delete();
await workspace.Delete(cancellationToken);
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task CompleteScriptAsync(CompleteScriptCommandV2 command, Cancellat
}

var workspace = workspaceFactory.GetWorkspace(command.Ticket);
workspace.Delete();
await workspace.Delete(cancellationToken);
}

RunningScript LaunchShell(ScriptTicket ticket, string serverTaskId, IScriptWorkspace workspace, IScriptStateStore stateStore, CancellationToken cancellationToken)
Expand Down