Skip to content

Commit

Permalink
test: TPM State
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Mar 14, 2024
1 parent 4a24c6f commit 075ecad
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 10 deletions.
72 changes: 62 additions & 10 deletions proxmox/config_qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/Telmate/proxmox-api-go/internal/util"
"github.com/Telmate/proxmox-api-go/test/data/test_data_qemu"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -1274,6 +1275,20 @@ func Test_ConfigQemu_mapToApiValues(t *testing.T) {
config: &ConfigQemu{Iso: &IsoFile{Storage: "test", File: "file.iso"}},
output: map[string]interface{}{"ide2": "test:iso/file.iso,media=cdrom"},
},
// Create TPM
{name: "Create TPM",
config: &ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion_2_0)}},
output: map[string]interface{}{"tpmstate0": "test:1,version=v2.0"},
},
// Delete

// Delete TPM
{name: "Delete TPM",
config: &ConfigQemu{TPM: &TpmState{Delete: true}},
output: map[string]interface{}{"delete": "tpmstate0"}},
{name: "Delete TPM Full",
config: &ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion_2_0), Delete: true}},
output: map[string]interface{}{"delete": "tpmstate0"}},
// Update

// Update Disk.Ide
Expand Down Expand Up @@ -3253,6 +3268,11 @@ func Test_ConfigQemu_mapToApiValues(t *testing.T) {
config: &ConfigQemu{Iso: &IsoFile{Storage: "NewStorage", File: "file.iso"}},
output: map[string]interface{}{"ide2": "NewStorage:iso/file.iso,media=cdrom"},
},
// Update TPM
{name: "Update TPM",
config: &ConfigQemu{TPM: &TpmState{Storage: "aaaa", Version: util.Pointer(TpmVersion_1_2)}},
currentConfig: ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion_2_0)}},
output: map[string]interface{}{}},
}
for _, test := range tests {
t.Run(test.name, func(*testing.T) {
Expand Down Expand Up @@ -5799,6 +5819,11 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) {
vmr: &VmRef{vmId: 100},
output: &ConfigQemu{VmID: 100},
},
// TPM
{name: "TPM",
input: map[string]interface{}{"tpmstate0": string("local-lvm:vm-101-disk-0,size=4M,version=v2.0")},
output: &ConfigQemu{TPM: &TpmState{Storage: "local-lvm", Version: util.Pointer(TpmVersion("v2.0"))}},
},
}
for _, test := range tests {
t.Run(test.name, func(*testing.T) {
Expand All @@ -5811,6 +5836,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) {
})
}
}

