RHEL(85+): move qemuStageOptions() from distros to osbuild2
De-duplicate the `qemuStageOptions()` implementation from many distro implementations and move it to the `osbuild2` package as `NewQEMUStageOptions()`. Adjust all affected pipeline implementations. The `NewQEMUStageOptions()` function takes an optional format-specific options argument, instead of a dedicated format-specific options (the previously used qcow2 compat version). This is a preparation to produce VMDK images with a different than default subformat. Add unit test for `NewQEMUStageOptions()`.
This commit is contained in:
parent
998421d597
commit
01880a76a2
10 changed files with 282 additions and 128 deletions
|
|
@ -40,6 +40,7 @@ const (
|
|||
type QEMUFormatOptions interface {
|
||||
isQEMUFormatOptions()
|
||||
validate() error
|
||||
formatType() QEMUFormat
|
||||
}
|
||||
|
||||
type QCOW2Options struct {
|
||||
|
|
@ -59,6 +60,10 @@ func (o QCOW2Options) validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o QCOW2Options) formatType() QEMUFormat {
|
||||
return o.Type
|
||||
}
|
||||
|
||||
type VDIOptions struct {
|
||||
// The type of the format must be 'vdi'
|
||||
Type QEMUFormat `json:"type"`
|
||||
|
|
@ -73,6 +78,10 @@ func (o VDIOptions) validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o VDIOptions) formatType() QEMUFormat {
|
||||
return o.Type
|
||||
}
|
||||
|
||||
type VPCOptions struct {
|
||||
// The type of the format must be 'vpc'
|
||||
Type QEMUFormat `json:"type"`
|
||||
|
|
@ -87,6 +96,10 @@ func (o VPCOptions) validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o VPCOptions) formatType() QEMUFormat {
|
||||
return o.Type
|
||||
}
|
||||
|
||||
type VMDKOptions struct {
|
||||
// The type of the format must be 'vmdk'
|
||||
Type QEMUFormat `json:"type"`
|
||||
|
|
@ -124,6 +137,10 @@ func (o VMDKOptions) validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o VMDKOptions) formatType() QEMUFormat {
|
||||
return o.Type
|
||||
}
|
||||
|
||||
type VHDXOptions struct {
|
||||
// The type of the format must be 'vhdx'
|
||||
Type QEMUFormat `json:"type"`
|
||||
|
|
@ -138,6 +155,10 @@ func (o VHDXOptions) validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o VHDXOptions) formatType() QEMUFormat {
|
||||
return o.Type
|
||||
}
|
||||
|
||||
type QEMUStageInputs struct {
|
||||
Image *QEMUStageInput `json:"image"`
|
||||
}
|
||||
|
|
@ -171,6 +192,70 @@ func NewQEMUStage(options *QEMUStageOptions, inputs *QEMUStageInputs) *Stage {
|
|||
}
|
||||
}
|
||||
|
||||
// NewQEMUStageOptions creates a new QEMU Stage options object.
|
||||
//
|
||||
// In case the format-specific options are provided, they are used for
|
||||
// the Stage options.
|
||||
func NewQEMUStageOptions(filename string, format QEMUFormat, formatOptions QEMUFormatOptions) *QEMUStageOptions {
|
||||
if formatOptions != nil {
|
||||
// If the format type is not set explicitly in the provided format
|
||||
// options, set it to the appropriate value based on the format options
|
||||
// object type.
|
||||
if formatOptions.formatType() == "" {
|
||||
switch o := formatOptions.(type) {
|
||||
case QCOW2Options:
|
||||
o.Type = QEMUFormatQCOW2
|
||||
formatOptions = o
|
||||
case VDIOptions:
|
||||
o.Type = QEMUFormatVDI
|
||||
formatOptions = o
|
||||
case VPCOptions:
|
||||
o.Type = QEMUFormatVPC
|
||||
formatOptions = o
|
||||
case VMDKOptions:
|
||||
o.Type = QEMUFormatVMDK
|
||||
formatOptions = o
|
||||
case VHDXOptions:
|
||||
o.Type = QEMUFormatVHDX
|
||||
formatOptions = o
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown format options type in qemu stage: %t", o))
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that the explicitly provided QEMU format and the format set
|
||||
// in the format options structure (set by user or by this function
|
||||
// above) are matching.
|
||||
if t := formatOptions.formatType(); t != format {
|
||||
panic(fmt.Sprintf("mismatch between passed format type %q and format options type %q", format, t))
|
||||
}
|
||||
|
||||
if err := formatOptions.validate(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
switch format {
|
||||
case QEMUFormatQCOW2:
|
||||
formatOptions = QCOW2Options{Type: format}
|
||||
case QEMUFormatVDI:
|
||||
formatOptions = VDIOptions{Type: format}
|
||||
case QEMUFormatVPC:
|
||||
formatOptions = VPCOptions{Type: format}
|
||||
case QEMUFormatVMDK:
|
||||
formatOptions = VMDKOptions{Type: format}
|
||||
case QEMUFormatVHDX:
|
||||
formatOptions = VHDXOptions{Type: format}
|
||||
default:
|
||||
panic("unknown format in qemu stage: " + format)
|
||||
}
|
||||
}
|
||||
|
||||
return &QEMUStageOptions{
|
||||
Filename: filename,
|
||||
Format: formatOptions,
|
||||
}
|
||||
}
|
||||
|
||||
// alias for custom marshaller
|
||||
type qemuStageOptions QEMUStageOptions
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package osbuild2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -52,3 +53,163 @@ func TestNewQemuStage(t *testing.T) {
|
|||
assert.Equal(t, expectedStage, actualStage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewQEMUStageOptions(t *testing.T) {
|
||||
tests := []struct {
|
||||
Filename string
|
||||
Format QEMUFormat
|
||||
FormatOptions QEMUFormatOptions
|
||||
ExpectedOptions *QEMUStageOptions
|
||||
Error bool
|
||||
}{
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: QCOW2Options{
|
||||
Compat: "1.1",
|
||||
},
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.qcow2",
|
||||
Format: QCOW2Options{
|
||||
Type: QEMUFormatQCOW2,
|
||||
Compat: "1.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: QCOW2Options{
|
||||
Type: QEMUFormatQCOW2,
|
||||
},
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.qcow2",
|
||||
Format: QCOW2Options{
|
||||
Type: QEMUFormatQCOW2,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: QCOW2Options{},
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.qcow2",
|
||||
Format: QCOW2Options{
|
||||
Type: QEMUFormatQCOW2,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: nil,
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.qcow2",
|
||||
Format: QCOW2Options{
|
||||
Type: QEMUFormatQCOW2,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.vdi",
|
||||
Format: QEMUFormatVDI,
|
||||
FormatOptions: nil,
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.vdi",
|
||||
Format: VDIOptions{
|
||||
Type: QEMUFormatVDI,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.vpc",
|
||||
Format: QEMUFormatVPC,
|
||||
FormatOptions: nil,
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.vpc",
|
||||
Format: VPCOptions{
|
||||
Type: QEMUFormatVPC,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.vmdk",
|
||||
Format: QEMUFormatVMDK,
|
||||
FormatOptions: nil,
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.vmdk",
|
||||
Format: VMDKOptions{
|
||||
Type: QEMUFormatVMDK,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.vmdk",
|
||||
Format: QEMUFormatVMDK,
|
||||
FormatOptions: VMDKOptions{
|
||||
Subformat: VMDKSubformatStreamOptimized,
|
||||
},
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.vmdk",
|
||||
Format: VMDKOptions{
|
||||
Type: QEMUFormatVMDK,
|
||||
Subformat: VMDKSubformatStreamOptimized,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Filename: "image.vhdx",
|
||||
Format: QEMUFormatVHDX,
|
||||
FormatOptions: nil,
|
||||
ExpectedOptions: &QEMUStageOptions{
|
||||
Filename: "image.vhdx",
|
||||
Format: VHDXOptions{
|
||||
Type: QEMUFormatVHDX,
|
||||
},
|
||||
},
|
||||
},
|
||||
// mismatch between format and format options type
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: VMDKOptions{},
|
||||
Error: true,
|
||||
},
|
||||
// mismatch between format and format options type
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: VMDKOptions{
|
||||
Type: QEMUFormatQCOW2,
|
||||
},
|
||||
Error: true,
|
||||
},
|
||||
// mismatch between format and format options type
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: QEMUFormatQCOW2,
|
||||
FormatOptions: VMDKOptions{
|
||||
Type: QEMUFormatVMDK,
|
||||
},
|
||||
Error: true,
|
||||
},
|
||||
// unknown format
|
||||
{
|
||||
Filename: "image.qcow2",
|
||||
Format: "",
|
||||
FormatOptions: nil,
|
||||
Error: true,
|
||||
},
|
||||
}
|
||||
for idx, test := range tests {
|
||||
t.Run(fmt.Sprintf("test-(%d/%d)", idx, len(tests)), func(t *testing.T) {
|
||||
if test.Error {
|
||||
assert.Panics(t, func() { NewQEMUStageOptions(test.Filename, test.Format, test.FormatOptions) })
|
||||
} else {
|
||||
stageOptions := NewQEMUStageOptions(test.Filename, test.Format, test.FormatOptions)
|
||||
assert.EqualValues(t, test.ExpectedOptions, stageOptions)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue