debian-forge-cli/internal/blueprintload/blueprintload_test.go
Michael Vogt 5fb17b967e image-builder: use github.com/osbuild/blueprint
Drop using the "images" library blueprint types and use the ones
from github.com/osbuild/blueprint/pkg/blueprint instead.

This also moves to the images library to v0.172.0 and the blueprints
library to v1.12.0 as this is required for this to work.
2025-08-07 16:56:35 +00:00

85 lines
2 KiB
Go

package blueprintload_test
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/osbuild/blueprint/pkg/blueprint"
"github.com/osbuild/image-builder-cli/internal/blueprintload"
)
var testBlueprintJSON = `{
"customizations": {
"user": [
{
"name": "alice"
}
]
}
}`
var testBlueprintTOML = `
[[customizations.user]]
name = "alice"
`
var testBlueprintJSONunknownKeys = `
{
"birds": {"name": "robin"}
}
`
var testBlueprintTOMLunknownKeys = `
[[birds]]
name = "robin"
`
var expectedBlueprint = &blueprint.Blueprint{
Customizations: &blueprint.Customizations{
User: []blueprint.UserCustomization{
{
Name: "alice",
},
},
},
}
func makeTestBlueprint(t *testing.T, name, content string) string {
tmpdir := t.TempDir()
blueprintPath := filepath.Join(tmpdir, name)
err := os.WriteFile(blueprintPath, []byte(content), 0644)
assert.NoError(t, err)
return blueprintPath
}
func TestBlueprintLoadJSON(t *testing.T) {
for _, tc := range []struct {
fname string
content string
expectedBp *blueprint.Blueprint
expectedError string
}{
{"bp.json", testBlueprintJSON, expectedBlueprint, ""},
{"bp.toml", testBlueprintTOML, expectedBlueprint, ""},
{"bp.toml", "wrong-content", nil, `cannot decode ".*/bp.toml": toml: `},
{"bp.json", "wrong-content", nil, `cannot decode ".*/bp.json": invalid `},
{"bp", "wrong-content", nil, `unsupported file extension for "/.*/bp"`},
{"bp.toml", testBlueprintTOMLunknownKeys, nil, `cannot decode ".*/bp.toml": unknown keys found: \[birds birds.name\]`},
{"bp.json", testBlueprintJSONunknownKeys, nil, `cannot decode ".*/bp.json": json: unknown field "birds"`},
} {
blueprintPath := makeTestBlueprint(t, tc.fname, tc.content)
bp, err := blueprintload.Load(blueprintPath)
if tc.expectedError == "" {
assert.NoError(t, err)
assert.Equal(t, tc.expectedBp, bp)
} else {
assert.NotNil(t, err)
assert.Regexp(t, tc.expectedError, err.Error())
}
}
}