func Test_ConfigQemu_Validate(t *testing.T) {
BandwidthValid0 := QemuDiskBandwidth{
MBps: QemuDiskBandwidthMBps{
Expand Down Expand Up @@ -5902,9 +5928,10 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}
validCloudInit := QemuCloudInitDisk{Format: QemuDiskFormat_Raw, Storage: "Test"}
testData := []struct {
name string
input ConfigQemu
err error
name string
input ConfigQemu
current *ConfigQemu
err error
}{
// Valid
// Valid Disks
Expand Down Expand Up @@ -6015,6 +6042,15 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}}},
}},
},
// Valid Tpm
{name: "Valid TPM Create",
input: ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion("v2.0"))}}},
{name: "Valid TPM Update",
input: ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion("v2.0"))}},
current: &ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion("v1.2"))}}},
{name: "Valid TPM Update Version=nil",
input: ConfigQemu{TPM: &TpmState{Storage: "test"}},
current: &ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion("v1.2"))}}},
// Invalid
// Invalid Disks Mutually exclusive Ide
{name: "Invalid Disks MutuallyExclusive Ide 0",
Expand Down Expand Up @@ -7094,15 +7130,31 @@ func Test_ConfigQemu_Validate(t *testing.T) {
input: ConfigQemu{Disks: &QemuStorages{VirtIO: &QemuVirtIODisks{Disk_13: &QemuVirtIOStorage{Passthrough: &QemuVirtIOPassthrough{File: "/dev/disk/by-id/scsi1", WorldWideName: "0x5004A3B2C1D0E0F1#"}}}}},
err: errors.New(Error_QemuWorldWideName_Invalid),
},
// invalid TMP
{name: "Invalid TPM errors.New(storage is required) Create",
input: ConfigQemu{TPM: &TpmState{Storage: ""}},
err: errors.New("storage is required")},
{name: "Invalid TPM errors.New(storage is required) Update",
input: ConfigQemu{TPM: &TpmState{Storage: ""}},
current: &ConfigQemu{TPM: &TpmState{}},
err: errors.New("storage is required")},
{name: "Invalid TPM errors.New(TmpState_Error_VersionRequired) Create",
input: ConfigQemu{TPM: &TpmState{Storage: "test", Version: nil}},
err: errors.New(TmpState_Error_VersionRequired)},
{name: "Invalid TPM errors.New(TmpVersion_Error_Invalid) Create",
input: ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion(""))}},
err: errors.New(TpmVersion_Error_Invalid)},
{name: "Invalid TPM errors.New(TmpVersion_Error_Invalid) Update",
input: ConfigQemu{TPM: &TpmState{Storage: "test", Version: util.Pointer(TpmVersion(""))}},
current: &ConfigQemu{TPM: &TpmState{}},
err: errors.New(TpmVersion_Error_Invalid)},
}
for _, test := range testData {
t.Run(test.name, func(*testing.T) {
if test.err != nil {
require.Equal(t, test.input.Validate(), test.err, test.name)
} else {
require.NoError(t, test.input.Validate(), test.name)
}
})
if test.current == nil {
t.Run(test.name, func(*testing.T) {
require.Equal(t, test.input.Validate(test.current), test.err, test.name)
})
}
}
}

Expand Down
65 changes: 65 additions & 0 deletions proxmox/config_qemu_tpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package proxmox

import (
"errors"
"testing"

"github.com/Telmate/proxmox-api-go/internal/util"
"github.com/stretchr/testify/require"
)

func Test_TpmState_Validate(t *testing.T) {
type testInput struct {
config TpmState
current *TpmState
}
tests := []struct {
name string
input testInput
output error
}{
{name: `Invalid Storage Create`, input: testInput{
config: TpmState{Storage: ""}},
output: errors.New("storage is required")},
{name: `Invalid Storage Update`, input: testInput{
config: TpmState{Storage: ""},
current: &TpmState{Storage: "local-lvm"}},
output: errors.New("storage is required")},
{name: `Invalid Version=nil Create`, input: testInput{
config: TpmState{Storage: "local-lvm"}},
output: errors.New(TmpState_Error_VersionRequired)},
{name: `Invalid Version="" Create`, input: testInput{
config: TpmState{Storage: "local-lvm", Version: util.Pointer(TpmVersion(""))}},
output: errors.New(TpmVersion_Error_Invalid)},
{name: `Invalid Version="" Update`, input: testInput{
config: TpmState{Storage: "local-lvm", Version: util.Pointer(TpmVersion(""))},
current: &TpmState{Storage: "local-lvm", Version: util.Pointer(TpmVersion("v2.0"))}},
output: errors.New(TpmVersion_Error_Invalid)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.config.Validate(test.input.current))
})
}
}

func Test_TpmVersion_Validate(t *testing.T) {
tests := []struct {
name string
input TpmVersion
output error
}{
{name: "Valid v1.2", input: TpmVersion_1_2},
{name: "Valid v2.0", input: TpmVersion_2_0},
{name: "Valid 1.2", input: "1.2"},
{name: "Valid 2", input: "2"},
{name: "Valid 2.0", input: "2.0"},
{name: "Valid v2", input: "v2"},
{name: `Invalid ""`, output: errors.New(TpmVersion_Error_Invalid)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.Validate())
})
}
}

0 comments on commit 075ecad

Please sign in to comment.