Skip to content

Commit

Permalink
Merge pull request #244 from bCamba/feature-fluent-memory-limit
Browse files Browse the repository at this point in the history
Add fluent memory limit
  • Loading branch information
mariotoffia authored Mar 9, 2022
2 parents 0e00bfe + 38fbb8e commit 51418e5
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Numerics;
using Ductus.FluentDocker.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ductus.FluentDocker.Tests.ExtensionTests
{
[TestClass]
public class ConversionExtensionTests
{
[TestMethod]
[DataRow("")]
[DataRow(null)]
public void NullStringShallGiveMinimumValue(string input)
{
var num = input.Convert();
Assert.IsTrue(string.IsNullOrEmpty(input));
Assert.AreEqual(long.MinValue, num);
}

[TestMethod]
[DataRow("2googles")]
[DataRow("42p")]
[DataRow("wrongFormat42")]
[DataRow("-3498lfk")]
public void InvalidUnitInputShallGiveMinimumValue(string input)
{
var num = input.Convert();
Assert.AreEqual(long.MinValue, num);
}

[TestMethod]
public void LessThanLongMinimumValueShallGiveMinimumValue()
{
var lessThanMinimum = (new BigInteger(long.MinValue)) - 1;
var input = lessThanMinimum.ToString() + "g";

var num = input.Convert();
Assert.AreEqual(long.MinValue, num);
}

[TestMethod]
public void GreaterThanLongMaximumValueShallGiveMinimumValue()
{
var greaterThanMaximum = (new BigInteger(long.MaxValue)) + 1;
var input = greaterThanMaximum.ToString() + "g";

var num = input.Convert();
Assert.AreEqual(long.MinValue, num);
}

[TestMethod]
public void ValidByteInputShallGiveExactNumber()
{
var input = "42b";

var num = input.Convert();
Assert.AreEqual(42, num);
}

[TestMethod]
public void ValidKilobyteInputShallGiveCorrectKilobyteNumber()
{
var input = "42k";

var num = input.Convert();
Assert.AreEqual(43008, num);
}

[TestMethod]
public void ValidMegabyteInputShallGiveCorrectMegabyteNumber()
{
var input = "42m";

var num = input.Convert();
Assert.AreEqual(44040192, num);
}

[TestMethod]
public void ValidGigabyteInputShallGiveCorrectGigabyteNumber()
{
var input = "42g";

var num = input.Convert();
Assert.AreEqual(45097156608, num);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void VersionInfoShallBePossibleToRetrieve()
var v = Fd.Version();
Assert.IsTrue(v != null && v.Length > 0);
}

[TestMethod]
[TestCategory("CI")]
public void BuildContainerRenderServiceInStoppedMode()
Expand Down Expand Up @@ -554,5 +554,22 @@ public void ContainerWithUlimitsShallWork()
var config = container.GetConfiguration(true);
}
}

[TestMethod]
[TestCategory("CI")]
public void ContainerWithMemoryLimitShallWork()
{
using (
var container =
Fd.UseContainer()
.UseImage("postgres:latest", true)
.WithMemoryLimit("2g")
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.Build()
.Start())
{
var config = container.GetConfiguration(true);
}
}
}
}
15 changes: 13 additions & 2 deletions Ductus.FluentDocker/Builders/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ public ContainerBuilder WithHostName(string name)
return this;
}

/// <summary>
/// Sets memory limit of the container as the -m,--memory parameters in docker run.
/// </summary>
/// <param name="memoryLimit">The memory limit with a suffix for the unit: b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes. E.g: 2g for 2 gigabytes</param>
/// <returns>Itself for fluent access.</returns>
public ContainerBuilder WithMemoryLimit(string memoryLimit)
{
_config.CreateParams.Memory = memoryLimit;
return this;
}

public ContainerBuilder Command(string command, params string[] arguments)
{
_config.Command = command;
Expand Down Expand Up @@ -664,7 +675,7 @@ public ContainerBuilder UseIpV6(string ipv6)
/// <returns>Itself for fluent access.</returns>
/// <remarks>
/// For example restricting the number of open files to 10 use <see cref="Ulimit.NoFile"/> and set soft / hard
/// to 10.
/// to 10.
/// </remarks>
public ContainerBuilder UseUlimit(Ulimit ulimit, string soft, string hard = null)
{
Expand All @@ -681,7 +692,7 @@ public ContainerBuilder UseUlimit(Ulimit ulimit, string soft, string hard = null
/// <returns>Itself for fluent access.</returns>
/// <remarks>
/// For example restricting the number of open files to 10 use <see cref="Ulimit.NoFile"/> and set soft / hard
/// to 10.
/// to 10.
/// </remarks>
public ContainerBuilder UseUlimit(Ulimit ulimit, long soft, long? hard = null)
{
Expand Down
20 changes: 12 additions & 8 deletions Ductus.FluentDocker/Extensions/ConversionExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace Ductus.FluentDocker.Extensions
{
Expand All @@ -19,22 +20,25 @@ public static long Convert(this string value, params string[] unit)
if (string.IsNullOrWhiteSpace(value))
return long.MinValue;

var num = value.Substring(0, value.Length - 2);
var u = value.Substring(value.Length - 2, 1).ToLower();
var regex = new Regex(@"(\d+)([a-zA-Z]+)");
var result = regex.Match(value);

if (char.IsDigit(u[0]))
return !long.TryParse(value, out var n) ? long.MinValue : n;
if (!result.Success)
return long.MinValue;

var digits = result.Groups[1].Value;
var letters = result.Groups[2].Value;

if (!unit.Contains(u))
if (!unit.Contains(letters))
return long.MinValue;

if (!long.TryParse(num, out var val))
if (!long.TryParse(digits, out var val))
return long.MinValue;

if (u == "b")
if (letters == "b")
return val;

switch (u)
switch (letters)
{
case "b":
return val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ public override string ToString()
}

// Memory management
sb.SizeOptionIfValid("--memory=", Memory, 4 * 1024 * 1024 /*4m*/);
sb.SizeOptionIfValid("--memory=", Memory);
sb.SizeOptionIfValid("--memory-swap=", MemorySwap);
sb.OptionIfExists("--memory-swappiness=", MemorySwappiness);
sb.SizeOptionIfValid("--memory-reservation=", MemoryReservation);
Expand Down

0 comments on commit 51418e5

Please sign in to comment.