From 3262ff82b0fc7a444bf8d4a63d17085646dab84c Mon Sep 17 00:00:00 2001 From: Simon Zeltser Date: Fri, 22 Jun 2018 17:17:34 -0700 Subject: [PATCH] Updating docker file to not include ports. Added scripts folder and script to run docker with env vars passed via command line. Also now the build and packaging is happening in container --- src/cartservice/Dockerfile | 8 +-- src/cartservice/Program.cs | 2 +- src/cartservice/{ => scripts}/build_image.bat | 4 +- src/cartservice/scripts/docker_setup.bat | 13 ++++ .../run_redis_emulator_windows.bat | 0 tests/cartservice/.gitignore | 3 + tests/cartservice/CartServiceTests.cs | 64 +++++++++++++++++++ tests/cartservice/cartservice.tests.csproj | 30 +++++++++ .../cartservice/cartservice.tests.csproj.user | 6 ++ 9 files changed, 123 insertions(+), 7 deletions(-) rename src/cartservice/{ => scripts}/build_image.bat (54%) create mode 100644 src/cartservice/scripts/docker_setup.bat rename src/cartservice/{ => scripts}/run_redis_emulator_windows.bat (100%) create mode 100644 tests/cartservice/.gitignore create mode 100644 tests/cartservice/CartServiceTests.cs create mode 100644 tests/cartservice/cartservice.tests.csproj create mode 100644 tests/cartservice/cartservice.tests.csproj.user diff --git a/src/cartservice/Dockerfile b/src/cartservice/Dockerfile index 76ce81e..6be8c6c 100644 --- a/src/cartservice/Dockerfile +++ b/src/cartservice/Dockerfile @@ -1,8 +1,8 @@ FROM gcr.io/google-appengine/aspnetcore:2.1.0 COPY . /app WORKDIR /app +RUN dotnet restore +RUN dotnet build +RUN dotnet publish +WORKDIR /app/bin/Debug/netcoreapp2.0 ENTRYPOINT ["dotnet", "cartservice.dll", "start"] -ENV REDIS_ADDR=172.30.147.193 -ENV CART_SERVICE_ADDR=127.0.0.1 -ENV CART_SERVICE_PORT=8080 -EXPOSE 8080 \ No newline at end of file diff --git a/src/cartservice/Program.cs b/src/cartservice/Program.cs index bf6562e..81a78ca 100644 --- a/src/cartservice/Program.cs +++ b/src/cartservice/Program.cs @@ -85,7 +85,7 @@ namespace cartservice int port = options.Port; if (options.Port <= 0) { - Console.WriteLine($"Reading cart service port from {CART_SERVICE_ADDRESS} environment variable"); + Console.WriteLine($"Reading cart service port from {CART_SERVICE_PORT} environment variable"); string portStr = Environment.GetEnvironmentVariable(CART_SERVICE_PORT); if (string.IsNullOrEmpty(portStr)) { diff --git a/src/cartservice/build_image.bat b/src/cartservice/scripts/build_image.bat similarity index 54% rename from src/cartservice/build_image.bat rename to src/cartservice/scripts/build_image.bat index 3f1888e..66f143c 100644 --- a/src/cartservice/build_image.bat +++ b/src/cartservice/scripts/build_image.bat @@ -1,7 +1,7 @@ @echo off echo building container image for cart service -docker build -t cartservice . +docker build -t cartservice ..\. echo running the image, mapping the port -docker run -it --rm -p 5000:8080 --name +rem echo docker run -it --rm -p 5000:8080 --name diff --git a/src/cartservice/scripts/docker_setup.bat b/src/cartservice/scripts/docker_setup.bat new file mode 100644 index 0000000..42d2f01 --- /dev/null +++ b/src/cartservice/scripts/docker_setup.bat @@ -0,0 +1,13 @@ +@echo off + +set REDIS_PORT=6379 +set REDIS_ADDR=172.30.147.193 +set CART_SERVICE_ADDR=127.0.0.1 +set CART_SERVICE_PORT=7070 + +rem run docker container with redis +rem docker run -d --name=redis -p %REDIS_PORT%:%REDIS_PORT% redis:alpine + +rem run docker container with cart service +docker run -it --rm -e REDIS_ADDR=%REDIS_ADDR%:%REDIS_PORT% -e CART_SERVICE_ADDR=%CART_SERVICE_ADDR% -e CART_SERVICE_PORT=%CART_SERVICE_PORT% -p %CART_SERVICE_PORT%:%CART_SERVICE_PORT% cartservice +rem -e GRPC_TRACE=all -e GRPC_VERBOSITY=debug \ No newline at end of file diff --git a/src/cartservice/run_redis_emulator_windows.bat b/src/cartservice/scripts/run_redis_emulator_windows.bat similarity index 100% rename from src/cartservice/run_redis_emulator_windows.bat rename to src/cartservice/scripts/run_redis_emulator_windows.bat diff --git a/tests/cartservice/.gitignore b/tests/cartservice/.gitignore new file mode 100644 index 0000000..61aadf7 --- /dev/null +++ b/tests/cartservice/.gitignore @@ -0,0 +1,3 @@ +/bin/* +/obj/* +/.vs/* \ No newline at end of file diff --git a/tests/cartservice/CartServiceTests.cs b/tests/cartservice/CartServiceTests.cs new file mode 100644 index 0000000..e4849ba --- /dev/null +++ b/tests/cartservice/CartServiceTests.cs @@ -0,0 +1,64 @@ +using System; +using System.Threading.Tasks; +using Grpc.Core; +using Hipstershop; +using Xunit; +using static Hipstershop.CartService; + +namespace cartservice +{ + public class E2ETests + { + private static string serverHostName = "172.17.0.2"; + private static int port = 7070; + + [Fact] + public async Task AddItem_ItemInserted() + { + string userId = "user1"; + + // Construct server's Uri + string targetUri = $"{serverHostName}:{port}"; + + // Create a GRPC communication channel between the client and the server + var channel = new Channel(targetUri, ChannelCredentials.Insecure); + //ar interceptorObject = new ObjecT(); + //var channel.Intercept(interceptorObject); + // Create a proxy object to work with the server + var client = new CartServiceClient(channel); + + var request = new AddItemRequest + { + UserId = userId, + Item = new CartItem + { + ProductId = "1", + Quantity = 1 + } + }; + + for (int i = 0; i < 3; i++) + { + try + { + Console.WriteLine("Try " + i+1); + await client.AddItemAsync(request); + break; + } + catch (Exception) + { + continue; + } + } + + var getCardRequest = new GetCartRequest + { + UserId = userId + }; + var cart = await client.GetCartAsync(getCardRequest); + + Assert.Equal(userId, cart.UserId); + Assert.Single(cart.Items); + } + } +} diff --git a/tests/cartservice/cartservice.tests.csproj b/tests/cartservice/cartservice.tests.csproj new file mode 100644 index 0000000..4ada297 --- /dev/null +++ b/tests/cartservice/cartservice.tests.csproj @@ -0,0 +1,30 @@ + + + + netcoreapp2.0 + + false + + + + + + + + + + + + + + + + + + + + Always + + + + diff --git a/tests/cartservice/cartservice.tests.csproj.user b/tests/cartservice/cartservice.tests.csproj.user new file mode 100644 index 0000000..0b0f24d --- /dev/null +++ b/tests/cartservice/cartservice.tests.csproj.user @@ -0,0 +1,6 @@ + + + + true + + \ No newline at end of file