test: add GetRedisURL to tests redis pkg and fix hardcoded redis url (#17581)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2022-10-17 17:21:37 +08:00 committed by GitHub
parent 7bbefca8c2
commit 25a42af277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 17 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/tests"
pkgart "github.com/goharbor/harbor/src/pkg/artifact"
"github.com/goharbor/harbor/src/pkg/artifactrash/model"
pkg_blob "github.com/goharbor/harbor/src/pkg/blob/models"
@ -276,9 +277,8 @@ func (suite *gcTestSuite) TestRun() {
}
params := map[string]interface{}{
"delete_untagged": false,
// ToDo add a redis testing pkg, we do have a 'localhost' redis server in UT
"redis_url_reg": "redis://localhost:6379",
"time_window": 1,
"redis_url_reg": tests.GetRedisURL(),
"time_window": 1,
}
suite.Nil(gc.Run(ctx, params))

View File

@ -27,20 +27,22 @@ import (
)
const (
poolMaxIdle = 6
PoolMaxActive = 6
dialConnectionTimeout = 30 * time.Second
healthCheckPeriod = time.Minute
dialReadTimeout = healthCheckPeriod + 10*time.Second
dialWriteTimeout = 10 * time.Second
testingRedisHost = "REDIS_HOST"
testingRedisHostEnv = "REDIS_HOST"
testingRedisPort = 6379
testingNamespace = "testing_job_service_v2"
)
// GiveMeRedisPool ...
func GiveMeRedisPool() *redis.Pool {
redisHost := getRedisHost()
pool, _ := redislib.GetRedisPool("test", fmt.Sprintf("redis://%s:%d", redisHost, 6379), &redislib.PoolParam{
PoolMaxIdle: 6,
PoolMaxActive: 6,
pool, _ := redislib.GetRedisPool("test", GetRedisURL(), &redislib.PoolParam{
PoolMaxIdle: poolMaxIdle,
PoolMaxActive: PoolMaxActive,
DialConnectionTimeout: dialConnectionTimeout,
DialReadTimeout: dialReadTimeout,
DialWriteTimeout: dialWriteTimeout,
@ -53,6 +55,16 @@ func GiveMeTestNamespace() string {
return testingNamespace
}
// GetRedisURL ...
func GetRedisURL() string {
redisHost := os.Getenv(testingRedisHostEnv)
if redisHost == "" {
redisHost = "127.0.0.1" // for local test
}
return fmt.Sprintf("redis://%s:%d", redisHost, testingRedisPort)
}
// Clear ...
func Clear(key string, conn redis.Conn) error {
if conn != nil {
@ -82,12 +94,3 @@ func ClearAll(namespace string, conn redis.Conn) error {
return conn.Flush()
}
func getRedisHost() string {
redisHost := os.Getenv(testingRedisHost)
if redisHost == "" {
redisHost = "127.0.0.1" // for local test
}
return redisHost
}