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 a way to read and initialize mocks from a directory #217

Open
FelipeEmerim opened this issue Sep 24, 2021 · 10 comments
Open

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

FelipeEmerim opened this issue Sep 24, 2021 · 10 comments

Comments

@FelipeEmerim
Copy link

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:

  • In case smocker already has some data you could create a new session and add the mocks in --mocks-directory to it?
  • You could have another cli argument indicating if we should wipe the data on startup
  • Just wipe the data if --mocks-directory has a value.

Thank you for your time!

@Thiht
Copy link
Collaborator

Thiht commented Sep 24, 2021

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
Copy link
Author

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
Copy link
Collaborator

Thiht commented Sep 25, 2021

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
Copy link
Collaborator

Thiht commented Sep 25, 2021

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

@FelipeEmerim
Copy link
Author

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

@FelipeEmerim
Copy link
Author

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.

@Thiht Thiht changed the title [FR] Add a way to read and initialize mocks from a directory Add a way to read and initialize mocks from a directory Sep 27, 2021
@OJOMB
Copy link

OJOMB commented Feb 19, 2023

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
Copy link

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

@gwleclerc
Copy link
Collaborator

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
Copy link

Thanks a lot for this very clear explanation.

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

No branches or pull requests

5 participants