Skip to content

Commit

Permalink
Add docker nats-server to testing environment
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaluc committed Apr 23, 2021
1 parent 0bad238 commit 136b40b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/dotnet-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 14 additions & 4 deletions backend/EventParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@
using Env = System.Environment;
namespace backend
{
class EventParser
public class EventParser
{
public DataStorage dataStorage;

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);

Expand Down
2 changes: 1 addition & 1 deletion backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Program
private static void Main(string[] args)
{
eventParser = new EventParser(new DataStorage());
eventParser.Parse();
//eventParser.Parse();

dateOfNatsRequest = DateTime.Now;

Expand Down
8 changes: 8 additions & 0 deletions tests/backend.Tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
41 changes: 41 additions & 0 deletions tests/backend.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -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);

}

}
}
36 changes: 36 additions & 0 deletions tests/backend.Tests/nats-serverA.conf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 136b40b

Please sign in to comment.