fix test and generated code

This commit is contained in:
sylviamoss 2020-11-20 16:42:49 +01:00
parent 3b523e147e
commit 99e7ac5f42
6 changed files with 66 additions and 164 deletions

View File

@ -3,7 +3,6 @@ package clone
import (
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
)
@ -95,10 +94,10 @@ func (*FlatGlobalRoutingSettings) HCL2Spec() map[string]hcldec.Spec {
// FlatLinuxOptions is an auto-generated flat version of LinuxOptions.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatLinuxOptions struct {
Domain *string `mapstructure:"domain" cty:"domain" hcl:"domain"`
Hostname *string `mapstructure:"host_name" cty:"host_name" hcl:"host_name"`
HWClockUTC *config.Trilean `mapstructure:"hw_clock_utc" cty:"hw_clock_utc" hcl:"hw_clock_utc"`
Timezone *string `mapstructure:"time_zone" cty:"time_zone" hcl:"time_zone"`
Domain *string `mapstructure:"domain" cty:"domain" hcl:"domain"`
Hostname *string `mapstructure:"host_name" cty:"host_name" hcl:"host_name"`
HWClockUTC *bool `mapstructure:"hw_clock_utc" cty:"hw_clock_utc" hcl:"hw_clock_utc"`
Timezone *string `mapstructure:"time_zone" cty:"time_zone" hcl:"time_zone"`
}
// FlatMapstructure returns a new FlatLinuxOptions.
@ -115,7 +114,7 @@ func (*FlatLinuxOptions) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"domain": &hcldec.AttrSpec{Name: "domain", Type: cty.String, Required: false},
"host_name": &hcldec.AttrSpec{Name: "host_name", Type: cty.String, Required: false},
"hw_clock_utc": &hcldec.AttrSpec{Name: "hw_clock_utc", Type: cty.Number, Required: false},
"hw_clock_utc": &hcldec.AttrSpec{Name: "hw_clock_utc", Type: cty.Bool, Required: false},
"time_zone": &hcldec.AttrSpec{Name: "time_zone", Type: cty.String, Required: false},
}
return s

View File

