Skip to content

Commit

Permalink
feedback updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed Sep 23, 2024
1 parent 97b862d commit 7a103f6
Showing 1 changed file with 74 additions and 80 deletions.
154 changes: 74 additions & 80 deletions pkg/image/validation/elemental_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package validation

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -11,7 +10,14 @@ import (
"github.com/suse-edge/edge-image-builder/pkg/image"
)

func TestValidateElemental(t *testing.T) {
func TestValidateElementalNoDir(t *testing.T) {
ctx := image.Context{}

failures := validateElemental(&ctx)
assert.Len(t, failures, 0)
}

func TestValidateElementalValid(t *testing.T) {
configDir, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)

Expand All @@ -29,7 +35,7 @@ func TestValidateElemental(t *testing.T) {
ImageDefinition *image.Definition
ExpectedFailedMessages []string
}{
`valid 1.1`: {
`valid`: {
ImageDefinition: &image.Definition{
OperatingSystem: image.OperatingSystem{
Packages: image.Packages{
Expand All @@ -38,7 +44,7 @@ func TestValidateElemental(t *testing.T) {
},
},
},
`1.1 no registration code`: {
`no registration code`: {
ImageDefinition: &image.Definition{},
ExpectedFailedMessages: []string{
"Operating system package registration code field must be defined when using Elemental with SL Micro 6.0",
Expand Down Expand Up @@ -68,105 +74,93 @@ func TestValidateElemental(t *testing.T) {
}
}

func TestValidateElementalConfigDir(t *testing.T) {
configDirValid, err := os.MkdirTemp("", "eib-config-")
func TestValidateElementalConfigDirValid(t *testing.T) {
configDir, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)

configDirEmpty, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(configDir))
}()

configDirMultipleFiles, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)
elementalDir := filepath.Join(configDir, "elemental")
require.NoError(t, os.MkdirAll(elementalDir, os.ModePerm))

configDirInvalidName, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)
elementalConfig := filepath.Join(elementalDir, "elemental_config.yaml")
require.NoError(t, os.WriteFile(elementalConfig, []byte(""), 0o600))

failures := validateElementalDir(elementalDir)
assert.Len(t, failures, 0)
}

configDirUnreadable, err := os.MkdirTemp("", "eib-config-")
func TestValidateElementalConfigDirEmptyDir(t *testing.T) {
configDir, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)

defer func() {
assert.NoError(t, os.RemoveAll(configDirValid))
assert.NoError(t, os.RemoveAll(configDirEmpty))
assert.NoError(t, os.RemoveAll(configDirMultipleFiles))
assert.NoError(t, os.RemoveAll(configDirInvalidName))
assert.NoError(t, os.RemoveAll(configDirUnreadable))
assert.NoError(t, os.RemoveAll(configDir))
}()

elementalDirValid := filepath.Join(configDirValid, "elemental")
require.NoError(t, os.MkdirAll(elementalDirValid, os.ModePerm))
elementalDir := filepath.Join(configDir, "elemental")
require.NoError(t, os.MkdirAll(elementalDir, os.ModePerm))

validElementalConfig := filepath.Join(elementalDirValid, "elemental_config.yaml")
require.NoError(t, os.WriteFile(validElementalConfig, []byte(""), 0o600))
failures := validateElementalDir(elementalDir)
assert.Len(t, failures, 1)

elementalDirEmpty := filepath.Join(configDirEmpty, "elemental")
require.NoError(t, os.MkdirAll(elementalDirEmpty, os.ModePerm))
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.UserMessage)
}

elementalDirMultipleFiles := filepath.Join(configDirMultipleFiles, "elemental")
require.NoError(t, os.MkdirAll(elementalDirMultipleFiles, os.ModePerm))
assert.Contains(t, foundMessages, "Elemental config directory should not be present if it is empty")
}

firstElementalConfig := filepath.Join(elementalDirMultipleFiles, "elemental_config1.yaml")
func TestValidateElementalConfigDirMultipleFiles(t *testing.T) {
configDir, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)

