From cfcaa04360e13fe2a80d96cb5d9492503a1f1789 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Thu, 1 Aug 2024 04:41:05 +0000 Subject: [PATCH] feat: add wasm plugin for wiremock --- .github/workflows/wiremock.yml | 33 +++++++++++++++++++++ Cargo.lock | 10 +++++++ wiremock/plugin/Cargo.toml | 20 +++++++++++++ wiremock/plugin/LICENSE | 19 ++++++++++++ wiremock/plugin/README.md | 53 ++++++++++++++++++++++++++++++++++ wiremock/plugin/fluentci.toml | 14 +++++++++ wiremock/plugin/src/helpers.rs | 52 +++++++++++++++++++++++++++++++++ wiremock/plugin/src/lib.rs | 45 +++++++++++++++++++++++++++++ wiremock/plugin/target | 1 + 9 files changed, 247 insertions(+) create mode 100644 .github/workflows/wiremock.yml create mode 100644 wiremock/plugin/Cargo.toml create mode 100644 wiremock/plugin/LICENSE create mode 100644 wiremock/plugin/README.md create mode 100644 wiremock/plugin/fluentci.toml create mode 100644 wiremock/plugin/src/helpers.rs create mode 100644 wiremock/plugin/src/lib.rs create mode 120000 wiremock/plugin/target diff --git a/.github/workflows/wiremock.yml b/.github/workflows/wiremock.yml new file mode 100644 index 0000000..f83a83b --- /dev/null +++ b/.github/workflows/wiremock.yml @@ -0,0 +1,33 @@ +name: ci +on: + push: + branches: + - main + paths: + - .github/workflows/wiremock.yml + - "wiremock/**" +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Fluent CI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: rust + args: | + target_add wasm32-unknown-unknown + build --release --target wasm32-unknown-unknown + working-directory: wiremock/plugin + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Run Dagger Pipelines + run: | + fluentci run --wasm plugin/target/wasm32-unknown-unknown/release/wiremock.wasm start + fluentci run --wasm plugin/target/wasm32-unknown-unknown/release/wiremock.wasm stop + deno run -A src/dagger/list_jobs.ts + dagger functions + working-directory: wiremock + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index 113f0fd..717adcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -603,6 +603,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "wiremock" +version = "0.1.0" +dependencies = [ + "anyhow", + "extism-pdk", + "fluentci-pdk 0.2.1", + "fluentci-types", +] + [[package]] name = "yamllint" version = "0.1.0" diff --git a/wiremock/plugin/Cargo.toml b/wiremock/plugin/Cargo.toml new file mode 100644 index 0000000..529cf90 --- /dev/null +++ b/wiremock/plugin/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Wiremock Service plugin for FluentCI" +edition = "2021" +license = "MIT" +name = "wiremock" +version = "0.1.0" + +[lib] +crate-type = [ + "cdylib", +] + +[dependencies] +anyhow = "1.0.82" +extism-pdk = "1.1.0" +fluentci-pdk = "0.2.1" +fluentci-types = "0.1.7" diff --git a/wiremock/plugin/LICENSE b/wiremock/plugin/LICENSE new file mode 100644 index 0000000..515dd5f --- /dev/null +++ b/wiremock/plugin/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Tsiry Sandratraina + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/wiremock/plugin/README.md b/wiremock/plugin/README.md new file mode 100644 index 0000000..1101dad --- /dev/null +++ b/wiremock/plugin/README.md @@ -0,0 +1,53 @@ +# Wiremock Plugin + +[![fluentci pipeline](https://shield.fluentci.io/x/wiremock)](https://pkg.fluentci.io/wiremock) + +Wiremock service plugin for FluentCI. [Wiremock](wiremock.org/) is a tool for mocking HTTP services. + +## 🚀 Usage + +Add the following command to your CI configuration file: + +```bash +fluentci run --wasm wiremock start +``` + +## Functions + +| Name | Description | +| ------ | --------------------------------------------| +| start | Start Wiremock server | +| stop | Stop Wiremock server | + +## Code Usage + +Add `fluentci-pdk` crate to your `Cargo.toml`: + +```toml +[dependencies] +fluentci-pdk = "0.2.1" +``` + +Use the following code to call the plugin: + +```rust +use fluentci_pdk::dag; + +// ... + +dag().call("https://pkg.fluentci.io/wiremock@v0.1.0?wasm=1", "start", vec![])?; +``` + +## 📚 Examples + +Github Actions: + +```yaml +- name: Setup Fluent CI CLI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: wiremock + args: | + start +``` diff --git a/wiremock/plugin/fluentci.toml b/wiremock/plugin/fluentci.toml new file mode 100644 index 0000000..6b9eb44 --- /dev/null +++ b/wiremock/plugin/fluentci.toml @@ -0,0 +1,14 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Wiremock Plugin for FluentCI" +keywords = [ + "wiremock", + "mock", + "testing", + "api", +] +license = "MIT" +name = "wiremock" +version = "0.1.0" diff --git a/wiremock/plugin/src/helpers.rs b/wiremock/plugin/src/helpers.rs new file mode 100644 index 0000000..998ea1e --- /dev/null +++ b/wiremock/plugin/src/helpers.rs @@ -0,0 +1,52 @@ +use anyhow::Error; +use fluentci_pdk::dag; + +pub fn setup_flox() -> Result<(), Error> { + let os = dag().get_os()?; + if os == "macos" { + dag() + .pipeline("setup-flox")? + .with_exec(vec![r#"type brew > /dev/null 2> /dev/null || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)""#])? + .with_exec(vec!["type flox > /dev/null 2> /dev/null || brew install flox"])? + .stdout()?; + } + Ok(()) +} + +pub fn setup() -> Result { + setup_flox()?; + dag() + .pipeline("setup")? + .with_exec(vec![ + "mkdir", + "-p", + ".fluentci/wiremock", + "__files", + "mappings", + ])? + .stdout()?; + + let wiremock_port = dag().get_env("WIREMOCK_PORT")?; + + if wiremock_port.is_empty() { + dag().set_envs(vec![("WIREMOCK_PORT".into(), "8080".into())])?; + } + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/wiremock")? + .with_exec(vec![ + "flox", + "install", + "wiremock", + "overmind", + "tmux", + ])? + .with_exec(vec!["[ -f ../../mappings/github.com-ghes-2.21-stubs.json ] || pkgx wget -O ../../mappings/github.com-ghes-2.21-stubs.json https://raw.githubusercontent.com/tsirysndr/daggerverse/main/wiremock/mappings/github.com-ghes-2.21-stubs.json"])? + .with_exec(vec![ + "grep -q wiremock: Procfile || echo -e 'wiremock: wiremock --port $WIREMOCK_PORT \\n' >> Procfile", + ])? + .stdout()?; + + Ok(stdout) +} diff --git a/wiremock/plugin/src/lib.rs b/wiremock/plugin/src/lib.rs new file mode 100644 index 0000000..20c6dcd --- /dev/null +++ b/wiremock/plugin/src/lib.rs @@ -0,0 +1,45 @@ +use extism_pdk::*; +use fluentci_pdk::dag; + +pub mod helpers; + +#[plugin_fn] +pub fn start(_args: String) -> FnResult { + helpers::setup()?; + + let port = dag().get_env("WIREMOCK_PORT")?; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/wiremock")? + .with_exec(vec!["overmind", "--version"])? + .with_exec(vec!["type", "overmind"])? + .with_exec(vec!["type", "wiremock"])? + .with_exec(vec!["wiremock", "--version"])? + .with_exec(vec!["echo -e \"Wiremock starting on port $WIREMOCK_PORT\""])? + .with_exec(vec![ + "overmind start -f Procfile --daemonize || flox activate -- overmind restart wiremock", + ])? + .wait_on(port.parse()?, None)? + .with_exec(vec!["overmind", "status"])? + .stdout()?; + Ok(stdout) +} + +#[plugin_fn] +pub fn stop(args: String) -> FnResult { + helpers::setup()?; + + let args = if args.is_empty() { + "wiremock".to_string() + } else { + args + }; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/wiremock")? + .with_exec(vec!["overmind", "stop", &args])? + .stdout()?; + Ok(stdout) +} diff --git a/wiremock/plugin/target b/wiremock/plugin/target new file mode 120000 index 0000000..6bcd2fc --- /dev/null +++ b/wiremock/plugin/target @@ -0,0 +1 @@ +../../target \ No newline at end of file