outofcoffee / imposter

Scriptable, multipurpose mock server. Run standalone mock servers, or embed mocks within your tests.
https://imposter.sh
Other
372 stars 60 forks source link

.imposter.env and .env not taken into account #425

Open hilsonp opened 1 year ago

hilsonp commented 1 year ago

We would like to feed imposter logs into ELK and I then went read https://docs.imposter.sh/metrics_logs_telemetry/

$HOME/.imposter/.imposter.env contains:

IMPOSTER_LOG_SUMMARY=true

Unfortunatelly, it seems it is not taken into account when starting imposter with an "imposter up".

[imposter~]$ /usr/local/bin/imposter up /home/sites/imposter/mocks/prov-soa-csesd-imposterTest-v1 -p 9002
DEBU[0000] engine binary '3.23.0' already present
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
11:04:07 INFO  i.g.i.Imposter - Starting mock engine 3.23.0
11:04:07 DEBUG i.g.i.c.u.ConfigUtil - Loading configuration file: ConfigReference(file=/home/sites/imposter/mocks/prov-soa-csesd-imposterTest-v1/imposter-config.yaml, configRoot=/home/sites/imposter/mocks/prov-soa-csesd-imposterTest-v1)
11:04:08 DEBUG i.g.i.p.PluginManager - Loaded 7 plugin(s): [js-detector, store-detector, meta-detector, js-nashorn-standalone, store-inmem, config-detector, rest]
11:04:09 DEBUG i.g.i.p.r.RestPluginImpl - Adding handler: POST -> /_imposter
11:04:09 DEBUG i.g.i.p.r.RestPluginImpl - Adding handler: POST -> /*
11:04:09 INFO  i.g.i.Imposter - Mock engine up and running on http://localhost:9002
INFO[0002] watching for changes to: /home/sites/imposter/mocks/prov-soa-csesd-imposterTest-v1

curl --noproxy localhost --request POST http://localhost:9002/_imposter

11:06:43 DEBUG i.g.i.h.AbstractResourceMatcher - Matched resource config for POST http://localhost:9002/_imposter
11:06:43 INFO  i.g.i.p.r.RestPluginImpl - Handling object request for: POST http://localhost:9002/_imposter
11:06:43 INFO  i.g.i.s.ResponseServiceImpl - Serving response data (77 bytes) for POST http://localhost:9002/_imposter with status code 200

I don't know why I created the $HOME/.imposter/.imposter.env and could not find the place in the docs suggesting this. I therefore tried moving this file as /home/sites/imposter/mocks/prov-soa-csesd-imposterTest-v1/.env as documented here: https://docs.imposter.sh/usage/ Unfortunatelly, this did not help (and I would like to keep those settings globally).

outofcoffee commented 1 year ago

Hi @hilsonp, the .env files are read from the same directory as the -config.yaml files. The file is named .env rather than .imposter.env

When an envfile is discovered, if you have the logging level set to trace you should be able to see this line:

Loading envfile: /path/to/some/.env

The .env mechanism is just a shorthand for setting normal environment variables, so if you're looking to set things globally, you might set them in your ~/.bash_profile (or equivalent for other shells) and then start a new shell session.


Here's a working example:

$ ls -la 
total 24
drwxr-xr-x@   5 user  wheel   160 Jun 26 00:30 .
-rw-r--r--@   1 user  wheel    26 Jun 26 00:29 .env
-rw-r--r--@   1 user  wheel    70 Jun 26 00:29 imposter-config.yaml

imposter-config.yaml

plugin: rest
path: "/example"
response:
  content: "hello"

.env

IMPOSTER_LOG_SUMMARY=true

Running it:

$  imposter up
DEBU[0000] downloading https://github.com/outofcoffee/imposter/releases/download/v3.24.0/imposter.jar 
00:31:02 INFO  i.g.i.Imposter - Starting mock engine 3.24.0
00:31:02 DEBUG i.g.i.c.u.ConfigUtil - Loading configuration file: ConfigReference(file=/tmp/imposter-logging/simple-example-config.yaml, configRoot=/tmp/imposter-logging)
00:31:03 DEBUG i.g.i.p.PluginManager - Loaded 7 plugin(s): [js-detector, store-detector, meta-detector, js-nashorn-standalone, store-inmem, config-detector, rest]
00:31:04 DEBUG i.g.i.p.r.RestPluginImpl - Adding handler: GET -> /example
00:31:04 INFO  i.g.i.Imposter - Mock engine up and running on http://localhost:8080
INFO[0039] watching for changes to: /tmp/imposter-logging 

Test:

$ curl localhost:8080/example
hello

Log output:

00:37:38 INFO  i.g.i.p.r.RestPluginImpl - Handling object request for: GET http://localhost:8080/example
00:37:38 INFO  i.g.i.s.ResponseServiceImpl - Serving response data (5 bytes) for GET http://localhost:8080/example with status code 200
{"timestamp":"2023-06-26T00:37:38.507976+01:00","uri":"http://localhost:8080/example","path":"/example","method":"GET","statusCode":"200","duration":"5.77"}
hilsonp commented 1 year ago

Thank you for the details.

The imposter mocks are lauched by "monit" that even have no $HOME when launching commands :-(

I somehow managed to work-around it by adding this to the imposter user .bashrc important note: monit is running as imposter user ;-)

# Source imposter environment
if [ -f ~imposter/.imposter/imposter.env ]; then
  . ~imposter/.imposter/imposter.env
fi

The imposter.env contains:

export JAVA_TOOL_OPTIONS="-Dfile.encoding=UTF-8 -Xms64m -Xmx512m"
export IMPOSTER_LOG_SUMMARY=true

With this, I get the "summary logs" !