Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pact_mock_server_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ expectest = "0.12.0"
trycmd = "0.15.7"
test-log = "0.2.16"
env_logger = "0.11.5"

# create bin and lib entries
[[bin]]
name = "pact-mock-server"
path = "src/bin.rs"
[lib]
name = "pact_mock_server_cli"
path = "src/lib.rs"
10 changes: 5 additions & 5 deletions pact_mock_server_cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ ARG BIN_ARCH=x86_64
ARG VERSION=1.0.5

RUN apk --no-cache add wget
RUN wget -O pact_mock_server_cli.gz https://github.com/pact-foundation/pact-reference/releases/download/pact_mock_server_cli-v${VERSION}/pact_mock_server_cli-linux-${BIN_ARCH}.gz
RUN gunzip pact_mock_server_cli.gz
RUN chmod +x pact_mock_server_cli
RUN wget -O pact-mock-server.gz https://github.com/pact-foundation/pact-reference/releases/download/pact_mock_server_cli-v${VERSION}/pact-mock-server-linux-${BIN_ARCH}.gz
RUN gunzip pact-mock-server.gz
RUN chmod +x pact-mock-server


# Now, we need to build our _real_ Docker container, copying in the executable.
FROM ${ARCH}alpine:3.16
RUN apk --no-cache add ca-certificates
COPY --from=builder \
/pact_mock_server_cli \
/pact-mock-server \
/usr/local/bin/

EXPOSE 8080 9000-9020

