diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index d23333e..d80a87c 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -22,6 +22,11 @@ jobs: fetch-depth: 0 - name: Set current commit sha as env var run: echo COMMIT_SHA=$(git rev-parse HEAD) >> $GITHUB_ENV + - name: build docker image + working-directory: ./tests/backend.Tests + run: | + docker build -t test-image:latest . + docker run -d --network="host" -p 4222:4222 test-image:latest - name: Test code run: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover - name: Upload code diff --git a/.github/workflows/dotnet-unit-test.yml b/.github/workflows/dotnet-unit-test.yml index 7d75050..ce30b52 100644 --- a/.github/workflows/dotnet-unit-test.yml +++ b/.github/workflows/dotnet-unit-test.yml @@ -14,6 +14,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: build docker image + working-directory: ./tests/backend.Tests + run: | + docker build -t test-image:latest . + docker run -d --network="host" -p 4222:4222 test-image:latest - name: Setup .NET uses: actions/setup-dotnet@v1 with: diff --git a/backend/EventParser.cs b/backend/EventParser.cs index 7a0b28b..1d4830e 100644 --- a/backend/EventParser.cs +++ b/backend/EventParser.cs @@ -8,7 +8,7 @@ using Env = System.Environment; namespace backend { - class EventParser + public class EventParser { public DataStorage dataStorage; @@ -16,12 +16,22 @@ public EventParser(DataStorage dataStorage) { this.dataStorage = dataStorage; SetEnv(); - } - public void Parse() - { var options = ConnectionFactory.GetDefaultOptions(); options.Url = Env.GetEnvironmentVariable("NATS_URL"); + + Parse(options); + } + + //Used to suply different credentials + public EventParser(DataStorage dataStorage, Options options){ + this.dataStorage = dataStorage; + + Parse(options); + } + + private void Parse(Options options) + { MessageHandler messageHandler = new MessageHandler(dataStorage); diff --git a/backend/Program.cs b/backend/Program.cs index b953040..15ecd89 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -14,7 +14,7 @@ class Program private static void Main(string[] args) { eventParser = new EventParser(new DataStorage()); - eventParser.Parse(); + //eventParser.Parse(); dateOfNatsRequest = DateTime.Now; diff --git a/tests/backend.Tests/Dockerfile b/tests/backend.Tests/Dockerfile new file mode 100644 index 0000000..b42214a --- /dev/null +++ b/tests/backend.Tests/Dockerfile @@ -0,0 +1,8 @@ +#source: https://github.com/nats-io/nats-docker/blob/060868360489ec6400f3238d633b986103445dd5/2.2.0/scratch/Dockerfile + +FROM scratch +COPY --from=nats:2.2.0-alpine3.13 /usr/local/bin/nats-server /nats-server +COPY nats-serverA.conf /nats-serverA.conf +EXPOSE 4222 8222 6222 +ENTRYPOINT ["/nats-server"] +CMD ["--config", "nats-serverA.conf"] diff --git a/tests/backend.Tests/IntegrationTests.cs b/tests/backend.Tests/IntegrationTests.cs new file mode 100644 index 0000000..3975a3a --- /dev/null +++ b/tests/backend.Tests/IntegrationTests.cs @@ -0,0 +1,41 @@ +using Xunit; +using System.Diagnostics; +using System.Threading; +using System; +using NATS.Client; + +namespace backend.Tests +{ + public class IntegrationTests + { + + private static readonly Uri Nats = new Uri("localhost:4222/"); + + [Fact] + public void Test() + { + Assert.True(true); + + DataStorage dataStorage = new DataStorage(); + //MessageHandler messageHandler = new MessageHandler(dataStorage); + + + Uri Nats = new Uri("localhost:4222"); + + var options = ConnectionFactory.GetDefaultOptions(); + options.Url = Nats.OriginalString; + options.User = "admin"; + options.Password = "changeit"; + + EventParser eventParser = new EventParser(dataStorage, options); + + Console.WriteLine(eventParser.dataStorage.servers.Count); + + Assert.Equal(1, eventParser.dataStorage.servers.Count); + //Assert.Equal("2.2.0", messageHandler.dataStorage.servers[0].version); + //Assert.Equal(1, messageHandler.dataStorage.servers[0].proto); + + } + + } +} \ No newline at end of file diff --git a/tests/backend.Tests/nats-serverA.conf b/tests/backend.Tests/nats-serverA.conf new file mode 100644 index 0000000..f6a407f --- /dev/null +++ b/tests/backend.Tests/nats-serverA.conf @@ -0,0 +1,36 @@ +# Client port of 4222 on all interfaces +port: 4222 + +# HTTP monitoring port +monitor_port: 8222 + +# This is for clustering multiple servers together. +cluster { + + # Route connections to be received on any interface on port 6222 + port: 6222 + + + # Routes are actively solicited and connected to from this server. + # This Docker image has none by default, but you can pass a + # flag to the nats-server docker image to create one to an existing server. + routes = [] +} + + +accounts: { + USERS: { + users: [ + {user: a, password: a} + ] + }, + SYS: { + users: [ + {user: admin, password: changeit} + ] + }, +} +system_account: SYS + + +host: localhost