osbuild2: Expand dnf_config stage
This commit is contained in:
parent
786fc2719d
commit
2543459a7f
5 changed files with 143 additions and 2 deletions
|
|
@ -571,6 +571,7 @@ func ec2SapPipelines(t *imageType, customizations *blueprint.Customizations, opt
|
|||
Value: "8.6",
|
||||
},
|
||||
},
|
||||
nil,
|
||||
)))
|
||||
|
||||
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
|
||||
|
|
|
|||
|
|
@ -564,6 +564,7 @@ func ec2SapPipelines(t *imageType, customizations *blueprint.Customizations, opt
|
|||
Value: t.arch.distro.osVersion,
|
||||
},
|
||||
},
|
||||
nil,
|
||||
)))
|
||||
|
||||
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
|
||||
|
|
|
|||
|
|
@ -548,6 +548,7 @@ func ec2SapPipelines(t *imageType, customizations *blueprint.Customizations, opt
|
|||
Value: "9.0",
|
||||
},
|
||||
},
|
||||
nil,
|
||||
)))
|
||||
|
||||
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
|
||||
|
|
|
|||
|
|
@ -1,22 +1,50 @@
|
|||
package osbuild2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DNFConfigStageOptions represents persistent DNF configuration.
|
||||
type DNFConfigStageOptions struct {
|
||||
// List of DNF variables.
|
||||
Variables []DNFVariable `json:"variables,omitempty"`
|
||||
Config *DNFConfig `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
func (DNFConfigStageOptions) isStageOptions() {}
|
||||
|
||||
// NewDNFConfigStageOptions creates a new DNFConfig Stage options object.
|
||||
func NewDNFConfigStageOptions(variables []DNFVariable) *DNFConfigStageOptions {
|
||||
func NewDNFConfigStageOptions(variables []DNFVariable, config *DNFConfig) *DNFConfigStageOptions {
|
||||
return &DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (o DNFConfigStageOptions) validate() error {
|
||||
if o.Config != nil && o.Config.Main != nil {
|
||||
valid := false
|
||||
allowedIPR := []string{"4", "IPv4", "6", "IPv6", ""}
|
||||
for _, v := range allowedIPR {
|
||||
if o.Config.Main.IPResolve == v {
|
||||
valid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
return fmt.Errorf("DNF config parameter ip_resolve does not allow '%s' as a value", o.Config.Main.IPResolve)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewDNFConfigStage creates a new DNFConfig Stage object.
|
||||
func NewDNFConfigStage(options *DNFConfigStageOptions) *Stage {
|
||||
if err := options.validate(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &Stage{
|
||||
Type: "org.osbuild.dnf.config",
|
||||
Options: options,
|
||||
|
|
@ -30,3 +58,11 @@ type DNFVariable struct {
|
|||
// Value of the variable.
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type DNFConfig struct {
|
||||
Main *DNFConfigMain `json:"main,omitempty"`
|
||||
}
|
||||
|
||||
type DNFConfigMain struct {
|
||||
IPResolve string `json:"ip_resolve,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package osbuild2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewDNFConfigStageOptions(t *testing.T) {
|
||||
|
|
@ -14,10 +17,17 @@ func TestNewDNFConfigStageOptions(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
dnfconfig := &DNFConfig{
|
||||
Main: &DNFConfigMain{
|
||||
IPResolve: "4",
|
||||
},
|
||||
}
|
||||
|
||||
expectedOptions := &DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: dnfconfig,
|
||||
}
|
||||
actualOptions := NewDNFConfigStageOptions(variables)
|
||||
actualOptions := NewDNFConfigStageOptions(variables, dnfconfig)
|
||||
assert.Equal(t, expectedOptions, actualOptions)
|
||||
}
|
||||
|
||||
|
|
@ -29,3 +39,95 @@ func TestNewDNFConfigStage(t *testing.T) {
|
|||
actualStage := NewDNFConfigStage(&DNFConfigStageOptions{})
|
||||
assert.Equal(t, expectedStage, actualStage)
|
||||
}
|
||||
|
||||
func TestJSONDNFConfigStage(t *testing.T) {
|
||||
expectedOptions := DNFConfigStageOptions{
|
||||
Variables: []DNFVariable{
|
||||
{
|
||||
Name: "release",
|
||||
Value: "8.4",
|
||||
},
|
||||
},
|
||||
Config: &DNFConfig{
|
||||
Main: &DNFConfigMain{
|
||||
IPResolve: "4",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
inputString := `{"variables":[{"name":"release","value":"8.4"}],"config":{"main":{"ip_resolve":"4"}}}`
|
||||
var inputOptions DNFConfigStageOptions
|
||||
err := json.Unmarshal([]byte(inputString), &inputOptions)
|
||||
assert.NoError(t, err, "failed to parse JSON dnf config")
|
||||
assert.True(t, reflect.DeepEqual(expectedOptions, inputOptions))
|
||||
|
||||
inputBytes, err := json.Marshal(expectedOptions)
|
||||
assert.NoError(t, err, "failed to marshal YUM config into JSON")
|
||||
assert.Equal(t, inputString, string(inputBytes))
|
||||
}
|
||||
|
||||
func TestDNFConfigValidate(t *testing.T) {
|
||||
variables := []DNFVariable{
|
||||
{
|
||||
Name: "release",
|
||||
Value: "8.4",
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
options DNFConfigStageOptions
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
DNFConfigStageOptions{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: &DNFConfig{
|
||||
Main: nil,
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: &DNFConfig{
|
||||
Main: &DNFConfigMain{},
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: &DNFConfig{
|
||||
Main: &DNFConfigMain{
|
||||
IPResolve: "4",
|
||||
},
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
DNFConfigStageOptions{
|
||||
Variables: variables,
|
||||
Config: &DNFConfig{
|
||||
Main: &DNFConfigMain{
|
||||
IPResolve: "urgh",
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if test.valid {
|
||||
require.NotPanics(t, func() { NewDNFConfigStage(&test.options) })
|
||||
} else {
|
||||
require.Panics(t, func() { NewDNFConfigStage(&test.options) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue