Implement initial test-run option (#113) #118

Open
mmarhin wants to merge 2 commits from mmarhin/autogits:initial-test-run into main
3 changed files with 60 additions and 1 deletions

View File

@@ -243,6 +243,7 @@ func main() {
disableTls := flag.Bool("no-tls", false, "Disable TLS")
ObsUrl = flag.String("obs-url", obsUrlDef, "OBS API endpoint for package buildlog information")
debug := flag.Bool("debug", false, "Enable debug logging")
testrun := flag.Bool("test-run", false, "Run in test mode. Will use a fake REDIS server with mock data")
flag.Parse()
if *debug {
@@ -251,6 +252,9 @@ func main() {
if redisUrl := os.Getenv("REDIS"); len(redisUrl) > 0 {
RedisConnect(redisUrl)
} else if *testrun {
RedisMock()
common.LogInfo("Running in TEST MODE, MockRedisConnect will be used")
} else {
common.LogError("REDIS needs to contains URL of the OBS Redis instance with login information")
return

View File

@@ -14,7 +14,19 @@ import (
var RepoStatus []*common.BuildResult = []*common.BuildResult{}
var RepoStatusLock *sync.RWMutex = &sync.RWMutex{}
var redisClient *redis.Client
// Interface to get just the methods from redis.Client that we are
// using. This makes it possible to mock the redis for testing
// purposes
type RedisClient interface {
HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *redis.ScanCmd
}
var redisClient RedisClient
func RedisMock() {
redisClient = &MockRedis{}
}
func RedisConnect(RedisUrl string) {
opts, err := redis.ParseURL(RedisUrl)

View File

@@ -0,0 +1,43 @@
package main
import (
"context"
"github.com/redis/go-redis/v9"
)
type MockRedis struct {}
// Implement RedisClient interface
// TODO: Read mock data from filesystem
func (m *MockRedis) HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd {
python := map[string]string{
"python313": "succeeded",
"python314": "failed",
}
autogits := map[string]string {
"autogits": "succeeded",
}
// result.PROJECT/REPO/ARCH -> packages build info
data := map[string](map[string]string) {
"result.devel:languages:python:Factory/openSUSE_Tumbleweed/x86_64": python,
"result.devel:Factory:git-workflow/openSUSE_Tumbleweed/x86_64": autogits,
}
cmd := redis.NewMapStringStringCmd(ctx, "hgetall", data[key])
cmd.SetVal(data[key])
return cmd
}
func (m *MockRedis) ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *redis.ScanCmd {
projects := []string{
"result.devel:languages:python:Factory/openSUSE_Tumbleweed/x86_64",
"result.devel:Factory:git-workflow/openSUSE_Tumbleweed/x86_64",
}
cmd := redis.NewScanCmd(ctx, nil)
cmd.SetVal(projects, 0)
return cmd
}