Skip to content

Commit

Permalink
Adds secret argument values support
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaMarkic committed Dec 24, 2017
1 parent a1a5f2f commit fbe8167
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 32 deletions.
50 changes: 40 additions & 10 deletions src/Cake.Docker.Tests/ArgumentsBuilderExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Cake.Docker.Tests
public class ArgumentsBuilderExtensionTest
{
public static PropertyInfo StringProperty => GetProperty(nameof(TestSettings.String));
public static PropertyInfo PasswordProperty => GetProperty(nameof(TestSettings.Password));
public static PropertyInfo StringsProperty => GetProperty(nameof(TestSettings.Strings));
public static PropertyInfo NullableIntProperty => GetProperty(nameof(TestSettings.NullableInt));
public static PropertyInfo NullableInt64Property => GetProperty(nameof(TestSettings.NullableInt64));
Expand Down Expand Up @@ -49,32 +50,52 @@ public class GetArgumentFromStringProperty
[Test]
public void WhenGivenStringProperty_FormatsProperly()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromStringProperty(StringProperty, "tubo");
var actual = ArgumentsBuilderExtension.GetArgumentFromStringProperty(StringProperty, "tubo", isSecret: false).Value;

Assert.That(actual, Is.EqualTo("--string \"tubo\""));
Assert.That(actual.Key, Is.EqualTo("--string"));
Assert.That(actual.Value, Is.EqualTo("tubo"));
Assert.That(actual.Quoting, Is.EqualTo(DockerArgumentQuoting.Quoted));
}
[Test]
public void WhenGivenNull_NullIsReturned()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromStringProperty(StringProperty, null);
var actual = ArgumentsBuilderExtension.GetArgumentFromStringProperty(StringProperty, null, isSecret: false);

Assert.That(actual, Is.Null);
}
}
[TestFixture]
public class GetArgumentFromPasswordProperty
{
[Test]
public void WhenGivenStringProperty_FormatsProperly()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromStringProperty(StringProperty, "tubo", isSecret: true).Value;

Assert.That(actual.Key, Is.EqualTo("--string"));
Assert.That(actual.Value, Is.EqualTo("tubo"));
Assert.That(actual.Quoting, Is.EqualTo(DockerArgumentQuoting.QuotedSecret));
}
}
[TestFixture]
public class GetArgumentFromStringArrayProperty
{
[Test]
public void WhenGivenStringArrayProperty_FormatsProperly()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromStringArrayProperty(StringsProperty, new string[] { "tubo1", "tubo2" });

Assert.AreEqual(actual.ToArray(), new string[] { "--strings \"tubo1\"", "--strings \"tubo2\"" });
var actual = ArgumentsBuilderExtension.GetArgumentFromStringArrayProperty(StringsProperty, new string[] { "tubo1", "tubo2" }, isSecret: false);

//Assert.AreEqual(actual.ToArray(), new DockerArgument[] {
// "--strings \"tubo1\"", "--strings \"tubo2\""
//});
CollectionAssert.AreEqual(actual, new DockerArgument[] {
new DockerArgument("--strings", "tubo1", DockerArgumentQuoting.Quoted),
new DockerArgument("--strings", "tubo2", DockerArgumentQuoting.Quoted)});
}
[Test]
public void WhenGivenNull_EmptyArrayReturned()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromStringArrayProperty(StringsProperty, null);
var actual = ArgumentsBuilderExtension.GetArgumentFromStringArrayProperty(StringsProperty, null, isSecret: false);

Assert.That(actual, Is.Empty);
}
Expand All @@ -85,14 +106,18 @@ public class GetArgumentFromDictionaryProperty
[Test]
public void WhenGivenStringArrayProperty_FormatsProperly()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromDictionaryProperty(StringsProperty, new Dictionary<string, string> { { "t1", "v1" }, { "t2", "v2" } });
var actual = ArgumentsBuilderExtension.GetArgumentFromDictionaryProperty(
StringsProperty, new Dictionary<string, string> { { "t1", "v1" }, { "t2", "v2" } }, isSecret: false);

