From 6daa23d9a3892a5281eea477e77d47b54c133817 Mon Sep 17 00:00:00 2001 From: Satnam Singh Date: Mon, 22 Sep 2014 16:25:06 -0700 Subject: [PATCH] Some Go style suggestions for client.go and client_test.go --- client/client.go | 25 ++++++++++++++----------- client/client_test.go | 7 +++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/client/client.go b/client/client.go index f49176e3..e47d1956 100644 --- a/client/client.go +++ b/client/client.go @@ -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 diff --git a/client/client_test.go b/client/client_test.go index eb6b7a45..5993f549 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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,