2020-11-14 11:24:49 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
package paramtable
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
import (
|
2020-12-08 14:41:04 +08:00
|
|
|
"os"
|
2022-08-25 11:02:53 +08:00
|
|
|
"strings"
|
2020-11-14 11:24:49 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-05-05 10:35:50 +08:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2020-11-14 11:24:49 +08:00
|
|
|
)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
var baseParams = BaseTable{}
|
2020-11-16 21:10:43 +08:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2021-06-18 15:20:08 +08:00
|
|
|
baseParams.Init()
|
2020-12-08 14:41:04 +08:00
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:02:53 +08:00
|
|
|
func TestBaseTable_GetConfigSubSet(t *testing.T) {
|
|
|
|
prefix := "rootcoord."
|
|
|
|
configs := baseParams.mgr.Configs()
|
|
|
|
|
|
|
|
configsWithPrefix := make(map[string]string)
|
|
|
|
for k, v := range configs {
|
|
|
|
if strings.HasPrefix(k, prefix) {
|
|
|
|
configsWithPrefix[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subSet := baseParams.GetConfigSubSet(prefix)
|
|
|
|
|
|
|
|
for k := range configs {
|
|
|
|
assert.Equal(t, subSet[k], configs[prefix+k])
|
|
|
|
}
|
|
|
|
assert.Equal(t, len(subSet), len(configsWithPrefix))
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:20:40 +08:00
|
|
|
func TestBaseTable_SaveAndLoad(t *testing.T) {
|
2021-06-18 15:20:08 +08:00
|
|
|
err1 := baseParams.Save("int", "10")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err1)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err2 := baseParams.Save("string", "testSaveAndLoad")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err3 := baseParams.Save("float", "1.234")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err3)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
r1, _ := baseParams.Load("int")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Equal(t, "10", r1)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
r2, _ := baseParams.Load("string")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Equal(t, "testSaveAndLoad", r2)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
r3, _ := baseParams.Load("float")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Equal(t, "1.234", r3)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err4 := baseParams.Remove("int")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err4)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err5 := baseParams.Remove("string")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err5)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err6 := baseParams.Remove("float")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err6)
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:20:40 +08:00
|
|
|
func TestBaseTable_Remove(t *testing.T) {
|
2021-06-18 15:20:08 +08:00
|
|
|
err1 := baseParams.Save("RemoveInt", "10")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err1)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err2 := baseParams.Save("RemoveString", "testRemove")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err3 := baseParams.Save("RemoveFloat", "1.234")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err3)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err4 := baseParams.Remove("RemoveInt")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err4)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err5 := baseParams.Remove("RemoveString")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err5)
|
|
|
|
|
2021-06-18 15:20:08 +08:00
|
|
|
err6 := baseParams.Remove("RemoveFloat")
|
2020-11-14 11:24:49 +08:00
|
|
|
assert.Nil(t, err6)
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:47:33 +08:00
|
|
|
func TestBaseTable_Get(t *testing.T) {
|
|
|
|
err := baseParams.Save("key", "10")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
v := baseParams.Get("key")
|
|
|
|
assert.Equal(t, "10", v)
|
|
|
|
|
|
|
|
v2 := baseParams.Get("none")
|
|
|
|
assert.Equal(t, "", v2)
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:28:11 +08:00
|
|
|
func TestBaseTable_Pulsar(t *testing.T) {
|
|
|
|
//test PULSAR ADDRESS
|
|
|
|
os.Setenv("PULSAR_ADDRESS", "pulsar://localhost:6650")
|
2022-08-01 10:04:33 +08:00
|
|
|
baseParams.Init()
|
2022-06-16 17:28:11 +08:00
|
|
|
|
2022-08-02 21:26:33 +08:00
|
|
|
address := baseParams.Get("pulsar.address")
|
2022-06-16 17:28:11 +08:00
|
|
|
assert.Equal(t, "pulsar://localhost:6650", address)
|
2022-08-02 21:26:33 +08:00
|
|
|
|
|
|
|
port := baseParams.Get("pulsar.port")
|
|
|
|
assert.NotEqual(t, "", port)
|
2022-06-16 17:28:11 +08:00
|
|
|
}
|
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
// func TestBaseTable_ConfDir(t *testing.T) {
|
|
|
|
// rightConfig := baseParams.configDir
|
|
|
|
// // fake dir
|
|
|
|
// baseParams.configDir = "./"
|
2021-09-26 19:46:17 +08:00
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
// assert.Panics(t, func() { baseParams.loadFromYaml(defaultYaml) })
|
2021-09-26 19:46:17 +08:00
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
// baseParams.configDir = rightConfig
|
|
|
|
// baseParams.loadFromYaml(defaultYaml)
|
|
|
|
// baseParams.GlobalInitWithYaml(defaultYaml)
|
|
|
|
// }
|
2021-09-26 19:46:17 +08:00
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
// func TestBateTable_ConfPath(t *testing.T) {
|
|
|
|
// os.Setenv("MILVUSCONF", "test")
|
|
|
|
// config := baseParams.initConfPath()
|
|
|
|
// assert.Equal(t, config, "test")
|
|
|
|
|
|
|
|
// os.Unsetenv("MILVUSCONF")
|
|
|
|
// dir, _ := os.Getwd()
|
|
|
|
// config = baseParams.initConfPath()
|
|
|
|
// assert.Equal(t, filepath.Clean(config), filepath.Clean(dir+"/../../../configs/"))
|
|
|
|
|
|
|
|
// // test use get dir
|
|
|
|
// os.Chdir(dir + "/../../../")
|
|
|
|
// defer os.Chdir(dir)
|
|
|
|
// config = baseParams.initConfPath()
|
|
|
|
// assert.Equal(t, filepath.Clean(config), filepath.Clean(dir+"/../../../configs/"))
|
|
|
|
// }
|
2021-09-26 19:46:17 +08:00
|
|
|
|
2021-11-10 14:27:37 +08:00
|
|
|
func TestBaseTable_Env(t *testing.T) {
|
|
|
|
os.Setenv("milvus.test", "test")
|
|
|
|
os.Setenv("milvus.test.test2", "test2")
|
|
|
|
|
|
|
|
baseParams.Init()
|
|
|
|
result, _ := baseParams.Load("test")
|
|
|
|
assert.Equal(t, result, "test")
|
|
|
|
|
|
|
|
result, _ = baseParams.Load("test.test2")
|
|
|
|
assert.Equal(t, result, "test2")
|
|
|
|
|
|
|
|
err := os.Setenv("milvus.invalid=xxx", "test")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = os.Setenv("milvus.invalid", "xxx=test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
baseParams.Init()
|
|
|
|
result, _ = baseParams.Load("invalid")
|
|
|
|
assert.Equal(t, result, "xxx=test")
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:20:40 +08:00
|
|
|
func TestBaseTable_Parse(t *testing.T) {
|
2021-09-02 22:18:10 +08:00
|
|
|
t.Run("ParseBool", func(t *testing.T) {
|
|
|
|
assert.Nil(t, baseParams.Save("key", "true"))
|
|
|
|
assert.True(t, baseParams.ParseBool("key", false))
|
|
|
|
assert.False(t, baseParams.ParseBool("not_exist_key", false))
|
|
|
|
|
|
|
|
assert.Nil(t, baseParams.Save("key", "rand"))
|
|
|
|
assert.Panics(t, func() { baseParams.ParseBool("key", false) })
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ParseFloat", func(t *testing.T) {
|
|
|
|
assert.Nil(t, baseParams.Save("key", "0"))
|
|
|
|
assert.Equal(t, float64(0), baseParams.ParseFloat("key"))
|
|
|
|
|
|
|
|
assert.Nil(t, baseParams.Save("key", "3.14"))
|
|
|
|
assert.Equal(t, float64(3.14), baseParams.ParseFloat("key"))
|
|
|
|
|
|
|
|
assert.Panics(t, func() { baseParams.ParseFloat("not_exist_key") })
|
|
|
|
assert.Nil(t, baseParams.Save("key", "abc"))
|
|
|
|
assert.Panics(t, func() { baseParams.ParseFloat("key") })
|
|
|
|
})
|
|
|
|
|
2021-10-28 23:16:40 +08:00
|
|
|
t.Run("ParseFloatWithDefault", func(t *testing.T) {
|
|
|
|
baseParams.Remove("key")
|
|
|
|
assert.Equal(t, float64(0.0), baseParams.ParseFloatWithDefault("key", 0.0))
|
|
|
|
assert.Equal(t, float64(3.14), baseParams.ParseFloatWithDefault("key", 3.14))
|
|
|
|
|
|
|
|
assert.Nil(t, baseParams.Save("key", "2"))
|
|
|
|
assert.Equal(t, float64(2.0), baseParams.ParseFloatWithDefault("key", 3.14))
|
|
|
|
})
|
|
|
|
|
2021-09-02 22:18:10 +08:00
|
|
|
t.Run("ParseInt32", func(t *testing.T) {
|
|
|
|
assert.Nil(t, baseParams.Save("key", "0"))
|
|
|
|
assert.Equal(t, int32(0), baseParams.ParseInt32("key"))
|
|
|
|
|
|
|
|
assert.Nil(t, baseParams.Save("key", "314"))
|
|
|
|
assert.Equal(t, int32(314), baseParams.ParseInt32("key"))
|
|
|
|
|
|
|
|
assert.Panics(t, func() { baseParams.ParseInt32("not_exist_key") })
|
|
|
|
assert.Nil(t, baseParams.Save("key", "abc"))
|
|
|
|
assert.Panics(t, func() { baseParams.ParseInt32("key") })
|
|
|
|
})
|
|
|
|
|
2021-10-28 23:16:40 +08:00
|
|
|
t.Run("ParseInt32WithDefault", func(t *testing.T) {
|
|
|
|
baseParams.Remove("key")
|
|
|
|
assert.Equal(t, int32(1), baseParams.ParseInt32WithDefault("key", 1))
|
|
|
|
assert.Nil(t, baseParams.Save("key", "2"))
|
|
|
|
assert.Equal(t, int32(2), baseParams.ParseInt32WithDefault("key", 1))
|
|
|
|
})
|
|
|
|
|
2021-09-02 22:18:10 +08:00
|
|
|
t.Run("ParseInt64", func(t *testing.T) {
|
|
|
|
assert.Nil(t, baseParams.Save("key", "0"))
|
|
|
|
assert.Equal(t, int64(0), baseParams.ParseInt64("key"))
|
|
|
|
|
|
|
|
assert.Nil(t, baseParams.Save("key", "314"))
|
|
|
|
assert.Equal(t, int64(314), baseParams.ParseInt64("key"))
|
|
|
|
|
|
|
|
assert.Panics(t, func() { baseParams.ParseInt64("not_exist_key") })
|
|
|
|
assert.Nil(t, baseParams.Save("key", "abc"))
|
|
|
|
assert.Panics(t, func() { baseParams.ParseInt64("key") })
|
|
|
|
})
|
2021-10-28 23:16:40 +08:00
|
|
|
|
|
|
|
t.Run("ParseInt64WithDefault", func(t *testing.T) {
|
|
|
|
baseParams.Remove("key")
|
|
|
|
assert.Equal(t, int64(1), baseParams.ParseInt64WithDefault("key", 1))
|
|
|
|
assert.Nil(t, baseParams.Save("key", "2"))
|
|
|
|
assert.Equal(t, int64(2), baseParams.ParseInt64WithDefault("key", 1))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ParseIntWithDefault", func(t *testing.T) {
|
|
|
|
baseParams.Remove("key")
|
|
|
|
assert.Equal(t, int(1), baseParams.ParseIntWithDefault("key", 1))
|
|
|
|
assert.Nil(t, baseParams.Save("key", "2"))
|
|
|
|
assert.Equal(t, int(2), baseParams.ParseIntWithDefault("key", 1))
|
|
|
|
})
|
2021-09-02 22:18:10 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 15:20:40 +08:00
|
|
|
func Test_ConvertRangeToIntSlice(t *testing.T) {
|
2021-09-02 22:18:10 +08:00
|
|
|
t.Run("ConvertRangeToIntSlice", func(t *testing.T) {
|
|
|
|
slice := ConvertRangeToIntSlice("0,10", ",")
|
|
|
|
assert.Equal(t, 10, len(slice))
|
|
|
|
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("0", ",") })
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("0, 10", ",") })
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("abc,10", ",") })
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("0,abc", ",") })
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("-1,9", ",") })
|
|
|
|
assert.Panics(t, func() { ConvertRangeToIntSlice("9,0", ",") })
|
|
|
|
})
|
|
|
|
}
|
2021-09-26 21:18:11 +08:00
|
|
|
|
2021-10-01 08:52:50 +08:00
|
|
|
func Test_SetLogger(t *testing.T) {
|
|
|
|
t.Run("TestSetLooger", func(t *testing.T) {
|
|
|
|
baseParams.RoleName = "rootcoord"
|
|
|
|
baseParams.Save("log.file.rootPath", ".")
|
|
|
|
baseParams.SetLogger(UniqueID(-1))
|
|
|
|
assert.Equal(t, "rootcoord.log", baseParams.Log.File.Filename)
|
|
|
|
|
|
|
|
baseParams.RoleName = "datanode"
|
|
|
|
baseParams.SetLogger(UniqueID(1))
|
|
|
|
assert.Equal(t, "datanode-1.log", baseParams.Log.File.Filename)
|
|
|
|
|
|
|
|
baseParams.RoleName = "datanode"
|
|
|
|
baseParams.SetLogger(UniqueID(0))
|
|
|
|
assert.Equal(t, "datanode-0.log", baseParams.Log.File.Filename)
|
2021-09-26 21:18:11 +08:00
|
|
|
})
|
2022-05-05 10:35:50 +08:00
|
|
|
|
|
|
|
t.Run("TestGrpclog", func(t *testing.T) {
|
|
|
|
baseParams.Save("grpc.log.level", "Warning")
|
|
|
|
baseParams.SetLogConfig()
|
|
|
|
|
|
|
|
baseParams.SetLogger(UniqueID(1))
|
|
|
|
assert.Equal(t, false, grpclog.V(0))
|
|
|
|
assert.Equal(t, true, grpclog.V(1))
|
|
|
|
assert.Equal(t, true, grpclog.V(2))
|
|
|
|
})
|
2021-09-26 21:18:11 +08:00
|
|
|
}
|