Blame internal/osbuild2/stage_test.go

Packit Service 15f37d
package osbuild2
Packit Service 15f37d
Packit Service 15f37d
import (
Packit Service 15f37d
	"bytes"
Packit Service 15f37d
	"encoding/json"
Packit Service 15f37d
	"reflect"
Packit Service 15f37d
	"testing"
Packit Service 15f37d
Packit Service 15f37d
	"github.com/google/uuid"
Packit Service 15f37d
)
Packit Service 15f37d
Packit Service 15f37d
func TestStage_UnmarshalJSON(t *testing.T) {
Packit Service 15f37d
	nullUUID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
Packit Service 15f37d
	type fields struct {
Packit Service 15f37d
		Type    string
Packit Service 15f37d
		Options StageOptions
Packit Service 15f37d
	}
Packit Service 15f37d
	type args struct {
Packit Service 15f37d
		data []byte
Packit Service 15f37d
	}
Packit Service 15f37d
	tests := []struct {
Packit Service 15f37d
		name    string
Packit Service 15f37d
		fields  fields
Packit Service 15f37d
		args    args
Packit Service 15f37d
		wantErr bool
Packit Service 15f37d
	}{
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "invalid json",
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.foo","options":{"bar":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
			wantErr: true,
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "unknown stage",
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.foo","options":{"bar":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
			wantErr: true,
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "missing options",
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.locale"}`),
Packit Service 15f37d
			},
Packit Service 15f37d
			wantErr: true,
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "missing name",
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"foo":null,"options":{"bar":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
			wantErr: true,
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "chrony",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.chrony",
Packit Service 15f37d
				Options: &ChronyStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.chrony","options":{"timeservers":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "firewall",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.firewall",
Packit Service 15f37d
				Options: &FirewallStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.firewall","options":{}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "fix-bls",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.fix-bls",
Packit Service 15f37d
				Options: &FixBLSStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.fix-bls","options":{}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "fstab",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.fstab",
Packit Service 15f37d
				Options: &FSTabStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.fstab","options":{"filesystems":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "groups",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.groups",
Packit Service 15f37d
				Options: &GroupsStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.groups","options":{"groups":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "grub2",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.grub2",
Packit Service 15f37d
				Options: &GRUB2StageOptions{
Packit Service 15f37d
					RootFilesystemUUID: nullUUID,
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.grub2","options":{"root_fs_uuid":"00000000-0000-0000-0000-000000000000"}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "grub2-uefi",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.grub2",
Packit Service 15f37d
				Options: &GRUB2StageOptions{
Packit Service 15f37d
					RootFilesystemUUID: nullUUID,
Packit Service 15f37d
					UEFI: &GRUB2UEFI{
Packit Service 15f37d
						Vendor: "vendor",
Packit Service 15f37d
					},
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.grub2","options":{"root_fs_uuid":"00000000-0000-0000-0000-000000000000","uefi":{"vendor":"vendor"}}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "grub2-separate-boot",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.grub2",
Packit Service 15f37d
				Options: &GRUB2StageOptions{
Packit Service 15f37d
					RootFilesystemUUID: nullUUID,
Packit Service 15f37d
					BootFilesystemUUID: &nullUUID,
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.grub2","options":{"root_fs_uuid":"00000000-0000-0000-0000-000000000000","boot_fs_uuid":"00000000-0000-0000-0000-000000000000"}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "hostname",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.hostname",
Packit Service 15f37d
				Options: &HostnameStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.hostname","options":{"hostname":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "keymap",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.keymap",
Packit Service 15f37d
				Options: &KeymapStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.keymap","options":{"keymap":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "locale",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.locale",
Packit Service 15f37d
				Options: &LocaleStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.locale","options":{"language":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "rhsm-empty",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.rhsm",
Packit Service 15f37d
				Options: &RHSMStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.rhsm","options":{}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "rhsm",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.rhsm",
Packit Service 15f37d
				Options: &RHSMStageOptions{
Packit Service 15f37d
					DnfPlugins: &RHSMStageOptionsDnfPlugins{
Packit Service 15f37d
						ProductID: &RHSMStageOptionsDnfPlugin{
Packit Service 15f37d
							Enabled: false,
Packit Service 15f37d
						},
Packit Service 15f37d
						SubscriptionManager: &RHSMStageOptionsDnfPlugin{
Packit Service 15f37d
							Enabled: false,
Packit Service 15f37d
						},
Packit Service 15f37d
					},
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.rhsm","options":{"dnf-plugins":{"product-id":{"enabled":false},"subscription-manager":{"enabled":false}}}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "script",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.script",
Packit Service 15f37d
				Options: &ScriptStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.script","options":{"script":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "selinux",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.selinux",
Packit Service 15f37d
				Options: &SELinuxStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.selinux","options":{"file_contexts":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "systemd",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.systemd",
Packit Service 15f37d
				Options: &SystemdStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.systemd","options":{}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "systemd-enabled",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.systemd",
Packit Service 15f37d
				Options: &SystemdStageOptions{
Packit Service 15f37d
					EnabledServices: []string{"foo.service"},
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.systemd","options":{"enabled_services":["foo.service"]}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "timezone",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.timezone",
Packit Service 15f37d
				Options: &TimezoneStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.timezone","options":{"zone":""}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "users",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.users",
Packit Service 15f37d
				Options: &UsersStageOptions{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.users","options":{"users":null}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
	}
Packit Service 15f37d
	for idx, tt := range tests {
Packit Service 15f37d
		t.Run(tt.name, func(t *testing.T) {
Packit Service 15f37d
			stage := &Stage{
Packit Service 15f37d
				Type:    tt.fields.Type,
Packit Service 15f37d
				Options: tt.fields.Options,
Packit Service 15f37d
			}
Packit Service 15f37d
			var gotStage Stage
Packit Service 15f37d
			if err := gotStage.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
Packit Service 15f37d
				t.Errorf("Stage.UnmarshalJSON() error = %v, wantErr %v [idx: %d]", err, tt.wantErr, idx)
Packit Service 15f37d
			}
Packit Service 15f37d
			if tt.wantErr {
Packit Service 15f37d
				return
Packit Service 15f37d
			}
Packit Service 15f37d
			gotBytes, err := json.Marshal(stage)
Packit Service 15f37d
			if err != nil {
Packit Service 15f37d
				t.Errorf("Could not marshal stage: %v", err)
Packit Service 15f37d
			}
Packit Service 15f37d
			if !bytes.Equal(gotBytes, tt.args.data) {
Packit Service 15f37d
				t.Errorf("Expected `%v`, got `%v` [idx: %d]", string(tt.args.data), string(gotBytes), idx)
Packit Service 15f37d
			}
Packit Service 15f37d
			if !reflect.DeepEqual(&gotStage, stage) {
Packit Service 15f37d
				t.Errorf("got {%v, %v}, expected {%v, %v} [%d]", gotStage.Type, gotStage.Options, stage.Type, stage.Options, idx)
Packit Service 15f37d
			}
Packit Service 15f37d
		})
Packit Service 15f37d
	}
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// Test new stages that have Inputs (osbuild v2 schema)
Packit Service 15f37d
func TestStageV2_UnmarshalJSON(t *testing.T) {
Packit Service 15f37d
	type fields struct {
Packit Service 15f37d
		Type    string
Packit Service 15f37d
		Options StageOptions
Packit Service 15f37d
		Inputs  Inputs
Packit Service 15f37d
	}
Packit Service 15f37d
	type args struct {
Packit Service 15f37d
		data []byte
Packit Service 15f37d
	}
Packit Service 15f37d
	tests := []struct {
Packit Service 15f37d
		name    string
Packit Service 15f37d
		fields  fields
Packit Service 15f37d
		args    args
Packit Service 15f37d
		wantErr bool
Packit Service 15f37d
	}{
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "rpm-empty",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type:    "org.osbuild.rpm",
Packit Service 15f37d
				Options: &RPMStageOptions{},
Packit Service 15f37d
				Inputs:  &RPMStageInputs{},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.rpm","inputs":{"packages":null},"options":{}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "rpm",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.rpm",
Packit Service 15f37d
				Inputs: &RPMStageInputs{
Packit Service 15f37d
					Packages: &RPMStageInput{
Packit Service 15f37d
						References: RPMStageReferences{
Packit Service 15f37d
							"checksum1",
Packit Service 15f37d
							"checksum2",
Packit Service 15f37d
						},
Packit Service 15f37d
					},
Packit Service 15f37d
				},
Packit Service 15f37d
				Options: &RPMStageOptions{
Packit Service 15f37d
					GPGKeys: []string{"key1", "key2"},
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.rpm","inputs":{"packages":{"type":"","origin":"","references":["checksum1","checksum2"]}},"options":{"gpgkeys":["key1","key2"]}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
		{
Packit Service 15f37d
			name: "ostree-preptree",
Packit Service 15f37d
			fields: fields{
Packit Service 15f37d
				Type: "org.osbuild.ostree.preptree",
Packit Service 15f37d
				Options: &OSTreePrepTreeStageOptions{
Packit Service 15f37d
					EtcGroupMembers: []string{
Packit Service 15f37d
						"wheel",
Packit Service 15f37d
					},
Packit Service 15f37d
				},
Packit Service 15f37d
			},
Packit Service 15f37d
			args: args{
Packit Service 15f37d
				data: []byte(`{"type":"org.osbuild.ostree.preptree","options":{"etc_group_members":["wheel"]}}`),
Packit Service 15f37d
			},
Packit Service 15f37d
		},
Packit Service 15f37d
	}
Packit Service 15f37d
	for idx, tt := range tests {
Packit Service 15f37d
		t.Run(tt.name, func(t *testing.T) {
Packit Service 15f37d
			stage := &Stage{
Packit Service 15f37d
				Type:    tt.fields.Type,
Packit Service 15f37d
				Options: tt.fields.Options,
Packit Service 15f37d
				Inputs:  tt.fields.Inputs,
Packit Service 15f37d
			}
Packit Service 15f37d
			var gotStage Stage
Packit Service 15f37d
			if err := gotStage.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
Packit Service 15f37d
				println("data: ", string(tt.args.data))
Packit Service 15f37d
				t.Errorf("Stage.UnmarshalJSON() error = %v, wantErr %v [idx: %d]", err, tt.wantErr, idx)
Packit Service 15f37d
			}
Packit Service 15f37d
			if tt.wantErr {
Packit Service 15f37d
				return
Packit Service 15f37d
			}
Packit Service 15f37d
			gotBytes, err := json.Marshal(stage)
Packit Service 15f37d
			if err != nil {
Packit Service 15f37d
				t.Errorf("Could not marshal stage: %v", err)
Packit Service 15f37d
			}
Packit Service 15f37d
			if !bytes.Equal(gotBytes, tt.args.data) {
Packit Service 15f37d
				t.Errorf("Expected `%v`, got `%v` [idx: %d]", string(tt.args.data), string(gotBytes), idx)
Packit Service 15f37d
			}
Packit Service 15f37d
			if !reflect.DeepEqual(&gotStage, stage) {
Packit Service 15f37d
				t.Errorf("got {%v, %v, %v}, expected {%v, %v, %v} [%d]", gotStage.Type, gotStage.Options, gotStage.Inputs, stage.Type, stage.Options, stage.Inputs, idx)
Packit Service 15f37d
			}
Packit Service 15f37d
		})
Packit Service 15f37d
	}
Packit Service 15f37d
}