Skip to content

Commit

Permalink
cloudapi: test GetTarget()
Browse files Browse the repository at this point in the history
Test some nil, valid, and invalid combinations for the GetTarget()
upload target selection.
  • Loading branch information
achilleas-k committed Oct 17, 2023
1 parent 2bc5a7d commit 77eb1a4
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions internal/cloudapi/v2/imagerequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package v2
import (
"testing"

"github.com/osbuild/images/pkg/distro/rhel9"
"github.com/osbuild/images/pkg/distro/test_distro"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/target"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -129,3 +131,98 @@ func TestGetOstreeOptions(t *testing.T) {
require.NotNil(t, err)
assert.Error(t, err)
}

func TestGetTarget(t *testing.T) {
assert := assert.New(t)

r9 := rhel9.NewRHEL93()
arch, err := r9.GetArch("x86_64")
assert.NoError(err)

cr := &ComposeRequest{
Distribution: r9.Name(),
}

it, err := arch.GetImageType("qcow2")
assert.NoError(err)

var uploadOptions UploadOptions // doesn't need a concrete value or type for this test

type testCase struct {
imageType ImageTypes
target *ImageRequestUploadTarget
expected target.TargetName
fail bool
}

testCases := map[string]testCase{
"guest-nil": {
imageType: ImageTypesGuestImage,
target: nil,
expected: target.TargetNameAWSS3,
},
"guest-s3": {
imageType: ImageTypesGuestImage,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildAwsS3),
expected: target.TargetNameAWSS3,
},
"guest-azure-fail": {
imageType: ImageTypesGuestImage,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildAzureImage),
expected: "",
fail: true,
},
"azure-nil": {
imageType: ImageTypesAzure,
target: nil,
expected: target.TargetNameAzureImage,
},
"azure-azure": {
imageType: ImageTypesAzure,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildAzureImage),
expected: target.TargetNameAzureImage,
},
"azure-gcp-fail": {
imageType: ImageTypesAzure,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildGcp),
expected: "",
fail: true,
},
"edge-nil": {
imageType: ImageTypesEdgeCommit,
target: nil,
expected: target.TargetNameAWSS3,
},
"edge-s3": {
imageType: ImageTypesEdgeCommit,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildAwsS3),
expected: target.TargetNameAWSS3,
},
"edge-gcp-fail": {
imageType: ImageTypesEdgeCommit,
target: common.ToPtr(ImageRequestUploadTargetOrgOsbuildGcp),
expected: "",
fail: true,
},
}

for name := range testCases {
t.Run(name, func(t *testing.T) {
testCase := testCases[name]
ir := ImageRequest{
Architecture: arch.Name(),
ImageType: testCase.imageType,
UploadTarget: testCase.target,
UploadOptions: &uploadOptions,
}

tar, err := ir.GetTarget(cr, it)
if !testCase.fail {
assert.NoError(err)
assert.Equal(tar.Name, testCase.expected)
} else {
assert.Error(err)
}
})
}
}

0 comments on commit 77eb1a4

Please sign in to comment.