store: move ComposeRequest to the store package

A ComposeRequest is data used to submit a compose to the store, so it
should live in that package.

Remove the json marshalling test, because ComposeRequest is never
marshalled to JSON.

This will allow to use types from `distro` in the ComposeRequest struct.
This commit is contained in:
Lars Karlitski 2020-03-03 01:28:34 +01:00 committed by Tom Gundersen
parent e3d1a34ab6
commit 72f8b07e8b
4 changed files with 13 additions and 142 deletions

View file

@ -3,9 +3,6 @@ package common
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
// CustomJsonConversionError is thrown when parsing strings into enumerations
@ -339,14 +336,3 @@ type ImageRequest struct {
ImgType ImageType `json:"image_type"`
UpTarget []UploadTarget `json:"upload_targets"`
}
// ComposeRequest is used to submit a new compose to the store
type ComposeRequest struct {
Blueprint blueprint.Blueprint `json:"blueprint"`
ComposeID uuid.UUID `json:"uuid"`
Distro Distribution `json:"distro"`
Arch Architecture `json:"arch"`
Repositories []rpmmd.RepoConfig `json:"repositories"`
Checksums map[string]string `json:"checksums"`
RequestedImages []ImageRequest `json:"requested_images"`
}

View file

@ -2,135 +2,9 @@ package common
import (
"encoding/json"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"reflect"
"testing"
)
func TestJSONConversionsComposeRequest(t *testing.T) {
cases := []struct {
input string
expectedConversionResult ComposeRequest
}{
// 1st case
{
`
{
"blueprint": {
"name": "",
"description": "",
"packages": [],
"modules": [],
"groups": []
},
"uuid": "789b4d42-da1a-49c9-a20c-054da3bb6c82",
"distro": "fedora-31",
"arch": "x86_64",
"repositories": [],
"requested_images": [
{
"image_type": "AWS",
"upload_targets": ["AWS EC2"]
}
]
}
`,
ComposeRequest{
Blueprint: blueprint.Blueprint{
Name: "",
Description: "",
Version: "",
Packages: nil,
Modules: nil,
Groups: nil,
Customizations: nil,
},
ComposeID: uuid.UUID{},
Distro: Fedora31,
Arch: X86_64,
Repositories: []rpmmd.RepoConfig{},
RequestedImages: []ImageRequest{{
ImgType: Aws,
UpTarget: []UploadTarget{EC2},
}},
},
},
// 2nd case
{
`
{
"blueprint": {
"name": "",
"description": "",
"packages": [],
"modules": [],
"groups": []
},
"uuid": "789b4d42-da1a-49c9-a20c-054da3bb6c82",
"distro": "rhel-8.2",
"arch": "aarch64",
"repositories": [],
"requested_images": [
{
"image_type": "Azure",
"upload_targets": ["Azure storage", "AWS EC2"]
}
]
}
`,
ComposeRequest{
Blueprint: blueprint.Blueprint{
Name: "",
Description: "",
Version: "",
Packages: nil,
Modules: nil,
Groups: nil,
Customizations: nil,
},
ComposeID: uuid.UUID{},
Distro: RHEL82,
Arch: Aarch64,
Repositories: []rpmmd.RepoConfig{},
RequestedImages: []ImageRequest{{
ImgType: Azure,
UpTarget: []UploadTarget{AzureStorage, EC2},
}},
},
},
}
for n, c := range cases {
// Test unmashaling the JSON from the string above
var inputStringAsStruct *ComposeRequest
err := json.Unmarshal([]byte(c.input), &inputStringAsStruct)
if err != nil {
t.Fatal("Failed to unmarshal ComposeRequest (", n, "):", err)
}
if reflect.DeepEqual(inputStringAsStruct, c.expectedConversionResult) {
t.Error("Unmarshaled compose request is not the one expected")
}
// Test marshaling the expected structure into JSON byte array, but since JSON package in golang std lib
// does not have a canonical form (a 3rd party library is necessary) I convert it back to struct and
// compare the resulting structure with the input one
data, err := json.Marshal(c.expectedConversionResult)
if err != nil {
t.Fatal("Failed to marshal ComposeRequest:", err)
}
var expectedResultAfterMarshaling *ComposeRequest
err = json.Unmarshal(data, &expectedResultAfterMarshaling)
if err != nil {
t.Fatal("Failed to unmarshal ComposeRequest:", err, ", input:", string(data))
}
if reflect.DeepEqual(expectedResultAfterMarshaling, c.expectedConversionResult) {
t.Error("Marshaled compose request is not the one expected")
}
}
}
func TestImageType_UnmarshalJSON(t *testing.T) {
dict := struct {
ImageTypes []ImageType `json:"image_types"`

View file

@ -178,7 +178,7 @@ func (api *API) submit(writer http.ResponseWriter, request *http.Request, _ http
// Push the requested compose to the store
composeUUID := uuid.New()
// nil is used as an upload target, because LocalTarget is already used in the PushCompose function
err = api.store.PushComposeRequest(common.ComposeRequest{
err = api.store.PushComposeRequest(store.ComposeRequest{
Blueprint: blueprint.Blueprint{},
ComposeID: composeUUID,
Distro: composeRequest.Distribution,

View file

@ -70,6 +70,17 @@ type SourceConfig struct {
System bool `json:"system"`
}
// ComposeRequest is used to submit a new compose to the store
type ComposeRequest struct {
Blueprint blueprint.Blueprint
ComposeID uuid.UUID
Distro common.Distribution
Arch common.Architecture
Repositories []rpmmd.RepoConfig
Checksums map[string]string
RequestedImages []common.ImageRequest
}
type NotFoundError struct {
message string
}
@ -645,7 +656,7 @@ func (s *Store) PushCompose(composeID uuid.UUID, bp *blueprint.Blueprint, packag
// PushComposeRequest is an alternative to PushCompose which does not assume a pre-defined distro, as such it is better
// suited for RCM API and possible future API that would respect the fact that we can build any distro and any arch
func (s *Store) PushComposeRequest(request common.ComposeRequest) error {
func (s *Store) PushComposeRequest(request ComposeRequest) error {
// This should never happen and once distro.Manifest is refactored this check will go away
arch, exists := request.Arch.ToString()
if !exists {