From 77eb1a4f80d265fd5b8739aac35f34ed6a6b0c0a Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 16 Oct 2023 20:00:03 +0200 Subject: [PATCH] cloudapi: test GetTarget() Test some nil, valid, and invalid combinations for the GetTarget() upload target selection. --- internal/cloudapi/v2/imagerequest_test.go | 97 +++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/internal/cloudapi/v2/imagerequest_test.go b/internal/cloudapi/v2/imagerequest_test.go index 2c257d40961..38e9aa4ed3b 100644 --- a/internal/cloudapi/v2/imagerequest_test.go +++ b/internal/cloudapi/v2/imagerequest_test.go @@ -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" @@ -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) + } + }) + } +}