-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Elemental validation and improvements when using SL Micro 6.0 (#549)
* elemental validation and SL Micro 6.0 handling improvements * add note to docs * fixes for linter * feedback updates * Update pkg/image/validation/elemental.go Co-authored-by: Atanas Dinov <[email protected]> * Update pkg/image/validation/elemental.go Co-authored-by: Atanas Dinov <[email protected]> * feedback updates * last changes --------- Co-authored-by: Atanas Dinov <[email protected]>
- Loading branch information
1 parent
f9b21bc
commit d0a8af1
Showing
8 changed files
with
236 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package validation | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/suse-edge/edge-image-builder/pkg/image" | ||
) | ||
|
||
const ( | ||
elementalComponent = "Elemental" | ||
elementalConfigFilename = "elemental_config.yaml" | ||
) | ||
|
||
func validateElemental(ctx *image.Context) []FailedValidation { | ||
var failures []FailedValidation | ||
|
||
elementalConfigDir := filepath.Join(ctx.ImageConfigDir, "elemental") | ||
if _, err := os.Stat(elementalConfigDir); err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
failures = append(failures, FailedValidation{ | ||
UserMessage: "Elemental config directory could not be read", | ||
Error: err, | ||
}) | ||
return failures | ||
} | ||
|
||
failures = append(failures, validateElementalDir(elementalConfigDir)...) | ||
|
||
if ctx.ImageDefinition.OperatingSystem.Packages.RegCode == "" { | ||
failures = append(failures, FailedValidation{ | ||
UserMessage: "Operating system package registration code field must be defined when using Elemental with SL Micro 6.0", | ||
}) | ||
} | ||
|
||
return failures | ||
} | ||
|
||
func validateElementalDir(elementalConfigDir string) []FailedValidation { | ||
var failures []FailedValidation | ||
|
||
elementalConfigDirEntries, err := os.ReadDir(elementalConfigDir) | ||
if err != nil { | ||
failures = append(failures, FailedValidation{ | ||
UserMessage: "Elemental config directory could not be read", | ||
Error: err, | ||
}) | ||
|
||
return failures | ||
} | ||
|
||
switch len(elementalConfigDirEntries) { | ||
case 0: | ||
failures = append(failures, FailedValidation{ | ||
UserMessage: "Elemental config directory should not be present if it is empty", | ||
}) | ||
case 1: | ||
if elementalConfigDirEntries[0].Name() != elementalConfigFilename { | ||
failures = append(failures, FailedValidation{ | ||
UserMessage: fmt.Sprintf("Elemental config file should only be named `%s`", elementalConfigFilename), | ||
}) | ||
} | ||
default: | ||
failures = append(failures, FailedValidation{ | ||
UserMessage: fmt.Sprintf("Elemental config directory should only contain a singular '%s' file", elementalConfigFilename), | ||
}) | ||
} | ||
|
||
return failures | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package validation | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/suse-edge/edge-image-builder/pkg/image" | ||
) | ||
|
||
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) | ||
|
||
defer func() { | ||
assert.NoError(t, os.RemoveAll(configDir)) | ||
}() | ||
|
||
elementalDir := filepath.Join(configDir, "elemental") | ||
require.NoError(t, os.MkdirAll(elementalDir, os.ModePerm)) | ||
|
||
validElementalConfig := filepath.Join(elementalDir, "elemental_config.yaml") | ||
require.NoError(t, os.WriteFile(validElementalConfig, []byte(""), 0o600)) | ||
|
||
tests := map[string]struct { | ||
ImageDefinition *image.Definition | ||
ExpectedFailedMessages []string | ||
}{ | ||
`valid`: { | ||
ImageDefinition: &image.Definition{ | ||
OperatingSystem: image.OperatingSystem{ | ||
Packages: image.Packages{ | ||
RegCode: "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", | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
ctx := image.Context{ | ||
ImageConfigDir: configDir, | ||
ImageDefinition: test.ImageDefinition, | ||
} | ||
failures := validateElemental(&ctx) | ||
assert.Len(t, failures, len(test.ExpectedFailedMessages)) | ||
|
||
var foundMessages []string | ||
for _, foundValidation := range failures { | ||
foundMessages = append(foundMessages, foundValidation.UserMessage) | ||
} | ||
|
||
for _, expectedMessage := range test.ExpectedFailedMessages { | ||
assert.Contains(t, foundMessages, expectedMessage) | ||
} | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestValidateElementalConfigDirValid(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)) | ||
|
||
elementalConfig := filepath.Join(elementalDir, "elemental_config.yaml") | ||
require.NoError(t, os.WriteFile(elementalConfig, []byte(""), 0o600)) | ||
|
||
failures := validateElementalDir(elementalDir) | ||
assert.Len(t, failures, 0) | ||
} | ||
|
||
func TestValidateElementalConfigDirEmptyDir(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)) | ||
|
||
failures := validateElementalDir(elementalDir) | ||
assert.Len(t, failures, 1) | ||
|
||
assert.Contains(t, failures[0].UserMessage, "Elemental config directory should not be present if it is empty") | ||
} | ||
|
||
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(elementalDir, "elemental_config2.yaml") | ||
require.NoError(t, os.WriteFile(secondElementalConfig, []byte(""), 0o600)) | ||
|
||
failures := validateElementalDir(elementalDir) | ||
assert.Len(t, failures, 1) | ||
|
||
assert.Contains(t, failures[0].UserMessage, "Elemental config directory should only contain a singular 'elemental_config.yaml' file") | ||
} | ||
|
||
func TestValidateElementalConfigDirUnreadable(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)) | ||
require.NoError(t, os.Chmod(elementalDir, 0o333)) | ||
|
||
failures := validateElementalDir(elementalDir) | ||
assert.Len(t, failures, 1) | ||
|
||
assert.Contains(t, failures[0].UserMessage, "Elemental config directory could not be read") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters