Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse more defensively #302

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public void ProcessShallParseResponse()
// Assert
Assert.AreEqual(id, result.Id);
Assert.AreEqual(name, result.Name);
Assert.AreEqual(driver, result.Driver);
Assert.AreEqual(scope, result.Scope);
Assert.AreEqual(ipv6, result.IPv6);
Assert.AreEqual(isInternal, result.Internal);
Assert.AreEqual(created, result.Created.ToUniversalTime());
}


Expand Down Expand Up @@ -76,11 +71,6 @@ public void ProcessShallParseResponseWithNegativeTimezone()
// Assert
Assert.AreEqual(id, result.Id);
Assert.AreEqual(name, result.Name);
Assert.AreEqual(driver, result.Driver);
Assert.AreEqual(scope, result.Scope);
Assert.AreEqual(ipv6, result.IPv6);
Assert.AreEqual(isInternal, result.Internal);
Assert.AreEqual(created, result.Created.AddMinutes(-1 * DateTimeOffset.Now.Offset.TotalMinutes).AddHours(tzShift));
}
}
}
34 changes: 34 additions & 0 deletions Ductus.FluentDocker/Common/JsonArrayOrSingleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ductus.FluentDocker.Common
{
public class JsonArrayOrSingleConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T[]);
}

public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
return token.ToObject<T[]>();
}

return new[] {token.ToObject<T>()};
}

public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) =>
throw new NotImplementedException();
}
}
24 changes: 2 additions & 22 deletions Ductus.FluentDocker/Executors/Parsers/NetworkLsResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Ductus.FluentDocker.Executors.Parsers
{
public sealed class NetworkLsResponseParser : IProcessResponseParser<IList<NetworkRow>>
{
public const string Format = "{{.ID}};{{.Name}};{{.Driver}};{{.Scope}};{{.IPv6}};{{.Internal}};{{.CreatedAt}}";
public const string Format = "{{.ID}};{{.Name}}";

public CommandResponse<IList<NetworkRow>> Response { get; private set; }

Expand All @@ -30,33 +30,13 @@ public IProcessResponse<IList<NetworkRow>> Process(ProcessExecutionResult respon
foreach (var row in response.StdOutAsArray)
{
var items = row.Split(';');
if (items.Length < 4)
if (items.Length < 2)
continue;

var created = DateTime.MinValue;
var ipv6 = false;
var intern = false;

if (items.Length > 4)
bool.TryParse(items[4], out ipv6);
if (items.Length > 5)
bool.TryParse(items[5], out intern);
if (items.Length > 6)
{
var split = items[6].Split(" ".ToCharArray());
var normalizedStr = $"{split[0]} {split[1]} {split[2].Insert(3, ":")}";
DateTime.TryParse(normalizedStr, out created);
}

result.Add(new NetworkRow
{
Id = items[0],
Name = items[1],
Driver = items[2],
Scope = items[3],
IPv6 = ipv6,
Internal = intern,
Created = created
});
}

Expand Down
3 changes: 3 additions & 0 deletions Ductus.FluentDocker/Model/Containers/ContainerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Ductus.FluentDocker.Common;
using Newtonsoft.Json;

namespace Ductus.FluentDocker.Model.Containers
{
Expand All @@ -26,6 +28,7 @@ public string Domainname
public string Image { get; set; }
public IDictionary<string, VolumeMount> Volumes { get; set; }
public string WorkingDir { get; set; }
[JsonConverter(typeof(JsonArrayOrSingleConverter<string>))]
public string[] EntryPoint { get; set; }
public IDictionary<string, string> Labels { get; set; }
public string StopSignal { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Ductus.FluentDocker/Model/Containers/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Ductus.FluentDocker.Model.Containers
{
public class Health
{
public HealthState Status { get; set; }
public HealthState? Status { get; set; }
public int FailingStreak { get; set; }
}
}
43 changes: 0 additions & 43 deletions Ductus.FluentDocker/Model/Containers/ProcessRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ public sealed class ProcessRow
private const string StartConst = "START";
private const string CmdConstWin = "Name";

public string User { get; internal set; }
public long Pid { get; internal set; }
public long ProcessPid { get; internal set; }
public string Command { get; internal set; }
public string Tty { get; internal set; }
public TimeSpan Time { get; internal set; }
public TimeSpan Started { get; internal set; }
public string Status { get; internal set; }
public float PercentCpuUtilization { get; internal set; }
public TimeSpan Cpu { get; private set; }
public float PercentMemoryUtilization { get; internal set; }
public IList<string> FullRow { get; internal set; }

internal static ProcessRow ToRow(IList<string> columns, IList<string> fullRow)
Expand All @@ -53,39 +43,6 @@ internal static ProcessRow ToRow(IList<string> columns, IList<string> fullRow)
case CmdConstWin:
row.Command = fullRow[i];
break;
case UserConst:
case UidConst:
row.User = fullRow[i];
break;
case PidConst:
row.Pid = long.Parse(fullRow[i]);
break;
case PpidConst:
row.ProcessPid = long.Parse(fullRow[i]);
break;
case StartConst:
case StartTimeConst:
row.Started = TimeSpan.Parse(fullRow[i]);
break;
case TimeConst:
row.Time = TimeSpan.Parse(fullRow[i]);
break;
case TerminalConst:
row.Tty = fullRow[i];
break;
case StatConst:
row.Status = fullRow[i];
break;
case CpuTime:
if (TimeSpan.TryParse(fullRow[i], out var cpuTime))
row.Cpu = cpuTime;
break;
case PercentCpuConst:
row.PercentCpuUtilization = float.Parse(fullRow[i], CultureInfo.InvariantCulture.NumberFormat);
break;
case PercentMemoryConst:
row.PercentMemoryUtilization = float.Parse(fullRow[i], CultureInfo.InvariantCulture.NumberFormat);
break;
}
}

Expand Down
8 changes: 0 additions & 8 deletions Ductus.FluentDocker/Model/Networks/NetworkRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@ public sealed class NetworkRow
{
public string Id { get; set; }
public string Name { get; set; }
public string Driver { get; set; }
public string Scope { get; set; }

// ReSharper disable once InconsistentNaming
public bool IPv6 { get; set; }

public bool Internal { get; set; }
public DateTime Created { get; set; }
}
}