Skip to content

Commit

Permalink
Merge branch 'experiment/actions-ci'
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Jan 1, 2024
2 parents 0d6e3ab + 6067b8a commit 8f71576
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 140 deletions.
71 changes: 0 additions & 71 deletions .circleci/config.yml

This file was deleted.

100 changes: 100 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# ITNOA
# Based on https://github.com/flcdrg/VsShowMissing

name: CI


# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
pull_request:

env:
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
DOTNET_VERSION: '8.0.x' # The .NET SDK version to use
PRODUCT_VERSION: '0.1.0'

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
name: build-${{matrix.os}}
strategy:
matrix:
# ubuntu-2004
os: [ubuntu-latest]

# The type of runner that the job will run on
runs-on: ${{ matrix.os }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Install dependencies
run: dotnet restore

- name: Build
id: build
run: dotnet build --configuration Release --no-restore

- name: Install system dependencies
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: build-essential libxtables-dev libip6tc-dev libip4tc-dev libiptc-dev libnl-3-dev libnl-3-200 libnetfilter-conntrack3 libnfnetlink-dev libnfnetlink0 libpcap0.8 libpcap0.8-dev sudo
version: 1.0

- name: Install dependencies
run: dotnet restore

- name: Modprobe IP6 tables
run: |
sudo modprobe ip6_tables
sudo modprobe ip6table_filter
sudo modprobe ip6table_mangle
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi
- name: NUnit Tests
run: SKIP_SYSTEM_TESTS=1 dotnet test IPTables.Net.Tests

- name: Install ipthelper dependency
run: |
cd ipthelper
CONFIG="Release" sudo -E bash install.sh
CONFIG="Debug" sudo -E bash install.sh
sudo ldconfig
cd ..
- name: All Tests
run: sudo dotnet test

upload:
name: upload
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Upload
run: bash nuget-upload.sh
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
2 changes: 1 addition & 1 deletion IPTables.Net.Benchmark/IPTables.Net.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\IPTables.Net\IPTables.Net.csproj" />
Expand Down
18 changes: 14 additions & 4 deletions IPTables.Net.TestFramework/MockIptablesSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@ public class MockIptablesSystemFactory : ISystemFactory
{
public List<KeyValuePair<String, String>> ExecutionLog = new List<KeyValuePair<string, string>>();
public Dictionary<KeyValuePair<String, String>,StreamReader[]> MockOutputs = new Dictionary<KeyValuePair<string, string>, StreamReader[]>();
private readonly bool _strict;

public MockIptablesSystemFactory(bool strict = false)
{
_strict = strict;
}

public ISystemProcess StartProcess(string command, string arguments)
{
var exe = new KeyValuePair<string, string>(command, arguments);
ExecutionLog.Add(exe);
StreamReader output = null, error = null;
if (MockOutputs.ContainsKey(exe))
if (MockOutputs.TryGetValue(exe, out var mo))
{
if(MockOutputs[exe].Length >= 1)
if(mo.Length >= 1)
{
output = MockOutputs[exe][0];
output = mo[0];
}
if (MockOutputs[exe].Length >= 2)
{
error = MockOutputs[exe][1];
error = mo[1];
}
}
else if(_strict)
{
throw new Exception("Mock output \"" + command + "\" " + arguments + " not found");
}
return new MockIptablesSystemProcess(output,error);
}

Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.TestFramework/MockIptablesSystemProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace IPTables.Net.TestFramework
{
public class MockIptablesSystemProcess : ISystemProcess
{
private ProcessStartInfo _startInfo = new ProcessStartInfo();
private ProcessStartInfo _startInfo = new ProcessStartInfo { UseShellExecute = false };
private Thread _outputReader;
private Thread _errorReader;

Expand Down
4 changes: 2 additions & 2 deletions IPTables.Net.Tests/IPTables.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="SystemInteract.Local" Version="1.1.3" />
<PackageReference Include="SystemInteract.Local" Version="1.1.4-cibuild0006" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 17 additions & 8 deletions IPTables.Net.Tests/IpUtilsRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using IPTables.Net.IpUtils;
using IPTables.Net.IpUtils.Utils;
using IPTables.Net.TestFramework;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using NUnit.Framework;

namespace IPTables.Net.Tests
Expand All @@ -18,7 +19,7 @@ class IpUtilsRuleTests
[Test]
public void TestParseRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var ipUtils = new IpRuleController(systemFactory);
var one = ipUtils.ParseObjectInternal("default via 10.17.199.1 dev s4 table 200", "to");
var two = ipUtils.ParseObjectInternal("default via 10.17.199.1 dev s4 table 200", "to");
Expand All @@ -31,15 +32,16 @@ public void TestParseRule()
[Test]
public void TestParsePref()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var ipUtils = new IpRuleController(systemFactory);
var one = ipUtils.ParseObject("0: from all fwmark 0x1000200/0x1ffff00 lookup 15002");
}

[Test]
public void TestAddRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Add("from","1.1.1.1","lookup","100");

Expand All @@ -54,7 +56,8 @@ public void TestAddRule()
[Test]
public void TestAddRule2()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add not from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Add("not", "from", "1.1.1.1", "lookup", "100");

Expand All @@ -69,7 +72,11 @@ public void TestAddRule2()
[Test]
public void TestAddObjRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);


systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add not from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });

var ipUtils = new IpRuleController(systemFactory);
var obj = new IpObject();
obj.Pairs.Add("from", "1.1.1.1");
Expand All @@ -88,7 +95,8 @@ public void TestAddObjRule()
[Test]
public void TestDeleteRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule delete from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Delete("from", "1.1.1.1", "lookup", "100");

Expand All @@ -103,7 +111,8 @@ public void TestDeleteRule()
[Test]
public void TestDeleteRuleId()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule delete pref 100 from 1.1.1.1"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
IpObject ipObject = new IpObject();
ipObject.Pairs.Add("pref","100");
Expand All @@ -121,7 +130,7 @@ public void TestDeleteRuleId()
[Test]
public void TestGetRules()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var output = "32766: from all lookup main\n32767: from all lookup default";
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip","rule show"), new StreamReader[]{new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(output)))});
var ipUtils = new IpRuleController(systemFactory);
Expand Down

0 comments on commit 8f71576

Please sign in to comment.