func TestMain(t *testing.T) { ctx := context.Background() for i, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { t.Logf("%d", i) defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } s := model.Service.NewServer() defer s.Close() s.URL.User = url.UserPassword("user", "pass") t.Logf("server URL: %s", s.URL) var sess *session.Session if i == 0 { sess, err = getESXSession(ctx, s.URL.String()) } else { sess, err = getVPXSession(ctx, s.URL.String()) } if err != nil { t.Fatal(err) } defer sess.Logout(ctx) testGetChildrenVMs(ctx, sess, t) testGetChildVM(ctx, sess, t) testFindResourcePool(ctx, sess, t) testGetCluster(ctx, sess, t) } }
func TestMain(t *testing.T) { log.SetLevel(log.DebugLevel) ctx := context.Background() for i, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { t.Logf("%d", i) defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } s := model.Service.NewServer() defer s.Close() s.URL.User = url.UserPassword("user", "pass") s.URL.Path = "" t.Logf("server URL: %s", s.URL) var input *data.Data if i == 0 { input = getESXData(s.URL) } else { input = getVPXData(s.URL) } if err != nil { t.Fatal(err) } installSettings := &data.InstallerData{} installSettings.ApplianceSize.CPU.Limit = 1 installSettings.ApplianceSize.Memory.Limit = 1024 validator, err := validate.NewValidator(ctx, input) if err != nil { t.Fatalf("Failed to validator: %s", err) } validator.DisableFirewallCheck = true validator.DisableDRSCheck = true conf, err := validator.Validate(ctx, input) if err != nil { log.Errorf("Failed to validate conf: %s", err) validator.ListIssues() } // FIXME: get host NetworkSystem failed in simulator // testCreateNetwork(ctx, validator.Session, conf, t) testCreateVolumeStores(ctx, validator.Session, conf, false, t) testDeleteVolumeStores(ctx, validator.Session, conf, 1, t) errConf := &config.VirtualContainerHostConfigSpec{} *errConf = *conf errConf.VolumeLocations = make(map[string]*url.URL) errConf.VolumeLocations["volume-store"], _ = url.Parse("ds://store_not_exist/volumes/test") testCreateVolumeStores(ctx, validator.Session, errConf, true, t) testCreateAppliance(ctx, validator.Session, conf, installSettings, false, t) } }
func TestWaitForKeyInExtraConfig(t *testing.T) { ctx := context.Background() m := simulator.ESX() defer m.Remove() err := m.Create() if err != nil { t.Fatal(err) } server := m.Service.NewServer() defer server.Close() config := &session.Config{ Service: server.URL.String(), } s, err := session.NewSession(config).Connect(ctx) if err != nil { t.Fatal(err) } if s, err = s.Populate(ctx); err != nil { t.Fatal(err) } vms, err := s.Finder.VirtualMachineList(ctx, "*") if err != nil { t.Fatal(err) } vm := NewVirtualMachineFromVM(ctx, s, vms[0]) opt := &types.OptionValue{Key: "foo", Value: "bar"} obj := simulator.Map.Get(vm.Reference()).(*simulator.VirtualMachine) obj.Config.ExtraConfig = append(obj.Config.ExtraConfig, opt) val, err := vm.WaitForKeyInExtraConfig(ctx, opt.Key) if err == nil { t.Error("expected error") } obj.Summary.Runtime.PowerState = types.VirtualMachinePowerStatePoweredOn val, err = vm.WaitForKeyInExtraConfig(ctx, opt.Key) if err != nil { t.Fatal(err) } if val != opt.Value { t.Errorf("%s != %s", val, opt.Value) } }
func TestMain(t *testing.T) { log.SetLevel(log.DebugLevel) trace.Logger.Level = log.DebugLevel ctx := context.Background() for i, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { t.Logf("%d", i) model.Datastore = 3 defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } s := model.Service.NewServer() defer s.Close() s.URL.User = url.UserPassword("user", "pass") s.URL.Path = "" t.Logf("server URL: %s", s.URL) var input *data.Data if i == 0 { input = getESXData(s.URL) } else { input = getVPXData(s.URL) } if err != nil { t.Fatal(err) } validator, err := NewValidator(ctx, input) if err != nil { t.Errorf("Failed to new validator: %s", err) } ds, _ := validator.Session.Finder.Datastore(validator.Context, "LocalDS_1") simulator.Map.Get(ds.Reference()).(mo.Entity).Entity().Name = "Local DS_0" ds, _ = validator.Session.Finder.Datastore(validator.Context, "LocalDS_2") simulator.Map.Get(ds.Reference()).(mo.Entity).Entity().Name = `😗` validator.DisableFirewallCheck = true validator.DisableDRSCheck = true t.Logf("session pool: %s", validator.Session.Pool) if err = createPool(ctx, validator.Session, input.ComputeResourcePath, "validator", t); err != nil { t.Errorf("Unable to create resource pool: %s", err) } conf := testCompute(validator, input, t) testTargets(validator, input, conf, t) testStorage(validator, input, conf, t) // testNetwork() need dvs support } }
func TestConnect(t *testing.T) { ctx := context.Background() for _, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } model.Service.TLS = new(tls.Config) s := model.Service.NewServer() defer s.Close() config := &Config{ Keepalive: time.Minute, Service: s.URL.String(), } for _, thumbprint := range []string{"", s.CertificateInfo().ThumbprintSHA1} { u := *s.URL config.Service = u.String() config.Thumbprint = thumbprint _, err = NewSession(config).Connect(ctx) if thumbprint == "" { if err == nil { t.Error("expected x509.UnknownAuthorityError error") } } else { if err != nil { t.Error(err) } } u.User = nil config.Service = u.String() _, err = NewSession(config).Connect(ctx) if err == nil { t.Fatal("expected login error") } config.Service = "" _, err = NewSession(config).Connect(ctx) if err == nil { t.Fatal("expected URL parse error") } } } }
func TestFinder(t *testing.T) { log.SetLevel(log.DebugLevel) trace.Logger.Level = log.DebugLevel ctx := context.Background() for i, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { t.Logf("%d", i) defer model.Remove() if i == 1 { model.Datacenter = 2 model.Cluster = 2 model.Host = 2 model.Pool = 0 } err := model.Create() if err != nil { t.Fatal(err) } s := model.Service.NewServer() defer s.Close() s.URL.User = url.UserPassword("user", "pass") s.URL.Path = "" t.Logf("server URL: %s", s.URL) var input *data.Data if i == 0 { input = getESXData(s.URL) } else { input = getVPXData(s.URL) } if err != nil { t.Fatal(err) } validator, err := validate.CreateNoDCCheck(ctx, input) if err != nil { t.Errorf("Failed to create validator: %s", err) } prefix := fmt.Sprintf("p%d-", i) if err = createTestData(ctx, validator.Session, prefix); err != nil { t.Errorf("Failed to create test data: %s", err) } // FIXME: ServerFaultCode: no such object: SearchIndex:ha-searchindex // testSearchVCHs(t, validator) } }
func main() { model := simulator.VPX() flag.IntVar(&model.Datacenter, "dc", model.Datacenter, "Number of datacenters") flag.IntVar(&model.Cluster, "cluster", model.Cluster, "Number of clusters") flag.IntVar(&model.ClusterHost, "host", model.ClusterHost, "Number of hosts per cluster") flag.IntVar(&model.Host, "standalone-host", model.Host, "Number of standalone hosts") flag.IntVar(&model.Datastore, "ds", model.Datastore, "Number of local datastores") flag.IntVar(&model.Machine, "vm", model.Machine, "Number of virtual machines per resource pool") flag.IntVar(&model.Pool, "pool", model.Pool, "Number of resource pools per compute resource") isESX := flag.Bool("esx", false, "Simulate standalone ESX") flag.Parse() f := flag.Lookup("httptest.serve") if f.Value.String() == "" { f.Value.Set("localhost:8989") } if *isESX { opts := model model = simulator.ESX() // Preserve options that also apply to ESX model.Datastore = opts.Datastore model.Machine = opts.Machine } tag := " (govmomi simulator)" model.ServiceContent.About.Name += tag model.ServiceContent.About.OsType = runtime.GOOS + "-" + runtime.GOARCH esx.HostSystem.Summary.Hardware.Vendor += tag defer model.Remove() err := model.Create() if err != nil { log.Fatal(err) } model.Service.NewServer() }
func main() { model := simulator.VPX() flag.IntVar(&model.Datacenter, "dc", model.Datacenter, "Number of datacenters") flag.IntVar(&model.Cluster, "cluster", model.Cluster, "Number of clusters") flag.IntVar(&model.ClusterHost, "host", model.ClusterHost, "Number of hosts per cluster") flag.IntVar(&model.Host, "standalone-host", model.Host, "Number of standalone hosts") flag.IntVar(&model.Datastore, "ds", model.Datastore, "Number of local datastores") flag.IntVar(&model.Machine, "vm", model.Machine, "Number of virtual machines per resource pool") flag.IntVar(&model.Pool, "pool", model.Pool, "Number of resource pools per compute resource") isESX := flag.Bool("esx", false, "Simulate standalone ESX") isTLS := flag.Bool("tls", false, "Enable TLS") cert := flag.String("tlscert", "", "Path to TLS certificate file") key := flag.String("tlskey", "", "Path to TLS key file") flag.Parse() f := flag.Lookup("httptest.serve") if f.Value.String() == "" { _ = f.Value.Set("127.0.0.1:8989") } if *isESX { opts := model model = simulator.ESX() // Preserve options that also apply to ESX model.Datastore = opts.Datastore model.Machine = opts.Machine } tag := " (govmomi simulator)" model.ServiceContent.About.Name += tag model.ServiceContent.About.OsType = runtime.GOOS + "-" + runtime.GOARCH esx.HostSystem.Summary.Hardware.Vendor += tag defer model.Remove() err := model.Create() if err != nil { log.Fatal(err) } if *isTLS { model.Service.TLS = new(tls.Config) if *cert != "" { c, err := tls.LoadX509KeyPair(*cert, *key) if err != nil { log.Fatal(err) } model.Service.TLS.Certificates = []tls.Certificate{c} } } s := model.Service.NewServer() fmt.Printf("GOVC_URL=%s", s.URL) select {} }
// TestSoapFaults covers the various soap fault checking paths func TestSoapFaults(t *testing.T) { ctx := context.Background() // Nothing VC specific in this test, so we use the simpler ESX model model := simulator.ESX() defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } server := model.Service.NewServer() defer server.Close() client, err := govmomi.NewClient(ctx, server.URL, true) if err != nil { t.Fatal(err) } // Any VM will do finder := find.NewFinder(client.Client, false) vm, err := finder.VirtualMachine(ctx, "/ha-datacenter/vm/*_VM0") if err != nil { t.Fatal(err) } // Test the success path err = Wait(ctx, func(ctx context.Context) (Task, error) { return vm.PowerOn(ctx) }) if err != nil { t.Fatal(err) } // Wrap existing vm MO with faultyVirtualMachine ref := simulator.Map.Get(vm.Reference()) fvm := &faultyVirtualMachine{*ref.(*simulator.VirtualMachine), nil} simulator.Map.Put(fvm) // Inject TaskInProgress fault fvm.fault = new(types.TaskInProgress) task, err := vm.PowerOff(ctx) if err != nil { t.Fatal(err) } // Test the task.Error path res, err := task.WaitForResult(ctx, nil) if !isTaskInProgress(err) { t.Error(err) } // Test the soap.IsVimFault() path if !isTaskInProgress(soap.WrapVimFault(res.Error.Fault)) { t.Errorf("fault=%#v", res.Error.Fault) } // Test the soap.IsSoapFault() path err = vm.MarkAsTemplate(ctx) if !isTaskInProgress(err) { t.Error(err) } // Test a fault other than TaskInProgress fvm.fault = &types.QuestionPending{ Text: "now why would you want to do such a thing?", } err = Wait(ctx, func(ctx context.Context) (Task, error) { return vm.PowerOff(ctx) }) if err == nil { t.Error("expected error") } if isTaskInProgress(err) { t.Error(err) } // Test with retry fvm.fault = new(types.TaskInProgress) called := 0 err = Wait(ctx, func(ctx context.Context) (Task, error) { called++ if called > 1 { simulator.Map.Put(ref) // remove fault injection } return vm.PowerOff(ctx) }) if err != nil { t.Error(err) } if called != 2 { t.Errorf("called=%d", called) } }
func TestDelete(t *testing.T) { log.SetLevel(log.DebugLevel) trace.Logger.Level = log.DebugLevel ctx := context.Background() for i, model := range []*simulator.Model{simulator.ESX(), simulator.VPX()} { t.Logf("%d", i) defer model.Remove() err := model.Create() if err != nil { t.Fatal(err) } s := model.Service.NewServer() defer s.Close() s.URL.User = url.UserPassword("user", "pass") s.URL.Path = "" t.Logf("server URL: %s", s.URL) var input *data.Data if i == 0 { input = getESXData(s.URL) } else { input = getVPXData(s.URL) } if err != nil { t.Fatal(err) } installSettings := &data.InstallerData{} installSettings.ApplianceSize.CPU.Limit = 1 installSettings.ApplianceSize.Memory.Limit = 1024 installSettings.ResourcePoolPath = path.Join(input.ComputeResourcePath, input.DisplayName) validator, err := validate.NewValidator(ctx, input) if err != nil { t.Errorf("Failed to validator: %s", err) } validator.DisableFirewallCheck = true validator.DisableDRSCheck = true conf, err := validator.Validate(ctx, input) if err != nil { log.Errorf("Failed to validate conf: %s", err) validator.ListIssues() } createAppliance(ctx, validator.Session, conf, installSettings, false, t) // FIXME: cannot check if it's VCH or not // testNewVCHFromCompute(input.ComputeResourcePath, input.DisplayName, validator, t) // testDeleteVCH(validator, conf, t) testDeleteDatastoreFiles(validator, t) } }