Some Go style suggestions for client.go and client_test.go

This commit is contained in:
Satnam Singh 2014-09-22 16:25:06 -07:00
parent 7663955763
commit 6daa23d9a3
2 changed files with 19 additions and 13 deletions

View File

@ -25,29 +25,33 @@ import (
"github.com/google/cadvisor/info"
)
// Client represents the base URL for a cAdvisor client.
type Client struct {
baseUrl string
}
// NewClient returns a new client with the specified base URL.
// TODO(cAdvisor): Currently the error result is always nil.
func NewClient(URL string) (*Client, error) {
c := &Client{
return &Client{
baseUrl: strings.Join([]string{
URL,
"api/v1.0",
}, "/"),
}
return c, nil
}, nil
}
func (self *Client) machineInfoUrl() string {
return strings.Join([]string{self.baseUrl, "machine"}, "/")
}
// MachineInfo returns the JSON machine information for this client.
// A non-nil error result indicates a problem with obtaining
// the JSON machine information data.
func (self *Client) MachineInfo() (minfo *info.MachineInfo, err error) {
u := self.machineInfoUrl()
ret := new(info.MachineInfo)
err = self.httpGetJsonData(ret, nil, u, "machine info")
if err != nil {
if err = self.httpGetJsonData(ret, nil, u, "machine info"); err != nil {
return
}
minfo = ret
@ -84,21 +88,20 @@ func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName st
err = fmt.Errorf("unable to read all %v: %v", infoName, err)
return err
}
err = json.Unmarshal(body, data)
if err != nil {
if err = json.Unmarshal(body, data); err != nil {
err = fmt.Errorf("unable to unmarshal %v (%v): %v", infoName, string(body), err)
return err
}
return nil
}
func (self *Client) ContainerInfo(
name string,
// ContainerInfo returns the JSON container information for the specified
// container and request.
func (self *Client) ContainerInfo(name string,
query *info.ContainerInfoRequest) (cinfo *info.ContainerInfo, err error) {
u := self.containerInfoUrl(name)
ret := new(info.ContainerInfo)
err = self.httpGetJsonData(ret, query, u, fmt.Sprintf("container info for %v", name))
if err != nil {
if err = self.httpGetJsonData(ret, query, u, fmt.Sprintf("container info for %v", name)); err != nil {
return
}
cinfo = ret

View File

@ -47,8 +47,7 @@ func cadvisorTestClient(path string, expectedPostObj, expectedPostObjEmpty, repl
if r.URL.Path == path {
if expectedPostObj != nil {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(expectedPostObjEmpty)
if err != nil {
if err := decoder.Decode(expectedPostObjEmpty); err != nil {
t.Errorf("Received invalid object: %v", err)
}
if !reflect.DeepEqual(expectedPostObj, expectedPostObjEmpty) {
@ -72,6 +71,8 @@ func cadvisorTestClient(path string, expectedPostObj, expectedPostObjEmpty, repl
return client, ts, err
}
// TestGetMachineInfo peforms one test to check if MachineInfo()
// in a cAdvisor cliernt returns the correct result.
func TestGetMachineinfo(t *testing.T) {
minfo := &info.MachineInfo{
NumCores: 8,
@ -91,6 +92,8 @@ func TestGetMachineinfo(t *testing.T) {
}
}
// TestGetContainerInfo generates a random container information object
// and then checks that ContainerInfo returns the expected result.
func TestGetContainerInfo(t *testing.T) {
query := &info.ContainerInfoRequest{
NumStats: 3,