From 5ea9aebb2e871f0031cbc57d19894ea777007427 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 17:49:09 -0300 Subject: [PATCH 01/24] add ci for building the server and running tests --- .github/workflows/autobahn.yml | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 .github/workflows/autobahn.yml diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml new file mode 100644 index 0000000..7d0fa8f --- /dev/null +++ b/.github/workflows/autobahn.yml @@ -0,0 +1,73 @@ +name: Autobahn + +on: + [pull_request, push] + +permissions: read-all + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: + - macos-latest + - ubuntu-latest + ocaml-compiler: + - "5.1" + allow-prerelease-opam: + - true + opam-repositories: + - |- + default: https://github.com/ocaml/opam-repository.git + # include: + # - os: windows-latest + # ocaml-compiler: ocaml-variants.5.1.0+options,ocaml-option-mingw + # allow-prerelease-opam: false + # opam-repositories: |- + # windows-5.0: https://github.com/dra27/opam-repository.git#windows-5.0 + # sunset: https://github.com/ocaml-opam/opam-repository-mingw.git#sunset + # default: https://github.com/ocaml/opam-repository.git + + runs-on: ubuntu-latest + + steps: + - name: Checkout tree + uses: actions/checkout@v4 + + - name: Set-up OCaml + uses: ocaml/setup-ocaml@v2 + with: + ocaml-compiler: ${{ matrix.ocaml-compiler }} + allow-prerelease-opam: ${{ matrix.allow-prerelease-opam }} + opam-repositories: ${{ matrix.opam-repositories }} + + + - name: Retrieve mix dependencies cache + uses: actions/cache@v4 + id: opam-cache + with: + path: | + _opam + _build + key: ${{ runner.os }}-opam + + - name: install dependencies + if: steps.opam-cache.outputs.cache-hit != 'true' + run: | + opam pin atacama.0.0.5 git+https://github.com/suri-framework/atacama -y + opam pin trail git+https://github.com/suri-framework/trail -y + + opam install . --deps-only --with-test + + - run: opam exec -- dune build test/autobahn/server.exe + + - run: | + _build/default/test/autobahn/server.exe& + sleep 1 && + test/autobahn/run + - name: upload reports + uses: actions/upload-artifact@v4 + with: + name: report.html + path: _build/reports/clients/index.html From 5b1756298dea5d9d700a9ea2e8264fb3929bf858 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 17:50:22 -0300 Subject: [PATCH 02/24] making the test server compile. --- test/autobahn/server.ml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 3c3a68d..66c1d1b 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -4,15 +4,21 @@ module Echo_server = struct type args = unit type state = int - let init conn _args = `continue (conn, 0) - let handle_frame frame _conn _state = + let init (_args : args) : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = + `ok 1 + + let handle_frame frame _conn _state : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); - `push [ frame ] + `push ([ frame ], _state) + + (* val handle_message : *) + (* Message.t -> state -> (state, [> `Unknown_opcode of int ]) handle_result *) + let handle_message _message _state : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = + `ok 2 end module Test : Application.Intf = struct - let name = "test" let start () = Logger.set_log_level (Some Debug); @@ -25,7 +31,7 @@ module Test : Application.Intf = struct conn |> Trail.Conn.upgrade (`websocket (upgrade_opts, handler)) in - let handler = Nomad.trail [ Trail.logger ~level:Debug (); ws_echo ] in + let handler = Nomad.trail [ws_echo] in Nomad.start_link ~port:2112 ~handler () end From 70178d2c7d4590c87893eb470024018c2554a363 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 17:52:15 -0300 Subject: [PATCH 03/24] add dune file for building the server. --- test/autobahn/dune | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/autobahn/dune diff --git a/test/autobahn/dune b/test/autobahn/dune new file mode 100644 index 0000000..7a8ec13 --- /dev/null +++ b/test/autobahn/dune @@ -0,0 +1,8 @@ +(executable + (public_name server) + (name server) + (libraries + nomad + riot + trail + )) From 0d7764567aa8ad67b58b56700aa1e1056ace49a6 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 17:53:05 -0300 Subject: [PATCH 04/24] using network=host(the other way didnt work for me) --- test/autobahn/run | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/autobahn/run b/test/autobahn/run index 1797538..4b1194b 100755 --- a/test/autobahn/run +++ b/test/autobahn/run @@ -3,8 +3,8 @@ docker run -it --rm \ -v "${PWD}/test/autobahn/fuzzingclient.json:/fuzzingclient.json" \ -v "${PWD}/_build/reports:/reports" \ - -p 2113:2113 \ --name nomad \ + --network="host" \ crossbario/autobahn-testsuite \ wstest --mode fuzzingclient \ - -w ws://host.docker.internal:2112 + -w ws://0.0.0.0:2112 From d97d4d4689d0dabc6d8b0be3424f4e3a15f36087 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 18:55:05 -0300 Subject: [PATCH 05/24] adding a docker image of the test server --- test/autobahn/Dockerfile | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/autobahn/Dockerfile diff --git a/test/autobahn/Dockerfile b/test/autobahn/Dockerfile new file mode 100644 index 0000000..33edb51 --- /dev/null +++ b/test/autobahn/Dockerfile @@ -0,0 +1,29 @@ +FROM alpine + +RUN apk add opam git musl-dev make m4 gcc bubblewrap bash coreutils pkgconfig openssl-libs-static openssl-dev gmp-dev +RUN opam init --disable-sandboxing +RUN opam install -y ocaml-base-compiler dune + +WORKDIR /app + +COPY *.opam ./ + +RUN opam update +RUN opam pin atacama.0.0.5 git+https://github.com/suri-framework/atacama -y || true +RUN opam pin trail.0.0.1 git+https://github.com/suri-framework/trail -y || true + +RUN opam install . --deps-only -y + +COPY . . + +RUN opam exec -- dune build test/autobahn/server.exe + +FROM alpine + +RUN apk add gmp + +WORKDIR /app + +COPY --from=0 /app/_build/default/test/autobahn/server.exe . + +CMD ["/app/server.exe"] From 0cfaeeb4a2bcbd1f7ac7db89290747d0b41364b2 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 18:55:34 -0300 Subject: [PATCH 06/24] test building and pushing. --- .github/workflows/autobahn.yml | 47 +++++++++------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 7d0fa8f..ea1e6f6 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -32,42 +32,19 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout tree - uses: actions/checkout@v4 - - - name: Set-up OCaml - uses: ocaml/setup-ocaml@v2 + - + name: Login to GitHub Container Registry + uses: docker/login-action@v3 with: - ocaml-compiler: ${{ matrix.ocaml-compiler }} - allow-prerelease-opam: ${{ matrix.allow-prerelease-opam }} - opam-repositories: ${{ matrix.opam-repositories }} - + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Retrieve mix dependencies cache - uses: actions/cache@v4 - id: opam-cache + - + name: Build and push + uses: docker/build-push-action@v5 with: - path: | - _opam - _build - key: ${{ runner.os }}-opam - - - name: install dependencies - if: steps.opam-cache.outputs.cache-hit != 'true' - run: | - opam pin atacama.0.0.5 git+https://github.com/suri-framework/atacama -y - opam pin trail git+https://github.com/suri-framework/trail -y + file: test/autobahn/Dockerfile + push: true + tags: francoganga/test_server:latest - opam install . --deps-only --with-test - - - run: opam exec -- dune build test/autobahn/server.exe - - - run: | - _build/default/test/autobahn/server.exe& - sleep 1 && - test/autobahn/run - - name: upload reports - uses: actions/upload-artifact@v4 - with: - name: report.html - path: _build/reports/clients/index.html From ff340cb80932ca314a9343f7caab23ba9c716e6a Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 19:15:31 -0300 Subject: [PATCH 07/24] figuring out build/push github action. --- .github/workflows/autobahn.yml | 37 +++++++++++----------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index ea1e6f6..29fca96 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -5,32 +5,13 @@ on: permissions: read-all +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + jobs: build: - strategy: - fail-fast: false - matrix: - os: - - macos-latest - - ubuntu-latest - ocaml-compiler: - - "5.1" - allow-prerelease-opam: - - true - opam-repositories: - - |- - default: https://github.com/ocaml/opam-repository.git - # include: - # - os: windows-latest - # ocaml-compiler: ocaml-variants.5.1.0+options,ocaml-option-mingw - # allow-prerelease-opam: false - # opam-repositories: |- - # windows-5.0: https://github.com/dra27/opam-repository.git#windows-5.0 - # sunset: https://github.com/ocaml-opam/opam-repository-mingw.git#sunset - # default: https://github.com/ocaml/opam-repository.git - runs-on: ubuntu-latest - steps: - name: Login to GitHub Container Registry @@ -40,11 +21,17 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push uses: docker/build-push-action@v5 with: file: test/autobahn/Dockerfile push: true - tags: francoganga/test_server:latest - + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From 4af788be117fd6deea3cbd0d08a03db2b75397fb Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 19:28:26 -0300 Subject: [PATCH 08/24] permissions. --- .github/workflows/autobahn.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 29fca96..06dde08 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -12,6 +12,9 @@ env: jobs: build: runs-on: ubuntu-latest + permissions: + contents: read + packages: write steps: - name: Login to GitHub Container Registry From 4fc419d7380733f6e8236e22d3a2c51cdffcdfc7 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 19:55:40 -0300 Subject: [PATCH 09/24] dont install ocaml 2 times. --- test/autobahn/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/autobahn/Dockerfile b/test/autobahn/Dockerfile index 33edb51..8d5eb48 100644 --- a/test/autobahn/Dockerfile +++ b/test/autobahn/Dockerfile @@ -2,7 +2,7 @@ FROM alpine RUN apk add opam git musl-dev make m4 gcc bubblewrap bash coreutils pkgconfig openssl-libs-static openssl-dev gmp-dev RUN opam init --disable-sandboxing -RUN opam install -y ocaml-base-compiler dune +RUN opam install -y dune WORKDIR /app From 573e3bcfd56a661297381c20fa7908c3244ed39a Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 19:55:58 -0300 Subject: [PATCH 10/24] try to run the tests with a server running as a service. --- .github/workflows/autobahn.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 06dde08..6da8d50 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -26,7 +26,7 @@ jobs: - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} @@ -38,3 +38,17 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + test: + runs-on: ubuntu-latest + services: + test_server: + image: ghcr.io/francoganga/nomad:autobahn_ci + ports: + - 2112:2112 + steps: + - name: Checkout tree + uses: actions/checkout@v4 + - + name: autobahn + run: test/autobahn/run From f8d58dce48dc55bf542421bd4279ccdec8e6932d Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 20:43:29 -0300 Subject: [PATCH 11/24] run the autobahn image. --- .github/workflows/autobahn.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 6da8d50..ec9a3d3 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -47,8 +47,11 @@ jobs: ports: - 2112:2112 steps: - - name: Checkout tree - uses: actions/checkout@v4 - - - name: autobahn - run: test/autobahn/run + - name: Checkout code + uses: actions/checkout@v4 + container: + image: crossbario/autobahn-testsuite + volumes: + - "${PWD}/test/autobahn/fuzzingclient.json:/fuzzingclient.json" + - "${PWD}/_build/reports:/reports" + From 58ef3482ae574317b279f141d79f7a9d6e0dc58d Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 20:48:16 -0300 Subject: [PATCH 12/24] run sequentially. --- .github/workflows/autobahn.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index ec9a3d3..5a7dbf3 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -41,6 +41,7 @@ jobs: test: runs-on: ubuntu-latest + needs: build services: test_server: image: ghcr.io/francoganga/nomad:autobahn_ci From 9b5fe9a995d762a24fc7d86a1068cd8ce1e2c426 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 20:58:06 -0300 Subject: [PATCH 13/24] try to fix mounts. --- .github/workflows/autobahn.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 5a7dbf3..53c6c0c 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -53,6 +53,6 @@ jobs: container: image: crossbario/autobahn-testsuite volumes: - - "${PWD}/test/autobahn/fuzzingclient.json:/fuzzingclient.json" - - "${PWD}/_build/reports:/reports" + - "$(pwd)/test/autobahn/fuzzingclient.json:/fuzzingclient.json" + - "$(pwd)/_build/reports:/reports" From 513c43bd9d2a4cf78f7207e02898b806ee6c5815 Mon Sep 17 00:00:00 2001 From: fganga Date: Wed, 6 Mar 2024 21:15:53 -0300 Subject: [PATCH 14/24] trying to fix mounts again. --- .github/workflows/autobahn.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 53c6c0c..0652a7a 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -53,6 +53,6 @@ jobs: container: image: crossbario/autobahn-testsuite volumes: - - "$(pwd)/test/autobahn/fuzzingclient.json:/fuzzingclient.json" - - "$(pwd)/_build/reports:/reports" + - "${{github.workspace}}/test/autobahn/fuzzingclient.json:/fuzzingclient.json" + - "${{github.workspace}}/_build/reports:/reports" From d26aed4d214c71b6ace76f2277a8bc4992e67703 Mon Sep 17 00:00:00 2001 From: fganga Date: Thu, 7 Mar 2024 23:19:05 -0300 Subject: [PATCH 15/24] running autobahn with riot. --- .github/workflows/autobahn.yml | 83 ++++++++++++++++---------------- test/autobahn/server.ml | 86 ++++++++++++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 47 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 0652a7a..cce3aba 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -5,54 +5,53 @@ on: permissions: read-all -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} - jobs: build: + strategy: + fail-fast: false + matrix: + ocaml-compiler: + - "5.1" + allow-prerelease-opam: + - true + opam-repositories: + - |- + default: https://github.com/ocaml/opam-repository.git + # include: + # - os: windows-latest + # ocaml-compiler: ocaml-variants.5.1.0+options,ocaml-option-mingw + # allow-prerelease-opam: false + # opam-repositories: |- + # windows-5.0: https://github.com/dra27/opam-repository.git#windows-5.0 + # sunset: https://github.com/ocaml-opam/opam-repository-mingw.git#sunset + # default: https://github.com/ocaml/opam-repository.git + runs-on: ubuntu-latest - permissions: - contents: read - packages: write + steps: - - - name: Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + - name: Checkout tree + uses: actions/checkout@v4 - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v5 + - name: Set-up OCaml + uses: ocaml/setup-ocaml@v2 with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + ocaml-compiler: ${{ matrix.ocaml-compiler }} + allow-prerelease-opam: ${{ matrix.allow-prerelease-opam }} + opam-repositories: ${{ matrix.opam-repositories }} - - - name: Build and push - uses: docker/build-push-action@v5 - with: - file: test/autobahn/Dockerfile - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + - name: install dependencies + run: | + opam pin atacama.0.0.5 git+https://github.com/suri-framework/atacama -y + opam pin trail git+https://github.com/suri-framework/trail -y - test: - runs-on: ubuntu-latest - needs: build - services: - test_server: - image: ghcr.io/francoganga/nomad:autobahn_ci - ports: - - 2112:2112 - steps: - - name: Checkout code - uses: actions/checkout@v4 - container: - image: crossbario/autobahn-testsuite - volumes: - - "${{github.workspace}}/test/autobahn/fuzzingclient.json:/fuzzingclient.json" - - "${{github.workspace}}/_build/reports:/reports" + opam install . --deps-only --with-test + + - run: opam exec -- dune build test/autobahn/server.exe + - run: _build/default/test/autobahn/server.exe + + - name: upload reports + uses: actions/upload-artifact@v4 + with: + name: report.html + path: _build/reports/clients/index.html diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 66c1d1b..02d26c4 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -1,4 +1,3 @@ -open Riot module Echo_server = struct type args = unit @@ -9,7 +8,7 @@ module Echo_server = struct `ok 1 let handle_frame frame _conn _state : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = - Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); + Riot.Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); `push ([ frame ], _state) (* val handle_message : *) @@ -18,9 +17,9 @@ module Echo_server = struct `ok 2 end -module Test : Application.Intf = struct - +module Test : Riot.Application.Intf = struct let start () = + let open Riot in Logger.set_log_level (Some Debug); sleep 0.1; Logger.info (fun f -> f "starting nomad server"); @@ -36,4 +35,81 @@ module Test : Application.Intf = struct Nomad.start_link ~port:2112 ~handler () end -let () = Riot.start ~apps:[ (module Logger); (module Test) ] () +module Autobahn = struct + + let get_cwd () = + try + Unix.getcwd () + with + | Unix.Unix_error (err, _, _) -> + Printf.eprintf "Could not get current directory: %s\n" (Unix.error_message err); + exit 1 + + + + let launch server_pid () = + Riot.link server_pid; + + let open Unix in + + let cwd = get_cwd () in + + let config_volume = Filename.concat cwd "/test/autobahn/fuzzingclient.json:/fuzzingclient.json" in + let reports_volume = Filename.concat cwd "/_build/reports:/reports" in + + let cmd = "docker" in + let args = [| + "docker"; + "run"; + "--rm"; + "-v"; + config_volume; + "-v"; + reports_volume; + "--name"; + "nomad"; + "--net=host"; + "crossbario/autobahn-testsuite"; + "wstest"; + "--mode"; + "fuzzingclient"; + "-w"; + "ws://0.0.0.0:2112" + |] in + + let pid = Unix.create_process cmd args stdin stdout stderr in + + let _, status = Unix.waitpid [] pid in + + match status with + | _ -> + (* the docker image always exits with 0, so if exits stop the server *) + Riot.Logger.info (fun f -> f "autobahn exited"); + Riot.(exit server_pid Normal); + () + + + let start server_pid = + let open Riot in + let pid = spawn @@ launch server_pid in + Ok pid +end + +let () = Riot.run @@ fun () -> + let (let*) = Result.bind in + + let launch () = + let* logger_pid = Riot.Logger.start () in + let* server_pid = Test.start () in + (* if i dont wait a little bit autobahn gets weird and hangs forever *) + Unix.sleep 2; + let* autobahn_pid = Autobahn.start server_pid in + Riot.Logger.info (fun f -> f "started autobahn pid: %a" Riot.Pid.pp autobahn_pid); + + Riot.wait_pids [ logger_pid; server_pid; autobahn_pid ]; + + Ok () in + + match launch () with + | Ok () -> () + | Error _ -> Riot.shutdown () From 6100fb244da67d22fa3d3875018506654196644b Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 8 Mar 2024 13:02:04 -0300 Subject: [PATCH 16/24] refactor: using spawn and error handling. --- test/autobahn/dune | 1 + test/autobahn/server.ml | 88 +++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 47 deletions(-) diff --git a/test/autobahn/dune b/test/autobahn/dune index 7a8ec13..1b542c0 100644 --- a/test/autobahn/dune +++ b/test/autobahn/dune @@ -5,4 +5,5 @@ nomad riot trail + spawn )) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 02d26c4..cb29539 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -30,35 +30,31 @@ module Test : Riot.Application.Intf = struct conn |> Trail.Conn.upgrade (`websocket (upgrade_opts, handler)) in - let handler = Nomad.trail [ws_echo] in + let handler = Nomad.trail [ ws_echo ] in Nomad.start_link ~port:2112 ~handler () end -module Autobahn = struct +module Utils = struct let get_cwd () = try - Unix.getcwd () + Ok(Unix.getcwd ()) with - | Unix.Unix_error (err, _, _) -> - Printf.eprintf "Could not get current directory: %s\n" (Unix.error_message err); - exit 1 + | Unix.Unix_error (_, _, _) -> + Error "Failed to get current working directory" - let launch server_pid () = - Riot.link server_pid; + let init () = + let (let*) = Result.bind in - let open Unix in - - let cwd = get_cwd () in + let* cwd = get_cwd () in let config_volume = Filename.concat cwd "/test/autobahn/fuzzingclient.json:/fuzzingclient.json" in let reports_volume = Filename.concat cwd "/_build/reports:/reports" in - let cmd = "docker" in - let args = [| + let args = [ "docker"; "run"; "--rm"; @@ -75,41 +71,39 @@ module Autobahn = struct "fuzzingclient"; "-w"; "ws://0.0.0.0:2112" - |] in - - let pid = Unix.create_process cmd args stdin stdout stderr in - - let _, status = Unix.waitpid [] pid in - - match status with - | _ -> - (* the docker image always exits with 0, so if exits stop the server *) - Riot.Logger.info (fun f -> f "autobahn exited"); - Riot.(exit server_pid Normal); - () - - - let start server_pid = - let open Riot in - let pid = spawn @@ launch server_pid in - Ok pid + ] in + + let path = + match Sys.getenv_opt "PATH" with + | None -> [] + | exception Not_found -> [] + | Some s -> String.split_on_char ':' s in + + let find_prog prog = + let rec search = function + | [] -> None + | x :: xs -> + let prog = Filename.concat x prog in + if Sys.file_exists prog then Some prog else search xs in + search path in + + match find_prog "docker" with + | None -> Error "Failed to find docker executable in PATH" + | Some prog -> + let process () = + let pid = Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout ~stderr:Unix.stderr () in + Riot.(Logger.info (fun f -> f "Spawed docker with pid %d" pid)); + in + Ok process end -let () = Riot.run @@ fun () -> - let (let*) = Result.bind in +module Autobahn : Riot.Application.Intf = struct - let launch () = - let* logger_pid = Riot.Logger.start () in - let* server_pid = Test.start () in - (* if i dont wait a little bit autobahn gets weird and hangs forever *) - Unix.sleep 2; - let* autobahn_pid = Autobahn.start server_pid in - Riot.Logger.info (fun f -> f "started autobahn pid: %a" Riot.Pid.pp autobahn_pid); - - Riot.wait_pids [ logger_pid; server_pid; autobahn_pid ]; - - Ok () in + let start () = + let process = Utils.init () in + match process with + | Ok p -> Ok (Riot.spawn p) + | Error err -> Error (`Application_error err) +end - match launch () with - | Ok () -> () - | Error _ -> Riot.shutdown () +let () = Riot.start ~apps:[ (module Riot.Logger); (module Test) ;(module Autobahn) ] () From cb9b538f2af44eb822c7b59ee4b82ef158a9d743 Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 8 Mar 2024 13:03:16 -0300 Subject: [PATCH 17/24] running test server with dune so it prints backtrace. --- .github/workflows/autobahn.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index cce3aba..2e0e303 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -6,7 +6,7 @@ on: permissions: read-all jobs: - build: + autobahn: strategy: fail-fast: false matrix: @@ -46,9 +46,7 @@ jobs: opam install . --deps-only --with-test - - run: opam exec -- dune build test/autobahn/server.exe - - - run: _build/default/test/autobahn/server.exe + - run: opam exec -- dune exec test/autobahn/server.exe - name: upload reports uses: actions/upload-artifact@v4 From 2f617dddccc8976a2215140ac6158b556c709c34 Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 8 Mar 2024 13:12:18 -0300 Subject: [PATCH 18/24] install spawn. --- .github/workflows/autobahn.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 2e0e303..873803d 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -45,6 +45,7 @@ jobs: opam pin trail git+https://github.com/suri-framework/trail -y opam install . --deps-only --with-test + opam install spawn -y - run: opam exec -- dune exec test/autobahn/server.exe From d614c0a5feaf85df38bee5d3ed511f9307e09ed2 Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 8 Mar 2024 14:11:35 -0300 Subject: [PATCH 19/24] trying to get the errors to show. --- .github/workflows/autobahn.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 873803d..aa5b0ec 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -47,7 +47,9 @@ jobs: opam install . --deps-only --with-test opam install spawn -y - - run: opam exec -- dune exec test/autobahn/server.exe + - run: | + eval $(opam env) + dune exec test/autobahn/server.exe - name: upload reports uses: actions/upload-artifact@v4 From 8fc3a1944f422d37deb192c0950e8c9e390588d3 Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 8 Mar 2024 14:27:12 -0300 Subject: [PATCH 20/24] delete unused file. --- test/autobahn/Dockerfile | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 test/autobahn/Dockerfile diff --git a/test/autobahn/Dockerfile b/test/autobahn/Dockerfile deleted file mode 100644 index 8d5eb48..0000000 --- a/test/autobahn/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -FROM alpine - -RUN apk add opam git musl-dev make m4 gcc bubblewrap bash coreutils pkgconfig openssl-libs-static openssl-dev gmp-dev -RUN opam init --disable-sandboxing -RUN opam install -y dune - -WORKDIR /app - -COPY *.opam ./ - -RUN opam update -RUN opam pin atacama.0.0.5 git+https://github.com/suri-framework/atacama -y || true -RUN opam pin trail.0.0.1 git+https://github.com/suri-framework/trail -y || true - -RUN opam install . --deps-only -y - -COPY . . - -RUN opam exec -- dune build test/autobahn/server.exe - -FROM alpine - -RUN apk add gmp - -WORKDIR /app - -COPY --from=0 /app/_build/default/test/autobahn/server.exe . - -CMD ["/app/server.exe"] From c3ec924b2b4875a76cd7b04ca03b384cba1ab0b6 Mon Sep 17 00:00:00 2001 From: fganga Date: Sat, 9 Mar 2024 20:01:28 -0300 Subject: [PATCH 21/24] cleaning up the code. --- nomad/adapter.ml | 4 +- test/autobahn/dune | 7 +-- test/autobahn/server.ml | 134 ++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 74 deletions(-) diff --git a/nomad/adapter.ml b/nomad/adapter.ml index 25d483d..ebfa39d 100644 --- a/nomad/adapter.ml +++ b/nomad/adapter.ml @@ -80,7 +80,7 @@ let has_no_transform (res : Response.t) = let maybe_compress (req : Request.t) buf = if Bytestring.length buf = 0 then (None, None) - else ( + else let accepted_encodings = Http.Header.get req.headers "accept-encoding" |> Option.map (fun enc -> String.split_on_char ',' enc) @@ -92,7 +92,7 @@ let maybe_compress (req : Request.t) buf = if accepts_deflate then (Some (deflate buf), Some "deflate") else if accepts_gzip then (Some (gzip buf), Some "gzip") else if accepts_x_gzip then (Some (gzip buf), Some "x-gzip") - else (Some buf, None)) + else (Some buf, None) let send conn (req : Request.t) (res : Response.t) = if req.version = `HTTP_1_0 && res.status = `Continue then () diff --git a/test/autobahn/dune b/test/autobahn/dune index 1b542c0..54cccab 100644 --- a/test/autobahn/dune +++ b/test/autobahn/dune @@ -1,9 +1,4 @@ (executable (public_name server) (name server) - (libraries - nomad - riot - trail - spawn - )) + (libraries nomad riot trail spawn)) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index cb29539..8ffca25 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -1,20 +1,23 @@ +open Riot module Echo_server = struct type args = unit type state = int + let init (_args : args) : + (state, [> `Unknown_opcode of int ]) Trail.Sock.handle_result = + `ok 1 - let init (_args : args) : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = - `ok 1 - - let handle_frame frame _conn _state : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = + let handle_frame frame _conn _state : + (state, [> `Unknown_opcode of int ]) Trail.Sock.handle_result = Riot.Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); `push ([ frame ], _state) (* val handle_message : *) (* Message.t -> state -> (state, [> `Unknown_opcode of int ]) handle_result *) - let handle_message _message _state : (state, [> `Unknown_opcode of int]) Trail.Sock.handle_result = - `ok 2 + let handle_message _message _state : + (state, [> `Unknown_opcode of int ]) Trail.Sock.handle_result = + `ok 2 end module Test : Riot.Application.Intf = struct @@ -35,75 +38,74 @@ module Test : Riot.Application.Intf = struct Nomad.start_link ~port:2112 ~handler () end -module Utils = struct - let get_cwd () = - try - Ok(Unix.getcwd ()) - with - | Unix.Unix_error (_, _, _) -> - Error "Failed to get current working directory" +module Autobahn = struct + let spawn_docker args = + let path = + match Sys.getenv_opt "PATH" with + | None -> [] + | exception Not_found -> [] + | Some s -> String.split_on_char ':' s + in + let find_prog prog = + let rec search = function + | [] -> None + | x :: xs -> + let prog = Filename.concat x prog in + if Sys.file_exists prog then Some prog else search xs + in + search path + in + + match find_prog "docker" with + | None -> failwith "Failed to find docker executable in PATH" + | Some prog -> + Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout + ~stderr:Unix.stderr () let init () = - let (let*) = Result.bind in - let* cwd = get_cwd () in - - let config_volume = Filename.concat cwd "/test/autobahn/fuzzingclient.json:/fuzzingclient.json" in - let reports_volume = Filename.concat cwd "/_build/reports:/reports" in - - let args = [ - "docker"; - "run"; - "--rm"; - "-v"; - config_volume; - "-v"; - reports_volume; - "--name"; - "nomad"; - "--net=host"; - "crossbario/autobahn-testsuite"; - "wstest"; - "--mode"; - "fuzzingclient"; - "-w"; - "ws://0.0.0.0:2112" - ] in + let cwd = Unix.getcwd() in - let path = - match Sys.getenv_opt "PATH" with - | None -> [] - | exception Not_found -> [] - | Some s -> String.split_on_char ':' s in - - let find_prog prog = - let rec search = function - | [] -> None - | x :: xs -> - let prog = Filename.concat x prog in - if Sys.file_exists prog then Some prog else search xs in - search path in + let config_volume = + Filename.concat cwd + "/test/autobahn/fuzzingclient.json:/fuzzingclient.json" + in + let reports_volume = Filename.concat cwd "/_build/reports:/reports" in + let args = + [ + "docker"; + "run"; + "--rm"; + "-v"; + config_volume; + "-v"; + reports_volume; + "--name"; + "nomad"; + "--net=host"; + "crossbario/autobahn-testsuite"; + "wstest"; + "--mode"; + "fuzzingclient"; + "-w"; + "ws://0.0.0.0:2112"; + ] + in + + let _ = spawn_docker args in + + match receive () with + | _ -> failwith "Should have never received a message" - match find_prog "docker" with - | None -> Error "Failed to find docker executable in PATH" - | Some prog -> - let process () = - let pid = Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout ~stderr:Unix.stderr () in - Riot.(Logger.info (fun f -> f "Spawed docker with pid %d" pid)); - in - Ok process -end -module Autobahn : Riot.Application.Intf = struct - let start () = - let process = Utils.init () in - match process with - | Ok p -> Ok (Riot.spawn p) - | Error err -> Error (`Application_error err) + let start () = + let pid = spawn init in + Ok pid end -let () = Riot.start ~apps:[ (module Riot.Logger); (module Test) ;(module Autobahn) ] () +let () = + Riot.start ~apps:[ (module Riot.Logger); (module Test); (module Autobahn) ] () From 4d74ef27cbb539782a7925ec312dd248cea8c5fe Mon Sep 17 00:00:00 2001 From: fganga Date: Thu, 14 Mar 2024 12:26:01 -0300 Subject: [PATCH 22/24] dune fmt. --- test/autobahn/server.ml | 114 +++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 60 deletions(-) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 8ffca25..9c7e592 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -38,73 +38,67 @@ module Test : Riot.Application.Intf = struct Nomad.start_link ~port:2112 ~handler () end - module Autobahn = struct + let spawn_docker args = + let path = + match Sys.getenv_opt "PATH" with + | None -> [] + | exception Not_found -> [] + | Some s -> String.split_on_char ':' s + in + + let find_prog prog = + let rec search = function + | [] -> None + | x :: xs -> + let prog = Filename.concat x prog in + if Sys.file_exists prog then Some prog else search xs + in + search path + in - let spawn_docker args = - let path = - match Sys.getenv_opt "PATH" with - | None -> [] - | exception Not_found -> [] - | Some s -> String.split_on_char ':' s - in - - let find_prog prog = - let rec search = function - | [] -> None - | x :: xs -> - let prog = Filename.concat x prog in - if Sys.file_exists prog then Some prog else search xs - in - search path - in - - match find_prog "docker" with - | None -> failwith "Failed to find docker executable in PATH" - | Some prog -> - Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout - ~stderr:Unix.stderr () - - let init () = - - let cwd = Unix.getcwd() in - - let config_volume = - Filename.concat cwd + match find_prog "docker" with + | None -> failwith "Failed to find docker executable in PATH" + | Some prog -> + Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout + ~stderr:Unix.stderr () + + let init () = + let cwd = Unix.getcwd () in + + let config_volume = + Filename.concat cwd "/test/autobahn/fuzzingclient.json:/fuzzingclient.json" - in - let reports_volume = Filename.concat cwd "/_build/reports:/reports" in - let args = - [ - "docker"; - "run"; - "--rm"; - "-v"; - config_volume; - "-v"; - reports_volume; - "--name"; - "nomad"; - "--net=host"; - "crossbario/autobahn-testsuite"; - "wstest"; - "--mode"; - "fuzzingclient"; - "-w"; - "ws://0.0.0.0:2112"; - ] - in - - let _ = spawn_docker args in - - match receive () with - | _ -> failwith "Should have never received a message" + in + let reports_volume = Filename.concat cwd "/_build/reports:/reports" in + let args = + [ + "docker"; + "run"; + "--rm"; + "-v"; + config_volume; + "-v"; + reports_volume; + "--name"; + "nomad"; + "--net=host"; + "crossbario/autobahn-testsuite"; + "wstest"; + "--mode"; + "fuzzingclient"; + "-w"; + "ws://0.0.0.0:2112"; + ] + in + let _ = spawn_docker args in + match receive () with _ -> failwith "Should have never received a message" let start () = - let pid = spawn init in - Ok pid + let pid = spawn init in + Ok pid end let () = From e166b2550e9f379ac577e21ac13c65c680b53114 Mon Sep 17 00:00:00 2001 From: fganga Date: Thu, 14 Mar 2024 12:32:20 -0300 Subject: [PATCH 23/24] using create_process instead of Spawn lib. --- .github/workflows/autobahn.yml | 1 - test/autobahn/dune | 2 +- test/autobahn/server.ml | 27 +++------------------------ 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index aa5b0ec..0016f47 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -45,7 +45,6 @@ jobs: opam pin trail git+https://github.com/suri-framework/trail -y opam install . --deps-only --with-test - opam install spawn -y - run: | eval $(opam env) diff --git a/test/autobahn/dune b/test/autobahn/dune index 54cccab..408b820 100644 --- a/test/autobahn/dune +++ b/test/autobahn/dune @@ -1,4 +1,4 @@ (executable (public_name server) (name server) - (libraries nomad riot trail spawn)) + (libraries nomad riot trail)) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 9c7e592..889bec6 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -40,28 +40,7 @@ end module Autobahn = struct let spawn_docker args = - let path = - match Sys.getenv_opt "PATH" with - | None -> [] - | exception Not_found -> [] - | Some s -> String.split_on_char ':' s - in - - let find_prog prog = - let rec search = function - | [] -> None - | x :: xs -> - let prog = Filename.concat x prog in - if Sys.file_exists prog then Some prog else search xs - in - search path - in - - match find_prog "docker" with - | None -> failwith "Failed to find docker executable in PATH" - | Some prog -> - Spawn.spawn ~prog ~argv:args ~stdin:Unix.stdin ~stdout:Unix.stdout - ~stderr:Unix.stderr () + Unix.create_process "docker" args Unix.stdin Unix.stdout Unix.stderr let init () = let cwd = Unix.getcwd () in @@ -72,7 +51,7 @@ module Autobahn = struct in let reports_volume = Filename.concat cwd "/_build/reports:/reports" in let args = - [ + [| "docker"; "run"; "--rm"; @@ -89,7 +68,7 @@ module Autobahn = struct "fuzzingclient"; "-w"; "ws://0.0.0.0:2112"; - ] + |] in let _ = spawn_docker args in From 29e3e87f8586f5f8597da13e65f54aee00d56306 Mon Sep 17 00:00:00 2001 From: fganga Date: Fri, 15 Mar 2024 10:49:58 -0300 Subject: [PATCH 24/24] removing unnecessary Riot prefixes. --- test/autobahn/server.ml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/autobahn/server.ml b/test/autobahn/server.ml index 889bec6..0852cb3 100644 --- a/test/autobahn/server.ml +++ b/test/autobahn/server.ml @@ -10,7 +10,7 @@ module Echo_server = struct let handle_frame frame _conn _state : (state, [> `Unknown_opcode of int ]) Trail.Sock.handle_result = - Riot.Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); + Logger.info (fun f -> f "handling frame: %a" Trail.Frame.pp frame); `push ([ frame ], _state) (* val handle_message : *) @@ -20,9 +20,8 @@ module Echo_server = struct `ok 2 end -module Test : Riot.Application.Intf = struct +module Test : Application.Intf = struct let start () = - let open Riot in Logger.set_log_level (Some Debug); sleep 0.1; Logger.info (fun f -> f "starting nomad server"); @@ -81,4 +80,4 @@ module Autobahn = struct end let () = - Riot.start ~apps:[ (module Riot.Logger); (module Test); (module Autobahn) ] () + Riot.start ~apps:[ (module Logger); (module Test); (module Autobahn) ] ()