config_module_test.go 6.3 KB
Newer Older
W
wangzelin.wzl 已提交
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
package config

import (
	"context"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"testing"

	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	. "github.com/smartystreets/goconvey/convey"
	"github.com/stretchr/testify/assert"

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

var (
	fooServer = &FooService{
		Foo: &Foo{Foo: "foo_value"},
	}
)

const (
	testFooModule                = "foo"
	testFooModuleType ModuleType = "fooType"
)

type Foo struct {
	Foo string `json:"foo"`
	Bar Bar    `json:"bar"`
}

type Bar struct {
	Bar      int    `json:"bar"`
	Duration string `json:"duration"`
}

type FooService struct {
	Foo         *Foo
	FooNoDefine string `json:"fooNoDefine"`
}

var (
	fooKVYaml = `configVersion: "foo"
configs:
    - key: foo.foo
      value: foo_value
    - key: foo.no.defined
      valueType: string
      value: foo-no-define
    - key: foo.bar.duration
      value: 10s
    - key: foo.bar.bar
      value: 3306`
C
chris-sun-star 已提交
58

59
	fooModuleYaml = `modules:
C
chris-sun-star 已提交
60 61 62
    -
      module: foo
      moduleType: fooType
O
ob-robot 已提交
63
      process: ob_mgragent
C
chris-sun-star 已提交
64 65 66 67 68 69
      config:
        foo: ${foo.foo}
        fooNoDefine: ${foo.not.defined}
        bar:
          bar: ${foo.bar.bar}
          duration: ${foo.bar.duration}`
W
wangzelin.wzl 已提交
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
)

func _init(t *testing.T) string {
	SetConfigPropertyMeta(
		&ConfigProperty{
			Key:          "foo.foo",
			DefaultValue: "foo",
			ValueType:    ValueString,
			Encrypted:    true,
			Fatal:        false,
			Masked:       false,
			NeedRestart:  false,
			Description:  "",
			Unit:         "",
			Valid:        nil,
		})
	SetConfigPropertyMeta(
		&ConfigProperty{
			Key:          "foo.bar.bar",
			DefaultValue: 3306,
			ValueType:    ValueInt64,
			Fatal:        false,
			Masked:       true,
			NeedRestart:  true,
			Description:  "",
			Unit:         "",
			Valid:        nil,
		})
	SetConfigPropertyMeta(
		&ConfigProperty{
			Key:          "foo.bar.duration",
			DefaultValue: "100ms",
			ValueType:    ValueString,
			Fatal:        false,
			Masked:       true,
			NeedRestart:  true,
			Description:  "",
			Unit:         "",
			Valid:        nil,
		})

O
ob-robot 已提交
111 112
	cryptoErr := InitCrypto("", crypto.PLAIN)
	assert.Nil(t, cryptoErr)
W
wangzelin.wzl 已提交
113 114 115 116 117 118

	tempDir := os.TempDir()

	moduleConfigDir := filepath.Join(tempDir, "module_config")
	err := os.MkdirAll(moduleConfigDir, 0755)
	assert.Nil(t, err)
119 120
	err = ioutil.WriteFile(filepath.Join(moduleConfigDir, "foo.yaml"), []byte(fooModuleYaml), 0755)
	assert.Nil(t, err)
W
wangzelin.wzl 已提交
121 122 123 124 125 126 127

	configPropertiesDir := filepath.Join(tempDir, "config_properties")
	err = os.MkdirAll(configPropertiesDir, 0755)
	assert.Nil(t, err)
	err = ioutil.WriteFile(filepath.Join(configPropertiesDir, "foo.yaml"), []byte(fooKVYaml), 0755)
	assert.Nil(t, err)

O
ob-robot 已提交
128
	err = InitModuleConfigs(context.Background(), moduleConfigDir)
W
wangzelin.wzl 已提交
129
	assert.Nil(t, err)
O
ob-robot 已提交
130
	err = InitConfigProperties(context.Background(), configPropertiesDir)
W
wangzelin.wzl 已提交
131 132 133
	assert.Nil(t, err)

	agentlog.InitLogger(agentlog.LoggerConfig{
O
ob-robot 已提交
134 135 136 137 138 139 140
		Level:      "debug",
		Filename:   "../tests/test.log",
		MaxSize:    10, // 10M
		MaxAge:     3,  // 3days
		MaxBackups: 3,
		LocalTime:  false,
		Compress:   false,
W
wangzelin.wzl 已提交
141 142
	})

O
ob-robot 已提交
143 144
	CurProcess = ProcessManagerAgent

W
wangzelin.wzl 已提交
145 146
	registerFooCallback()

O
ob-robot 已提交
147 148
	SetProcessModuleConfigNotifyAddress(ProcessConfigNotifyAddress{Process: ProcessManagerAgent})

W
wangzelin.wzl 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	return tempDir
}

func cleanup() {
	mainModuleConfig = nil
	mainConfigProperties = nil
	configPropertyMetas = map[string]*ConfigProperty{}
	callbacks = map[ModuleType]*ConfigCallback{}
}

func registerFooCallback() {

	RegisterConfigCallback(testFooModuleType,
		// create an instance of foo
		func() interface{} {
			return new(Foo)
		},
		// init config
		func(ctx context.Context, conf interface{}) error {
			log.WithField("module config", conf).Info("init foo config")
			foo, ok := conf.(*Foo)
			if !ok {
				return errors.Errorf("config is not *Foo, but %s", reflect.TypeOf(conf))
			}
			log.WithField("module", foo).Info("init foo config")
			fooServer.Foo = foo
O
ob-robot 已提交
175
			log.Infof("init module %s config successfully", testFooModule)
W
wangzelin.wzl 已提交
176 177 178 179 180 181 182 183 184 185 186
			return nil
		},
		// notify updated config
		func(ctx context.Context, conf interface{}) error {
			log.WithField("module config", conf).Info("update foo config")
			foo, ok := conf.(*Foo)
			if !ok {
				return errors.Errorf("config is not *Foo, but %s", reflect.TypeOf(conf))
			}
			log.WithField("module", foo).Info("update foo config")
			fooServer.Foo = foo
O
ob-robot 已提交
187
			log.Infof("update module %s config successfully", testFooModule)
W
wangzelin.wzl 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
			return nil
		},
	)
}

func TestModuleConfigCallback_Success(t *testing.T) {
	_init(t)
	defer cleanup()

	t.Run("foo callback", func(t *testing.T) {
		callback, ex := getModuleCallback(testFooModule)
		Convey("getModuleCallback", t, func() {
			So(ex, ShouldBeTrue)
			So(callback, ShouldNotBeNil)
		})

		err := callback.InitConfigCallback(context.Background(), testFooModule)
		Convey("InitConfigCallback before UpdateConfigCallback", t, func() {
			So(err, ShouldBeNil)
			So(fooServer.Foo.Foo, ShouldEqual, "foo_value")
			So(fooServer.Foo.Bar.Bar, ShouldEqual, 3306)
			So(fooServer.Foo.Bar.Duration, ShouldEqual, "10s")
		})
O
ob-robot 已提交
211 212 213 214 215 216 217 218 219 220 221

		err = callback.NotifyConfigCallback(context.Background(), &NotifyModuleConfig{
			Process: ProcessManagerAgent,
			Module:  testFooModule,
			Config:  &Foo{Foo: "foofoo", Bar: Bar{Bar: 2883}},
		})
		Convey("NotifyConfigCallback", t, func() {
			So(err, ShouldBeNil)
			So(fooServer.Foo.Foo, ShouldEqual, "foofoo")
			So(fooServer.Foo.Bar.Bar, ShouldEqual, 2883)
		})
W
wangzelin.wzl 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	})
}

func TestGetFinalModuleConfig(t *testing.T) {
	_init(t)
	defer cleanup()

	Convey("GetFinalModuleConfig with exist module", t, func() {
		conf, err := GetFinalModuleConfig("foo")
		So(err, ShouldBeNil)
		foo, ok := conf.Config.(*Foo)
		So(ok, ShouldBeTrue)
		So(foo.Foo, ShouldEqual, "foo_value")
		So(foo.Bar.Bar, ShouldEqual, 3306)
		So(foo.Bar.Duration, ShouldEqual, "10s")
	})

	Convey("GetFinalModuleConfig with not exist module", t, func() {
		_, err := GetFinalModuleConfig("module-not-exist")
		So(err, ShouldNotBeNil)
	})

	Convey("GetFinalModuleConfig with not exist module callback", t, func() {
		delete(callbacks, "foo")
		_, err := GetFinalModuleConfig("foo")
		So(err, ShouldBeNil)
	})

}

func TestInitModule(t *testing.T) {
	_init(t)
	defer cleanup()

	err := InitModuleTypeConfig(context.Background(), testFooModuleType)
	Convey("InitModuleTypeConfig", t, func() {
		So(err, ShouldBeNil)
	})

	err = InitModuleConfig(context.Background(), testFooModule)
	Convey("InitModuleConfig", t, func() {
		So(err, ShouldBeNil)
	})
}