Assert.AreEqual(actual.ToArray(), new string[] { "--strings \"t1=v1\"", "--strings \"t2=v2\"" });
CollectionAssert.AreEqual(actual, new DockerArgument[] {
new DockerArgument("--strings", "t1=v1", DockerArgumentQuoting.Quoted),
new DockerArgument("--strings","t2=v2", DockerArgumentQuoting.Quoted),
});
}
[Test]
public void WhenGivenNull_EmptyArrayReturned()
{
var actual = ArgumentsBuilderExtension.GetArgumentFromDictionaryProperty(StringsProperty, null);
var actual = ArgumentsBuilderExtension.GetArgumentFromDictionaryProperty(StringsProperty, null, isSecret: false);

Assert.That(actual, Is.Empty);
}
Expand Down Expand Up @@ -320,6 +345,7 @@ public class TestSettings: AutoToolSettings
{
public string String { get; set; }
public string[] Strings { get; set; }
public string Password { get; set; }
public int? NullableInt { get; set; }
public Int64? NullableInt64 { get; set; }
public UInt64? NullableUInt64 { get; set; }
Expand All @@ -333,5 +359,9 @@ public class TestSettings: AutoToolSettings
public bool DecoratedBool { get; set; }
[AutoProperty(Format = "-e {1}")]
public string[] DecoratedStrings { get; set; }
protected override string[] CollectSecretProperties()
{
return new[] { nameof(Password) };
}
}
}
50 changes: 50 additions & 0 deletions src/Cake.Docker.Tests/DockerArgumentComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections;

namespace Cake.Docker.Tests
{
public class DockerArgumentComparer : IComparer
{
public int Compare(object x, object y)
{
if (ReferenceEquals(x, null) || !(x is DockerArgument))
{
throw new ArgumentException(nameof(x));
}
if (ReferenceEquals(y, null) ||!(y is DockerArgument))
{
throw new ArgumentNullException(nameof(y));
}
var a = (DockerArgument)x;
var b = (DockerArgument)y;
int keyCompare = CompareStrings(a.Key, b.Key);
if (keyCompare != 0)
{
return keyCompare;
}
int valueCompare = CompareStrings(a.Value, b.Value);
if (valueCompare != 0)
{
return valueCompare;
}
return a.Quoting.CompareTo(b.Quoting);

}
static int CompareStrings(string a, string b)
{
if (a == b)
{
return 0;
}
if (a == null)
{
return 1;
}
if (b == null)
{
return -1;
}
return a.CompareTo(b);
}
}
}
47 changes: 47 additions & 0 deletions src/Cake.Docker.Tests/Registry/Login/DockerLoginTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Cake.Core.IO;
using Cake.Docker.Tests.Build;
using NUnit.Framework;

namespace Cake.Docker.Tests.Registry.Login
{
[TestFixture]
public class DockerLoginTest
{
[Test]
public void WhenOnlyPathIsSet_CommandLineIsCorrect()
{
var fixture = new DockerRegistryLoginFixture
{
Settings = new DockerRegistryLoginSettings(),
Path = "path"
};

var actual = fixture.Run();

Assert.That(actual.Args, Is.EqualTo("login path"));
}
[Test]
public void WhenOnlyUsernameIsSet_CommandLineIsCorrect()
{
var fixture = new DockerRegistryLoginFixture
{
Settings = new DockerRegistryLoginSettings { Username = "Tubo" },
Path = "path"
};

var actual = fixture.Run();

Assert.That(actual.Args, Is.EqualTo("login --username \"Tubo\" path"));
}
[Test]
public void WhenOnlyPasswordIsSet_ArgumentIsRedacted()
{
var builder = new ProcessArgumentBuilder();
builder.AppendAll("login", new DockerRegistryLoginSettings { Password = "Tubo" }, new string[0]);

var actual = builder.RenderSafe();

Assert.That(actual, Is.EqualTo("login --password \"[REDACTED]\""));
}
}
}
35 changes: 35 additions & 0 deletions src/Cake.Docker.Tests/Registry/Login/DockerRegistryLoginFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Cake.Docker;
using Cake.Testing.Fixtures;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;

namespace Cake.Docker.Tests.Build
{
public class DockerRegistryLoginFixture : ToolFixture<DockerRegistryLoginSettings>, ICakeContext
{
public string Path { get; set; }

IFileSystem ICakeContext.FileSystem => FileSystem;

ICakeEnvironment ICakeContext.Environment => Environment;

public ICakeLog Log => Log;

ICakeArguments ICakeContext.Arguments => throw new NotImplementedException();

IProcessRunner ICakeContext.ProcessRunner => ProcessRunner;

public IRegistry Registry => Registry;

public DockerRegistryLoginFixture(): base("docker")
{
ProcessRunner.Process.SetStandardOutput(new string[] { });
}
protected override void RunTool()
{
this.DockerLogin(Settings, Path);
}
}
}
Loading

0 comments on commit fbe8167

Please sign in to comment.