Skip to content

Commit

Permalink
closes #14 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored Nov 9, 2017
1 parent 5ed6bbf commit e008d40
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/Cake.Openshift/Delete/OpenshiftDeleter.cs
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;
}
}
}
38 changes: 38 additions & 0 deletions src/Cake.Openshift/Delete/OpenshiftDeleterSettings.cs
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; }
}
}
37 changes: 37 additions & 0 deletions src/Cake.Openshift/OpenshiftAliases.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Openshift.Delete;
using Cake.Openshift.Login;
using Cake.Openshift.StartBuild;

Expand Down Expand Up @@ -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);
}

/// <summary>
/// Deletes an openshift resource.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="settings">The settings</param>
/// <example>
/// <code>
/// <para>Cake task:</para>
/// <![CDATA[
/// Task("Openshift-Delete")
/// .Does(() =>
/// {
/// OpenshiftDelete(buildConfig, new OpenshiftDeleterSettings
/// {
/// ObjectType = "pod",
/// ObjectName = "node-1-vsjnm",
/// All = true,
/// Label = "app=appName",
/// IgnoreNotFound = true
/// });
/// });
/// ]]>
/// </code>
/// </example>
[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);
}
}
}
13 changes: 13 additions & 0 deletions test/Cake.Openshift.Tests/Delete/OpenshiftDeleterFixture.cs
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);
}
}
}
97 changes: 97 additions & 0 deletions test/Cake.Openshift.Tests/Delete/OpenshiftDeleterTests.cs
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");
}
}
}
}

0 comments on commit e008d40

Please sign in to comment.