github.com/google/cadvisor@v0.49.1/client/v2/client_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package v2 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net/http" 21 "net/http/httptest" 22 "reflect" 23 "strings" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 29 v1 "github.com/google/cadvisor/info/v1" 30 v2 "github.com/google/cadvisor/info/v2" 31 ) 32 33 func cadvisorTestClient(path string, expectedPostObj *v1.ContainerInfoRequest, replyObj interface{}, t *testing.T) (*Client, *httptest.Server, error) { 34 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 35 if r.URL.Path == path { 36 if expectedPostObj != nil { 37 expectedPostObjEmpty := new(v1.ContainerInfoRequest) 38 decoder := json.NewDecoder(r.Body) 39 if err := decoder.Decode(expectedPostObjEmpty); err != nil { 40 t.Errorf("Received invalid object: %v", err) 41 } 42 if expectedPostObj.NumStats != expectedPostObjEmpty.NumStats || 43 expectedPostObj.Start.Unix() != expectedPostObjEmpty.Start.Unix() || 44 expectedPostObj.End.Unix() != expectedPostObjEmpty.End.Unix() { 45 t.Errorf("Received unexpected object: %+v, expected: %+v", expectedPostObjEmpty, expectedPostObj) 46 } 47 } 48 encoder := json.NewEncoder(w) 49 err := encoder.Encode(replyObj) 50 assert.NoError(t, err) 51 } else if r.URL.Path == "/api/v2.1/version" { 52 fmt.Fprintf(w, "0.1.2") 53 } else { 54 w.WriteHeader(http.StatusNotFound) 55 fmt.Fprintf(w, "Page not found.") 56 } 57 })) 58 client, err := NewClient(ts.URL) 59 if err != nil { 60 ts.Close() 61 return nil, nil, err 62 } 63 return client, ts, err 64 } 65 66 // TestGetMachineInfo performs one test to check if MachineInfo() 67 // in a cAdvisor client returns the correct result. 68 func TestGetMachineInfo(t *testing.T) { 69 mv1 := &v1.MachineInfo{ 70 NumCores: 8, 71 MemoryCapacity: 31625871360, 72 DiskMap: map[string]v1.DiskInfo{ 73 "8:0": { 74 Name: "sda", 75 Major: 8, 76 Minor: 0, 77 Size: 10737418240, 78 }, 79 }, 80 } 81 client, server, err := cadvisorTestClient("/api/v2.1/machine", nil, mv1, t) 82 if err != nil { 83 t.Fatalf("unable to get a client %v", err) 84 } 85 defer server.Close() 86 returned, err := client.MachineInfo() 87 if err != nil { 88 t.Fatal(err) 89 } 90 if !reflect.DeepEqual(returned, mv1) { 91 t.Fatalf("received unexpected machine v1") 92 } 93 } 94 95 // TestGetVersionV1 performs one test to check if VersionV1() 96 // in a cAdvisor client returns the correct result. 97 func TestGetVersionv1(t *testing.T) { 98 version := "0.1.2" 99 client, server, err := cadvisorTestClient("", nil, version, t) 100 if err != nil { 101 t.Fatalf("unable to get a client %v", err) 102 } 103 defer server.Close() 104 returned, err := client.VersionInfo() 105 if err != nil { 106 t.Fatal(err) 107 } 108 if returned != version { 109 t.Fatalf("received unexpected version v1") 110 } 111 } 112 113 // TestAttributes performs one test to check if Attributes() 114 // in a cAdvisor client returns the correct result. 115 func TestGetAttributes(t *testing.T) { 116 attr := &v2.Attributes{ 117 KernelVersion: "3.3.0", 118 ContainerOsVersion: "Ubuntu 14.4", 119 DockerVersion: "Docker 1.5", 120 CadvisorVersion: "0.1.2", 121 NumCores: 8, 122 MemoryCapacity: 31625871360, 123 DiskMap: map[string]v1.DiskInfo{ 124 "8:0": { 125 Name: "sda", 126 Major: 8, 127 Minor: 0, 128 Size: 10737418240, 129 }, 130 }, 131 } 132 client, server, err := cadvisorTestClient("/api/v2.1/attributes", nil, attr, t) 133 if err != nil { 134 t.Fatalf("unable to get a client %v", err) 135 } 136 defer server.Close() 137 returned, err := client.Attributes() 138 if err != nil { 139 t.Fatal(err) 140 } 141 if !reflect.DeepEqual(returned, attr) { 142 t.Fatalf("received unexpected attributes") 143 } 144 } 145 146 // TestMachineStats performs one test to check if MachineStats() 147 // in a cAdvisor client returns the correct result. 148 func TestMachineStats(t *testing.T) { 149 machineStats := []v2.MachineStats{ 150 { 151 Timestamp: time.Now(), 152 Cpu: &v1.CpuStats{ 153 Usage: v1.CpuUsage{ 154 Total: 100000, 155 }, 156 LoadAverage: 10, 157 }, 158 Filesystem: []v2.MachineFsStats{ 159 { 160 Device: "sda1", 161 }, 162 }, 163 }, 164 } 165 client, server, err := cadvisorTestClient("/api/v2.1/machinestats", nil, &machineStats, t) 166 if err != nil { 167 t.Fatalf("unable to get a client %v", err) 168 } 169 defer server.Close() 170 returned, err := client.MachineStats() 171 if err != nil { 172 t.Fatal(err) 173 } 174 assert.Len(t, returned, len(machineStats)) 175 if !reflect.DeepEqual(returned[0].Cpu, machineStats[0].Cpu) { 176 t.Fatalf("received unexpected machine stats\nExp: %+v\nAct: %+v", machineStats, returned) 177 } 178 if !reflect.DeepEqual(returned[0].Filesystem, machineStats[0].Filesystem) { 179 t.Fatalf("received unexpected machine stats\nExp: %+v\nAct: %+v", machineStats, returned) 180 } 181 } 182 183 func TestRequestFails(t *testing.T) { 184 errorText := "there was an error" 185 // Setup a server that simply fails. 186 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 187 http.Error(w, errorText, 500) 188 })) 189 client, err := NewClient(ts.URL) 190 if err != nil { 191 ts.Close() 192 t.Fatal(err) 193 } 194 defer ts.Close() 195 196 _, err = client.MachineInfo() 197 if err == nil { 198 t.Fatalf("Expected non-nil error") 199 } 200 expectedError := fmt.Sprintf("request failed with error: %q", errorText) 201 if strings.Contains(err.Error(), expectedError) { 202 t.Fatalf("Expected error %q but received %q", expectedError, err) 203 } 204 }