osbuild2: support for x11-keymap option in org.osbuild.keymap

Add support for the newly added `x11-keymap` option in the
`org.osbuild.keymap` osbuild stage [1]. The option allows one to
set layouts for the X11 keyboard.

Add unit test cases for the added functionality.

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

Signed-off-by: Tomas Hozza <thozza@redhat.com>
This commit is contained in:
Tomas Hozza 2021-07-01 13:25:18 +02:00 committed by Ondřej Budai
parent f06e66b94b
commit 006ff98025
3 changed files with 60 additions and 1 deletions

View file

@ -1,7 +1,13 @@
package osbuild2
import (
"encoding/json"
"fmt"
)
type KeymapStageOptions struct {
Keymap string `json:"keymap"`
Keymap string `json:"keymap"`
X11Keymap *X11KeymapOptions `json:"x11-keymap,omitempty"`
}
func (KeymapStageOptions) isStageOptions() {}
@ -12,3 +18,20 @@ func NewKeymapStage(options *KeymapStageOptions) *Stage {
Options: options,
}
}
type X11KeymapOptions struct {
Layouts []string `json:"layouts"`
}
// Unexported struct for use in X11KeymapOptions's MarshalJSON() to prevent recursion
type x11KeymapOptions struct {
Layouts []string `json:"layouts"`
}
func (o X11KeymapOptions) MarshalJSON() ([]byte, error) {
if len(o.Layouts) == 0 {
return nil, fmt.Errorf("at least one layout must be provided for X11 keymap")
}
keymapOptions := x11KeymapOptions(o)
return json.Marshal(keymapOptions)
}

View file

@ -1,6 +1,7 @@
package osbuild2
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
@ -14,3 +15,23 @@ func TestNewKeymapStage(t *testing.T) {
actualStage := NewKeymapStage(&KeymapStageOptions{})
assert.Equal(t, expectedStage, actualStage)
}
func TestKeymapStage_MarshalJSON_Invalid(t *testing.T) {
tests := []struct {
name string
options KeymapStageOptions
}{
{
name: "x11-keymap-empty-layout-list",
options: KeymapStageOptions{
X11Keymap: &X11KeymapOptions{},
},
},
}
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)
})
}
}

View file

@ -268,6 +268,21 @@ func TestStage_UnmarshalJSON(t *testing.T) {
data: []byte(`{"type":"org.osbuild.keymap","options":{"keymap":""}}`),
},
},
{
name: "keymap-x11-keymap",
fields: fields{
Type: "org.osbuild.keymap",
Options: &KeymapStageOptions{
Keymap: "us",
X11Keymap: &X11KeymapOptions{
Layouts: []string{"cz"},
},
},
},
args: args{
data: []byte(`{"type":"org.osbuild.keymap","options":{"keymap":"us","x11-keymap":{"layouts":["cz"]}}}`),
},
},
{
name: "modprobe",
fields: fields{