sdk_init_test.go 2.5 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
package sdk

import (
	"context"
	"sync"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"github.com/stretchr/testify/assert"

	"github.com/oceanbase/obagent/api/common"
	"github.com/oceanbase/obagent/config"
	"github.com/oceanbase/obagent/lib/crypto"
	agentlog "github.com/oceanbase/obagent/log"
)

func init() {
	agentlog.InitLogger(agentlog.LoggerConfig{
		Level:      "debug",
		Filename:   "../tests/test.log",
		MaxSize:    10, // 10M
		MaxAge:     3,  // 3days
		MaxBackups: 3,
		LocalTime:  false,
		Compress:   false,
	})
}

var sdkOnce sync.Once

func initSDK() error {
	var err error
	sdkOnce.Do(func() {
		ctx := context.Background()
		err = InitSDK(ctx, config.SDKConfig{
			ConfigPropertiesDir: "../../etc/config_properties",
			ModuleConfigDir:     "../../etc/module_config",
			CryptoPath:          "../../etc/.config_secret.key",
			CryptoMethod:        crypto.PLAIN,
		})
		RegisterMgragentCallbacks(ctx)
		RegisterMonagentCallbacks(ctx)
	})

	return err
}

func TestInitSDK_Example(t *testing.T) {
	err := initSDK()
	assert.Nil(t, err)

	config.CurProcess = config.ProcessManagerAgent

	Convey("mgragent config", t, func() {

		common.InitBasicAuthConf(context.Background())

		err = config.NotifyModules(context.Background(), []string{config.ManagerAgentBasicAuthConfigModule})
		So(err, ShouldBeNil)
	})

	Convey("mgragent config", t, func() {
		err = config.InitModuleConfig(context.Background(), config.NotifyProcessConfigModule)
		So(err, ShouldBeNil)

		err = config.NotifyModules(context.Background(), []string{config.NotifyProcessConfigModule})
		So(err, ShouldBeNil)
	})
}

func TestInitSDK_WithWrongPath_Fail(t *testing.T) {
	Convey("init sdk with wrong config properties path", t, func() {
		err := InitSDK(context.Background(), config.SDKConfig{
			ConfigPropertiesDir: "../../etc/no-exist-path/config_properties",
			ModuleConfigDir:     "../../etc/module_config",
			CryptoPath:          "../../etc/.config_secret.key",
			CryptoMethod:        crypto.PLAIN,
		})
		So(err, ShouldNotBeNil)
	})

	Convey("init sdk with wrong module config path", t, func() {
		err := InitSDK(context.Background(), config.SDKConfig{
			ConfigPropertiesDir: "../../etc/config_properties",
			ModuleConfigDir:     "../../etc/no-exist-path/module_config",
			CryptoPath:          "../../etc/.config_secret.key",
			CryptoMethod:        crypto.PLAIN,
		})
		So(err, ShouldNotBeNil)
	})
}
func Test_processNotifyModuleConfigCallback(t *testing.T) {
	err := processNotifyModuleConfigCallback(context.Background(), nil)
	assert.NotNil(t, err)
}