debian-forge-composer/internal/osbuild2/files_input_test.go
Tomas Hozza 8271910051 osbuild2: refactor files inputs
osbuild stage inputs were originally implemented in composer as
stage-specific inputs, while in reality, they are defined as individual
inputs, usually accepted by multiple stages. Therefore a single stage
input can be passed to any stage, as long as the stage accepts it.

Files inputs type was previously defined, but not used by any stage.
Creation of proper inputs type structures is currently handled in
`internal/distro/rhel85/stage_inputs.go` instead.

Refactor files inputs type to be usable directly as an input type
structure for stages, which accept it. For now, implement only the
`org.osbuild.pipeline` origin and related input reference.

Add unit tests for the `FilesInputs`.

Define input origin names as string constants, so that they can be used
by inputs implementations, instead of using string literals.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
2021-08-02 19:05:09 +02:00

101 lines
2.4 KiB
Go

package osbuild2
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewFilesInputs(t *testing.T) {
inputFilename := "image.raw"
pipeline := "os"
expectedInput := &FilesInputs{
File: &FilesInput{
inputCommon: inputCommon{
Type: InputTypeFiles,
Origin: InputOriginPipeline,
},
References: &FilesInputReferencesPipeline{
fmt.Sprintf("name:%s", pipeline): FileReference{File: inputFilename},
},
},
}
actualInput := NewFilesInputs(NewFilesInputReferencesPipeline(pipeline, inputFilename))
assert.Equal(t, expectedInput, actualInput)
}
func TestFilesInput_UnmarshalJSON(t *testing.T) {
type fields struct {
Type string
Origin string
References FilesInputReferences
}
type args struct {
data []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "pipeline-origin",
fields: fields{
Type: InputTypeFiles,
Origin: InputOriginPipeline,
References: NewFilesInputReferencesPipeline("os", "image.raw"),
},
args: args{
data: []byte(`{"type":"org.osbuild.files","origin":"org.osbuild.pipeline","references":{"name:os":{"file":"image.raw"}}}`),
},
},
{
name: "unknown-origin",
fields: fields{
Type: InputTypeFiles,
Origin: InputOriginSource,
References: nil,
},
wantErr: true,
},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := &FilesInput{
inputCommon: inputCommon{
Type: tt.fields.Type,
Origin: tt.fields.Origin,
},
References: tt.fields.References,
}
var gotInput FilesInput
if err := json.Unmarshal(tt.args.data, &gotInput); (err != nil) != tt.wantErr {
println("data: ", string(tt.args.data))
t.Errorf("FilesInput.UnmarshalJSON() error = %v, wantErr %v [idx: %d]", err, tt.wantErr, idx)
}
if tt.wantErr {
return
}
gotBytes, err := json.Marshal(input)
if err != nil {
t.Errorf("Could not marshal FilesInput: %v", err)
}
if !bytes.Equal(gotBytes, tt.args.data) {
t.Errorf("Expected `%v`, got `%v` [idx: %d]", string(tt.args.data), string(gotBytes), idx)
}
if !reflect.DeepEqual(&gotInput, input) {
t.Errorf("got {%v, %v, %v}, expected {%v, %v, %v} [%d]", gotInput.Type, gotInput.Origin, gotInput.References, input.Type, input.Origin, input.References, idx)
}
})
}
}