func TestKeepAliveHandler(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } m1, u1 := newManager(t) m2, u2 := newManager(t) reauth := make(chan bool) // Keep alive handler that will re-login. // Real-world case: connectivity to ESX/VC is down long enough for the session to expire // Test-world case: we call TerminateSession below k := KeepAliveHandler(m2.client.RoundTripper, 2*time.Second, func(roundTripper soap.RoundTripper) error { _, err := methods.GetCurrentTime(context.Background(), roundTripper) if err != nil { if isNotAuthenticated(err) { err = m2.Login(context.Background(), u2.User) if err != nil { if isInvalidLogin(err) { reauth <- false t.Log("failed to re-authenticate, quitting keep alive handler") return err } } else { reauth <- true } } } return nil }) m2.client.RoundTripper = k // Logging in starts keep alive if err := m1.Login(context.Background(), u1.User); err != nil { t.Error(err) } if err := m2.Login(context.Background(), u2.User); err != nil { t.Error(err) } // Terminate session for m2. Note that self terminate fails, so we need 2 sessions for this test. s, err := m2.UserSession(context.Background()) if err != nil { t.Fatal(err) } err = m1.TerminateSession(context.Background(), []string{s.Key}) if err != nil { t.Fatal(err) } _, err = methods.GetCurrentTime(context.Background(), m2.client) if err == nil { t.Error("expected to fail") } // Wait for keepalive to re-authenticate <-reauth _, err = methods.GetCurrentTime(context.Background(), m2.client) if err != nil { t.Fatal(err) } // Clear credentials to test re-authentication failure u2.User = nil s, err = m2.UserSession(context.Background()) if err != nil { t.Fatal(err) } err = m1.TerminateSession(context.Background(), []string{s.Key}) if err != nil { t.Fatal(err) } // Wait for keepalive re-authenticate attempt result := <-reauth _, err = methods.GetCurrentTime(context.Background(), m2.client) if err == nil { t.Error("expected to fail") } if result { t.Errorf("expected reauth to fail") } }
func TestSessionIsActive(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } session := sessionClient(u, t) // Skip test against ESXi -- SessionIsActive is not implemented if session.client.ServiceContent.About.ApiType != "VirtualCenter" { t.Skipf("Talking to %s instead of %s", session.client.ServiceContent.About.ApiType, "VirtualCenter") } err := session.Login(context.Background(), u.User) if err != nil { t.Error("Login Error: ", err) } active, err := session.SessionIsActive(context.Background()) if err != nil || !active { t.Errorf("Expected %t, got %t", true, active) t.Errorf("Expected nil, got %v", err) } session.Logout(context.Background()) active, err = session.SessionIsActive(context.Background()) if err == nil || active { t.Errorf("Expected %t, got %t", false, active) t.Errorf("Expected NotAuthenticated, got %v", err) } }
func TestNewClient(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } c, err := NewClient(context.Background(), u, true) if err != nil { t.Fatal(err) } f := func() error { var x mo.Folder err = mo.RetrieveProperties(context.Background(), c, c.ServiceContent.PropertyCollector, c.ServiceContent.RootFolder, &x) if err != nil { return err } if len(x.Name) == 0 { return errors.New("empty response") } return nil } // check cookie is valid with an sdk request if err := f(); err != nil { t.Fatal(err) } // check cookie is valid with a non-sdk request u.User = nil // turn off Basic auth u.Path = "/folder" r, err := c.Client.Get(u.String()) if err != nil { t.Fatal(err) } if r.StatusCode != http.StatusOK { t.Fatal(r) } // sdk request should fail w/o a valid cookie c.Client.Jar = nil if err := f(); err == nil { t.Fatal("should fail") } // invalid login u.Path = "/sdk" u.User = url.UserPassword("ENOENT", "EINVAL") _, err = NewClient(context.Background(), u, true) if err == nil { t.Fatal("should fail") } }
func TestLogin(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } session := sessionClient(u, t) err := session.Login(context.Background(), u.User) if err != nil { t.Errorf("Expected no error, got %v", err) } }
func TestInvalidSdk(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } // a URL other than a valid /sdk should error, not panic u.Path = "/mob" _, err := NewClient(context.Background(), u, true) if err == nil { t.Fatal("should fail") } }
func newManager(t *testing.T) (*Manager, *url.URL) { u := test.URL() if u == nil { t.SkipNow() } soapClient := soap.NewClient(u, true) vimClient, err := vim25.NewClient(context.Background(), soapClient) if err != nil { t.Fatal(err) } return NewManager(vimClient), u }
func TestPropertiesN(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } c, err := NewClient(context.Background(), u, true) if err != nil { t.Fatal(err) } var f mo.Folder err = c.RetrieveOne(context.Background(), c.ServiceContent.RootFolder, nil, &f) if err != nil { t.Fatal(err) } var dc mo.Datacenter err = c.RetrieveOne(context.Background(), f.ChildEntity[0], nil, &dc) if err != nil { t.Fatal(err) } var folderReferences = []types.ManagedObjectReference{ dc.DatastoreFolder, dc.HostFolder, dc.NetworkFolder, dc.VmFolder, } var folders []mo.Folder err = c.Retrieve(context.Background(), folderReferences, []string{"name"}, &folders) if err != nil { t.Fatal(err) } if len(folders) != len(folderReferences) { t.Fatalf("Expected %d, got %d", len(folderReferences), len(folders)) } }
func TestLogout(t *testing.T) { u := test.URL() if u == nil { t.SkipNow() } session := sessionClient(u, t) err := session.Login(context.Background(), u.User) if err != nil { t.Error("Login Error: ", err) } err = session.Logout(context.Background()) if err != nil { t.Errorf("Expected nil, got %v", err) } err = session.Logout(context.Background()) if err == nil { t.Errorf("Expected NotAuthenticated, got nil") } }