debian-forge-composer/internal/osbuild/files_input_test.go
Tomáš Hozza dd59ce6a16 osbuild: rework XZ stage inputs
The `FilesInputs` was since the beginning an XZ-specific implementation
of the input, but it was implemented in the `files_input.go` in a false
hope that it could be used as a generic stage inputs by any stages. It
turned out that various stages require different implementation of
its input. Specifically there is usually a stage-specific key, which has
assigned a common input type. For XZ stage, the key is `file`.

Remove `FilesInputs` and instead implement `XzStageInputs` which is now
accepted by the XZ stage.

Fix all affected pipeline implementations that use XZ stage.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2023-01-30 11:24:08 +01:00

93 lines
3.2 KiB
Go

package osbuild
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilesInput_UnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
ref FilesInputRef
rawJson []byte
}{
{
name: "pipeline-object-ref",
ref: NewFilesInputPipelineObjectRef("os", "image.raw", nil),
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.pipeline","references":{"name:os":{"file":"image.raw"}}}`),
},
{
name: "pipeline-array-ref",
ref: NewFilesInputPipelineArrayRef("os", "image.raw", nil),
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.pipeline","references":[{"id":"name:os","options":{"file":"image.raw"}}]}`),
},
{
name: "source-plain-ref",
ref: NewFilesInputSourcePlainRef([]string{"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"}),
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.source","references":["sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"]}`),
},
{
name: "source-array-ref",
ref: NewFilesInputSourceArrayRef([]FilesInputSourceArrayRefEntry{
NewFilesInputSourceArrayRefEntry("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", nil),
}),
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.source","references":[{"id":"sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"}]}`),
},
{
name: "source-object-ref",
ref: NewFilesInputSourceObjectRef(map[string]FilesInputRefMetadata{
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef": nil,
}),
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.source","references":{"sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef":{}}}`),
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
var gotInput FilesInput
err := json.Unmarshal(tt.rawJson, &gotInput)
assert.NoErrorf(t, err, "FilesInput.UnmarshalJSON() error = %v", err)
input := NewFilesInput(tt.ref)
gotBytes, err := json.Marshal(input)
assert.NoErrorf(t, err, "FilesInput.MarshalJSON() error = %v", err)
assert.EqualValuesf(t, tt.rawJson, gotBytes, "Expected JSON `%v`, got JSON `%v`", string(tt.rawJson), string(gotBytes))
assert.EqualValuesf(t, input, &gotInput, "Expected input `%v`, got input `%v` [test: %q]", input, &gotInput, tt.name)
})
}
// test invalid cases
invalidTestCases := []struct {
name string
rawJson []byte
}{
{
name: "invalid-pipeline-ref",
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.pipeline","references":1}`),
},
{
name: "invalid-source-ref",
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.source","references":2}`),
},
{
name: "invalid-origin",
rawJson: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.invalid","references":{}}`),
},
{
name: "invalid-input",
rawJson: []byte(`[]`),
},
}
for _, tt := range invalidTestCases {
t.Run(tt.name, func(t *testing.T) {
var gotInput FilesInput
err := json.Unmarshal(tt.rawJson, &gotInput)
assert.Error(t, err)
})
}
}