|
| 1 | +// Copyright 2026 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +package testutil |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/json" |
| 14 | + "errors" |
| 15 | + "fmt" |
| 16 | + "os" |
| 17 | + "path/filepath" |
| 18 | + "runtime" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | +) |
| 22 | + |
| 23 | +// ImageConfig represents a Docker image configuration. |
| 24 | +type ImageConfig struct { |
| 25 | + Repository string `json:"repository"` |
| 26 | + Tag string `json:"tag"` |
| 27 | +} |
| 28 | + |
| 29 | +// String returns the full image reference (repository:tag or repository@digest). |
| 30 | +func (c ImageConfig) String() string { |
| 31 | + if strings.HasPrefix(c.Tag, "sha256:") { |
| 32 | + return c.Repository + "@" + c.Tag |
| 33 | + } |
| 34 | + return c.Repository + ":" + c.Tag |
| 35 | +} |
| 36 | + |
| 37 | +type imagesConfig struct { |
| 38 | + Images map[string]ImageConfig `json:"images"` |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | + configOnce sync.Once |
| 43 | + parsedConfig imagesConfig |
| 44 | + errConfig error |
| 45 | +) |
| 46 | + |
| 47 | +func findTestImagesJSON() (string, error) { |
| 48 | + // Get the directory of this source file |
| 49 | + _, currentFile, _, ok := runtime.Caller(0) |
| 50 | + if !ok { |
| 51 | + return "", errors.New("failed to get current file path") |
| 52 | + } |
| 53 | + |
| 54 | + // Navigate from backend/pkg/testutil to repo root |
| 55 | + dir := filepath.Dir(currentFile) |
| 56 | + for i := 0; i < 10; i++ { |
| 57 | + candidate := filepath.Join(dir, "test-images.json") |
| 58 | + if _, err := os.Stat(candidate); err == nil { |
| 59 | + return candidate, nil |
| 60 | + } |
| 61 | + parent := filepath.Dir(dir) |
| 62 | + if parent == dir { |
| 63 | + break |
| 64 | + } |
| 65 | + dir = parent |
| 66 | + } |
| 67 | + |
| 68 | + return "", errors.New("test-images.json not found") |
| 69 | +} |
| 70 | + |
| 71 | +func loadConfig() (imagesConfig, error) { |
| 72 | + configOnce.Do(func() { |
| 73 | + path, err := findTestImagesJSON() |
| 74 | + if err != nil { |
| 75 | + errConfig = err |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + //nolint:gosec // G304: path is derived from runtime.Caller, not user input |
| 80 | + data, err := os.ReadFile(path) |
| 81 | + if err != nil { |
| 82 | + errConfig = fmt.Errorf("failed to read test-images.json: %w", err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + errConfig = json.Unmarshal(data, &parsedConfig) |
| 87 | + }) |
| 88 | + return parsedConfig, errConfig |
| 89 | +} |
| 90 | + |
| 91 | +func getImage(name, envVar string) string { |
| 92 | + // Check environment variable override first |
| 93 | + if override := os.Getenv(envVar); override != "" { |
| 94 | + return override |
| 95 | + } |
| 96 | + |
| 97 | + cfg, err := loadConfig() |
| 98 | + if err != nil { |
| 99 | + panic(fmt.Sprintf("failed to load test-images.json: %v", err)) |
| 100 | + } |
| 101 | + |
| 102 | + img, ok := cfg.Images[name] |
| 103 | + if !ok { |
| 104 | + panic(fmt.Sprintf("image %q not found in test-images.json", name)) |
| 105 | + } |
| 106 | + |
| 107 | + return img.String() |
| 108 | +} |
| 109 | + |
| 110 | +// RedpandaImage returns the Docker image for Redpanda tests. |
| 111 | +// Can be overridden with TEST_IMAGE_REDPANDA environment variable. |
| 112 | +func RedpandaImage() string { |
| 113 | + return getImage("redpanda", "TEST_IMAGE_REDPANDA") |
| 114 | +} |
| 115 | + |
| 116 | +// KafkaConnectImage returns the Docker image for Kafka Connect tests. |
| 117 | +// Can be overridden with TEST_IMAGE_KAFKA_CONNECT environment variable. |
| 118 | +func KafkaConnectImage() string { |
| 119 | + return getImage("kafkaConnect", "TEST_IMAGE_KAFKA_CONNECT") |
| 120 | +} |
| 121 | + |
| 122 | +// OwlShopImage returns the Docker image for OwlShop tests. |
| 123 | +// Can be overridden with TEST_IMAGE_OWL_SHOP environment variable. |
| 124 | +func OwlShopImage() string { |
| 125 | + return getImage("owlShop", "TEST_IMAGE_OWL_SHOP") |
| 126 | +} |
0 commit comments