-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BaseKubernetesApplyExecutor - Composition over inheritance (#1317)
Tidy up some Kube execution
- Loading branch information
Showing
6 changed files
with
226 additions
and
101 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
source/Calamari.Tests/KubernetesFixtures/Commands/Executors/GlobberGroupingTests.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,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Calamari.Common.Plumbing.FileSystem; | ||
using Calamari.Kubernetes.Commands; | ||
using Calamari.Kubernetes.Commands.Executors; | ||
using Calamari.Tests.Fixtures.Integration.FileSystem; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Calamari.Tests.KubernetesFixtures.Commands.Executors | ||
{ | ||
[TestFixture] | ||
public class GlobberGroupingTests | ||
{ | ||
readonly ICalamariFileSystem fileSystem = TestCalamariPhysicalFileSystem.GetPhysicalFileSystem(); | ||
string StagingDirectory => Path.Combine(tempDirectory, "staging"); | ||
string tempDirectory; | ||
string PackageDirectory => Path.Combine(tempDirectory, "staging", KubernetesDeploymentCommandBase.PackageDirectoryName); | ||
|
||
[Test] | ||
public void FilesInDirectoryWithGlob_StageAllFilesInDirectory() | ||
{ | ||
var dirName = "dirA"; | ||
var dirA = Path.Combine(PackageDirectory, dirName); | ||
CreateTemporaryTestFile(dirA); | ||
CreateTemporaryTestFile(dirA); | ||
|
||
var globber = new GlobberGrouping(fileSystem); | ||
|
||
var globDirectories = globber.Group(StagingDirectory, new List<string>(){ "dirA/*"}); | ||
globDirectories.Count().Should().Be(1); | ||
fileSystem.EnumerateFiles(globDirectories[0].Directory, globDirectories[0].Glob).Count().Should().Be(2); | ||
} | ||
|
||
[Test] | ||
public void GlobsAreSortedInOrderOfPattern() | ||
{ | ||
CreateTemporaryTestFile( Path.Combine(PackageDirectory, "dirB")); | ||
CreateTemporaryTestFile( Path.Combine(PackageDirectory, "dirA")); | ||
CreateTemporaryTestFile( Path.Combine(PackageDirectory, "dirC")); | ||
|
||
var globber = new GlobberGrouping(fileSystem); | ||
var globDirectories = globber.Group(StagingDirectory, new List<string>(){"dirC/*", "dirB/*", "dirA/*"}); | ||
|
||
globDirectories.Select(d => d.Glob).Should().BeEquivalentTo(new[] { "dirC/*", "dirB/*", "dirA/*" }); | ||
} | ||
|
||
[Test] | ||
public void EmptyGlobPatternReturnsNoResults() | ||
{ | ||
CreateTemporaryTestFile( Path.Combine(PackageDirectory, "dirB")); | ||
|
||
var globber = new GlobberGrouping(fileSystem); | ||
var globDirectories = globber.Group(StagingDirectory, new List<string>()); | ||
|
||
globDirectories.Should().BeEmpty(); | ||
} | ||
|
||
void CreateTemporaryTestFile(string directory) | ||
{ | ||
if (!Directory.Exists(directory)) | ||
Directory.CreateDirectory(directory); | ||
var path = Path.Combine(directory, Guid.NewGuid() + ".tmp"); | ||
using (fileSystem.OpenFile(path, FileMode.OpenOrCreate, FileAccess.Read)) | ||
{ | ||
} | ||
} | ||
|
||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
tempDirectory = fileSystem.CreateTemporaryDirectory(); | ||
} | ||
|
||
[TearDown] | ||
public void Cleanup() | ||
{ | ||
fileSystem.DeleteDirectory(tempDirectory, FailureOptions.IgnoreFailure); | ||
} | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
source/Calamari/Kubernetes/Commands/Executors/CommandResultWithOutputExtensionMethods.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,35 @@ | ||
#if !NET40 | ||
using System; | ||
using System.IO; | ||
using Calamari.Common.Plumbing.Logging; | ||
using Calamari.Kubernetes.Integration; | ||
|
||
namespace Calamari.Kubernetes.Commands.Executors | ||
{ | ||
public static class CommandResultWithOutputExtensionMethods | ||
{ | ||
public static void LogErrorsWithSanitizedDirectory(this CommandResultWithOutput commandResult, ILog log, string directory) | ||
{ | ||
var directoryWithTrailingSlash = directory + Path.DirectorySeparatorChar; | ||
|
||
foreach (var message in commandResult.Output.Messages) | ||
{ | ||
switch (message.Level) | ||
{ | ||
case Level.Info: | ||
//No need to log as it's the output json from above. | ||
break; | ||
case Level.Error: | ||
//Files in the error are shown with the full path in their batch directory, | ||
//so we'll remove that for the user. | ||
log.Error(message.Text.Replace($"{directoryWithTrailingSlash}", "")); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
#endif |
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
Oops, something went wrong.