mirror of https://github.com/milvus-io/milvus.git
test: use `T.Setenv` to set env vars in tests (#19649)
This commit replaces `os.Setenv` with `t.Setenv` in tests. The environment variable is automatically restored to its original value when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.Setenv Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>pull/19706/head
parent
667982a239
commit
438790744d
|
@ -32,7 +32,7 @@ func TestConfigFromEnv(t *testing.T) {
|
|||
_, err := mgr.GetConfig("test.env")
|
||||
assert.EqualError(t, err, "key not found: test.env")
|
||||
|
||||
os.Setenv("TEST_ENV", "value")
|
||||
t.Setenv("TEST_ENV", "value")
|
||||
mgr, _ = Init(WithEnvSource(formatKey))
|
||||
|
||||
v, err := mgr.GetConfig("test.env")
|
||||
|
@ -54,8 +54,8 @@ func TestConfigFromRemote(t *testing.T) {
|
|||
|
||||
client := v3client.New(e.Server)
|
||||
|
||||
os.Setenv("TMP_KEY", "1")
|
||||
os.Setenv("log.level", "info")
|
||||
t.Setenv("TMP_KEY", "1")
|
||||
t.Setenv("log.level", "info")
|
||||
mgr, _ := Init(WithEnvSource(formatKey),
|
||||
WithFilesSource("../../configs/milvus.yaml"),
|
||||
WithEtcdSource(&EtcdInfo{
|
||||
|
|
|
@ -18,7 +18,6 @@ package datacoord
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
@ -92,7 +91,7 @@ func TestChannelManager_StateTransfer(t *testing.T) {
|
|||
}()
|
||||
|
||||
p := "/tmp/milvus_ut/rdb_data"
|
||||
os.Setenv("ROCKSMQ_PATH", p)
|
||||
t.Setenv("ROCKSMQ_PATH", p)
|
||||
|
||||
prefix := Params.DataCoordCfg.ChannelWatchSubPath
|
||||
|
||||
|
|
|
@ -29,13 +29,13 @@ import (
|
|||
)
|
||||
|
||||
func TestEtcdConfigLoad(te *testing.T) {
|
||||
os.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
te.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
param := new(paramtable.ServiceParam)
|
||||
|
||||
os.Setenv("etcd.use.embed", "true")
|
||||
te.Setenv("etcd.use.embed", "true")
|
||||
// TODO, not sure if the relative path works for ci environment
|
||||
os.Setenv("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
||||
os.Setenv("etcd.data.dir", "etcd.test.data.dir")
|
||||
te.Setenv("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
||||
te.Setenv("etcd.data.dir", "etcd.test.data.dir")
|
||||
|
||||
param.Init()
|
||||
//clean up data
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package etcdkv_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -31,13 +30,13 @@ import (
|
|||
)
|
||||
|
||||
func TestEmbedEtcd(te *testing.T) {
|
||||
os.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
te.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
param := new(paramtable.ServiceParam)
|
||||
os.Setenv("etcd.use.embed", "true")
|
||||
os.Setenv("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
||||
te.Setenv("etcd.use.embed", "true")
|
||||
te.Setenv("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
||||
|
||||
dir := te.TempDir()
|
||||
os.Setenv("etcd.data.dir", dir)
|
||||
te.Setenv("etcd.data.dir", dir)
|
||||
|
||||
param.Init()
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ import (
|
|||
|
||||
func TestEtcdRestartLoad(te *testing.T) {
|
||||
etcdDataDir := "/tmp/_etcd_data"
|
||||
os.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
te.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
param := new(paramtable.ServiceParam)
|
||||
param.Init()
|
||||
param.BaseTable.Save("etcd.use.embed", "true")
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
@ -42,6 +41,6 @@ func TestRegisterMetrics(t *testing.T) {
|
|||
func TestGetMetricsAddr(t *testing.T) {
|
||||
assert.Equal(t, getMetricsAddr(), ":"+DefaultListenPort)
|
||||
testPort := "9092"
|
||||
os.Setenv(ListenPortEnvKey, testPort)
|
||||
t.Setenv(ListenPortEnvKey, testPort)
|
||||
assert.Equal(t, getMetricsAddr(), ":"+testPort)
|
||||
}
|
||||
|
|
|
@ -412,9 +412,8 @@ func TestProxy(t *testing.T) {
|
|||
var wg sync.WaitGroup
|
||||
|
||||
path := "/tmp/milvus/rocksmq" + funcutil.GenRandomStr()
|
||||
err = os.Setenv("ROCKSMQ_PATH", path)
|
||||
t.Setenv("ROCKSMQ_PATH", path)
|
||||
defer os.RemoveAll(path)
|
||||
assert.NoError(t, err)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx = GetContext(ctx, "root:123456")
|
||||
|
|
|
@ -18,7 +18,6 @@ package flowgraph
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -27,7 +26,7 @@ import (
|
|||
)
|
||||
|
||||
func TestInputNode(t *testing.T) {
|
||||
os.Setenv("ROCKSMQ_PATH", "/tmp/MilvusTest/FlowGraph/TestInputNode")
|
||||
t.Setenv("ROCKSMQ_PATH", "/tmp/MilvusTest/FlowGraph/TestInputNode")
|
||||
factory := dependency.NewDefaultFactory(true)
|
||||
|
||||
msgStream, _ := factory.NewMsgStream(context.TODO())
|
||||
|
|
|
@ -19,7 +19,6 @@ package flowgraph
|
|||
import (
|
||||
"context"
|
||||
"math"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -57,7 +56,7 @@ func generateMsgPack() msgstream.MsgPack {
|
|||
}
|
||||
|
||||
func TestNodeCtx_Start(t *testing.T) {
|
||||
os.Setenv("ROCKSMQ_PATH", "/tmp/MilvusTest/FlowGraph/TestNodeStart")
|
||||
t.Setenv("ROCKSMQ_PATH", "/tmp/MilvusTest/FlowGraph/TestNodeStart")
|
||||
factory := dependency.NewDefaultFactory(true)
|
||||
|
||||
msgStream, _ := factory.NewMsgStream(context.TODO())
|
||||
|
|
|
@ -12,41 +12,28 @@
|
|||
package metricsinfo
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFillDeployMetricsWithEnv(t *testing.T) {
|
||||
var err error
|
||||
|
||||
var m DeployMetrics
|
||||
|
||||
commit := "commit"
|
||||
originalCommit := os.Getenv(GitCommitEnvKey)
|
||||
err = os.Setenv(GitCommitEnvKey, commit)
|
||||
assert.NoError(t, err)
|
||||
t.Setenv(GitCommitEnvKey, commit)
|
||||
|
||||
deploy := "deploy"
|
||||
originalDeploy := os.Getenv(DeployModeEnvKey)
|
||||
err = os.Setenv(DeployModeEnvKey, deploy)
|
||||
assert.NoError(t, err)
|
||||
t.Setenv(DeployModeEnvKey, deploy)
|
||||
|
||||
version := "version"
|
||||
originalVersion := os.Getenv(GitBuildTagsEnvKey)
|
||||
err = os.Setenv(GitBuildTagsEnvKey, version)
|
||||
assert.NoError(t, err)
|
||||
t.Setenv(GitBuildTagsEnvKey, version)
|
||||
|
||||
goVersion := "go"
|
||||
originalGoVersion := os.Getenv(MilvusUsedGoVersion)
|
||||
err = os.Setenv(MilvusUsedGoVersion, goVersion)
|
||||
assert.NoError(t, err)
|
||||
t.Setenv(MilvusUsedGoVersion, goVersion)
|
||||
|
||||
buildTime := "build"
|
||||
originalBuildTime := os.Getenv(MilvusBuildTimeEnvKey)
|
||||
err = os.Setenv(MilvusBuildTimeEnvKey, buildTime)
|
||||
assert.NoError(t, err)
|
||||
t.Setenv(MilvusBuildTimeEnvKey, buildTime)
|
||||
|
||||
FillDeployMetricsWithEnv(&m)
|
||||
assert.NotNil(t, m)
|
||||
|
@ -55,19 +42,4 @@ func TestFillDeployMetricsWithEnv(t *testing.T) {
|
|||
assert.Equal(t, version, m.BuildVersion)
|
||||
assert.Equal(t, goVersion, m.UsedGoVersion)
|
||||
assert.Equal(t, buildTime, m.BuildTime)
|
||||
|
||||
err = os.Setenv(GitCommitEnvKey, originalCommit)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Setenv(DeployModeEnvKey, originalDeploy)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Setenv(GitBuildTagsEnvKey, originalVersion)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Setenv(MilvusUsedGoVersion, originalGoVersion)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Setenv(MilvusBuildTimeEnvKey, originalBuildTime)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -109,7 +109,7 @@ func TestBaseTable_Get(t *testing.T) {
|
|||
|
||||
func TestBaseTable_Pulsar(t *testing.T) {
|
||||
//test PULSAR ADDRESS
|
||||
os.Setenv("PULSAR_ADDRESS", "pulsar://localhost:6650")
|
||||
t.Setenv("PULSAR_ADDRESS", "pulsar://localhost:6650")
|
||||
baseParams.Init()
|
||||
|
||||
address := baseParams.Get("pulsar.address")
|
||||
|
@ -149,8 +149,8 @@ func TestBaseTable_Pulsar(t *testing.T) {
|
|||
// }
|
||||
|
||||
func TestBaseTable_Env(t *testing.T) {
|
||||
os.Setenv("milvus.test", "test")
|
||||
os.Setenv("milvus.test.test2", "test2")
|
||||
t.Setenv("milvus.test", "test")
|
||||
t.Setenv("milvus.test.test2", "test2")
|
||||
|
||||
baseParams.Init()
|
||||
result, _ := baseParams.Load("test")
|
||||
|
@ -159,11 +159,7 @@ func TestBaseTable_Env(t *testing.T) {
|
|||
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)
|
||||
t.Setenv("milvus.invalid", "xxx=test")
|
||||
|
||||
baseParams.Init()
|
||||
result, _ = baseParams.Load("invalid")
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
package paramtable
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/milvus-io/milvus/internal/util/metricsinfo"
|
||||
|
@ -52,10 +51,10 @@ func TestServiceParam(t *testing.T) {
|
|||
|
||||
// test UseEmbedEtcd
|
||||
Params.Base.Save("etcd.use.embed", "true")
|
||||
assert.Nil(t, os.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.ClusterDeployMode))
|
||||
t.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.ClusterDeployMode)
|
||||
assert.Panics(t, func() { Params.initUseEmbedEtcd() })
|
||||
|
||||
assert.Nil(t, os.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode))
|
||||
t.Setenv(metricsinfo.DeployModeEnvKey, metricsinfo.StandaloneDeployMode)
|
||||
Params.LoadCfgToMemory()
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue