-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
255 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using Cake.Core.Tooling; | ||
|
||
namespace Cake.Openshift.Delete | ||
{ | ||
/// <summary> | ||
/// The openshift delete command | ||
/// </summary> | ||
public class OpenshiftDeleter : OpenshiftTool<OpenshiftDeleterSettings> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OpenshiftDeleter"/> class. | ||
/// </summary> | ||
/// <param name="fileSystem">The file system.</param> | ||
/// <param name="environment">The environment.</param> | ||
/// <param name="processRunner">The process runner.</param> | ||
/// <param name="tools">The tool locator.</param> | ||
public OpenshiftDeleter(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools) | ||
: base(fileSystem, environment, processRunner, tools) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Run the delete command | ||
/// </summary> | ||
/// <param name="settings">The settings.</param> | ||
public void Run(OpenshiftDeleterSettings settings) | ||
{ | ||
Check.NotNull(settings, nameof(settings)); | ||
|
||
RunCommand(settings, GetArguments(settings)); | ||
} | ||
|
||
private ProcessArgumentBuilder GetArguments(OpenshiftDeleterSettings settings) | ||
{ | ||
var builder = new ProcessArgumentBuilder(); | ||
|
||
builder.Append("delete"); | ||
|
||
if (!string.IsNullOrEmpty(settings.ObjectType)) | ||
{ | ||
builder.Append(settings.ObjectType); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(settings.ObjectName)) | ||
{ | ||
builder.Append(settings.ObjectName); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(settings.Label)) | ||
{ | ||
builder.AppendSwitchQuoted("--selector", "=", settings.Label); | ||
} | ||
|
||
if (settings.All) | ||
{ | ||
builder.Append("--all"); | ||
} | ||
|
||
// no need to specify "ignore-not-found" when the all option is set | ||
if (settings.IgnoreNotFound && !settings.All) | ||
{ | ||
builder.Append("--ignore-not-found"); | ||
} | ||
|
||
return builder; | ||
} | ||
} | ||
} |
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,38 @@ | ||
namespace Cake.Openshift.Delete | ||
{ | ||
/// <summary> | ||
/// Contains settings used by <see cref="OpenshiftDeleter" />. | ||
/// </summary> | ||
public class OpenshiftDeleterSettings : OpenshiftSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the object type to delete. | ||
/// </summary> | ||
/// <remarks> | ||
/// Can represent multiple object types by using a comma separated list | ||
/// </remarks> | ||
public string ObjectType { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the object to delete. | ||
/// </summary> | ||
public string ObjectName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether to select all resources in the namespace of the specified resource types. | ||
/// </summary> | ||
/// <c>true</c> to select all resources in the namespace of the specified resource types; otherwise, <c>false</c>. | ||
public bool All { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the selector (label query) to filter on. | ||
/// </summary> | ||
public string Label { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether to treat "resource not found" as a successful delete. Defaults to "true" when <see cref="All"/> is specified. | ||
/// </summary> | ||
/// <c>true</c> to treat "resource not found" as a successful delete; otherwise, <c>false</c>. | ||
public bool IgnoreNotFound { get; set; } | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.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,13 @@ | ||
using Cake.Openshift.Delete; | ||
|
||
namespace Cake.Openshift.Tests.Delete | ||
{ | ||
public class OpenshiftDeleterFixture : OpenshiftFixture<OpenshiftDeleterSettings> | ||
{ | ||
protected override void RunTool() | ||
{ | ||
var deleter = new OpenshiftDeleter(FileSystem, Environment, ProcessRunner, Tools); | ||
deleter.Run(Settings); | ||
} | ||
} | ||
} |
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,97 @@ | ||
using Cake.Core; | ||
using Cake.Openshift.Delete; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using System; | ||
|
||
namespace Cake.Openshift.Tests.Delete | ||
{ | ||
public sealed class OpenshiftDeleterTests | ||
{ | ||
[TestClass] | ||
public sealed class TheRunMethod | ||
{ | ||
[TestMethod] | ||
public void Should_Throw_If_Settings_Are_Null() => ToolTests.The_Run_Method_Should_Throw_If_Settings_Are_Null(new OpenshiftDeleterFixture()); | ||
|
||
[TestMethod] | ||
public void Should_Add_ObjectType_And_ObjectName() | ||
{ | ||
// Given | ||
var fixture = new OpenshiftDeleterFixture(); | ||
fixture.Settings.ObjectType = "pod"; | ||
fixture.Settings.ObjectName = "node-1-vsjnm"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
result.Args.Should().Be($"delete {fixture.Settings.ObjectType} {fixture.Settings.ObjectName}"); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Not_Add_IgnoreNotFound_If_All_Option_Set() | ||
{ | ||
// Given | ||
var fixture = new OpenshiftDeleterFixture(); | ||
fixture.Settings.All = true; | ||
fixture.Settings.Label = "app=appName"; | ||
fixture.Settings.IgnoreNotFound = true; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
result.Args.Should().Be($"delete --selector={fixture.Settings.Label.Quote()} --all"); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Add_IgnoreNotFound_If_All_Option_Not_Set() | ||
{ | ||
// Given | ||
var fixture = new OpenshiftDeleterFixture(); | ||
fixture.Settings.IgnoreNotFound = true; | ||
fixture.Settings.Label = "app=appName"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
result.Args.Should().Be($"delete --selector={fixture.Settings.Label.Quote()} --ignore-not-found"); | ||
} | ||
} | ||
|
||
[TestClass] | ||
public sealed class TheAliases | ||
{ | ||
[TestMethod] | ||
public void Should_Throw_If_Context_Is_Null() | ||
{ | ||
// Given | ||
ICakeContext context = null; | ||
|
||
// When | ||
Action action = () => OpenshiftAliases.OpenshiftDelete(context, new OpenshiftDeleterSettings()); | ||
|
||
// Then | ||
action.Should().Throw<ArgumentNullException>() | ||
.Which.ParamName.Should().Be("context"); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Throw_If_Settings_Is_Null() | ||
{ | ||
// Given | ||
ICakeContext context = Mock.Of<ICakeContext>(); | ||
|
||
// When | ||
Action action = () => OpenshiftAliases.OpenshiftDelete(context, null); | ||
|
||
// Then | ||
action.Should().Throw<ArgumentNullException>() | ||
.Which.ParamName.Should().Be("settings"); | ||
} | ||
} | ||
} | ||
} |