diff --git a/obs-status-service/main.go b/obs-status-service/main.go index 80446fe..15a632e 100644 --- a/obs-status-service/main.go +++ b/obs-status-service/main.go @@ -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 diff --git a/obs-status-service/redis.go b/obs-status-service/redis.go index 89daf0c..7e171ee 100644 --- a/obs-status-service/redis.go +++ b/obs-status-service/redis.go @@ -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) diff --git a/obs-status-service/redismock.go b/obs-status-service/redismock.go new file mode 100644 index 0000000..8303413 --- /dev/null +++ b/obs-status-service/redismock.go @@ -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 +}