github.com/vmware/govmomi@v0.51.0/simulator/host_datastore_system_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package simulator 6 7 import ( 8 "context" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/vmware/govmomi" 14 "github.com/vmware/govmomi/object" 15 "github.com/vmware/govmomi/simulator/esx" 16 "github.com/vmware/govmomi/vim25/types" 17 ) 18 19 func TestHostDatastoreSystem(t *testing.T) { 20 s := New(NewServiceInstance(NewContext(), esx.ServiceContent, esx.RootFolder)) 21 22 ts := s.NewServer() 23 defer ts.Close() 24 25 ctx := context.Background() 26 27 c, err := govmomi.NewClient(ctx, ts.URL, true) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 host := object.NewHostSystem(c.Client, esx.HostSystem.Reference()) 33 34 dss, err := host.ConfigManager().DatastoreSystem(ctx) 35 if err != nil { 36 t.Error(err) 37 } 38 39 pwd, err := os.Getwd() 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 spec := types.HostNasVolumeSpec{ 45 Type: string(types.HostFileSystemVolumeFileSystemTypeNFS), 46 RemoteHost: "localhost", 47 } 48 49 tests := []func(string) (*object.Datastore, error){ 50 func(dir string) (*object.Datastore, error) { 51 spec.LocalPath = dir 52 spec.RemotePath = dir 53 return dss.CreateNasDatastore(ctx, spec) 54 }, 55 func(dir string) (*object.Datastore, error) { 56 return dss.CreateLocalDatastore(ctx, filepath.Base(dir), dir) 57 }, 58 } 59 60 for _, create := range tests { 61 for _, fail := range []bool{false, true} { 62 if fail { 63 _, err = create(pwd) 64 if err == nil { 65 t.Error("expected error") 66 } 67 68 // TODO: hds.Remove(ds) 69 pwd = filepath.Join(pwd, "esx") 70 } else { 71 _, err = create(pwd) 72 if err != nil { 73 t.Error(err) 74 } 75 } 76 } 77 } 78 79 for _, create := range tests { 80 for _, dir := range []string{"./enoent", "host_datastore_system.go"} { 81 _, err = create(dir) 82 83 if err == nil { 84 t.Error("expected error") 85 } 86 } 87 } 88 } 89 90 func TestCreateNasDatastoreValidation(t *testing.T) { 91 s := New(NewServiceInstance(NewContext(), esx.ServiceContent, esx.RootFolder)) 92 93 ts := s.NewServer() 94 defer ts.Close() 95 96 ctx := context.Background() 97 98 c, err := govmomi.NewClient(ctx, ts.URL, true) 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 host := object.NewHostSystem(c.Client, esx.HostSystem.Reference()) 104 105 dss, err := host.ConfigManager().DatastoreSystem(ctx) 106 if err != nil { 107 t.Error(err) 108 } 109 110 pwd, err := os.Getwd() 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 tests := []struct { 116 name string 117 spec types.HostNasVolumeSpec 118 }{ 119 { 120 "RemotePath is not specified", 121 types.HostNasVolumeSpec{ 122 Type: string(types.HostFileSystemVolumeFileSystemTypeNFS), 123 LocalPath: pwd, 124 RemoteHost: "localhost", 125 }, 126 }, 127 { 128 "RemoteHost is not specified", 129 types.HostNasVolumeSpec{ 130 Type: string(types.HostFileSystemVolumeFileSystemTypeNFS), 131 LocalPath: pwd, 132 RemotePath: pwd, 133 }, 134 }, 135 } 136 137 for _, tt := range tests { 138 t.Run(tt.name, func(t *testing.T) { 139 _, err := dss.CreateNasDatastore(ctx, tt.spec) 140 141 if err == nil { 142 t.Error("expected error") 143 } 144 }) 145 } 146 }