Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI option -mocks-directory #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marema31
Copy link
Contributor

@marema31 marema31 commented Sep 3, 2021

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.

@FelipeEmerim
Copy link

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

@Sawiq
Copy link

Sawiq commented May 13, 2022

+1 IMO very good idea for quick mock deployments.

@joshua-temple
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants