test: fix modules/queue tests to use TEST_REDIS_SERVER when present (#10139)
`modules/queue` tests are intended to be running against a redis server in the test-remote-cacher workflow action, but they are not running because the tests always try to start a redis server by running a `redis-server` subprocess. If that subprocess fails to start, the tests are skipped. This change forces the tests to execute whenever `TEST_REDIS_SERVER` is present, and removes the unnecessary Forgejo-managed redis server during testing. This change is tested manually; if `TEST_REDIS_SERVER` is set to an invalid value, the tests fail, when previously it could be set to any value and the tests would always be skipped. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10139 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forgejo.org/modules/nosql"
|
||||
"forgejo.org/modules/setting"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
const defaultTestRedisServer = "127.0.0.1:6379"
|
||||
|
||||
type baseRedisWithServerTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
@@ -30,23 +24,6 @@ func (suite *baseRedisWithServerTestSuite) TestNormal() {
|
||||
ConnStr: redisAddress,
|
||||
}
|
||||
|
||||
redisServer, accessible := suite.startRedisServer(redisAddress)
|
||||
|
||||
// If it's accessible, but redisServer command is nil, that means we are using
|
||||
// an already running redis server.
|
||||
if redisServer == nil && !accessible {
|
||||
suite.T().Skip("redis-server not found in Forgejo test yet")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if redisServer != nil {
|
||||
_ = redisServer.Process.Signal(os.Interrupt)
|
||||
_ = redisServer.Wait()
|
||||
}
|
||||
}()
|
||||
|
||||
testQueueBasic(suite.T(), newBaseRedisSimple, toBaseConfig("baseRedis", queueSettings), false)
|
||||
testQueueBasic(suite.T(), newBaseRedisUnique, toBaseConfig("baseRedisUnique", queueSettings), true)
|
||||
}
|
||||
@@ -58,76 +35,14 @@ func (suite *baseRedisWithServerTestSuite) TestWithPrefix() {
|
||||
ConnStr: redisAddress,
|
||||
}
|
||||
|
||||
redisServer, accessible := suite.startRedisServer(redisAddress)
|
||||
|
||||
// If it's accessible, but redisServer command is nil, that means we are using
|
||||
// an already running redis server.
|
||||
if redisServer == nil && !accessible {
|
||||
suite.T().Skip("redis-server not found in Forgejo test yet")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if redisServer != nil {
|
||||
_ = redisServer.Process.Signal(os.Interrupt)
|
||||
_ = redisServer.Wait()
|
||||
}
|
||||
}()
|
||||
|
||||
testQueueBasic(suite.T(), newBaseRedisSimple, toBaseConfig("baseRedis", queueSettings), false)
|
||||
testQueueBasic(suite.T(), newBaseRedisUnique, toBaseConfig("baseRedisUnique", queueSettings), true)
|
||||
}
|
||||
|
||||
func (suite *baseRedisWithServerTestSuite) startRedisServer(address string) (*exec.Cmd, bool) {
|
||||
var redisServer *exec.Cmd
|
||||
|
||||
if !suite.waitRedisReady(address, 0) {
|
||||
redisServerProg, err := exec.LookPath("redis-server")
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
redisServer = &exec.Cmd{
|
||||
Path: redisServerProg,
|
||||
Args: []string{redisServerProg, "--bind", "127.0.0.1", "--port", "6379"},
|
||||
Dir: suite.T().TempDir(),
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
Stderr: os.Stderr,
|
||||
}
|
||||
|
||||
suite.Require().NoError(redisServer.Start())
|
||||
|
||||
if !suite.True(suite.waitRedisReady(address, 5*time.Second), "start redis-server") {
|
||||
// Return with redis server even if it's not available. It was started,
|
||||
// even if it's not reachable for any reasons, it's still started, the
|
||||
// parent will close it.
|
||||
return redisServer, false
|
||||
}
|
||||
}
|
||||
|
||||
return redisServer, true
|
||||
}
|
||||
|
||||
func (suite *baseRedisWithServerTestSuite) waitRedisReady(conn string, dur time.Duration) (ready bool) {
|
||||
ctxTimed, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
for t := time.Now(); ; time.Sleep(50 * time.Millisecond) {
|
||||
ret := nosql.GetManager().GetRedisClient(conn).Ping(ctxTimed)
|
||||
if ret.Err() == nil {
|
||||
return true
|
||||
}
|
||||
if time.Since(t) > dur {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *baseRedisWithServerTestSuite) testRedisHost() string {
|
||||
value := os.Getenv("TEST_REDIS_SERVER")
|
||||
if value != "" {
|
||||
return value
|
||||
host := os.Getenv("TEST_REDIS_SERVER")
|
||||
if host == "" {
|
||||
suite.T().Skip("redis-server not found in Forgejo test yet")
|
||||
}
|
||||
|
||||
return defaultTestRedisServer
|
||||
return host
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user