Skip to content

Commit 7ddf6eb

Browse files
committed
Merge pull request #1294 from timstclair/partial-failure
Fix nil-interface partialFailure bug
2 parents fb5fb83 + 81786ec commit 7ddf6eb

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

manager/manager.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (self *manager) GetDerivedStats(containerName string, options v2.RequestOpt
382382
}
383383
stats[name] = d
384384
}
385-
return stats, errs
385+
return stats, errs.OrNil()
386386
}
387387

388388
func (self *manager) GetContainerSpec(containerName string, options v2.RequestOptions) (map[string]v2.ContainerSpec, error) {
@@ -400,7 +400,7 @@ func (self *manager) GetContainerSpec(containerName string, options v2.RequestOp
400400
spec := self.getV2Spec(cinfo)
401401
specs[name] = spec
402402
}
403-
return specs, errs
403+
return specs, errs.OrNil()
404404
}
405405

406406
// Get V2 container spec from v1 container info.
@@ -461,7 +461,7 @@ func (self *manager) GetContainerInfoV2(containerName string, options v2.Request
461461
infos[name] = result
462462
}
463463

464-
return infos, errs
464+
return infos, errs.OrNil()
465465
}
466466

467467
func (self *manager) containerDataToContainerInfo(cont *containerData, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
@@ -614,7 +614,7 @@ func (self *manager) GetRequestedContainersInfo(containerName string, options v2
614614
}
615615
containersMap[name] = info
616616
}
617-
return containersMap, errs
617+
return containersMap, errs.OrNil()
618618
}
619619

620620
func (self *manager) getRequestedContainers(containerName string, options v2.RequestOptions) (map[string]*containerData, error) {
@@ -1241,3 +1241,10 @@ func (f *partialFailure) append(id, operation string, err error) {
12411241
func (f partialFailure) Error() string {
12421242
return fmt.Sprintf("partial failures: %s", strings.Join(f, ", "))
12431243
}
1244+
1245+
func (f partialFailure) OrNil() error {
1246+
if len(f) == 0 {
1247+
return nil
1248+
}
1249+
return f
1250+
}

manager/manager_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/google/cadvisor/info/v2"
3333
"github.com/google/cadvisor/utils/sysfs/fakesysfs"
3434
"github.com/stretchr/testify/assert"
35-
"github.com/stretchr/testify/require"
3635
)
3736

3837
// TODO(vmarmol): Refactor these tests.
@@ -174,7 +173,9 @@ func TestGetContainerInfoV2(t *testing.T) {
174173
m, _, handlerMap := expectManagerWithContainers(containers, query, t)
175174

176175
infos, err := m.GetContainerInfoV2("/", options)
177-
require.NoError(t, err, "Error calling GetContainerInfoV2")
176+
if err != nil {
177+
t.Fatalf("GetContainerInfoV2 failed: %v", err)
178+
}
178179

179180
for container, handler := range handlerMap {
180181
handler.AssertExpectations(t)
@@ -205,7 +206,10 @@ func TestGetContainerInfoV2Failure(t *testing.T) {
205206
m, _, handlerMap := expectManagerWithContainers(containers, query, t)
206207

207208
// Remove /c1 stats
208-
require.NoError(t, m.memoryCache.RemoveContainer(statless))
209+
err := m.memoryCache.RemoveContainer(statless)
210+
if err != nil {
211+
t.Fatalf("RemoveContainer failed: %v", err)
212+
}
209213

210214
// Make GetSpec fail on /c2
211215
mockErr := fmt.Errorf("intentional GetSpec failure")
@@ -216,7 +220,9 @@ func TestGetContainerInfoV2Failure(t *testing.T) {
216220
m.containers[namespacedContainerName{Name: failing}].lastUpdatedTime = time.Time{} // Force GetSpec.
217221

218222
infos, err := m.GetContainerInfoV2("/", options)
219-
assert.Error(t, err, "Expected error calling GetContainerInfoV2")
223+
if err == nil {
224+
t.Error("Expected error calling GetContainerInfoV2")
225+
}
220226

221227
// Successful containers still successful.
222228
info, ok := infos[successful]

0 commit comments

Comments
 (0)