Skip to content

Commit

Permalink
Added SNMP Unit and Integration Test
Browse files Browse the repository at this point in the history
  • Loading branch information
DAQEM committed Nov 3, 2023
1 parent c43fc77 commit e7015d9
Show file tree
Hide file tree
Showing 14 changed files with 737 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Services/AdminWebApp/src/routes/device/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
</Button>
</div>
</div>
<DevicesTable {devices} />
<DevicesTable {devices} id={""} />
</div>
</div>
5 changes: 4 additions & 1 deletion Services/Netmon.SNMPPolling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@
app.UseMiddleware<ExceptionMiddleware>();
}

app.Run();
app.Run();


public partial class SNMPPollingProgram { }
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,40 @@ public class MIBCpuConverter : IMIBComponentConverter<ICpu>
{
public List<ICpu> ConvertMIBsToComponent(List<IMIB> mibs)
{
Cpu? cpu = null;
Cpu? cpu = new()
{
Index = 1,
Metrics = new List<ICpuMetric>(),
Cores = new List<ICpuCore>()
};

HostResourcesMIB? hostResourcesMIB = mibs.OfType<HostResourcesMIB>().FirstOrDefault();

if (hostResourcesMIB != null && hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries.Any())
{
cpu = new Cpu
{
Index = 1,
Cores = hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries
.Select(e => new CpuCore
cpu.Cores = hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries
.Select(e => new CpuCore
{
Index = e.HrProcessorIndex.ToInt32(),
Name = hostResourcesMIB.HrDevice.HrDeviceTable.HrDeviceEntries
.FirstOrDefault(e1 => e1.HrDeviceIndex.ToInt32() == e.HrProcessorIndex.ToInt32())?.HrDeviceDescr
.ToString() ?? "Unknown",
Metrics = new List<ICpuCoreMetric>
{
Index = e.HrProcessorIndex.ToInt32(),
Name = hostResourcesMIB.HrDevice.HrDeviceTable.HrDeviceEntries
.FirstOrDefault(e1 => e1.HrDeviceIndex.ToInt32() == e.HrProcessorIndex.ToInt32())?.HrDeviceDescr
.ToString() ?? "Unknown",
Metrics = new List<ICpuCoreMetric>
new CpuCoreMetric
{
new CpuCoreMetric
{
Load = e.HrProcessorLoad.ToInt32()
}
Load = e.HrProcessorLoad.ToInt32()
}
})
.Cast<ICpuCore>()
.ToList()
};
}
})
.Cast<ICpuCore>()
.ToList();
}

UCDavisMIB? ucDavisMIB = mibs.OfType<UCDavisMIB>().FirstOrDefault();

if (ucDavisMIB != null && ucDavisMIB.LaLoadTable.LaLoadEntries.Any())
{
cpu ??= new Cpu
{
Index = 1
};
List<LaLoadEntry> laLoadEntries = ucDavisMIB.LaLoadTable.LaLoadEntries;
CpuMetric cpuMetric = new()
{
Expand All @@ -60,6 +57,6 @@ public List<ICpu> ConvertMIBsToComponent(List<IMIB> mibs)
cpu.Metrics = new List<ICpuMetric> { cpuMetric };
}

return cpu == null ? new List<ICpu>() : new List<ICpu> { cpu };
return !cpu.Cores.Any() && !cpu.Metrics.Any() ? new List<ICpu>() : new List<ICpu> { cpu };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class MIBDeviceConverter : IMIBDeviceConverter
{
public IDevice ConvertMIBsToDevice(SNMPConnectionInfo connectionInfo, List<IMIB> mibs)
{
Console.WriteLine("Found {0} MIBs", mibs.Count);
IDevice device = new Models.Device.Device
{
IpAddress = connectionInfo.IpAddress,
Expand Down
11 changes: 0 additions & 11 deletions Tests/Netmon.SNMPPolling.Tests/ExampleTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Net.Http.Json;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

namespace Netmon.SNMPPolling.Tests.Integration;

public class SNMPDiscoverIntegrationTests : IClassFixture<WebApplicationFactory<SNMPPollingProgram>>
{
private readonly WebApplicationFactory<SNMPPollingProgram> _factory;

public SNMPDiscoverIntegrationTests(WebApplicationFactory<SNMPPollingProgram> factory)
{
_factory = factory;
}

[Theory]
[InlineData("/Discover/Details")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
HttpClient client = _factory.CreateClient();

// Act
HttpResponseMessage response = await client.PostAsJsonAsync(url, new
{
version = "V2",
ipAddress = "192.168.178.7",
port = 161,
community = "public"
});

// Assert
response.EnsureSuccessStatusCode();
Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType?.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="xunit" Version="2.6.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Services\Netmon.SNMPPolling\Netmon.SNMPPolling.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Unit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Lextm.SharpSnmpLib;
using Netmon.Models.Component.Cpu;
using Netmon.Models.Component.Cpu.Core;
using Netmon.Models.Component.Cpu.Core.Metric;
using Netmon.Models.Component.Cpu.Metric;
using Netmon.SNMPPolling.SNMP.Converter.Component;
using Netmon.SNMPPolling.SNMP.MIB;
using Netmon.SNMPPolling.SNMP.MIB.HostResources;
using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device;
using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device.Device;
using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device.Processor;
using Netmon.SNMPPolling.SNMP.MIB.UCDavis;
using Netmon.SNMPPolling.SNMP.MIB.UCDavis.CpuLoad;
using NUnit.Framework;

namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Component;

[TestFixture]
public class MIBCpuConverterTests
{
[Test]
public void ConvertMIBsToComponent_ShouldReturnEmptyList_WhenNoHostResourcesMIB()
{
// Arrange
List<IMIB> mibs = new();
MIBCpuConverter converter = new();

// Act
List<ICpu> result = converter.ConvertMIBsToComponent(mibs);

// Assert
Assert.IsEmpty(result);
}

[Test]
public void ConvertMIBsToComponent_ShouldReturnCpuWithCoresAndMetrics_WhenHostResourcesMIBExists()
{
// Arrange
HostResourcesMIB hostResourcesMIB = new()
{
HrDevice = new HrDevice
{
HrProcessorTable = new HrProcessorTable
{
HrProcessorEntries = new List<HrProcessorEntry>
{
new()
{
HrProcessorIndex = new Integer32(1),
HrProcessorLoad = new Integer32(50)
}
}
},
HrDeviceTable = new HrDeviceTable
{
HrDeviceEntries = new List<HrDeviceEntry>
{
new()
{
HrDeviceIndex = new Integer32(1),
HrDeviceDescr = new OctetString("Processor 1")
}
}
}
}
};

List<IMIB> mibs = new() { hostResourcesMIB };

MIBCpuConverter converter = new();

// Act
List<ICpu> result = converter.ConvertMIBsToComponent(mibs);

// Assert
Assert.IsNotEmpty(result);
Assert.That(result, Has.Count.EqualTo(1));

ICpu cpu = result.First();
Assert.That(cpu.Index, Is.EqualTo(1));
Assert.IsNotEmpty(cpu.Cores);
Assert.That(cpu.Cores, Has.Count.EqualTo(1));

ICpuCore core = cpu.Cores.First();
Assert.That(core.Index, Is.EqualTo(1));
Assert.That(core.Name, Is.EqualTo("Processor 1"));
Assert.That(core.Metrics, Is.Not.Empty);
Assert.That(core.Metrics, Has.Count.EqualTo(1));

ICpuCoreMetric metric = core.Metrics.First();
Assert.That(metric, Is.InstanceOf<CpuCoreMetric>());
Assert.That(((CpuCoreMetric)metric).Load, Is.EqualTo(50));
}

[Test]
public void ConvertMIBsToComponent_ShouldReturnCpuWithMetrics_WhenUCDavisMIBExists()
{
// Arrange
UCDavisMIB ucDavisMIB = new()
{
LaLoadTable = new LaLoadTable
{
LaLoadEntries = new List<LaLoadEntry>
{
new() { LaLoadInt = new Integer32(1) },
new() { LaLoadInt = new Integer32(2) },
new() { LaLoadInt = new Integer32(3) }
}
}
};

List<IMIB> mibs = new() { ucDavisMIB };

MIBCpuConverter converter = new();

// Act
List<ICpu> result = converter.ConvertMIBsToComponent(mibs);

// Assert
Assert.IsNotEmpty(result);
Assert.That(result, Has.Count.EqualTo(1));

ICpu cpu = result.First();
Assert.That(cpu.Index, Is.EqualTo(1));
Assert.IsEmpty(cpu.Cores);

ICpuMetric metric = cpu.Metrics.First();
Assert.IsInstanceOf<CpuMetric>(metric);
Assert.That(((CpuMetric)metric).OneMinuteLoad, Is.EqualTo(1));
Assert.That(((CpuMetric)metric).FiveMinuteLoad, Is.EqualTo(2));
Assert.That(((CpuMetric)metric).FifteenMinuteLoad, Is.EqualTo(3));
}
}
Loading

0 comments on commit e7015d9

Please sign in to comment.