From ed58333abaaa47ccef6a2ee416b1472241dacccf Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 14 Mar 2023 18:04:37 +0300 Subject: [PATCH] [#11] Support reading env variables from file Signed-off-by: Denis Kirillov --- frostfs.go | 1 + go.mod | 1 + go.sum | 2 ++ internal/env/parser.go | 50 ++++++++++++++++++++++++++++++++++++ scenarios/grpc.js | 3 +++ scenarios/http.js | 3 +++ scenarios/libs/env-parser.js | 10 ++++++++ scenarios/run_scenarios.md | 6 +++++ scenarios/s3.js | 3 +++ scenarios/verify.js | 3 +++ 10 files changed, 82 insertions(+) create mode 100644 internal/env/parser.go create mode 100644 scenarios/libs/env-parser.js diff --git a/frostfs.go b/frostfs.go index 3fa1df6..1f1ab3a 100644 --- a/frostfs.go +++ b/frostfs.go @@ -2,6 +2,7 @@ package xk6_frostfs import ( _ "git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/datagen" + _ "git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/env" _ "git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/logging" _ "git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/native" _ "git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/registry" diff --git a/go.mod b/go.mod index 0cbf3d8..117ce81 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/s3 v1.26.9 github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf github.com/google/uuid v1.3.0 + github.com/joho/godotenv v1.5.1 github.com/nspcc-dev/neo-go v0.100.1 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index db92cc6..cd733e3 100644 --- a/go.sum +++ b/go.sum @@ -281,6 +281,8 @@ github.com/jhump/protoreflect v1.11.0/go.mod h1:U7aMIjN0NWq9swDP7xDdoMfRHb35uiuT github.com/jhump/protoreflect v1.12.0/go.mod h1:JytZfP5d0r8pVNLZvai7U/MCuTWITgrI4tTg7puQFKI= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= diff --git a/internal/env/parser.go b/internal/env/parser.go new file mode 100644 index 0000000..af317e6 --- /dev/null +++ b/internal/env/parser.go @@ -0,0 +1,50 @@ +package env + +import ( + "os" + + "github.com/joho/godotenv" + "go.k6.io/k6/js/modules" +) + +// RootModule is the global module object type. It is instantiated once per test +// run and will be used to create k6/x/frostfs/registry module instances for each VU. +type RootModule struct{} + +// Parser represents an instance of the module for every VU. +type Parser struct { + vu modules.VU +} + +// Ensure the interfaces are implemented correctly. +var ( + _ modules.Instance = &Parser{} + _ modules.Module = &RootModule{} +) + +func init() { + modules.Register("k6/x/frostfs/env", new(RootModule)) +} + +// NewModuleInstance implements the modules.Module interface and returns +// a new instance for each VU. +func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { + mi := &Parser{vu: vu} + return mi +} + +// Exports implements the modules.Instance interface and returns the exports +// of the JS module. +func (p *Parser) Exports() modules.Exports { + return modules.Exports{Default: p} +} + +func (p *Parser) Parse(fileName string) (map[string]string, error) { + f, err := os.Open(fileName) + if err != nil { + return nil, err + } + defer f.Close() + + return godotenv.Parse(f) +} diff --git a/scenarios/grpc.js b/scenarios/grpc.js index 53588cf..64ae400 100644 --- a/scenarios/grpc.js +++ b/scenarios/grpc.js @@ -5,6 +5,9 @@ import registry from 'k6/x/frostfs/registry'; import { SharedArray } from 'k6/data'; import { sleep } from 'k6'; import { textSummary } from './libs/k6-summary-0.0.2.js'; +import { parseEnv } from './libs/env-parser.js'; + +parseEnv(); const obj_list = new SharedArray('obj_list', function () { return JSON.parse(open(__ENV.PREGEN_JSON)).objects; diff --git a/scenarios/http.js b/scenarios/http.js index 2537387..45cc627 100644 --- a/scenarios/http.js +++ b/scenarios/http.js @@ -5,6 +5,9 @@ import http from 'k6/http'; import { SharedArray } from 'k6/data'; import { sleep } from 'k6'; import { textSummary } from './libs/k6-summary-0.0.2.js'; +import { parseEnv } from './libs/env-parser.js'; + +parseEnv(); const obj_list = new SharedArray('obj_list', function () { return JSON.parse(open(__ENV.PREGEN_JSON)).objects; diff --git a/scenarios/libs/env-parser.js b/scenarios/libs/env-parser.js new file mode 100644 index 0000000..0703ac0 --- /dev/null +++ b/scenarios/libs/env-parser.js @@ -0,0 +1,10 @@ +import env from 'k6/x/frostfs/env'; + +export function parseEnv() { + if (__ENV.ENV_FILE) { + const parsedVars = env.parse(__ENV.ENV_FILE) + for (const prop in parsedVars) { + __ENV[prop] = __ENV[prop] || parsedVars[prop]; + } + } +} diff --git a/scenarios/run_scenarios.md b/scenarios/run_scenarios.md index c008cc6..dde3cd7 100644 --- a/scenarios/run_scenarios.md +++ b/scenarios/run_scenarios.md @@ -2,6 +2,12 @@ # How to execute scenarios +**Note:** you can provide file with all environment variables (system env variables overrides env from file) using +`-e ENV_FILE=.env` (relative path to that file must start from working directory): +```shell +$ ./k6 run -e ENV_FILE=.env some-scenario.js +``` + ## Common options for gRPC, HTTP, S3 scenarios: Scenarios `grpc.js`, `http.js` and `s3.js` support the following options: diff --git a/scenarios/s3.js b/scenarios/s3.js index 8a8e0ae..0694932 100644 --- a/scenarios/s3.js +++ b/scenarios/s3.js @@ -5,6 +5,9 @@ import s3 from 'k6/x/frostfs/s3'; import { SharedArray } from 'k6/data'; import { sleep } from 'k6'; import { textSummary } from './libs/k6-summary-0.0.2.js'; +import { parseEnv } from './libs/env-parser.js'; + +parseEnv(); const obj_list = new SharedArray('obj_list', function () { return JSON.parse(open(__ENV.PREGEN_JSON)).objects; diff --git a/scenarios/verify.js b/scenarios/verify.js index ac055c8..a65f265 100644 --- a/scenarios/verify.js +++ b/scenarios/verify.js @@ -4,6 +4,9 @@ import s3 from 'k6/x/frostfs/s3'; import { sleep } from 'k6'; import { Counter } from 'k6/metrics'; import { textSummary } from './libs/k6-summary-0.0.2.js'; +import { parseEnv } from './libs/env-parser.js'; + +parseEnv(); const obj_registry = registry.open(__ENV.REGISTRY_FILE);