osbuild2: new stage pwquality conf

This stage was introduced in osbuild 41. Add support into
osbuild-composer and a test using test data from osbuild repo.
This commit is contained in:
Martin Sehnoutka 2021-11-10 13:20:27 +01:00 committed by Tomáš Hozza
parent 59be127daf
commit b159d04af7
4 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package osbuild2
type PwqualityConfConfig struct {
Minlen *int `json:"minlen,omitempty"`
Dcredit *int `json:"dcredit,omitempty"`
Ucredit *int `json:"ucredit,omitempty"`
Lcredit *int `json:"lcredit,omitempty"`
Ocredit *int `json:"ocredit,omitempty"`
Minclass *int `json:"minclass,omitempty"`
}
type PwqualityConfStageOptions struct {
Config PwqualityConfConfig `json:"config"`
}
func (PwqualityConfStageOptions) isStageOptions() {}
func NewPwqualityConfStage(options *PwqualityConfStageOptions) *Stage {
return &Stage{
Type: "org.osbuild.pwquality.conf",
Options: options,
}
}

View file

@ -0,0 +1,54 @@
package osbuild2
import (
"encoding/json"
"reflect"
"testing"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/stretchr/testify/assert"
)
func TestNewPwqualityConfStage(t *testing.T) {
expectedStage := &Stage{
Type: "org.osbuild.pwquality.conf",
Options: &PwqualityConfStageOptions{},
}
actualStage := NewPwqualityConfStage(&PwqualityConfStageOptions{})
assert.Equal(t, expectedStage, actualStage)
}
func TestJsonPwqualityConfStage(t *testing.T) {
// First test that the JSON can be parsed into the expected structure.
expectedOptions := PwqualityConfStageOptions{
Config: PwqualityConfConfig{
Minlen: common.IntToPtr(9),
Minclass: common.IntToPtr(0),
Dcredit: common.IntToPtr(1),
},
}
inputString := `{
"config": {
"minlen": 9,
"minclass": 0,
"dcredit": 1
}
}`
var inputOptions PwqualityConfStageOptions
err := json.Unmarshal([]byte(inputString), &inputOptions)
assert.NoError(t, err, "failed to parse JSON yum config")
assert.True(t, reflect.DeepEqual(expectedOptions, inputOptions))
// Second try the other way around with stress on missing values
// for those parameters that the user didn't specify.
inputOptions = PwqualityConfStageOptions{
Config: PwqualityConfConfig{
Minlen: common.IntToPtr(9),
Minclass: common.IntToPtr(0),
},
}
expectedString := `{"config":{"minlen":9,"minclass":0}}`
inputBytes, err := json.Marshal(inputOptions)
assert.NoError(t, err, "failed to marshal sshd config into JSON")
assert.Equal(t, expectedString, string(inputBytes))
}

View file

@ -150,6 +150,8 @@ func (stage *Stage) UnmarshalJSON(data []byte) error {
options = new(SshdConfigStageOptions)
case "org.osbuild.authconfig":
options = new(AuthconfigStageOptions)
case "org.osbuild.pwquality.conf":
options = new(PwqualityConfStageOptions)
default:
return fmt.Errorf("unexpected stage type: %s", rawStage.Type)
}

View file

@ -657,6 +657,16 @@ func TestStage_UnmarshalJSON(t *testing.T) {
data: []byte(`{"type":"org.osbuild.authconfig","options":{}}`),
},
},
{
name: "pwquality.conf",
fields: fields{
Type: "org.osbuild.pwquality.conf",
Options: &PwqualityConfStageOptions{},
},
args: args{
data: []byte(`{"type":"org.osbuild.pwquality.conf","options":{"config":{}}}`),
},
},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {