0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 19:12:59 +00:00

fix(vm): fix index out of range when unmarshalling custompcidevice (#496)

* fix(vm): fix index out of range when unmarshalling custompcidevice

* fix: linter errors

---------

Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Michael Iseli 2023-08-16 01:44:57 +02:00 committed by GitHub
parent 404ac66628
commit 78d668377f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -1606,7 +1606,7 @@ func (r *CustomPCIDevice) UnmarshalJSON(b []byte) error {
for _, p := range pairs {
v := strings.Split(strings.TrimSpace(p), "=")
if len(v) == 1 {
r.DeviceIDs = strings.Split(v[1], ";")
r.DeviceIDs = strings.Split(v[0], ";")
} else if len(v) == 2 {
switch v[0] {
case "host":

View File

@ -15,6 +15,8 @@ import (
)
func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) {
t.Parallel()
ds8gig := types.DiskSizeFromGigabytes(8)
tests := []struct {
name string
@ -49,8 +51,11 @@ func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) {
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := &CustomStorageDevice{}
if err := r.UnmarshalJSON([]byte(tt.line)); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
@ -59,3 +64,50 @@ func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) {
})
}
}
func TestCustomPCIDevice_UnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
line string
want *CustomPCIDevice
wantErr bool
}{
{
name: "id only pci device",
line: `"0000:81:00.2"`,
want: &CustomPCIDevice{
DeviceIDs: []string{"0000:81:00.2"},
MDev: nil,
PCIExpress: types.BoolPtr(false),
ROMBAR: types.BoolPtr(true),
ROMFile: nil,
XVGA: types.BoolPtr(false),
},
},
{
name: "pci device with more details",
line: `"host=81:00.4,pcie=0,rombar=1,x-vga=0"`,
want: &CustomPCIDevice{
DeviceIDs: []string{"81:00.4"},
MDev: nil,
PCIExpress: types.BoolPtr(false),
ROMBAR: types.BoolPtr(true),
ROMFile: nil,
XVGA: types.BoolPtr(false),
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := &CustomPCIDevice{}
if err := r.UnmarshalJSON([]byte(tt.line)); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}