Skip to content

Commit 8a36baf

Browse files
committed
support component-extension for openshift-tests so that it can get and run extenal cases besides default extension
1 parent 757cf53 commit 8a36baf

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

pkg/cmd/openshift-tests/images/images_command.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func NewImagesCommand() *cobra.Command {
8080
if err := imagesetup.VerifyImages(); err != nil {
8181
return err
8282
}
83-
lines, err := createImageMirrorForInternalImages(prefix, ref, !o.Upstream)
83+
lines, err := createImageMirrorForInternalImages(prefix, ref, !o.Upstream, o.ComponentExtensions)
8484
if err != nil {
8585
return err
8686
}
@@ -95,13 +95,15 @@ func NewImagesCommand() *cobra.Command {
9595
// this is a private flag for debugging only
9696
cmd.Flags().BoolVar(&o.Verify, "verify", o.Verify, "Verify the contents of the image mappings")
9797
cmd.Flags().MarkHidden("verify")
98+
cmd.Flags().Var(extensions.NewStringToMap(&o.ComponentExtensions), "component-extension", "Set extension which is used for the component as <component tag>=<component extension id> pairs (e.g. --component-extension=tag1:id1,tag2=id2 or --component-extension=tag1:id1 --component-extension=tag2=id2)")
9899
return cmd
99100
}
100101

101102
type imagesOptions struct {
102-
Repository string
103-
Upstream bool
104-
Verify bool
103+
Repository string
104+
Upstream bool
105+
Verify bool
106+
ComponentExtensions map[string]string
105107
}
106108

107109
// createImageMirrorForInternalImages returns a list of 'oc image mirror' mappings from source to
@@ -110,7 +112,7 @@ type imagesOptions struct {
110112
// of the original internal name and the index of the image in the array. Otherwise the mappings will
111113
// be set to mirror the location as defined in the test code into our official mirror, where the target
112114
// TAG is the hash described above.
113-
func createImageMirrorForInternalImages(prefix string, ref reference.DockerImageReference, mirrored bool) ([]string, error) {
115+
func createImageMirrorForInternalImages(prefix string, ref reference.DockerImageReference, mirrored bool, compExts map[string]string) ([]string, error) {
114116
source := ref.Exact()
115117

116118
initialImageSets := []extensions.ImageSet{
@@ -122,7 +124,7 @@ func createImageMirrorForInternalImages(prefix string, ref reference.DockerImage
122124
// Extract all test binaries
123125
extractionContext, extractionContextCancel := context.WithTimeout(context.Background(), 30*time.Minute)
124126
defer extractionContextCancel()
125-
cleanUpFn, externalBinaries, err := extensions.ExtractAllTestBinaries(extractionContext, 10)
127+
cleanUpFn, externalBinaries, err := extensions.ExtractAllTestBinaries(extractionContext, 10, compExts)
126128
if err != nil {
127129
return nil, err
128130
}

pkg/test/extensions/binary.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type TestBinary struct {
3434
// The binary path to extract from the image
3535
binaryPath string
3636

37+
ext string
38+
3739
// Cache the info after gathering it
3840
info *ExtensionInfo
3941
}
@@ -72,6 +74,9 @@ func (b *TestBinary) Info(ctx context.Context) (*ExtensionInfo, error) {
7274

7375
logrus.Infof("Fetching info for %s", binName)
7476
command := exec.Command(b.binaryPath, "info")
77+
if len(b.ext) != 0 {
78+
command.Args = append(command.Args, fmt.Sprintf("--component=%s", b.ext))
79+
}
7580
infoJson, err := runWithTimeout(ctx, command, 10*time.Minute)
7681
if err != nil {
7782
return nil, fmt.Errorf("failed running '%s info': %w\nOutput: %s", b.binaryPath, err, infoJson)
@@ -108,6 +113,9 @@ func (b *TestBinary) ListTests(ctx context.Context, envFlags EnvironmentFlags) (
108113
command := exec.Command(b.binaryPath, "list", "-o", "jsonl")
109114
binLogger.Infof("Adding the following applicable flags to the list command: %s", envFlags.String())
110115
command.Args = append(command.Args, envFlags.ArgStrings()...)
116+
if len(b.ext) != 0 {
117+
command.Args = append(command.Args, fmt.Sprintf("--component=%s", b.ext))
118+
}
111119
testList, err := runWithTimeout(ctx, command, 10*time.Minute)
112120
if err != nil {
113121
return nil, fmt.Errorf("failed running '%s list': %w\nOutput: %s", b.binaryPath, err, testList)
@@ -158,6 +166,9 @@ func (b *TestBinary) RunTests(ctx context.Context, timeout time.Duration, env []
158166
}
159167
args = append(args, "-o", "jsonl")
160168
command := exec.Command(b.binaryPath, args...)
169+
if len(b.ext) != 0 {
170+
command.Args = append(command.Args, fmt.Sprintf("--component=%s", b.ext))
171+
}
161172
if len(env) == 0 {
162173
env = os.Environ()
163174
}
@@ -218,6 +229,9 @@ func (b *TestBinary) ListImages(ctx context.Context) (ImageSet, error) {
218229

219230
logrus.Infof("Listing images for %q", binName)
220231
command := exec.Command(b.binaryPath, "images")
232+
if len(b.ext) != 0 {
233+
command.Args = append(command.Args, fmt.Sprintf("--component=%s", b.ext))
234+
}
221235
output, err := runWithTimeout(ctx, command, 10*time.Minute)
222236
if err != nil {
223237
return nil, fmt.Errorf("failed running '%s list': %w\nOutput: %s", b.binaryPath, err, output)
@@ -244,7 +258,7 @@ func (b *TestBinary) ListImages(ctx context.Context) (ImageSet, error) {
244258

245259
// ExtractAllTestBinaries determines the optimal release payload to use, and extracts all the external
246260
// test binaries from it, and returns a slice of them.
247-
func ExtractAllTestBinaries(ctx context.Context, parallelism int) (func(), TestBinaries, error) {
261+
func ExtractAllTestBinaries(ctx context.Context, parallelism int, compExts map[string]string) (func(), TestBinaries, error) {
248262
if parallelism < 1 {
249263
return nil, nil, errors.New("parallelism must be greater than zero")
250264
}
@@ -352,7 +366,7 @@ func ExtractAllTestBinaries(ctx context.Context, parallelism int) (func(), TestB
352366
if !ok {
353367
return // Channel is closed
354368
}
355-
testBinary, err := externalBinaryProvider.ExtractBinaryFromReleaseImage(b.imageTag, b.binaryPath)
369+
testBinary, err := externalBinaryProvider.ExtractBinaryFromReleaseImage(b.imageTag, b.binaryPath, compExts)
356370
if err != nil {
357371
errCh <- err
358372
continue

pkg/test/extensions/provider.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ func (provider *ExternalBinaryProvider) Cleanup() {
9393
// Note: When developing openshift-tests on a non-Linux non-AMD64 computer (i.e. on Apple Silicon), external
9494
// binaries won't work. You would need to run it in a Linux environment (VM or container), and even then
9595
// override the payload selection with an aarch64 payload unless x86 emulation is enabled.
96-
func (provider *ExternalBinaryProvider) ExtractBinaryFromReleaseImage(tag, binary string) (*TestBinary, error) {
96+
func (provider *ExternalBinaryProvider) ExtractBinaryFromReleaseImage(tag, binary string, compExts map[string]string) (*TestBinary, error) {
9797
if provider.binPath == "" {
9898
return nil, fmt.Errorf("extraction path is not set, cleanup was already run")
9999
}
100100

101+
var ext string
102+
if val, ok := compExts[tag]; ok {
103+
ext = val
104+
}
105+
101106
// Allow overriding image path to an already existing local path, mostly useful
102107
// for development.
103108
if override := binaryPathOverride(tag, binary); override != "" {
@@ -109,6 +114,7 @@ func (provider *ExternalBinaryProvider) ExtractBinaryFromReleaseImage(tag, binar
109114
return &TestBinary{
110115
imageTag: tag,
111116
binaryPath: override,
117+
ext: ext,
112118
}, nil
113119
}
114120

@@ -134,6 +140,7 @@ func (provider *ExternalBinaryProvider) ExtractBinaryFromReleaseImage(tag, binar
134140
return &TestBinary{
135141
imageTag: tag,
136142
binaryPath: binPath,
143+
ext: ext,
137144
}, nil
138145
}
139146

@@ -184,6 +191,7 @@ func (provider *ExternalBinaryProvider) ExtractBinaryFromReleaseImage(tag, binar
184191

185192
return &TestBinary{
186193
binaryPath: extractedBinary,
194+
ext: ext,
187195
}, nil
188196
}
189197

pkg/test/extensions/util.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"debug/elf"
88
"encoding/json"
99
"fmt"
10-
"github.com/sirupsen/logrus"
1110
"io"
1211
"os"
1312
"os/exec"
@@ -18,6 +17,8 @@ import (
1817
"strings"
1918
"time"
2019

20+
"github.com/sirupsen/logrus"
21+
2122
imagev1 "github.com/openshift/api/image/v1"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324

@@ -260,3 +261,45 @@ func extractReleaseImageStream(extractPath, releaseImage string,
260261

261262
return is, releaseImage, nil
262263
}
264+
265+
type StringToMap struct {
266+
e *map[string]string
267+
}
268+
269+
func NewStringToMap(p *map[string]string) *StringToMap {
270+
if *p == nil {
271+
*p = make(map[string]string)
272+
}
273+
return &StringToMap{e: p}
274+
}
275+
276+
func (s2m *StringToMap) String() string {
277+
var sb strings.Builder
278+
first := true
279+
for k, v := range *s2m.e {
280+
if !first {
281+
sb.WriteString(",")
282+
}
283+
sb.WriteString(fmt.Sprintf("%s=%s", k, v))
284+
first = false
285+
}
286+
return sb.String()
287+
}
288+
289+
func (s2m *StringToMap) Set(s string) error {
290+
items := strings.Split(s, ",")
291+
for _, item := range items {
292+
kv := strings.SplitN(item, "=", 2)
293+
if len(kv) != 2 {
294+
return fmt.Errorf("invalid component-extenstion format, expected key=value: %q", item)
295+
}
296+
k := strings.TrimSpace(kv[0])
297+
v := strings.TrimSpace(kv[1])
298+
(*s2m.e)[k] = v
299+
}
300+
return nil
301+
}
302+
303+
func (s *StringToMap) Type() string {
304+
return "stringToMap"
305+
}

pkg/test/ginkgo/cmd_runsuite.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ type GinkgoRunSuiteOptions struct {
7979

8080
IncludeSuccessOutput bool
8181

82+
ComponentExtensions map[string]string
83+
8284
CommandEnv []string
8385

8486
DryRun bool
@@ -118,6 +120,7 @@ func (o *GinkgoRunSuiteOptions) BindFlags(flags *pflag.FlagSet) {
118120
flags.IntVar(&o.ShardID, "shard-id", o.ShardID, "When tests are sharded across instances, which instance we are")
119121
flags.IntVar(&o.ShardCount, "shard-count", o.ShardCount, "Number of shards used to run tests across multiple instances")
120122
flags.StringVar(&o.ShardStrategy, "shard-strategy", o.ShardStrategy, "Which strategy to use for sharding (hash)")
123+
flags.Var(extensions.NewStringToMap(&o.ComponentExtensions), "component-extension", "Set extension which is used for the component as <component tag>=<component extension id> pairs (e.g. --component-extension=tag1:id1,tag2=id2 or --component-extension=tag1:id1 --component-extension=tag2=id2)")
121124
}
122125

123126
func (o *GinkgoRunSuiteOptions) Validate() error {
@@ -170,7 +173,7 @@ func (o *GinkgoRunSuiteOptions) Run(suite *TestSuite, junitSuiteName string, mon
170173
// Extract all test binaries
171174
extractionContext, extractionContextCancel := context.WithTimeout(context.Background(), 30*time.Minute)
172175
defer extractionContextCancel()
173-
cleanUpFn, externalBinaries, err := extensions.ExtractAllTestBinaries(extractionContext, 10)
176+
cleanUpFn, externalBinaries, err := extensions.ExtractAllTestBinaries(extractionContext, 10, o.ComponentExtensions)
174177
if err != nil {
175178
return err
176179
}

0 commit comments

Comments
 (0)