github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/configmgr/apollo_test.go (about) 1 package configmgr_test 2 3 import ( 4 "github.com/apolloconfig/agollo/v4" 5 "github.com/apolloconfig/agollo/v4/agcache/memory" 6 apolloConfig "github.com/apolloconfig/agollo/v4/env/config" 7 "github.com/golang/mock/gomock" 8 "github.com/pkg/errors" 9 . "github.com/smartystreets/goconvey/convey" 10 "github.com/unionj-cloud/go-doudou/framework/configmgr" 11 "github.com/unionj-cloud/go-doudou/framework/configmgr/mock" 12 "github.com/unionj-cloud/go-doudou/framework/internal/config" 13 "testing" 14 ) 15 16 func TestLoadFromApollo(t *testing.T) { 17 Convey("Should not have error", t, func() { 18 ctrl := gomock.NewController(t) 19 defer ctrl.Finish() 20 configClient := mock.NewMockClient(ctrl) 21 factory := &memory.DefaultCacheFactory{} 22 cache := factory.Create() 23 cache.Set("gdd.retry.count", "3", 0) 24 cache.Set("gdd.weight", "5", 0) 25 configClient. 26 EXPECT(). 27 GetConfigCache(config.DefaultGddApolloNamespace). 28 AnyTimes(). 29 Return(cache) 30 31 configmgr.StartWithConfig = func(loadAppConfig func() (*apolloConfig.AppConfig, error)) (agollo.Client, error) { 32 _, _ = loadAppConfig() 33 return configClient, nil 34 } 35 36 if configmgr.ApolloClient != nil { 37 configmgr.ApolloClient = configClient 38 } 39 40 apolloCluster := config.DefaultGddApolloCluster 41 apolloAddr := config.GddApolloAddr.Load() 42 apolloNamespace := config.DefaultGddApolloNamespace 43 apolloBackupPath := config.DefaultGddApolloBackupPath 44 c := &apolloConfig.AppConfig{ 45 AppID: config.GddServiceName.Load(), 46 Cluster: apolloCluster, 47 IP: apolloAddr, 48 NamespaceName: apolloNamespace, 49 IsBackupConfig: false, 50 BackupConfigPath: apolloBackupPath, 51 MustStart: false, 52 } 53 So(func() { 54 configmgr.LoadFromApollo(c) 55 }, ShouldNotPanic) 56 }) 57 } 58 59 func TestInitialiseApolloConfig(t *testing.T) { 60 Convey("Should have error", t, func() { 61 configmgr.StartWithConfig = func(loadAppConfig func() (*apolloConfig.AppConfig, error)) (agollo.Client, error) { 62 return nil, errors.New("mock test error") 63 } 64 So(func() { 65 configmgr.InitialiseApolloConfig(nil) 66 }, ShouldPanic) 67 }) 68 }