From f636e960f599dd4ddc3622bf3b1f7f82618488ac Mon Sep 17 00:00:00 2001 From: George Hicken Date: Mon, 7 Aug 2023 16:17:33 +0000 Subject: [PATCH] Address review comments I expect to squash this into an earlier commit once it passes tests vcsim: support container backing for hosts --- simulator/container_host_system.go | 25 ++++--------- simulator/container_virtual_machine_test.go | 14 ++++++-- simulator/esx/host_config_filesystemvolume.go | 35 +++++++------------ simulator/host_system.go | 2 +- 4 files changed, 33 insertions(+), 43 deletions(-) diff --git a/simulator/container_host_system.go b/simulator/container_host_system.go index f84f3d840..c3d283abb 100644 --- a/simulator/container_host_system.go +++ b/simulator/container_host_system.go @@ -20,22 +20,11 @@ import ( "fmt" "strings" + "github.com/vmware/govmomi/units" "github.com/vmware/govmomi/vim25/methods" "github.com/vmware/govmomi/vim25/types" ) -const KiB = 1024 -const MiB = 1024 * KiB -const GiB = 1024 * MiB -const TiB = 1024 * GiB -const Pib = 1024 * TiB - -const KB = 1000 -const MB = 1000 * KB -const GB = 1000 * MB -const TB = 1000 * GB -const PB = 1000 * TB - const ( advOptPrefixPnicToUnderlayPrefix = "RUN.underlay." advOptContainerBackingImage = "RUN.container" @@ -74,8 +63,8 @@ func createSimHostMounts(ctx *Context, containerName string, mounts []types.Host switch vol := info.Volume.(type) { case *types.HostVmfsVolume: vol.BlockSizeMb = 1 - vol.BlockSize = KiB - vol.UnmapGranularity = KiB + vol.BlockSize = units.KB + vol.UnmapGranularity = units.KB vol.UnmapPriority = "low" vol.MajorVersion = 6 vol.Version = "6.82" @@ -309,7 +298,7 @@ var defaultSimVolumes = []types.HostFileSystemMountInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "VMFS", Name: "datastore1", - Capacity: 1 * TiB, + Capacity: 1 * units.TB, }, Extent: []types.HostScsiDiskPartition{ { @@ -326,7 +315,7 @@ var defaultSimVolumes = []types.HostFileSystemMountInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "OSDATA-%__UUID__%", - Capacity: 128 * GiB, + Capacity: 128 * units.GB, }, Extent: []types.HostScsiDiskPartition{ { @@ -343,7 +332,7 @@ var defaultSimVolumes = []types.HostFileSystemMountInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "BOOTBANK1", - Capacity: 4 * GiB, + Capacity: 4 * units.GB, }, }, }, @@ -355,7 +344,7 @@ var defaultSimVolumes = []types.HostFileSystemMountInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "BOOTBANK2", - Capacity: 4 * GiB, + Capacity: 4 * units.GB, }, }, }, diff --git a/simulator/container_virtual_machine_test.go b/simulator/container_virtual_machine_test.go index af7419a77..de32bd375 100644 --- a/simulator/container_virtual_machine_test.go +++ b/simulator/container_virtual_machine_test.go @@ -27,6 +27,8 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" + "github.com/vmware/govmomi/find" "github.com/vmware/govmomi/object" "github.com/vmware/govmomi/vim25" @@ -39,14 +41,22 @@ import ( // port - the port to forward to the container port 80 func constructNginxBacking(t *testing.T, content string, port int) []types.BaseOptionValue { dir := t.TempDir() + // experience shows that a parent directory created as part of the TempDir call may not have + // o+rx, preventing use within a container that doesn't have the same uid for dirpart := dir; dirpart != "/"; dirpart = filepath.Dir(dirpart) { os.Chmod(dirpart, 0755) + stat, err := os.Stat(dirpart) + require.Nil(t, err, "must be able to check file and directory permissions") + require.NotZero(t, stat.Mode()&0005, "does not have o+rx permissions", dirpart) } fpath := filepath.Join(dir, "index.html") - os.WriteFile(fpath, []byte(content), 0644) + err := os.WriteFile(fpath, []byte(content), 0644) + require.Nil(t, err, "Expected to cleanly write content to file: %s", err) + // just in case umask gets in the way - os.Chmod(fpath, 0644) + err = os.Chmod(fpath, 0644) + require.Nil(t, err, "Expected to cleanly set file permissions on content: %s", err) args := fmt.Sprintf("-v '%s:/usr/share/nginx/html:ro' nginx", dir) diff --git a/simulator/esx/host_config_filesystemvolume.go b/simulator/esx/host_config_filesystemvolume.go index 578fc5f64..01c62d0a4 100644 --- a/simulator/esx/host_config_filesystemvolume.go +++ b/simulator/esx/host_config_filesystemvolume.go @@ -16,19 +16,10 @@ limitations under the License. package esx -import "github.com/vmware/govmomi/vim25/types" - -const KiB = 1024 -const MiB = 1024 * KiB -const GiB = 1024 * MiB -const TiB = 1024 * GiB -const Pib = 1024 * TiB - -const KB = 1000 -const MB = 1000 * KB -const GB = 1000 * MB -const TB = 1000 * GB -const PB = 1000 * TB +import ( + "github.com/vmware/govmomi/units" + "github.com/vmware/govmomi/vim25/types" +) // HostConfigInfo is the default template for the HostSystem config property. // Capture method: @@ -50,14 +41,14 @@ var HostFileSystemVolumeInfo = types.HostFileSystemVolumeInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "VMFS", Name: "datastore1", - Capacity: 3.5 * TiB, + Capacity: 3.5 * units.TB, }, BlockSizeMb: 1, - BlockSize: KiB, - UnmapGranularity: KiB, + BlockSize: units.KB, + UnmapGranularity: units.KB, UnmapPriority: "low", UnmapBandwidthSpec: (*types.VmfsUnmapBandwidthSpec)(nil), - MaxBlocks: 61 * MiB, + MaxBlocks: 61 * units.MB, MajorVersion: 6, Version: "6.82", Uuid: "deadbeef-01234567-89ab-cdef00000003", @@ -88,14 +79,14 @@ var HostFileSystemVolumeInfo = types.HostFileSystemVolumeInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "OSDATA-deadbeef-01234567-89ab-cdef00000002", - Capacity: 128 * GiB, + Capacity: 128 * units.GB, }, BlockSizeMb: 1, - BlockSize: KiB, + BlockSize: units.KB, UnmapGranularity: 0, UnmapPriority: "", UnmapBandwidthSpec: (*types.VmfsUnmapBandwidthSpec)(nil), - MaxBlocks: 256 * KiB, + MaxBlocks: 256 * units.KB, MajorVersion: 1, Version: "1.00", Uuid: "deadbeef-01234567-89ab-cdef00000002", @@ -126,7 +117,7 @@ var HostFileSystemVolumeInfo = types.HostFileSystemVolumeInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "BOOTBANK1", - Capacity: 4 * GiB, + Capacity: 4 * units.GB, }, }, VStorageSupport: "", @@ -144,7 +135,7 @@ var HostFileSystemVolumeInfo = types.HostFileSystemVolumeInfo{ HostFileSystemVolume: types.HostFileSystemVolume{ Type: "OTHER", Name: "BOOTBANK2", - Capacity: 4 * GiB, + Capacity: 4 * units.GB, }, }, VStorageSupport: "", diff --git a/simulator/host_system.go b/simulator/host_system.go index c52210948..f8cd3fe7c 100644 --- a/simulator/host_system.go +++ b/simulator/host_system.go @@ -163,7 +163,7 @@ func (h *HostSystem) configureContainerBacking(ctx *Context, image string, mount Value: image, } - advOpts := Map.Get(h.ConfigManager.AdvancedOption.Reference()).(*OptionManager) + advOpts := ctx.Map.Get(h.ConfigManager.AdvancedOption.Reference()).(*OptionManager) fault := advOpts.UpdateOptions(&types.UpdateOptions{ChangedValue: []types.BaseOptionValue{option}}).Fault() if fault != nil { panic(fault)