defer func() {
assert.NoError(t, os.RemoveAll(configDir))
}()

elementalDir := filepath.Join(configDir, "elemental")
require.NoError(t, os.MkdirAll(elementalDir, os.ModePerm))

firstElementalConfig := filepath.Join(elementalDir, "elemental_config1.yaml")
require.NoError(t, os.WriteFile(firstElementalConfig, []byte(""), 0o600))
secondElementalConfig := filepath.Join(elementalDirMultipleFiles, "elemental_config2.yaml")
secondElementalConfig := filepath.Join(elementalDir, "elemental_config2.yaml")
require.NoError(t, os.WriteFile(secondElementalConfig, []byte(""), 0o600))

elementalDirInvalidName := filepath.Join(configDirInvalidName, "elemental")
require.NoError(t, os.MkdirAll(elementalDirInvalidName, os.ModePerm))
failures := validateElementalDir(elementalDir)
assert.Len(t, failures, 1)

invalidElementalConfig := filepath.Join(elementalDirInvalidName, "elemental.yaml")
require.NoError(t, os.WriteFile(invalidElementalConfig, []byte(""), 0o600))
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.UserMessage)
}

elementalDirUnreadable := filepath.Join(configDirUnreadable, "elemental")
require.NoError(t, os.MkdirAll(elementalDirUnreadable, os.ModePerm))
require.NoError(t, os.Chmod(elementalDirUnreadable, 0o333))
assert.Contains(t, foundMessages, "Elemental config directory should only contain a singular 'elemental_config.yaml' file")
}

tests := map[string]struct {
ExpectedFailedMessages []string
ElementalDir string
}{
`valid elemental dir`: {
ElementalDir: elementalDirValid,
},
`empty elemental dir`: {
ElementalDir: elementalDirEmpty,
ExpectedFailedMessages: []string{
"Elemental config directory should not be present if it is empty",
},
},
`multiple files in elemental dir`: {
ElementalDir: elementalDirMultipleFiles,
ExpectedFailedMessages: []string{
"Elemental config directory should only contain a singular 'elemental_config.yaml' file",
},
},
`invalid name in elemental dir`: {
ElementalDir: elementalDirInvalidName,
ExpectedFailedMessages: []string{
"Elemental config file should only be named `elemental_config.yaml`",
},
},
`unreadable elemental dir`: {
ElementalDir: elementalDirUnreadable,
ExpectedFailedMessages: []string{
fmt.Sprintf("Elemental config directory could not be read: open %s: permission denied", elementalDirUnreadable),
"Elemental config directory should not be present if it is empty",
},
},
}
func TestValidateElementalConfigDirUnreadable(t *testing.T) {
configDir, err := os.MkdirTemp("", "eib-config-")
require.NoError(t, err)

for name, test := range tests {
t.Run(name, func(t *testing.T) {
failures := validateElementalDir(test.ElementalDir)
assert.Len(t, failures, len(test.ExpectedFailedMessages))
defer func() {
assert.NoError(t, os.RemoveAll(configDir))
}()

var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.UserMessage)
}
elementalDir := filepath.Join(configDir, "elemental")
require.NoError(t, os.MkdirAll(elementalDir, os.ModePerm))
require.NoError(t, os.Chmod(elementalDir, 0o333))

for _, expectedMessage := range test.ExpectedFailedMessages {
assert.Contains(t, foundMessages, expectedMessage)
}
failures := validateElementalDir(elementalDir)
assert.Len(t, failures, 2)

})
var foundMessages []string
for _, foundValidation := range failures {
foundMessages = append(foundMessages, foundValidation.UserMessage)
}

assert.Contains(t, foundMessages, "Elemental config directory could not be read")
assert.Contains(t, foundMessages, "Elemental config directory should not be present if it is empty")
}

0 comments on commit 7a103f6

Please sign in to comment.