From 8aa3d83f8c9471519a02f7779219238340fb86f8 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 16 Feb 2021 22:08:50 +0300 Subject: Add support for parsing env variables in the `.rr.yaml` config Signed-off-by: Valery Piashchynski --- tests/plugins/config/.rr.yaml | 24 --------- tests/plugins/config/config_test.go | 85 +++++++++++++++++++++++++++++-- tests/plugins/config/configs/.rr-env.yaml | 24 +++++++++ tests/plugins/config/configs/.rr.yaml | 24 +++++++++ 4 files changed, 130 insertions(+), 27 deletions(-) delete mode 100755 tests/plugins/config/.rr.yaml create mode 100755 tests/plugins/config/configs/.rr-env.yaml create mode 100755 tests/plugins/config/configs/.rr.yaml (limited to 'tests/plugins') diff --git a/tests/plugins/config/.rr.yaml b/tests/plugins/config/.rr.yaml deleted file mode 100755 index f449dcf3..00000000 --- a/tests/plugins/config/.rr.yaml +++ /dev/null @@ -1,24 +0,0 @@ -rpc: - listen: tcp://localhost:6060 - -logs: - mode: development - level: error - -reload: - interval: 1s - patterns: [".php"] - services: - http: - recursive: true - ignore: ["vendor"] - patterns: [".php", ".go",".md",] - dirs: ["."] - jobs: - recursive: false - ignore: ["service/metrics"] - dirs: ["./jobs"] - rpc: - recursive: true - patterns: [".json"] - dirs: [""] diff --git a/tests/plugins/config/config_test.go b/tests/plugins/config/config_test.go index dc387086..0ce82bd9 100755 --- a/tests/plugins/config/config_test.go +++ b/tests/plugins/config/config_test.go @@ -19,7 +19,7 @@ func TestViperProvider_Init(t *testing.T) { t.Fatal(err) } vp := &config.Viper{} - vp.Path = ".rr.yaml" + vp.Path = "configs/.rr.yaml" vp.Prefix = "rr" vp.Flags = nil @@ -73,7 +73,7 @@ func TestConfigOverwriteFail(t *testing.T) { t.Fatal(err) } vp := &config.Viper{} - vp.Path = ".rr.yaml" + vp.Path = "configs/.rr.yaml" vp.Prefix = "rr" vp.Flags = []string{"rpc.listen=tcp//not_exist"} @@ -95,7 +95,7 @@ func TestConfigOverwriteValid(t *testing.T) { t.Fatal(err) } vp := &config.Viper{} - vp.Path = ".rr.yaml" + vp.Path = "configs/.rr.yaml" vp.Prefix = "rr" vp.Flags = []string{"rpc.listen=tcp://localhost:6061"} @@ -136,3 +136,82 @@ func TestConfigOverwriteValid(t *testing.T) { } } } + +func TestConfigEnvVariables(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(false), endure.SetLogLevel(endure.ErrorLevel)) + if err != nil { + t.Fatal(err) + } + + err = os.Setenv("SUPER_RPC_ENV", "tcp://localhost:6061") + assert.NoError(t, err) + + vp := &config.Viper{} + vp.Path = "configs/.rr-env.yaml" + vp.Prefix = "rr" + + err = container.RegisterAll( + &logger.ZapLogger{}, + &rpc.Plugin{}, + vp, + &Foo2{}, + ) + assert.NoError(t, err) + + err = container.Init() + assert.NoError(t, err) + + errCh, err := container.Serve() + assert.NoError(t, err) + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + tt := time.NewTicker(time.Second * 3) + defer tt.Stop() + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + assert.NoError(t, er) + return + case <-tt.C: + assert.NoError(t, container.Stop()) + return + } + } +} + +func TestConfigEnvVariablesFail(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(false), endure.SetLogLevel(endure.ErrorLevel)) + if err != nil { + t.Fatal(err) + } + + err = os.Setenv("SUPER_RPC_ENV", "tcp://localhost:6065") + assert.NoError(t, err) + + vp := &config.Viper{} + vp.Path = "configs/.rr-env.yaml" + vp.Prefix = "rr" + + err = container.RegisterAll( + &logger.ZapLogger{}, + &rpc.Plugin{}, + vp, + &Foo2{}, + ) + assert.NoError(t, err) + + err = container.Init() + assert.NoError(t, err) + + _, err = container.Serve() + assert.Error(t, err) +} \ No newline at end of file diff --git a/tests/plugins/config/configs/.rr-env.yaml b/tests/plugins/config/configs/.rr-env.yaml new file mode 100755 index 00000000..3cacb5d0 --- /dev/null +++ b/tests/plugins/config/configs/.rr-env.yaml @@ -0,0 +1,24 @@ +rpc: + listen: ${SUPER_RPC_ENV} + +logs: + mode: development + level: error + +reload: + interval: 1s + patterns: [ ".php" ] + services: + http: + recursive: true + ignore: [ "vendor" ] + patterns: [ ".php", ".go",".md", ] + dirs: [ "." ] + jobs: + recursive: false + ignore: [ "service/metrics" ] + dirs: [ "./jobs" ] + rpc: + recursive: true + patterns: [ ".json" ] + dirs: [ "" ] diff --git a/tests/plugins/config/configs/.rr.yaml b/tests/plugins/config/configs/.rr.yaml new file mode 100755 index 00000000..f449dcf3 --- /dev/null +++ b/tests/plugins/config/configs/.rr.yaml @@ -0,0 +1,24 @@ +rpc: + listen: tcp://localhost:6060 + +logs: + mode: development + level: error + +reload: + interval: 1s + patterns: [".php"] + services: + http: + recursive: true + ignore: ["vendor"] + patterns: [".php", ".go",".md",] + dirs: ["."] + jobs: + recursive: false + ignore: ["service/metrics"] + dirs: ["./jobs"] + rpc: + recursive: true + patterns: [".json"] + dirs: [""] -- cgit v1.2.3