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

Open
mmarhin wants to merge 3 commits from mmarhin/autogits:initial-test-run into main
3 changed files with 98 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 = NewMockRedis()
}
func RedisConnect(RedisUrl string) {
opts, err := redis.ParseURL(RedisUrl)

View File

@@ -0,0 +1,81 @@
package main
import (
"compress/bzip2"
"context"
"encoding/json"
"io"
"os"
"github.com/redis/go-redis/v9"
"src.opensuse.org/autogits/common"
)
type MockRedis struct {
data map[string]map[string]string
projects []string
}
// Load mock data from factory.results.json.bz2
func NewMockRedis() *MockRedis {
mock := &MockRedis{
data: make(map[string]map[string]string),
projects: []string{},
}
// Read and decompress the file
file, err := os.Open("factory.results.json.bz2")
if err != nil {
common.LogError("Failed to open factory.results.json.bz2:", err)
return mock
}
defer file.Close()
uncompressedData, err := io.ReadAll(bzip2.NewReader(file))
if err != nil {
common.LogError("Failed to decompress factory.results.json.bz2:", err)
return mock
}
// Parse the JSON
var results []*common.BuildResult
if err := json.Unmarshal(uncompressedData, &results); err != nil {
common.LogError("Failed to parse factory.results.json.bz2:", err)
return mock
}
// Build the mock data structure
for _, result := range results {
key := "result." + result.Project + "/" + result.Repository + "/" + result.Arch
mock.projects = append(mock.projects, key)
packages := make(map[string]string)
for _, pkg := range result.Status {
value := pkg.Code
if pkg.Details != "" {
value += ":" + pkg.Details
}
packages[pkg.Package] = value
}
mock.data[key] = packages
}
return mock
}
// Implement RedisClient interface
func (m *MockRedis) HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd {
cmd := redis.NewMapStringStringCmd(ctx, "hgetall", key)
if data, exists := m.data[key]; exists {
cmd.SetVal(data)
} else {
cmd.SetVal(make(map[string]string))
}
return cmd
}
func (m *MockRedis) ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *redis.ScanCmd {
cmd := redis.NewScanCmd(ctx, nil)
cmd.SetVal(m.projects, 0)
return cmd
}