summaryrefslogtreecommitdiff
path: root/internal/rpc
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2022-03-06 12:13:02 +0100
committerValery Piashchynski <[email protected]>2022-03-06 12:13:02 +0100
commit70e4f020afd0352dc52114651f0f65c1965ed399 (patch)
treecc6595214e4ff39600099fb8c6f39e7a3dd560e9 /internal/rpc
parent587702be62b65c151d27dc79e62fcbbd11290e6f (diff)
remove config plugin usage from the root
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal/rpc')
-rw-r--r--internal/rpc/client.go20
-rw-r--r--internal/rpc/client_test.go21
-rw-r--r--internal/rpc/test/config_rpc_conn_err.yaml2
-rw-r--r--internal/rpc/test/config_rpc_empty.yaml0
-rw-r--r--internal/rpc/test/config_rpc_ok.yaml2
-rw-r--r--internal/rpc/test/config_rpc_wrong.yaml2
6 files changed, 27 insertions, 20 deletions
diff --git a/internal/rpc/client.go b/internal/rpc/client.go
index 4e58972b..7d945add 100644
--- a/internal/rpc/client.go
+++ b/internal/rpc/client.go
@@ -1,24 +1,34 @@
-// Package prc contains wrapper around RPC client ONLY for internal usage.
+// Package rpc contains wrapper around RPC client ONLY for internal usage.
package rpc
import (
"net/rpc"
- "github.com/roadrunner-server/config/v2"
"github.com/roadrunner-server/errors"
goridgeRpc "github.com/roadrunner-server/goridge/v3/pkg/rpc"
rpcPlugin "github.com/roadrunner-server/rpc/v2"
+ "github.com/spf13/viper"
)
// NewClient creates client ONLY for internal usage (communication between our application with RR side).
// Client will be connected to the RPC.
-func NewClient(cfgPlugin *config.Plugin) (*rpc.Client, error) {
- if !cfgPlugin.Has(rpcPlugin.PluginName) {
+func NewClient(cfg string) (*rpc.Client, error) {
+ v := viper.New()
+ v.SetConfigFile(cfg)
+
+ err := v.ReadInConfig()
+ if err != nil {
+ return nil, err
+ }
+
+ if !v.IsSet(rpcPlugin.PluginName) {
return nil, errors.E("rpc service disabled")
}
rpcConfig := &rpcPlugin.Config{}
- if err := cfgPlugin.UnmarshalKey(rpcPlugin.PluginName, rpcConfig); err != nil {
+
+ err = v.UnmarshalKey(rpcPlugin.PluginName, rpcConfig)
+ if err != nil {
return nil, err
}
diff --git a/internal/rpc/client_test.go b/internal/rpc/client_test.go
index 34240826..0744e167 100644
--- a/internal/rpc/client_test.go
+++ b/internal/rpc/client_test.go
@@ -14,28 +14,22 @@ func TestNewClient_RpcServiceDisabled(t *testing.T) {
cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte{}}
assert.NoError(t, cfgPlugin.Init())
- c, err := rpc.NewClient(cfgPlugin)
+ c, err := rpc.NewClient("test/config_rpc_empty.yaml")
assert.Nil(t, c)
assert.EqualError(t, err, "rpc service disabled")
}
func TestNewClient_WrongRcpConfiguration(t *testing.T) {
- cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n $foo bar")}
- assert.NoError(t, cfgPlugin.Init())
-
- c, err := rpc.NewClient(cfgPlugin)
+ c, err := rpc.NewClient("test/config_rpc_wrong.yaml")
assert.Nil(t, c)
assert.Error(t, err)
- assert.Contains(t, err.Error(), "config_plugin_unmarshal_key")
+ assert.Contains(t, err.Error(), "'' expected a map, got 'string'")
}
func TestNewClient_ConnectionError(t *testing.T) {
- cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://127.0.0.1:0")}
- assert.NoError(t, cfgPlugin.Init())
-
- c, err := rpc.NewClient(cfgPlugin)
+ c, err := rpc.NewClient("test/config_rpc_conn_err.yaml")
assert.Nil(t, c)
assert.Error(t, err)
@@ -43,15 +37,12 @@ func TestNewClient_ConnectionError(t *testing.T) {
}
func TestNewClient_SuccessfullyConnected(t *testing.T) {
- l, err := net.Listen("tcp", "127.0.0.1:0")
+ l, err := net.Listen("tcp", "127.0.0.1:55555")
assert.NoError(t, err)
defer func() { assert.NoError(t, l.Close()) }()
- cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://" + l.Addr().String())}
- assert.NoError(t, cfgPlugin.Init())
-
- c, err := rpc.NewClient(cfgPlugin)
+ c, err := rpc.NewClient("test/config_rpc_ok.yaml")
assert.NotNil(t, c)
assert.NoError(t, err)
diff --git a/internal/rpc/test/config_rpc_conn_err.yaml b/internal/rpc/test/config_rpc_conn_err.yaml
new file mode 100644
index 00000000..706c68a3
--- /dev/null
+++ b/internal/rpc/test/config_rpc_conn_err.yaml
@@ -0,0 +1,2 @@
+rpc:
+ listen: tcp://127.0.0.1:0
diff --git a/internal/rpc/test/config_rpc_empty.yaml b/internal/rpc/test/config_rpc_empty.yaml
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/internal/rpc/test/config_rpc_empty.yaml
diff --git a/internal/rpc/test/config_rpc_ok.yaml b/internal/rpc/test/config_rpc_ok.yaml
new file mode 100644
index 00000000..8b3140ce
--- /dev/null
+++ b/internal/rpc/test/config_rpc_ok.yaml
@@ -0,0 +1,2 @@
+rpc:
+ listen: tcp://127.0.0.1:55555
diff --git a/internal/rpc/test/config_rpc_wrong.yaml b/internal/rpc/test/config_rpc_wrong.yaml
new file mode 100644
index 00000000..8282ee1f
--- /dev/null
+++ b/internal/rpc/test/config_rpc_wrong.yaml
@@ -0,0 +1,2 @@
+rpc:
+ $foo bar