forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support keyspace by sys env or config
Signed-off-by: ystaticy <[email protected]>
- Loading branch information
Showing
17 changed files
with
197 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package domain | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pingcap/tidb/config" | ||
) | ||
|
||
const EnvVarKeyspaceName = "KEYSPACE_NAME" | ||
|
||
func GetKeyspaceNameBySettings() (keyspaceName string) { | ||
|
||
// The cfg.keyspaceName get higher weights than KEYSPACE_NAME in system env. | ||
keyspaceName = config.GetGlobalConfig().KeyspaceName | ||
if !IsKeyspaceNameEmpty(keyspaceName) { | ||
return keyspaceName | ||
} | ||
|
||
keyspaceName = os.Getenv(EnvVarKeyspaceName) | ||
return keyspaceName | ||
} | ||
|
||
func IsKeyspaceNameEmpty(keyspaceName string) bool { | ||
return keyspaceName == "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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. | ||
|
||
package domain | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/pingcap/tidb/config" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type keyspaceSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestSetKeyspaceName(t *testing.T) { | ||
suite.Run(t, new(keyspaceSuite)) | ||
} | ||
|
||
func (k *keyspaceSuite) TearDownTest() { | ||
// Clear keyspace setting | ||
conf := config.GetGlobalConfig() | ||
conf.KeyspaceName = "" | ||
config.StoreGlobalConfig(conf) | ||
|
||
os.Unsetenv(EnvVarKeyspaceName) | ||
} | ||
|
||
func (k *keyspaceSuite) TestSetKeyspaceNameInSysEnv() { | ||
|
||
keyspaceName := "test_keyspace_env" | ||
os.Setenv(EnvVarKeyspaceName, keyspaceName) | ||
|
||
getKeyspaceName := GetKeyspaceNameBySettings() | ||
// Check the keyspaceName which get from GetKeyspaceNameBySettings, equals the keyspaceName which is in system env. | ||
k.Equal(keyspaceName, getKeyspaceName) | ||
k.Equal(false, IsKeyspaceNameEmpty(getKeyspaceName)) | ||
|
||
} | ||
|
||
func (k *keyspaceSuite) TestSetKeyspaceNameInConf() { | ||
|
||
keyspaceNameInCfg := "test_keyspace_cfg" | ||
keyspaceNameInSysenv := "test_keyspace_env" | ||
os.Setenv(EnvVarKeyspaceName, keyspaceNameInSysenv) | ||
|
||
// Set KeyspaceName in conf | ||
c1 := config.GetGlobalConfig() | ||
c1.KeyspaceName = keyspaceNameInCfg | ||
|
||
getKeyspaceName := GetKeyspaceNameBySettings() | ||
|
||
// Check the keyspaceName which get from GetKeyspaceNameBySettings, equals keyspaceNameInCfg which is in conf. | ||
// The cfg.keyspaceName get higher weights than KEYSPACE_NAME in system env. | ||
k.Equal(keyspaceNameInCfg, getKeyspaceName) | ||
k.Equal(false, IsKeyspaceNameEmpty(getKeyspaceName)) | ||
|
||
} | ||
|
||
func (k *keyspaceSuite) TestNoKeyspaceNameSet() { | ||
|
||
getKeyspaceName := GetKeyspaceNameBySettings() | ||
|
||
k.Equal("", getKeyspaceName) | ||
k.Equal(true, IsKeyspaceNameEmpty(getKeyspaceName)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters