2016-10-24 22:55:29 +00:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerStatsContext(t *testing.T) {
|
|
|
|
containerID := stringid.GenerateRandomID()
|
|
|
|
|
|
|
|
var ctx containerStatsContext
|
|
|
|
tt := []struct {
|
|
|
|
stats StatsEntry
|
2017-02-05 16:55:30 +00:00
|
|
|
osType string
|
2016-10-24 22:55:29 +00:00
|
|
|
expValue string
|
|
|
|
expHeader string
|
|
|
|
call func() string
|
|
|
|
}{
|
2017-02-05 16:55:30 +00:00
|
|
|
{StatsEntry{Container: containerID}, "", containerID, containerHeader, ctx.Container},
|
|
|
|
{StatsEntry{CPUPercentage: 5.5}, "", "5.50%", cpuPercHeader, ctx.CPUPerc},
|
|
|
|
{StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, "", "--", cpuPercHeader, ctx.CPUPerc},
|
2017-02-07 20:58:56 +00:00
|
|
|
{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, "", "0.31B / 12.3B", netIOHeader, ctx.NetIO},
|
2017-02-05 16:55:30 +00:00
|
|
|
{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, "", "--", netIOHeader, ctx.NetIO},
|
2017-02-07 20:58:56 +00:00
|
|
|
{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, "", "0.1B / 2.3B", blockIOHeader, ctx.BlockIO},
|
2017-02-05 16:55:30 +00:00
|
|
|
{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, "", "--", blockIOHeader, ctx.BlockIO},
|
|
|
|
{StatsEntry{MemoryPercentage: 10.2}, "", "10.20%", memPercHeader, ctx.MemPerc},
|
|
|
|
{StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, "", "--", memPercHeader, ctx.MemPerc},
|
|
|
|
{StatsEntry{MemoryPercentage: 10.2}, "windows", "--", memPercHeader, ctx.MemPerc},
|
2017-02-07 20:58:56 +00:00
|
|
|
{StatsEntry{Memory: 24, MemoryLimit: 30}, "", "24B / 30B", memUseHeader, ctx.MemUsage},
|
2017-02-05 16:55:30 +00:00
|
|
|
{StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, "", "-- / --", memUseHeader, ctx.MemUsage},
|
2017-02-07 20:58:56 +00:00
|
|
|
{StatsEntry{Memory: 24, MemoryLimit: 30}, "windows", "24B", winMemUseHeader, ctx.MemUsage},
|
2017-02-05 16:55:30 +00:00
|
|
|
{StatsEntry{PidsCurrent: 10}, "", "10", pidsHeader, ctx.PIDs},
|
|
|
|
{StatsEntry{PidsCurrent: 10, IsInvalid: true}, "", "--", pidsHeader, ctx.PIDs},
|
|
|
|
{StatsEntry{PidsCurrent: 10}, "windows", "--", pidsHeader, ctx.PIDs},
|
2016-10-24 22:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, te := range tt {
|
2017-02-05 16:55:30 +00:00
|
|
|
ctx = containerStatsContext{s: te.stats, os: te.osType}
|
2016-10-24 22:55:29 +00:00
|
|
|
if v := te.call(); v != te.expValue {
|
|
|
|
t.Fatalf("Expected %q, got %q", te.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerStatsContextWrite(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Context{Format: "{{InvalidFunction}}"},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{nil}}"},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "table {{.MemUsage}}"},
|
|
|
|
`MEM USAGE / LIMIT
|
2017-02-07 20:58:56 +00:00
|
|
|
20B / 20B
|
2016-10-24 22:55:29 +00:00
|
|
|
-- / --
|
Fix panic of "docker stats --format {{.Name}} --all"
This commit fixes panic when execute stats command:
* use --format {{.Name}} with --all when there're exited containers.
* use --format {{.Name}} while stating exited container.
The root cause is when stating an exited container, the result from the
api didn't contain the Name and ID field, which will make format
process panic.
Panic log is like this:
```
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0xb20f80, 0xc420014110)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
text/template.errRecover(0xc4201773e8)
/usr/local/go/src/text/template/exec.go:140 +0x2ad
panic(0xb20f80, 0xc420014110)
/usr/local/go/src/runtime/panic.go:458 +0x243
github.com/docker/docker/cli/command/formatter.(*containerStatsContext).Name(0xc420430160,
0x0, 0x0)
/go/src/github.com/docker/docker/cli/command/formatter/stats.go:148
+0x86
reflect.Value.call(0xb9a3a0, 0xc420430160, 0x2213, 0xbe3657, 0x4,
0x11bc9f8, 0x0, 0x0, 0x4d75b3, 0x1198940, ...)
/usr/local/go/src/reflect/value.go:434 +0x5c8
reflect.Value.Call(0xb9a3a0, 0xc420430160, 0x2213, 0x11bc9f8, 0x0, 0x0,
0xc420424028, 0xb, 0xb)
/usr/local/go/src/reflect/value.go:302 +0xa4
text/template.(*state).evalCall(0xc420177368, 0xb9a3a0, 0xc420430160,
0x16, 0xb9a3a0, 0xc420430160, 0x2213, 0x1178fa0, 0xc4203ea330,
0xc4203de283, ...)
/usr/local/go/src/text/template/exec.go:658 +0x530
```
Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
2017-02-07 02:27:40 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{.Container}} {{.ID}} {{.Name}}"},
|
|
|
|
`container1 abcdef foo
|
|
|
|
container2 --
|
2016-10-24 22:55:29 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{.Container}} {{.CPUPerc}}"},
|
|
|
|
`container1 20.00%
|
|
|
|
container2 --
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, te := range tt {
|
|
|
|
stats := []StatsEntry{
|
|
|
|
{
|
2016-11-03 06:20:46 +00:00
|
|
|
Container: "container1",
|
Fix panic of "docker stats --format {{.Name}} --all"
This commit fixes panic when execute stats command:
* use --format {{.Name}} with --all when there're exited containers.
* use --format {{.Name}} while stating exited container.
The root cause is when stating an exited container, the result from the
api didn't contain the Name and ID field, which will make format
process panic.
Panic log is like this:
```
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0xb20f80, 0xc420014110)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
text/template.errRecover(0xc4201773e8)
/usr/local/go/src/text/template/exec.go:140 +0x2ad
panic(0xb20f80, 0xc420014110)
/usr/local/go/src/runtime/panic.go:458 +0x243
github.com/docker/docker/cli/command/formatter.(*containerStatsContext).Name(0xc420430160,
0x0, 0x0)
/go/src/github.com/docker/docker/cli/command/formatter/stats.go:148
+0x86
reflect.Value.call(0xb9a3a0, 0xc420430160, 0x2213, 0xbe3657, 0x4,
0x11bc9f8, 0x0, 0x0, 0x4d75b3, 0x1198940, ...)
/usr/local/go/src/reflect/value.go:434 +0x5c8
reflect.Value.Call(0xb9a3a0, 0xc420430160, 0x2213, 0x11bc9f8, 0x0, 0x0,
0xc420424028, 0xb, 0xb)
/usr/local/go/src/reflect/value.go:302 +0xa4
text/template.(*state).evalCall(0xc420177368, 0xb9a3a0, 0xc420430160,
0x16, 0xb9a3a0, 0xc420430160, 0x2213, 0x1178fa0, 0xc4203ea330,
0xc4203de283, ...)
/usr/local/go/src/text/template/exec.go:658 +0x530
```
Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
2017-02-07 02:27:40 +00:00
|
|
|
ID: "abcdef",
|
|
|
|
Name: "/foo",
|
2016-10-24 22:55:29 +00:00
|
|
|
CPUPercentage: 20,
|
|
|
|
Memory: 20,
|
|
|
|
MemoryLimit: 20,
|
|
|
|
MemoryPercentage: 20,
|
|
|
|
NetworkRx: 20,
|
|
|
|
NetworkTx: 20,
|
|
|
|
BlockRead: 20,
|
|
|
|
BlockWrite: 20,
|
|
|
|
PidsCurrent: 2,
|
|
|
|
IsInvalid: false,
|
|
|
|
},
|
|
|
|
{
|
2016-11-03 06:20:46 +00:00
|
|
|
Container: "container2",
|
2016-10-24 22:55:29 +00:00
|
|
|
CPUPercentage: 30,
|
|
|
|
Memory: 30,
|
|
|
|
MemoryLimit: 30,
|
|
|
|
MemoryPercentage: 30,
|
|
|
|
NetworkRx: 30,
|
|
|
|
NetworkTx: 30,
|
|
|
|
BlockRead: 30,
|
|
|
|
BlockWrite: 30,
|
|
|
|
PidsCurrent: 3,
|
|
|
|
IsInvalid: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
te.context.Output = &out
|
2017-02-05 16:55:30 +00:00
|
|
|
err := ContainerStatsWrite(te.context, stats, "linux")
|
2016-10-24 22:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, te.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), te.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerStatsContextWriteWindows(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Context{Format: "table {{.MemUsage}}"},
|
|
|
|
`PRIV WORKING SET
|
2017-02-07 20:58:56 +00:00
|
|
|
20B
|
2016-10-24 22:55:29 +00:00
|
|
|
-- / --
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{.Container}} {{.CPUPerc}}"},
|
|
|
|
`container1 20.00%
|
|
|
|
container2 --
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{.Container}} {{.MemPerc}} {{.PIDs}}"},
|
|
|
|
`container1 -- --
|
|
|
|
container2 -- --
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, te := range tt {
|
|
|
|
stats := []StatsEntry{
|
|
|
|
{
|
2016-11-03 06:20:46 +00:00
|
|
|
Container: "container1",
|
2016-10-24 22:55:29 +00:00
|
|
|
CPUPercentage: 20,
|
|
|
|
Memory: 20,
|
|
|
|
MemoryLimit: 20,
|
|
|
|
MemoryPercentage: 20,
|
|
|
|
NetworkRx: 20,
|
|
|
|
NetworkTx: 20,
|
|
|
|
BlockRead: 20,
|
|
|
|
BlockWrite: 20,
|
|
|
|
PidsCurrent: 2,
|
|
|
|
IsInvalid: false,
|
|
|
|
},
|
|
|
|
{
|
2016-11-03 06:20:46 +00:00
|
|
|
Container: "container2",
|
2016-10-24 22:55:29 +00:00
|
|
|
CPUPercentage: 30,
|
|
|
|
Memory: 30,
|
|
|
|
MemoryLimit: 30,
|
|
|
|
MemoryPercentage: 30,
|
|
|
|
NetworkRx: 30,
|
|
|
|
NetworkTx: 30,
|
|
|
|
BlockRead: 30,
|
|
|
|
BlockWrite: 30,
|
|
|
|
PidsCurrent: 3,
|
|
|
|
IsInvalid: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
te.context.Output = &out
|
2017-02-05 16:55:30 +00:00
|
|
|
err := ContainerStatsWrite(te.context, stats, "windows")
|
2016-10-24 22:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, te.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), te.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "{{.Container}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "table {{.Container}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"CONTAINER\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "table {{.Container}}\t{{.CPUPerc}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"CONTAINER CPU %\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
2017-02-05 16:55:30 +00:00
|
|
|
ContainerStatsWrite(context.context, []StatsEntry{}, "linux")
|
2016-10-24 22:55:29 +00:00
|
|
|
assert.Equal(t, context.expected, out.String())
|
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 16:55:30 +00:00
|
|
|
|
|
|
|
func TestContainerStatsContextWriteWithNoStatsWindows(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "{{.Container}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "table {{.Container}}\t{{.MemUsage}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"CONTAINER PRIV WORKING SET\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{
|
|
|
|
Format: "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}",
|
|
|
|
Output: &out,
|
|
|
|
},
|
|
|
|
"CONTAINER CPU % PRIV WORKING SET\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
|
|
|
ContainerStatsWrite(context.context, []StatsEntry{}, "windows")
|
|
|
|
assert.Equal(t, out.String(), context.expected)
|
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|