smocker icon indicating copy to clipboard operation
smocker copied to clipboard

Add CLI option -mocks-directory

Open marema31 opened this issue 4 years ago • 3 comments

If not empty loads all the mocks from files (YAML/JSON) in a folder at startup.

Can be used to create dedicated mocks docker image from the smocker official image.

marema31 avatar Sep 03 '21 20:09 marema31

This is exactly what we need to fit our use case of deploying smocker as a mock server on k8s

FelipeEmerim avatar Sep 24 '21 20:09 FelipeEmerim

+1 IMO very good idea for quick mock deployments.

Sawiq avatar May 13 '22 13:05 Sawiq

This is exactly what we need to fit our use case of deploying smocker as a mock server on k8s

While not ideal, this is the interim solution I've implemented to solve this.

The file in question is mounted as a sidecar in K8s, so it can be updated without needing to update the image itself.

Dockerfile

# Necessary to wrap the smocker image so that we can install curl and inotify-tools
# This allows us to post the mocks to smocker without having to restart the container

# Use the original image
FROM --platform=linux/amd64 thiht/smocker

USER root

# Install curl and inotify-tools
RUN apk --no-cache add curl inotify-tools sed

RUN cd /opt

# Add a script to run smocker and watch for file changes
COPY run-smocker-and-watch.sh .
RUN chmod +x run-smocker-and-watch.sh
RUN sed -i 's/\r$//' run-smocker-and-watch.sh # Trim Windows line endings
RUN chown 1000:1000 run-smocker-and-watch.sh

EXPOSE 8080
EXPOSE 8081

# Set the script as the default command
CMD ["/opt/run-smocker-and-watch.sh"]

Script

#!/bin/sh

echo "Starting script..."

set -x

# Start smocker in the background
/opt/smocker --history-retention=100 &

echo "Smocker started..."

# Wait for smocker to start up
sleep 5

echo "Sending mocks..."

# Define the file to watch
FILE_TO_WATCH="/scripts/mock-definitions.yaml"

echo "File to watch: $FILE_TO_WATCH"

ls -l "$FILE_TO_WATCH"

curl -X POST -H "Content-Type: application/x-yaml" http://localhost:8081/mocks --data-binary @"$FILE_TO_WATCH"

echo "Mocks sent..."

# Watch for changes to the file and re-split and send when it changes
inotifywait -m -e modify "$FILE_TO_WATCH" | while read -r path event file; do
  echo "Mocks file changed, reloading mocks"
  curl -X POST http://localhost:8081/reset
  curl -X POST -H "Content-Type: application/x-yaml" http://localhost:8081/mocks --data-binary @"$FILE_TO_WATCH"
done

joshua-temple avatar Mar 19 '24 19:03 joshua-temple