Skip to content

Commit 6dc1ce3

Browse files
committed
cloudapi: Add architecture support to the optional Blueprint
The blueprint may have the architecture set. If set it must match the architecture set by the request. This ensures that clients have correctly set the architecture and prevents unexpectedly ignoring the blueprint architecture. With the WELR API the blueprint architecture would override the host arch. But with the Cloud API there is no host arch to override so the request must have the expected architecture set as part of the request. Related: RHEL-60125
1 parent 3a6bea3 commit 6dc1ce3

File tree

4 files changed

+284
-192
lines changed

4 files changed

+284
-192
lines changed

internal/cloudapi/v2/compose.go

+7
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ func ConvertRequestBP(rbp Blueprint) (blueprint.Blueprint, error) {
518518
if rbp.Distro != nil {
519519
bp.Distro = *rbp.Distro
520520
}
521+
if rbp.Architecture != nil {
522+
bp.Arch = *rbp.Architecture
523+
}
521524

522525
if rbp.Packages != nil {
523526
for _, pkg := range *rbp.Packages {
@@ -1154,6 +1157,10 @@ func (request *ComposeRequest) GetImageRequests(distroFactory *distrofactory.Fac
11541157
}
11551158
var irs []imageRequest
11561159
for _, ir := range *request.ImageRequests {
1160+
// If there is an architecture in the blueprint it must match the request's arch
1161+
if len(bp.Arch) > 0 && ir.Architecture != bp.Arch {
1162+
return nil, HTTPError(ErrorMismatchedArchitecture)
1163+
}
11571164
arch, err := distribution.GetArch(ir.Architecture)
11581165
if err != nil {
11591166
return nil, HTTPError(ErrorUnsupportedArchitecture)

internal/cloudapi/v2/compose_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,81 @@ func TestGetImageRequests_BlueprintDistro(t *testing.T) {
848848
assert.Equal(t, got[0].blueprint.Distro, "fedora-39")
849849
}
850850

851+
// TestGetImageRequests_BlueprintArchEmpty test blueprint with no arch
852+
func TestGetImageRequests_BlueprintArchEmpty(t *testing.T) {
853+
uo := UploadOptions(struct{}{})
854+
request := &ComposeRequest{
855+
Distribution: "fedora-40",
856+
ImageRequest: &ImageRequest{
857+
Architecture: "x86_64",
858+
ImageType: ImageTypesAws,
859+
UploadOptions: &uo,
860+
Repositories: []Repository{},
861+
},
862+
Blueprint: &Blueprint{
863+
Name: "arch-test",
864+
},
865+
}
866+
// NOTE: current directory is the location of this file, back up so it can use ./repositories/
867+
rr, err := reporegistry.New([]string{"../../../"})
868+
require.NoError(t, err)
869+
got, err := request.GetImageRequests(distrofactory.NewDefault(), rr)
870+
assert.NoError(t, err)
871+
require.Len(t, got, 1)
872+
require.Greater(t, len(got[0].repositories), 0)
873+
assert.Equal(t, got[0].imageType.Arch().Name(), "x86_64")
874+
}
875+
876+
// TestGetImageRequests_BlueprintArch test to make sure matching arch works
877+
func TestGetImageRequests_BlueprintArch(t *testing.T) {
878+
uo := UploadOptions(struct{}{})
879+
request := &ComposeRequest{
880+
Distribution: "fedora-40",
881+
ImageRequest: &ImageRequest{
882+
Architecture: "x86_64",
883+
ImageType: ImageTypesAws,
884+
UploadOptions: &uo,
885+
Repositories: []Repository{},
886+
},
887+
Blueprint: &Blueprint{
888+
Name: "arch-test",
889+
Architecture: common.ToPtr("x86_64"),
890+
},
891+
}
892+
// NOTE: current directory is the location of this file, back up so it can use ./repositories/
893+
rr, err := reporegistry.New([]string{"../../../"})
894+
require.NoError(t, err)
895+
got, err := request.GetImageRequests(distrofactory.NewDefault(), rr)
896+
assert.NoError(t, err)
897+
require.Len(t, got, 1)
898+
require.Greater(t, len(got[0].repositories), 0)
899+
assert.Equal(t, got[0].imageType.Arch().Name(), "x86_64")
900+
}
901+
902+
// TestGetImageRequests_BlueprintArchError test to make sure mismatched arch returns error
903+
func TestGetImageRequests_BlueprintArchError(t *testing.T) {
904+
uo := UploadOptions(struct{}{})
905+
request := &ComposeRequest{
906+
Distribution: "fedora-40",
907+
ImageRequest: &ImageRequest{
908+
Architecture: "x86_64",
909+
ImageType: ImageTypesAws,
910+
UploadOptions: &uo,
911+
Repositories: []Repository{},
912+
},
913+
Blueprint: &Blueprint{
914+
Name: "arch-test",
915+
Architecture: common.ToPtr("aarch64"),
916+
},
917+
}
918+
// NOTE: current directory is the location of this file, back up so it can use ./repositories/
919+
rr, err := reporegistry.New([]string{"../../../"})
920+
require.NoError(t, err)
921+
_, err = request.GetImageRequests(distrofactory.NewDefault(), rr)
922+
require.Error(t, err)
923+
assert.Equal(t, HTTPError(ErrorMismatchedArchitecture), err)
924+
}
925+
851926
func TestOpenSCAPTailoringOptions(t *testing.T) {
852927
cr := ComposeRequest{
853928
Customizations: &Customizations{

0 commit comments

Comments
 (0)