worker/koji-finalize: handle multiple upload targets

Enhance the `koji-finalize` job implementation to be able to cope with
multiple upload targets being specified for an `OSBuildJob`.

Implement a convenience method `OSBuildJobResult.TargetResultsByName()`
for filtering the target results attached to the job result by their
name. Cover the method with an unit test. And lastly use this method in
the `koji-finalize` job to find the appropriate Koji upload target
results.

This is a preparation for enabling cloud uploads for Koji composes.
This commit is contained in:
Tomas Hozza 2022-07-20 12:48:03 +02:00 committed by Tom Gundersen
parent 58696e849f
commit 77a1672b79
3 changed files with 123 additions and 5 deletions

View file

@ -153,13 +153,15 @@ func (impl *KojiFinalizeJobImpl) Run(job worker.Job) error {
// multiple
buildRPMs = rpmmd.DeduplicateRPMs(buildRPMs)
// TODO: support multiple upload targets
if len(buildArgs.TargetResults) != 1 {
kojiFinalizeJobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorKojiFinalize, "Koji compose OSBuild job result must contain exactly one target result")
kojiTargetResults := buildArgs.TargetResultsByName(target.TargetNameKoji)
// Only a single Koji target is allowed per osbuild job
if len(kojiTargetResults) != 1 {
kojiFinalizeJobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorKojiFinalize, "Exactly one Koji target results are expected per osbuild job")
return nil
}
kojiTarget := buildArgs.TargetResults[0]
kojiTargetOptions := kojiTarget.Options.(*target.KojiTargetResultOptions)
kojiTargetResult := kojiTargetResults[0]
kojiTargetOptions := kojiTargetResult.Options.(*target.KojiTargetResultOptions)
buildRoots = append(buildRoots, koji.BuildRoot{
ID: uint64(i),

View file

@ -75,6 +75,20 @@ func (j *OSBuildJobResult) TargetErrors() []*clienterrors.Error {
return targetErrors
}
// TargetResultsByName iterates over TargetResults attached to the Job result and
// returns a slice of Target results of the provided name (type). If there were no
// TargetResults of the desired type attached to the Job results, the returned
// slice will be empty.
func (j *OSBuildJobResult) TargetResultsByName(name target.TargetName) []*target.TargetResult {
targetResults := []*target.TargetResult{}
for _, targetResult := range j.TargetResults {
if targetResult.Name == name {
targetResults = append(targetResults, targetResult)
}
}
return targetResults
}
type KojiInitJob struct {
Server string `json:"server"`
Name string `json:"name"`

View file

@ -87,6 +87,108 @@ func TestOSBuildJobResultTargetErrors(t *testing.T) {
}
}
func TestOSBuildJobResultTargetResultsByName(t *testing.T) {
testCases := []struct {
jobResult OSBuildJobResult
targetName target.TargetName
targetResults []*target.TargetResult
}{
// one target results of a given name
{
jobResult: OSBuildJobResult{
TargetResults: []*target.TargetResult{
{
Name: target.TargetNameAWS,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "can't login to AWS"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameAWSS3,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "failed to upload image to AWS S3"),
},
},
},
targetName: target.TargetNameAWS,
targetResults: []*target.TargetResult{
{
Name: target.TargetNameAWS,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "can't login to AWS"),
},
},
},
// multiple target results of a given name
{
jobResult: OSBuildJobResult{
TargetResults: []*target.TargetResult{
{
Name: target.TargetNameAWS,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "can't login to AWS"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameAWSS3,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "failed to upload image to AWS S3"),
},
},
},
targetName: target.TargetNameVMWare,
targetResults: []*target.TargetResult{
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
},
},
// no target result of a given name
{
jobResult: OSBuildJobResult{
TargetResults: []*target.TargetResult{
{
Name: target.TargetNameAWS,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "can't login to AWS"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameVMWare,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "can't upload image to VMWare"),
},
{
Name: target.TargetNameAWSS3,
TargetError: clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "failed to upload image to AWS S3"),
},
},
},
targetName: target.TargetNameKoji,
targetResults: []*target.TargetResult{},
},
}
for _, testCase := range testCases {
assert.EqualValues(t, testCase.targetResults, testCase.jobResult.TargetResultsByName(testCase.targetName))
}
}
func TestOSBuildJobExports(t *testing.T) {
testCases := []struct {
job *OSBuildJob