osbuild2: add support for org.osbuild.systemd-logind stage

Add support for the `org.osbuild.systemd-logind` osbuild stage [1],
which allows one to configure systemd-logind by creating
configuration drop-ins.

Add unit test cases for the newly added stage.

[1] https://github.com/osbuild/osbuild/pull/668

Signed-off-by: Tomas Hozza <thozza@redhat.com>
This commit is contained in:
Tomas Hozza 2021-06-30 12:45:50 +02:00 committed by Ondřej Budai
parent 5e97dcf2b3
commit bef4628a08
4 changed files with 117 additions and 0 deletions

View file

@ -101,6 +101,8 @@ func (stage *Stage) UnmarshalJSON(data []byte) error {
options = new(RHSMStageOptions)
case "org.osbuild.systemd":
options = new(SystemdStageOptions)
case "org.osbuild.systemd-logind":
options = new(SystemdLogindStageOptions)
case "org.osbuild.script":
options = new(ScriptStageOptions)
case "org.osbuild.sysconfig":

View file

@ -329,6 +329,34 @@ func TestStage_UnmarshalJSON(t *testing.T) {
data: []byte(`{"type":"org.osbuild.systemd","options":{"enabled_services":["foo.service"]}}`),
},
},
{
name: "systemd-logind",
fields: fields{
Type: "org.osbuild.systemd-logind",
Options: &SystemdLogindStageOptions{},
},
args: args{
data: []byte(`{"type":"org.osbuild.systemd-logind","options":{}}`),
},
},
{
name: "systemd-logind-data",
fields: fields{
Type: "org.osbuild.systemd-logind",
Options: &SystemdLogindStageOptions{
ConfigDropins: map[string]SystemdLogindConfigDropin{
"10-ec2-getty-fix.conf": {
Login: SystemdLogindConfigLoginSection{
NAutoVT: common.IntToPtr(0),
},
},
},
},
},
args: args{
data: []byte(`{"type":"org.osbuild.systemd-logind","options":{"configuration_dropins":{"10-ec2-getty-fix.conf":{"Login":{"NAutoVT":0}}}}}`),
},
},
{
name: "timezone",
fields: fields{

View file

@ -0,0 +1,46 @@
package osbuild2
import (
"encoding/json"
"fmt"
)
type SystemdLogindStageOptions struct {
ConfigDropins map[string]SystemdLogindConfigDropin `json:"configuration_dropins,omitempty"`
}
func (SystemdLogindStageOptions) isStageOptions() {}
func NewSystemdLogindStage(options *SystemdLogindStageOptions) *Stage {
return &Stage{
Type: "org.osbuild.systemd-logind",
Options: options,
}
}
// Drop-in configuration for systemd-logind
type SystemdLogindConfigDropin struct {
Login SystemdLogindConfigLoginSection `json:"Login"`
}
// 'Login' configuration section - at least one option must be specified
type SystemdLogindConfigLoginSection struct {
// Configures how many virtual terminals (VTs) to allocate by default
// The option is optional, but zero is a valid value
NAutoVT *int `json:"NAutoVT,omitempty"`
}
// Unexported struct for use in SystemdLogindConfigLoginSection's MarshalJSON() to prevent recursion
type systemdLogindConfigLoginSection struct {
// Configures how many virtual terminals (VTs) to allocate by default
// The option is optional, but zero is a valid value
NAutoVT *int `json:"NAutoVT,omitempty"`
}
func (s SystemdLogindConfigLoginSection) MarshalJSON() ([]byte, error) {
if s.NAutoVT == nil {
return nil, fmt.Errorf("at least one 'Login' section option must be specified")
}
loginSection := systemdLogindConfigLoginSection(s)
return json.Marshal(loginSection)
}

View file

@ -0,0 +1,41 @@
package osbuild2
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewSystemdLogindStage(t *testing.T) {
expectedStage := &Stage{
Type: "org.osbuild.systemd-logind",
Options: &SystemdLogindStageOptions{},
}
actualStage := NewSystemdLogindStage(&SystemdLogindStageOptions{})
assert.Equal(t, expectedStage, actualStage)
}
func TestSystemdLogindStage_MarshalJSON_Invalid(t *testing.T) {
tests := []struct {
name string
options SystemdLogindStageOptions
}{
{
name: "no-section-options",
options: SystemdLogindStageOptions{
ConfigDropins: map[string]SystemdLogindConfigDropin{
"10-ec2-getty-fix.conf": {
Login: SystemdLogindConfigLoginSection{},
},
},
},
},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotBytes, err := json.Marshal(tt.options)
assert.NotNilf(t, err, "json.Marshal() didn't return an error, but: %s [idx: %d]", string(gotBytes), idx)
})
}
}