Skip to content

Commit

Permalink
Finished porting OS validations to the new framework (#114)
Browse files Browse the repository at this point in the history
- Added test for overall OS validation function
- Added component assertions for image validation
  • Loading branch information
jdob authored Jan 18, 2024
1 parent 8a5cf9f commit e5e2e68
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/image/validation/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestValidateImage(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failedValidations {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, imageComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down
25 changes: 24 additions & 1 deletion pkg/image/validation/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func validateOperatingSystem(ctx *image.Context) []FailedValidation {

var failures []FailedValidation

if isOperatingSystemDefined(&def.OperatingSystem) {
if !isOperatingSystemDefined(&def.OperatingSystem) {
return failures
}

Expand All @@ -25,6 +25,7 @@ func validateOperatingSystem(ctx *image.Context) []FailedValidation {
failures = append(failures, validateUsers(&def.OperatingSystem)...)
failures = append(failures, validateSuma(&def.OperatingSystem)...)
failures = append(failures, validatePackages(&def.OperatingSystem)...)
failures = append(failures, validateUnattended(def)...)

return failures
}
Expand Down Expand Up @@ -195,3 +196,25 @@ func validatePackages(os *image.OperatingSystem) []FailedValidation {

return failures
}

func validateUnattended(def *image.Definition) []FailedValidation {
var failures []FailedValidation

if def.Image.ImageType != image.TypeISO && def.OperatingSystem.Unattended {
msg := fmt.Sprintf("The 'unattended' field can only be used when 'imageType' is '%s'.", image.TypeISO)
failures = append(failures, FailedValidation{
userMessage: msg,
component: osComponent,
})
}

if def.Image.ImageType != image.TypeISO && def.OperatingSystem.InstallDevice != "" {
msg := fmt.Sprintf("The 'installDevice' field can only be used when 'imageType' is '%s'.", image.TypeISO)
failures = append(failures, FailedValidation{
userMessage: msg,
component: osComponent,
})
}

return failures
}
162 changes: 162 additions & 0 deletions pkg/image/validation/os_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
package validation

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/suse-edge/edge-image-builder/pkg/image"
)

func TestValidateOperatingSystem(t *testing.T) {
tests := map[string]struct {
Definition image.Definition
ExpectedFailedMessages []string
}{
`no os defined`: {
Definition: image.Definition{},
},
`all valid`: {
Definition: image.Definition{
Image: image.Image{
ImageType: image.TypeISO,
},
OperatingSystem: image.OperatingSystem{
KernelArgs: []string{"foo=bar", "baz"},
Systemd: image.Systemd{
Enable: []string{"runMe"},
Disable: []string{"dontRunMe"},
},
Users: []image.OperatingSystemUser{
{
Username: "danny",
EncryptedPassword: "InternNoMore",
SSHKey: "asdf",
},
},
Suma: image.Suma{
Host: "example.com",
ActivationKey: "please?",
},
Packages: image.Packages{
PKGList: []string{"zsh", "git"},
AdditionalRepos: []string{"myrepo.com"},
RegCode: "letMeIn",
},
Unattended: true,
InstallDevice: "/dev/sda",
},
},
},
`all invalid`: {
Definition: image.Definition{
Image: image.Image{
ImageType: image.TypeRAW,
},
OperatingSystem: image.OperatingSystem{
KernelArgs: []string{"foo="},
Systemd: image.Systemd{
Enable: []string{"confusedUser"},
Disable: []string{"confusedUser"},
},
Users: []image.OperatingSystemUser{
{
Username: "danny",
},
},
Suma: image.Suma{
ActivationKey: "please?",
},
Packages: image.Packages{
PKGList: []string{"zsh", "git"},
},
Unattended: true,
InstallDevice: "/dev/sda",
},
},
ExpectedFailedMessages: []string{
"Kernel arguments must be specified as 'key=value'.",
"Systemd conflict found, 'confusedUser' is both enabled and disabled.",
"User 'danny' must have either a password or SSH key.",
"The 'host' field is required for the 'suma' section.",
"When including the 'packageList' field, either additional repositories or a registration code must be included.",
fmt.Sprintf("The 'unattended' field can only be used when 'imageType' is '%s'.", image.TypeISO),
fmt.Sprintf("The 'installDevice' field can only be used when 'imageType' is '%s'.", image.TypeISO),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
def := test.Definition
ctx := image.Context{
ImageDefinition: &def,
}
failures := validateOperatingSystem(&ctx)
assert.Len(t, failures, len(test.ExpectedFailedMessages))
})
}
}

func TestIsOperatingSystemDefined(t *testing.T) {
tests := map[string]struct {
OS image.OperatingSystem
Expand Down Expand Up @@ -102,6 +193,7 @@ func TestValidateKernelArgs(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down Expand Up @@ -158,6 +250,7 @@ func TestValidateSystemd(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down Expand Up @@ -230,6 +323,7 @@ func TestValidateUsers(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down Expand Up @@ -291,6 +385,7 @@ func TestValidateSuma(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down Expand Up @@ -353,6 +448,73 @@ func TestPackages(t *testing.T) {
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
assert.Contains(t, foundMessages, expectedMessage)
}
})
}
}

func TestValidateUnattended(t *testing.T) {
tests := map[string]struct {
Definition image.Definition
ExpectedFailedMessages []string
}{
`not included`: {
Definition: image.Definition{},
},
`iso both specified`: {
Definition: image.Definition{
Image: image.Image{
ImageType: image.TypeISO,
},
OperatingSystem: image.OperatingSystem{
Unattended: true,
InstallDevice: "/dev/sda",
},
},
},
`not iso unattended`: {
Definition: image.Definition{
Image: image.Image{
ImageType: image.TypeRAW,
},
OperatingSystem: image.OperatingSystem{
Unattended: true,
},
},
ExpectedFailedMessages: []string{
fmt.Sprintf("The 'unattended' field can only be used when 'imageType' is '%s'.", image.TypeISO),
},
},
`not iso install device`: {
Definition: image.Definition{
Image: image.Image{
ImageType: image.TypeRAW,
},
OperatingSystem: image.OperatingSystem{
InstallDevice: "/dev/sda",
},
},
ExpectedFailedMessages: []string{
fmt.Sprintf("The 'installDevice' field can only be used when 'imageType' is '%s'.", image.TypeISO),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
def := test.Definition
failures := validateUnattended(&def)
assert.Len(t, failures, len(test.ExpectedFailedMessages))

var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.userMessage)
assert.Equal(t, osComponent, foundValidation.component)
}

for _, expectedMessage := range test.ExpectedFailedMessages {
Expand Down

0 comments on commit e5e2e68

Please sign in to comment.