示例#1
0
文件: client_test.go 项目: vmware/vic
func sessionCheck(t *testing.T, c *Client) {
	var mgr mo.SessionManager

	err := mo.RetrieveProperties(context.Background(), c, c.ServiceContent.PropertyCollector, *c.ServiceContent.SessionManager, &mgr)
	if err != nil {
		t.Fatal(err)
	}
}
示例#2
0
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")
	}
}
示例#3
0
文件: manager.go 项目: EricYT/govmomi
// UserSession retrieves and returns the SessionManager's CurrentSession field.
// Nil is returned if the session is not authenticated.
func (sm *Manager) UserSession(ctx context.Context) (*types.UserSession, error) {
	var mgr mo.SessionManager

	err := mo.RetrieveProperties(ctx, sm.client, sm.client.ServiceContent.PropertyCollector, sm.Reference(), &mgr)
	if err != nil {
		// It's OK if we can't retrieve properties because we're not authenticated
		if f, ok := err.(types.HasFault); ok {
			switch f.Fault().(type) {
			case *types.NotAuthenticated:
				return nil, nil
			}
		}

		return nil, err
	}

	return mgr.CurrentSession, nil
}