-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read Script Pod logs from K8s API Server (#867)
- Loading branch information
Showing
17 changed files
with
813 additions
and
833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
source/Octopus.Tentacle.Tests/Kubernetes/PodLogLineParserFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using Octopus.Tentacle.Contracts; | ||
using Octopus.Tentacle.Kubernetes; | ||
|
||
namespace Octopus.Tentacle.Tests.Kubernetes | ||
{ | ||
[TestFixture] | ||
public class PodLogLineParserFixture | ||
{ | ||
[TestCase("a|b|c", Reason = "Doesn't have 4 parts")] | ||
public void NotCorrectlyPipeDelimited(string line) | ||
{ | ||
var result = PodLogLineParser.ParseLine(line); | ||
result.Error.Should().Contain("delimited"); | ||
} | ||
|
||
[TestCase("a|b|c|d", Reason = "Not a line number")] | ||
public void FirstPartIsNotALineNumber(string line) | ||
{ | ||
var result = PodLogLineParser.ParseLine(line); | ||
result.Error.Should().Contain("line number"); | ||
} | ||
|
||
[TestCase("1|b|c|d", Reason = "Not a date")] | ||
public void SecondPartIsNotALineDate(string line) | ||
{ | ||
var result = PodLogLineParser.ParseLine(line); | ||
result.Error.Should().Contain("DateTimeOffset"); | ||
} | ||
|
||
[TestCase("1|2024-04-03T06:03:10.501025551Z|c|d", Reason = "Not a valid source")] | ||
public void ThirdPartIsNotAValidSource(string line) | ||
{ | ||
var result = PodLogLineParser.ParseLine(line); | ||
result.Error.Should().Contain("source"); | ||
} | ||
|
||
[Test] | ||
public void SimpleMessage() | ||
{ | ||
var logLine = PodLogLineParser.ParseLine("123|2024-04-03T06:03:10.501025551Z|stdout|This is the message").LogLine; | ||
logLine.Should().NotBeNull(); | ||
|
||
logLine.LineNumber.Should().Be(123); | ||
logLine.Source.Should().Be(ProcessOutputSource.StdOut); | ||
logLine.Message.Should().Be("This is the message"); | ||
logLine.Occurred.Should().BeCloseTo(new DateTimeOffset(2024, 4, 3, 6, 3, 10, 501, TimeSpan.Zero), TimeSpan.FromMilliseconds(1)); | ||
} | ||
|
||
[Test] | ||
public void ServiceMessage() | ||
{ | ||
var logLine = PodLogLineParser.ParseLine("123|2024-04-03T06:03:10.501025551Z|stdout|##octopus[stdout-verbose]").LogLine; | ||
logLine.Should().NotBeNull(); | ||
|
||
logLine.LineNumber.Should().Be(123); | ||
logLine.Source.Should().Be(ProcessOutputSource.StdOut); | ||
logLine.Message.Should().Be("##octopus[stdout-verbose]"); | ||
logLine.Occurred.Should().BeCloseTo(new DateTimeOffset(2024, 4, 3, 6, 3, 10, 501, TimeSpan.Zero), TimeSpan.FromMilliseconds(1)); | ||
} | ||
|
||
[Test] | ||
public void ErrorMessage() | ||
{ | ||
var logLine = PodLogLineParser.ParseLine("123|2024-04-03T06:03:10.501025551Z|stderr|Error!").LogLine; | ||
logLine.Should().NotBeNull(); | ||
|
||
logLine.LineNumber.Should().Be(123); | ||
logLine.Source.Should().Be(ProcessOutputSource.StdErr); | ||
logLine.Message.Should().Be("Error!"); | ||
logLine.Occurred.Should().BeCloseTo(new DateTimeOffset(2024, 4, 3, 6, 3, 10, 501, TimeSpan.Zero), TimeSpan.FromMilliseconds(1)); | ||
} | ||
|
||
[Test] | ||
public void MessageHasPipeInIt() | ||
{ | ||
var logLine = PodLogLineParser.ParseLine("123|2024-04-03T06:03:10.501025551Z|stdout|This is the me|ss|age").LogLine; | ||
logLine.Should().NotBeNull(); | ||
|
||
logLine.LineNumber.Should().Be(123); | ||
logLine.Source.Should().Be(ProcessOutputSource.StdOut); | ||
logLine.Message.Should().Be("This is the me|ss|age"); | ||
logLine.Occurred.Should().BeCloseTo(new DateTimeOffset(2024, 4, 3, 6, 3, 10, 501, TimeSpan.Zero), TimeSpan.FromMilliseconds(1)); | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
source/Octopus.Tentacle.Tests/Kubernetes/PodLogReaderFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using Octopus.Tentacle.Contracts; | ||
using Octopus.Tentacle.Kubernetes; | ||
|
||
namespace Octopus.Tentacle.Tests.Kubernetes | ||
{ | ||
[TestFixture] | ||
public class PodLogReaderFixture | ||
{ | ||
[TestCase(0, Reason = "Initial position")] | ||
[TestCase(4, Reason = "Subsequent position")] | ||
[TestCase(12387126, Reason = "Large position")] | ||
public async Task NoLines_SameSequenceNumber(long lastLogSequence) | ||
{ | ||
string[] podLines = Array.Empty<string>(); | ||
|
||
var reader = SetupReader(podLines); | ||
var result = await PodLogReader.ReadPodLogs(lastLogSequence, reader); | ||
result.NextSequenceNumber.Should().Be(lastLogSequence); | ||
result.Lines.Should().BeEmpty(); | ||
} | ||
|
||
[Test] | ||
public async Task FirstLine_SequenceNumberIncreasesByOne() | ||
{ | ||
string[] podLines = { | ||
"1|2024-04-03T06:03:10.517865655Z|stdout|Kubernetes Script Pod completed", | ||
}; | ||
|
||
var reader = SetupReader(podLines); | ||
var result = await PodLogReader.ReadPodLogs(0, reader); | ||
result.NextSequenceNumber.Should().Be(1); | ||
result.Lines.Should().BeEquivalentTo(new[] | ||
{ | ||
new ProcessOutput(ProcessOutputSource.StdOut, "Kubernetes Script Pod completed", DateTimeOffset.Parse("2024-04-03T06:03:10.517865655Z")) | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task ThreeSubsequentLines_SequenceNumberIncreasesByThree() | ||
{ | ||
string[] podLines = { | ||
"5|2024-04-03T06:03:10.517857755Z|stdout|##octopus[stdout-verbose]", | ||
"6|2024-04-03T06:03:10.517865655Z|stderr|Kubernetes Script Pod completed", | ||
"7|2024-04-03T06:03:10.517867355Z|stdout|##octopus[stdout-default]" | ||
}; | ||
|
||
var reader = SetupReader(podLines); | ||
var result = await PodLogReader.ReadPodLogs(4, reader); | ||
result.NextSequenceNumber.Should().Be(7); | ||
result.Lines.Should().BeEquivalentTo(new[] | ||
{ | ||
new ProcessOutput(ProcessOutputSource.StdOut, "##octopus[stdout-verbose]", DateTimeOffset.Parse("2024-04-03T06:03:10.517857755Z")), | ||
new ProcessOutput(ProcessOutputSource.StdErr, "Kubernetes Script Pod completed", DateTimeOffset.Parse("2024-04-03T06:03:10.517865655Z")), | ||
new ProcessOutput(ProcessOutputSource.StdOut, "##octopus[stdout-default]", DateTimeOffset.Parse("2024-04-03T06:03:10.517867355Z")), | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task StreamContainsPreviousLines_Deduplicates() | ||
{ | ||
string[] podLines = { | ||
"5|2024-04-03T06:03:10.517857755Z|stdout|##octopus[stdout-verbose]", | ||
"6|2024-04-03T06:03:10.517865655Z|stderr|Kubernetes Script Pod completed", | ||
"7|2024-04-03T06:03:10.517867355Z|stdout|##octopus[stdout-default]" | ||
}; | ||
|
||
var allTaskLogs = new List<ProcessOutput>(); | ||
var reader = SetupReader(podLines.Take(1).ToArray()); | ||
var result = await PodLogReader.ReadPodLogs(4, reader); | ||
result.NextSequenceNumber.Should().Be(5); | ||
allTaskLogs.AddRange(result.Lines); | ||
|
||
reader = SetupReader(podLines.ToArray()); | ||
result = await PodLogReader.ReadPodLogs(5, reader); | ||
result.NextSequenceNumber.Should().Be(7); | ||
allTaskLogs.AddRange(result.Lines); | ||
|
||
allTaskLogs.Should().BeEquivalentTo(new[] | ||
{ | ||
new ProcessOutput(ProcessOutputSource.StdOut, "##octopus[stdout-verbose]", DateTimeOffset.Parse("2024-04-03T06:03:10.517857755Z")), | ||
new ProcessOutput(ProcessOutputSource.StdErr, "Kubernetes Script Pod completed", DateTimeOffset.Parse("2024-04-03T06:03:10.517865655Z")), | ||
new ProcessOutput(ProcessOutputSource.StdOut, "##octopus[stdout-default]", DateTimeOffset.Parse("2024-04-03T06:03:10.517867355Z")), | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task MissingLine_Throws() | ||
{ | ||
string[] podLines = { | ||
"100|2024-04-03T06:03:10.517865655Z|stdout|Kubernetes Script Pod completed", | ||
}; | ||
|
||
var reader = SetupReader(podLines); | ||
Func<Task> action = async () => await PodLogReader.ReadPodLogs(50, reader); | ||
await action.Should().ThrowAsync<MissingPodLogException>(); | ||
} | ||
|
||
static StreamReader SetupReader(params string[] lines) | ||
{ | ||
return new StreamReader(new MemoryStream(Encoding.Default.GetBytes(string.Join("\n", lines)))); | ||
} | ||
} | ||
} |
Oops, something went wrong.