@ -97,7 +97,6 @@ type HardwareConfig struct {
VGPUProfile string
Firmware string
ForceBIOSSetup bool
StorageConfig StorageConfig
}
type NIC struct {
@ -521,76 +520,6 @@ func (vm *VirtualMachineDriver) Configure(config *HardwareConfig) error {
confSpec.CpuHotAddEnabled = &config.CpuHotAddEnabled
confSpec.MemoryHotAddEnabled = &config.MemoryHotAddEnabled
//
//if len(config.StorageConfig.Storage) > 0 {
// ds, err := vm.vm.Device(vm.driver.ctx)
// if err != nil {
// return err
// }
// vd := ds.SelectByType((*types.VirtualDisk)(nil))
// vc := ds.SelectByType((*types.VirtualController)(nil))
//
// // Use existing devices to avoid wrong configuration
// devices := object.VirtualDeviceList{}
// devices = append(devices, vd...)
// devices = append(devices, vc...)
//
// newDevices := object.VirtualDeviceList{}
//
// // Adds new controllers
// var controllers []types.BaseVirtualController
// for _, controllerType := range config.StorageConfig.DiskControllerType {
// var device types.BaseVirtualDevice
// var err error
// if controllerType == "nvme" {
// device, err = devices.CreateNVMEController()
// } else {
// device, err = devices.CreateSCSIController(controllerType)
// }
// if err != nil {
// return err
// }
// devices = append(devices, device)
// newDevices = append(newDevices, device)
// name := devices.Name(device)
// log.Printf("MOSS controller name %s", name)
// controller, err := devices.FindDiskController(name)
// if err != nil {
// return err
// }
// controllers = append(controllers, controller)
// }
//
// for _, dc := range config.StorageConfig.Storage {
// key := devices.NewKey()
// disk := &types.VirtualDisk{
// VirtualDevice: types.VirtualDevice{
// Key: key,
// Backing: &types.VirtualDiskFlatVer2BackingInfo{
// DiskMode: string(types.VirtualDiskModePersistent),
// ThinProvisioned: types.NewBool(dc.DiskThinProvisioned),
// EagerlyScrub: types.NewBool(dc.DiskEagerlyScrub),
// },
// },
// CapacityInKB: dc.DiskSize * 1024,
// }
//
// log.Printf("MOSS device key %d", key)
//
// devices.AssignController(disk, controllers[dc.ControllerIndex])
// newDevices = append(newDevices, disk)
// }
// //devices, err = config.StorageConfig.AddStorageDevices(devices)
// //if err != nil {
// // return err
// //}
//
// devicesConfigSpec, err := newDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
// if err != nil {
// return err
// }
// confSpec.DeviceChange = append(confSpec.DeviceChange, devicesConfigSpec...)
//}
if config.VideoRAM != 0 {
devices, err := vm.vm.Device(vm.driver.ctx)

View File

@ -17,9 +17,18 @@ import (
func TestCreateConfig_Prepare(t *testing.T) {
// Empty config - check defaults
config := &CreateConfig{}
config := &CreateConfig{
// Storage is required
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
}
if errs := config.Prepare(); len(errs) != 0 {
t.Fatalf("Config preprare should not fail")
t.Fatalf("Config preprare should not fail: %s", errs[0])
}
if config.GuestOSType != "otherGuest" {
t.Fatalf("GuestOSType should default to 'otherGuest'")
@ -69,6 +78,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate 'usb' and 'xhci' can be set together",
config: &CreateConfig{
USBController: []string{"usb", "xhci"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: false,
},
@ -76,6 +92,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate '1' and '0' can be set together",
config: &CreateConfig{
USBController: []string{"1", "0"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: false,
},
@ -83,6 +106,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate 'true' and 'false' can be set together",
config: &CreateConfig{
USBController: []string{"true", "false"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: false,
},
@ -90,6 +120,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate 'true' and 'usb' cannot be set together",
config: &CreateConfig{
USBController: []string{"true", "usb"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: true,
expectedErrMsg: "there can only be one usb controller and one xhci controller",
@ -98,6 +135,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate '1' and 'usb' cannot be set together",
config: &CreateConfig{
USBController: []string{"1", "usb"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: true,
expectedErrMsg: "there can only be one usb controller and one xhci controller",
@ -106,6 +150,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate 'xhci' cannot be set more that once",
config: &CreateConfig{
USBController: []string{"xhci", "xhci"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: true,
expectedErrMsg: "there can only be one usb controller and one xhci controller",
@ -114,6 +165,13 @@ func TestCreateConfig_Prepare(t *testing.T) {
name: "USBController validate unknown value cannot be set",
config: &CreateConfig{
USBController: []string{"unknown"},
StorageConfig: common.StorageConfig{
Storage: []common.DiskConfig{
{
DiskSize: 32768,
},
},
},
},
fail: true,
expectedErrMsg: "usb_controller[0] references an unknown usb controller",
@ -131,7 +189,7 @@ func TestCreateConfig_Prepare(t *testing.T) {
}
} else {
if len(errs) != 0 {
t.Fatalf("Config preprare should not fail")
t.Fatalf("Config preprare should not fail: %s", errs[0])
}
}
}

View File

@ -1,7 +0,0 @@
<!-- Code generated from the comments of the DiskConfig struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->
- `disk_thin_provisioned` (bool) - Enable VMDK thin provisioning for VM. Defaults to `false`.
- `disk_eagerly_scrub` (bool) - Enable VMDK eager scrubbing for VM. Defaults to `false`.
- `disk_controller_index` (int) - The assigned disk controller. Defaults to the first one (0)

View File

@ -1,3 +0,0 @@
<!-- Code generated from the comments of the DiskConfig struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->
- `disk_size` (int64) - The size of the disk in MB.

View File

@ -1,74 +0,0 @@
<!-- Code generated from the comments of the DiskConfig struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->
Defines the disk storage for a VM.
Example that will create a 15GB and a 20GB disk on the VM. The second disk will be thin provisioned:
In JSON:
```json
"storage": [
{
"disk_size": 15000
},
{
"disk_size": 20000,
"disk_thin_provisioned": true
}
],
```
In HCL2:
```hcl
storage {
disk_size = 15000
}
storage {
disk_size = 20000
disk_thin_provisioned = true
}
```
Example that creates 2 pvscsi controllers and adds 2 disks to each one:
In JSON:
```json
"disk_controller_type": ["pvscsi", "pvscsi"],
"storage": [
{
"disk_size": 15000,
"disk_controller_index": 0
},
{
"disk_size": 15000,
"disk_controller_index": 0
},
{
"disk_size": 15000,
"disk_controller_index": 1
},
{
"disk_size": 15000,
"disk_controller_index": 1
}
],
```
In HCL2:
```hcl
disk_controller_type = ["pvscsi", "pvscsi"]
storage {
disk_size = 15000,
disk_controller_index = 0
}
storage {
disk_size = 15000
disk_controller_index = 0
}
storage {
disk_size = 15000
disk_controller_index = 1
}
storage {
disk_size = 15000
disk_controller_index = 1
}
```