From 6e0d6a8e23bc1c31c4a294eb26bf817c306eddc9 Mon Sep 17 00:00:00 2001 From: David Jones Date: Fri, 4 Oct 2019 16:24:51 +0100 Subject: [PATCH] Add implementation of Autobahn echo server with Dockerfile Add run script for building server and running client tests --- Autobahn/.dockerignore | 6 ++++ Autobahn/.gitignore | 5 +++ Autobahn/Dockerfile | 11 +++++++ Autobahn/Package.swift | 16 +++++++++ Autobahn/README.md | 3 ++ .../WebSocketEchoServer/EchoService.swift | 25 ++++++++++++++ .../Sources/WebSocketEchoServer/main.swift | 16 +++++++++ Autobahn/config/fuzzingclient.json | 11 +++++++ Autobahn/run.sh | 33 +++++++++++++++++++ AutobahnTests.md | 6 ++++ 10 files changed, 132 insertions(+) create mode 100644 Autobahn/.dockerignore create mode 100644 Autobahn/.gitignore create mode 100644 Autobahn/Dockerfile create mode 100644 Autobahn/Package.swift create mode 100644 Autobahn/README.md create mode 100644 Autobahn/Sources/WebSocketEchoServer/EchoService.swift create mode 100644 Autobahn/Sources/WebSocketEchoServer/main.swift create mode 100644 Autobahn/config/fuzzingclient.json create mode 100755 Autobahn/run.sh diff --git a/Autobahn/.dockerignore b/Autobahn/.dockerignore new file mode 100644 index 0000000..e97bc90 --- /dev/null +++ b/Autobahn/.dockerignore @@ -0,0 +1,6 @@ +.build +Package.resolved +run.sh +Dockerfile +reports +config diff --git a/Autobahn/.gitignore b/Autobahn/.gitignore new file mode 100644 index 0000000..95c4320 --- /dev/null +++ b/Autobahn/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ diff --git a/Autobahn/Dockerfile b/Autobahn/Dockerfile new file mode 100644 index 0000000..7623249 --- /dev/null +++ b/Autobahn/Dockerfile @@ -0,0 +1,11 @@ +FROM swift:5.1 + +RUN apt-get update && apt-get install -y libssl-dev libcurl4-openssl-dev libz-dev + +COPY . /WebSocketEchoServer + +WORKDIR /WebSocketEchoServer + +RUN swift build + +CMD .build/debug/WebSocketEchoServer diff --git a/Autobahn/Package.swift b/Autobahn/Package.swift new file mode 100644 index 0000000..ca6493b --- /dev/null +++ b/Autobahn/Package.swift @@ -0,0 +1,16 @@ +// swift-tools-version:5.1 +import PackageDescription + +let package = Package( + name: "WebSocketEchoServer", + dependencies: [ + .package(url: "https://github.com/IBM-Swift/Kitura.git", from: "2.8.0"), + .package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.7.0"), + .package(url: "https://github.com/IBM-Swift/Kitura-WebSocket.git", from: "2.0.0") + ], + targets: [ + .target( + name: "WebSocketEchoServer", + dependencies: ["Kitura", "HeliumLogger", "Kitura-WebSocket"]), + ] +) diff --git a/Autobahn/README.md b/Autobahn/README.md new file mode 100644 index 0000000..ddc4a39 --- /dev/null +++ b/Autobahn/README.md @@ -0,0 +1,3 @@ +# Autobahn + +A description of this package. diff --git a/Autobahn/Sources/WebSocketEchoServer/EchoService.swift b/Autobahn/Sources/WebSocketEchoServer/EchoService.swift new file mode 100644 index 0000000..d7c6d08 --- /dev/null +++ b/Autobahn/Sources/WebSocketEchoServer/EchoService.swift @@ -0,0 +1,25 @@ +import Foundation + +import KituraWebSocket +import LoggerAPI + +class EchoService: WebSocketService { + + public func connected(connection: WebSocketConnection) {} + + public func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode) {} + + public func received(message: Data, from: WebSocketConnection) { + from.send(message: message) + } + + public func received(message: String, from: WebSocketConnection) { + let msgLength = message.utf8.count + if msgLength > 100 { + Log.info("Got message of length \(msgLength)... sending it back") + } else { + Log.info("Got message '\(message)'... sending it back") + } + from.send(message: message) + } +} diff --git a/Autobahn/Sources/WebSocketEchoServer/main.swift b/Autobahn/Sources/WebSocketEchoServer/main.swift new file mode 100644 index 0000000..8ff9600 --- /dev/null +++ b/Autobahn/Sources/WebSocketEchoServer/main.swift @@ -0,0 +1,16 @@ +import Foundation +import Kitura +import KituraWebSocket +import HeliumLogger + +// Using an implementation for a Logger +HeliumLogger.use(.info) + +let router = Router() + +WebSocket.register(service: EchoService(), onPath: "/") + +let port = 9001 + +Kitura.addHTTPServer(onPort: port, with: router) +Kitura.run() diff --git a/Autobahn/config/fuzzingclient.json b/Autobahn/config/fuzzingclient.json new file mode 100644 index 0000000..29f74af --- /dev/null +++ b/Autobahn/config/fuzzingclient.json @@ -0,0 +1,11 @@ +{ + "outdir": "./reports/servers", + "servers": [ + { + "url": "ws://wsserver:9001" + } + ], + "cases": ["*"], + "exclude-cases": [], + "exclude-agent-cases": {} +} diff --git a/Autobahn/run.sh b/Autobahn/run.sh new file mode 100755 index 0000000..086fb5b --- /dev/null +++ b/Autobahn/run.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Convenience script to build and run a Kitura-WebSocket echo server in a Docker container, +# and then to run the autobahn test suite Docker container against it. +# +# Once complete, the autobahn report (HTML format) will be opened. +# + +# Create network +docker network rm autobahn +docker network create --driver bridge autobahn + +# Build server +docker build -t wsserver . + +# Execute server +docker container rm wsserver +docker run -d --network autobahn --name wsserver wsserver + +# Execute client +docker run -it --rm \ + -v ${PWD}/config:/config \ + -v ${PWD}/reports:/reports \ + --name fuzzingclient \ + --network autobahn \ + crossbario/autobahn-testsuite \ + wstest -m fuzzingclient -s config/fuzzingclient.json + +# Stop server +docker container stop wsserver + +# Check out test report! +open ./reports/servers/index.html diff --git a/AutobahnTests.md b/AutobahnTests.md index f315cd9..137d35c 100644 --- a/AutobahnTests.md +++ b/AutobahnTests.md @@ -2,6 +2,12 @@ This document will take you through the steps to test Kitura-Websockets using [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite). +### Convenience (ready-to-run) Docker implementation + +For convenience, a project is provided in `Autobahn/` that implements the code below. + +A script `Autobahn/run.sh` is included that will build this within a Docker container, run the container, and then run the autobahn test suite (in client mode) against this server. + ### Creating a EchoServer These tests are run against a WebSocket EchoServer so we must first set one up.