smocker-dev / smocker

Smocker is a simple and efficient HTTP mock server and proxy
https://smocker.dev
MIT License
1.14k stars 60 forks source link

Add a way to read and initialize mocks from a directory #217

Open FelipeEmerim opened 2 years ago

FelipeEmerim commented 2 years ago

Hi,

First of all, I really liked your project. Compared to similar solutions like Wiremock, this tool seems much more modern and easy to use. We are considering to use it in our microservices architecture to replace third-party services with a mock implementation on dev/qa environments to save money and make testing easier.

We want to be able to make a Docker image containing our mocks. To do that we would need Smocker to read mocks from a directory on initialization, like Wiremock does. I see #214 contains exactly what we need. Are there any plans to implementing this feature?

I saw in another ticket that someone suggested using the API to import the mocks, but that is not practical to us since we are in a k8s environment, ideally Smocker would load the mocks from a folder on startup and the environment would be ready to serve the mock requests.

Here is what I imagine:

smocker --mocks-directory=/opt/mocks

A problem may arise in cases where smocker already has some data and we want to import from --mocks-directory, for example, a crash restart where Smocker will attempt to import the mocks again. I think you could do one of the following:

Thank you for your time!

Thiht commented 2 years ago

First of all, I really liked your project

Thanks <3

Are there any plans to implementing this feature?

Yes. We're currently in the middle of a rework that will change Smocker internals (#210) but after that we'll look into adding this feature. Note however that the already existing --persistence-directory parameter can answer this need and more (it can load full sessions, not just mocks).

A problem may arise in cases where smocker already has some data

We'll need to think about that for sure, I don't have a preferred solution yet

FelipeEmerim commented 2 years ago

Thank you for the detailed response! Glad to hear Smocker is being actively improved :)

I saw --persistence-directory when digging through the source code, but I noticed that Smocker generates a .db file inside that directory. Perhaps we could ship that file in the docker image and cp it into --persistence-directory before startup to reset the database. Although a .db file is a bit harder to maintain and extend, it should work.

Thiht commented 2 years ago

Yeah this is because of an issue on our side, we accidentally overwrote the docker latest with the storm-preview tag, so the latest currently tags a preview image. If you use the 0.16.4 specifically, the persistence directory stores the sessions as a yaml file, not as a .db file. I'll fix the latest asap

Thiht commented 2 years ago

Docker latest is fixed, it now correctly aliases 0.16.4, you can try again with the persistence directory :)

FelipeEmerim commented 2 years ago

Wow, that was really fast! Thank you! I will try it later today!

FelipeEmerim commented 2 years ago

Sorry for the late reply, I tested it today, it did generate yaml files the files contain internal metadata and some fields slightly changed. Either way I managed to a docker-compose volume to sync those files to my filesystem and used the UI to manage the mocks.

OJOMB commented 1 year ago

Firstly I just wanted to second the praise that Felipe gave for the project and to thank you for all the work that's been put into it.

I am just curious if there is any update on whether Smocker will include a means to load mocks from a directory on startup without having to compile a session.yml in the persistence directory?

hoping to use smocker for E2E testing and this feature would be incredibly useful

shingara commented 6 months ago

I have the same question than @OJOMB. Why nothing move on this subject ?

gwleclerc commented 6 months ago

Because Smocker was not designed to load mocks at start. The idiomatic way of using Smocker is to setup it before your tests (like a databases, or a cache), set a new session with mocks at the start of every test suite then dump the sessions and trash it with the rest of your service dependencies. We use it every day for automated E2E tests without needing to put mocks at start.

Smocker was created to be ephemeral. That's why all is stored in memory and everything is immuatable. What is the point of setup mocks at start when your automated tests are doing clean insert strategy ? What about mocks modification where everything should be executed in an automated way ?

We know that some people want to use it as a persistant reverse proxy with mock supports but we do not use it this way so this is not a priority for us because we need to rework the way the sessions are stored (a database) which will make it harder to start and use it locally.

For me the best way to unite the two ways is to allow to use a sqlite database for local environment + a postgres one for a persistant instance. This is not a little evoluton and then what about current persistence system ?

shingara commented 6 months ago

Thanks a lot for this very clear explanation.