From e008d402ae135de80d59e51a21dbedbaef842943 Mon Sep 17 00:00:00 2001 From: Meir017 Date: Thu, 9 Nov 2017 18:04:48 +0200 Subject: [PATCH] closes #14 (#17) --- src/Cake.Openshift/Delete/OpenshiftDeleter.cs | 70 +++++++++++++ .../Delete/OpenshiftDeleterSettings.cs | 38 ++++++++ src/Cake.Openshift/OpenshiftAliases.cs | 37 +++++++ .../Delete/OpenshiftDeleterFixture.cs | 13 +++ .../Delete/OpenshiftDeleterTests.cs | 97 +++++++++++++++++++ 5 files changed, 255 insertions(+) create mode 100644 src/Cake.Openshift/Delete/OpenshiftDeleter.cs create mode 100644 src/Cake.Openshift/Delete/OpenshiftDeleterSettings.cs create mode 100644 test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.cs create mode 100644 test/Cake.Openshift.Tests/Delete/OpenshiftDeleterTests.cs diff --git a/src/Cake.Openshift/Delete/OpenshiftDeleter.cs b/src/Cake.Openshift/Delete/OpenshiftDeleter.cs new file mode 100644 index 0000000..cb1e3a1 --- /dev/null +++ b/src/Cake.Openshift/Delete/OpenshiftDeleter.cs @@ -0,0 +1,70 @@ +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Openshift.Delete +{ + /// + /// The openshift delete command + /// + public class OpenshiftDeleter : OpenshiftTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public OpenshiftDeleter(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools) + : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Run the delete command + /// + /// The settings. + 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; + } + } +} diff --git a/src/Cake.Openshift/Delete/OpenshiftDeleterSettings.cs b/src/Cake.Openshift/Delete/OpenshiftDeleterSettings.cs new file mode 100644 index 0000000..cbdb4c4 --- /dev/null +++ b/src/Cake.Openshift/Delete/OpenshiftDeleterSettings.cs @@ -0,0 +1,38 @@ +namespace Cake.Openshift.Delete +{ + /// + /// Contains settings used by . + /// + public class OpenshiftDeleterSettings : OpenshiftSettings + { + /// + /// Gets or sets the object type to delete. + /// + /// + /// Can represent multiple object types by using a comma separated list + /// + public string ObjectType { get; set; } + + /// + /// Gets or sets the name of the object to delete. + /// + public string ObjectName { get; set; } + + /// + /// Gets or sets a value indicating whether to select all resources in the namespace of the specified resource types. + /// + /// true to select all resources in the namespace of the specified resource types; otherwise, false. + public bool All { get; set; } + + /// + /// Gets or sets the selector (label query) to filter on. + /// + public string Label { get; set; } + + /// + /// Gets or sets a value indicating whether to treat "resource not found" as a successful delete. Defaults to "true" when is specified. + /// + /// true to treat "resource not found" as a successful delete; otherwise, false. + public bool IgnoreNotFound { get; set; } + } +} \ No newline at end of file diff --git a/src/Cake.Openshift/OpenshiftAliases.cs b/src/Cake.Openshift/OpenshiftAliases.cs index f7566ae..29200c5 100644 --- a/src/Cake.Openshift/OpenshiftAliases.cs +++ b/src/Cake.Openshift/OpenshiftAliases.cs @@ -1,5 +1,6 @@ using Cake.Core; using Cake.Core.Annotations; +using Cake.Openshift.Delete; using Cake.Openshift.Login; using Cake.Openshift.StartBuild; @@ -143,5 +144,41 @@ public static void OpenshiftStartBuild(this ICakeContext context, string buildCo var buildStarter = new OpenshiftBuildStarter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); buildStarter.Run(buildConfig, settings); } + + /// + /// Deletes an openshift resource. + /// + /// The context. + /// The settings + /// + /// + /// Cake task: + /// + /// { + /// OpenshiftDelete(buildConfig, new OpenshiftDeleterSettings + /// { + /// ObjectType = "pod", + /// ObjectName = "node-1-vsjnm", + /// All = true, + /// Label = "app=appName", + /// IgnoreNotFound = true + /// }); + /// }); + /// ]]> + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Build")] + [CakeNamespaceImport("Cake.Openshift.Delete")] + public static void OpenshiftDelete(this ICakeContext context, OpenshiftDeleterSettings settings) + { + Check.NotNull(context, nameof(context)); + Check.NotNull(settings, nameof(settings)); + + var deleter = new OpenshiftDeleter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + deleter.Run(settings); + } } } diff --git a/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.cs b/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.cs new file mode 100644 index 0000000..4d0c21b --- /dev/null +++ b/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.cs @@ -0,0 +1,13 @@ +using Cake.Openshift.Delete; + +namespace Cake.Openshift.Tests.Delete +{ + public class OpenshiftDeleterFixture : OpenshiftFixture + { + protected override void RunTool() + { + var deleter = new OpenshiftDeleter(FileSystem, Environment, ProcessRunner, Tools); + deleter.Run(Settings); + } + } +} diff --git a/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterTests.cs b/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterTests.cs new file mode 100644 index 0000000..7c91bd4 --- /dev/null +++ b/test/Cake.Openshift.Tests/Delete/OpenshiftDeleterTests.cs @@ -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() + .Which.ParamName.Should().Be("context"); + } + + [TestMethod] + public void Should_Throw_If_Settings_Is_Null() + { + // Given + ICakeContext context = Mock.Of(); + + // When + Action action = () => OpenshiftAliases.OpenshiftDelete(context, null); + + // Then + action.Should().Throw() + .Which.ParamName.Should().Be("settings"); + } + } + } +}