the method
import (
"fmt"
remote "github.com/shima-park/agollo/viper-remote"
"github.com/spf13/viper"
)
var configType = "prop"
func fetchApollo(ip, namespace string, conf *common.Config) error {
v := viper.New()
v.SetConfigType(configType)
err := v.AddRemoteProvider("apollo", ip, namespace)
if err != nil {
log.Error(fmt.Sprintf("AddRemoteProvider err : %s", err.Error()))
return err
}
err = v.ReadRemoteConfig()
if err != nil {
log.Error(fmt.Sprintf("ReadRemoteConfig err : %s", err.Error()))
return err
}
err = v.Unmarshal(conf)
if err != nil {
log.Error(fmt.Sprintf("unmarshal Config err : %s", err.Error()))
return err
}
return nil
}
the test case
import (
"errors"
"testing"
"github.com/agiledragon/gomonkey"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
)
func Test_fetchApollo(t *testing.T) {
var viperpt *viper.Viper
var conf = &common.Config{}
Convey(casenameprefix, t, func() {
Convey(casenameprefix+"error[fetchApollo][Unmarshal]", func() {
patch := gomonkey.ApplyMethod(reflect.TypeOf(viperpt), "AddRemoteProvider", func(_ *viper.Viper, provider, endpoint, path string) error {
log.Debug("ApplyMethod viper.Viper.AddRemoteProvider success")
return nil
})
patch.ApplyMethod(reflect.TypeOf(viperpt), "ReadRemoteConfig", func(_ *viper.Viper) error {
log.Debug("ApplyMethod viper.Viper.ReadRemoteConfig success")
return nil
})
patch.ApplyMethod(reflect.TypeOf(viperpt), "Unmarshal", func(_ *viper.Viper, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
log.Debug("ApplyMethod viper.Viper.Unmarshal success")
return errors.New("custom errors")
})
defer patch.Reset()
fetchApollo(conf)
})
})
}
the result
=== RUN Test_fetchApollo
custom:
custom:error[fetchApollo][Unmarshal] {"T":"2020-11-06T09:16:20.694+0800","C":"config/agollo_test.go:69","L":"debug","timestamp":1604625380,"M":"ApplyMethod viper.Viper.AddRemoteProvider success"}
{"T":"2020-11-06T09:16:20.697+0800","C":"config/agollo.go:51","L":"error","timestamp":1604625380,"M":"ReadRemoteConfig err : Remote Configurations Error: No Files Found"}
{"T":"2020-11-06T09:16:20.698+0800","C":"config/agollo.go:26","L":"error","timestamp":1604625380,"M":"get fetchApollo err : Remote Configurations Error: No Files Found"}
0 total assertions
--- PASS: Test_fetchApollo (0.01s)
PASS
ok config 1.537s
从运行结果来看,只有第一个 method AddRemoteProvider
patch成功了,后面的两个方法都没patch上,如果不patch AddRemoteProvider
,将测试方法改为如下
import (
"errors"
"testing"
"github.com/agiledragon/gomonkey"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
)
func Test_fetchApollo(t *testing.T) {
var viperpt *viper.Viper
var conf = &common.Config{}
Convey(casenameprefix, t, func() {
Convey(casenameprefix+"error[fetchApollo][Unmarshal]", func() {
//patch := gomonkey.ApplyMethod(reflect.TypeOf(viperpt), "AddRemoteProvider", func(_ *viper.Viper, provider, endpoint, path string) error {
// log.Debug("ApplyMethod viper.Viper.AddRemoteProvider success")
// return nil
//})
patch := gomonkey.ApplyMethod(reflect.TypeOf(viperpt), "ReadRemoteConfig", func(_ *viper.Viper) error {
log.Debug("ApplyMethod viper.Viper.ReadRemoteConfig success")
return nil
})
patch.ApplyMethod(reflect.TypeOf(viperpt), "Unmarshal", func(_ *viper.Viper, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
log.Debug("ApplyMethod viper.Viper.Unmarshal success")
return errors.New("custom errors")
})
defer patch.Reset()
fetchApollo(conf)
})
})
}
=== RUN Test_fetchApollo
custom:
custom:error[fetchApollo][Unmarshal] {"T":"2020-11-06T09:24:01.800+0800","C":"config/agollo.go:51","L":"error","timestamp":1604625841,"M":"ReadRemoteConfig err : Remote Configurations Error: No Files Found"}
{"T":"2020-11-06T09:24:01.801+0800","C":"config/agollo.go:26","L":"error","timestamp":1604625841,"M":"get fetchApollo err : Remote Configurations Error: No Files Found"}
0 total assertions
--- PASS: Test_fetchApollo (0.00s)
PASS
ok config 1.300s
从结果来看 ReadRemoteConfig
也没有patch成功
想知道这个是什么原因引起的,看了 github.com/spf13/viper
包中的两个方法,没看出有什么不同