ENTRYPOINT ["/usr/local/bin/pact_mock_server_cli"]
ENTRYPOINT ["/usr/local/bin/pact-mock-server"]
CMD ["start", "--base-port", "9000"]
61 changes: 31 additions & 30 deletions pact_mock_server_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ restful web api or through the command line interface. It implements the [V4 Pac

## Command line interface

The mock server is bundles as a single binary executable `pact_mock_server_cli`. Running this with out any options displays
The mock server is bundles as a single binary executable `pact-mock-server`. Running this with out any options displays
the standard help.

```console,ignore
$ ./pact_mock_server_cli --help
./pact_mock_server_cli v1.0.0
$ ./pact-mock-server --help
./pact-mock-server v1.0.0
Standalone Pact mock server

USAGE:
pact_mock_server_cli [FLAGS] [OPTIONS] <SUBCOMMAND>
pact-mock-server [FLAGS] [OPTIONS] <SUBCOMMAND>

FLAGS:
--help Prints help information
Expand Down Expand Up @@ -72,10 +72,10 @@ This prints either the main help or the help for a sub-command.
This starts the master mock server. This server needs to be running for the other sub-commands to work.

```console
$ ./pact_mock_server_cli help start
$ ./pact-mock-server help start
Starts the master mock server

Usage: pact_mock_server_cli start [OPTIONS]
Usage: pact-mock-server start [OPTIONS]

Options:
--help Print help and exit
Expand All @@ -101,7 +101,7 @@ This sets the output directory that log files and pact files are written to. It
##### Example

```console,ignore
$ ./pact_mock_server_cli start -l debug -o logs/
$ ./pact-mock-server start -l debug -o logs/
15:40:08 [DEBUG] hyper::server: threads = 10
15:40:08 [INFO] pact_mock_server_cli::server: Server started on port 8080
```
Expand All @@ -114,22 +114,23 @@ will be displayed.
Note, the tls option will only be available if the tls create feature is enabled.

```console
$ ./pact_mock_server_cli help create
$ ./pact-mock-server help create
Creates a new mock server from a pact file

Usage: pact_mock_server_cli create [OPTIONS] --file <file>
Usage: pact-mock-server create [OPTIONS] --file <file>

Options:
-f, --file <file> the pact file to define the mock server
--help Print help and exit
-c, --cors-preflight Handle CORS pre-flight requests
-v, --version Print version information and exit
-p, --port <port> port the master mock server runs on (defaults to 8080)
--tls Enable TLS with the mock server (will use a self-signed certificate)
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
--no-term-log Turns off using terminal ANSI escape codes
--no-file-log Do not log to an output file
-f, --file <file> the pact file to define the mock server
--help Print help and exit
-c, --cors-preflight Handle CORS pre-flight requests
-v, --version Print version information and exit
-p, --port <port> port the master mock server runs on (defaults to 8080)
--specification <specification> The Pact specification version to use (defaults to V4)
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
--tls Enable TLS with the mock server (will use a self-signed certificate)
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
--no-term-log Turns off using terminal ANSI escape codes
--no-file-log Do not log to an output file


```
Expand All @@ -143,7 +144,7 @@ This option specifies the pact file to base the mock server on. It is a mandator
##### Example

```console,ignore
$ ./pact_mock_server_cli create -f ../../../libpact_matching/tests/pact.json
$ ./pact-mock-server create -f ../../../libpact_matching/tests/pact.json
15:43:47 [INFO] pact_mock_server_cli::create_mock: Creating mock server from file ../../../libpact_matching/tests/pact.json
Mock server "7d1bf906d0ff42528f2d7d794dd19c5b" started on port 52943
```
Expand All @@ -153,10 +154,10 @@ Mock server "7d1bf906d0ff42528f2d7d794dd19c5b" started on port 52943
Lists out all running mock servers with their ID, port, provider name and status.

```console
$ ./pact_mock_server_cli list --help
$ ./pact-mock-server list --help
Lists all the running mock servers

Usage: pact_mock_server_cli list [OPTIONS]
Usage: pact-mock-server list [OPTIONS]

Options:
--help Print help and exit
Expand All @@ -173,7 +174,7 @@ Options:
##### Example

```console,ignore
$ ./pact_mock_server_cli list
$ ./pact-mock-server list
Mock Server Id Port Provider Status
7d1bf906d0ff42528f2d7d794dd19c5b 52943 Alice Service error
```
Expand All @@ -185,10 +186,10 @@ expectations have been met, the pact file will be written out to the output dire
sub-command. If there is any errors, no pact file will be written and the errors displayed to the console.

```console
$ ./pact_mock_server_cli verify --help
$ ./pact-mock-server verify --help
Verify the mock server by id or port number, and generate a pact file if all ok

Usage: pact_mock_server_cli verify [OPTIONS]
Usage: pact-mock-server verify [OPTIONS]

Options:
--help
Expand Down Expand Up @@ -228,7 +229,7 @@ The port number of the mock server to verify. Either this option or the mock ser
In the case of a mock server that has issues:

```console,ignore
$ ./pact_mock_server_cli verify -m 52943
$ ./pact-mock-server verify -m 52943
Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 failed verification with 1 errors

0 - Expected request was not received - {"method":"GET","path":"/mallory","query":"name=ron&status=good"}
Expand All @@ -237,7 +238,7 @@ Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 failed verification with 1 er
and for a mock server that has matched all requests:

```console,ignore
$ ./pact_mock_server_cli verify -m 52943
$ ./pact-mock-server verify -m 52943
Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 verified ok
```

Expand All @@ -246,10 +247,10 @@ Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 verified ok
Shutdown the mock server by id or port number, releasing all its resources.

```console
$ ./pact_mock_server_cli help shutdown
$ ./pact-mock-server help shutdown
Shutdown the mock server by id or port number, releasing all its resources

Usage: pact_mock_server_cli shutdown [OPTIONS]
Usage: pact-mock-server shutdown [OPTIONS]

Options:
--help
Expand Down Expand Up @@ -287,7 +288,7 @@ The port number of the mock server to shutdown. Either this option or the mock s
##### Example

```console,ignore
$ ./pact_mock_server_cli shutdown -i 3a94a472d04849048b78109e288702d0
$ ./pact-mock-server shutdown -i 3a94a472d04849048b78109e288702d0
Mock server with id '3a94a472d04849048b78109e288702d0' shutdown ok
```

Expand Down
8 changes: 4 additions & 4 deletions pact_mock_server_cli/release-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ build_x86_64() {

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-linux-x86_64.gz"
"$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/pact-mock-server" \
"$ARTIFACTS_DIR/pact-mock-server-linux-x86_64.gz"
fi
}

Expand All @@ -34,8 +34,8 @@ build_aarch64() {

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-linux-aarch64.gz"
"$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/pact-mock-server" \
"$ARTIFACTS_DIR/pact-mock-server-linux-aarch64.gz"
fi
}

Expand Down
14 changes: 4 additions & 10 deletions pact_mock_server_cli/release-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ build_x86_64() {

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-apple-darwin/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-osx-x86_64.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-apple-darwin/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-macos-x86_64.gz"
"$CARGO_TARGET_DIR/x86_64-apple-darwin/release/pact-mock-server" \
"$ARTIFACTS_DIR/pact-mock-server-macos-x86_64.gz"
fi
}

Expand All @@ -36,11 +33,8 @@ build_aarch64() {

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-apple-darwin/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-osx-aarch64.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-apple-darwin/release/pact_mock_server_cli" \
"$ARTIFACTS_DIR/pact_mock_server_cli-macos-aarch64.gz"
"$CARGO_TARGET_DIR/aarch64-apple-darwin/release/pact-mock-server" \
"$ARTIFACTS_DIR/pact-mock-server-macos-aarch64.gz"
fi
}

Expand Down
8 changes: 4 additions & 4 deletions pact_mock_server_cli/release-win.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ build_x86_64() {
# If --release in cargo flags, then gzip and sum the release artifacts
if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/pact_mock_server_cli.exe" \
"$ARTIFACTS_DIR/pact_mock_server_cli-windows-x86_64.exe.gz"
"$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/pact-mock-server.exe" \
"$ARTIFACTS_DIR/pact-mock-server-windows-x86_64.exe.gz"
fi
}

Expand All @@ -31,8 +31,8 @@ build_aarch64() {

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/pact_mock_server_cli.exe" \
"$ARTIFACTS_DIR/pact_mock_server_cli-windows-aarch64.exe.gz"
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/pact-mock-server.exe" \
"$ARTIFACTS_DIR/pact-mock-server-windows-aarch64.exe.gz"
fi
}

Expand Down
7 changes: 7 additions & 0 deletions pact_mock_server_cli/src/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[tokio::main]
async fn main() {
match pact_mock_server_cli::handle_command_args().await {
Ok(_) => (),
Err(err) => std::process::exit(err)
}
}
11 changes: 8 additions & 3 deletions pact_mock_server_cli/src/create_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ pub async fn create_mock_server(host: &str, port: u16, matches: &ArgMatches, usa

match RequestResponsePact::read_pact(Path::new(file)) {
Ok(ref pact) => {
let mut args = vec![];
let mut args = Vec::<String>::new();
if matches.get_flag("cors") {
info!("Setting mock server to handle CORS pre-flight requests");
args.push("cors=true");
args.push("cors=true".to_string());
}
if let Some(specification) = matches.get_one::<String>("specification") {
info!("Setting mock server to use pact specification {}", specification);
let spec_arg = format!("specification={}", specification);
args.push(spec_arg);
}
if matches.get_flag("tls") {
info!("Setting mock server to use TLS");
args.push("tls=true");
args.push("tls=true".to_string());
}
let url = if args.is_empty() {
format!("http://{}:{}/", host, port)
Expand Down
Loading