-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add format option for pouch ps and integrate/unit test
Signed-off-by: ao hang <aohang111@gmail.com>
- Loading branch information
Showing
7 changed files
with
354 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package formatter | ||
|
||
import ( | ||
"github.com/alibaba/pouch/apis/types" | ||
"github.com/alibaba/pouch/pkg/utils" | ||
) | ||
|
||
// ContainerHeader is the map to show container head | ||
var ContainerHeader = map[string]string{ | ||
"Name": "Name", | ||
"ID": "ID", | ||
"Status": "Status", | ||
"Created": "Created", | ||
"Image": "Image", | ||
"Runtime": "Runtime", | ||
"Command": "Command", | ||
"ImageID": "ImageID", | ||
"Labels": "Labels", | ||
"Mounts": "Mounts", | ||
"State": "State", | ||
} | ||
|
||
// ContainerContext is the map to show container context detail | ||
type ContainerContext map[string]string | ||
|
||
// NewContainerContext is to generate a ContainerContext to be show | ||
func NewContainerContext(c *types.Container, flagNoTrunc bool) ContainerContext { | ||
id := c.ID[:6] | ||
if flagNoTrunc { | ||
id = c.ID | ||
} | ||
created, _ := utils.FormatTimeInterval(c.Created) | ||
labels := LabelsToString(c.Labels) | ||
mount := MountPointToString(c.Mounts) | ||
containerContext := ContainerContext{ | ||
"Name": c.Names[0], | ||
"ID": id, | ||
"Status": c.Status, | ||
"Created": created + " ago", | ||
"Image": c.Image, | ||
"Runtime": c.HostConfig.Runtime, | ||
"Command": c.Command, | ||
"ImageID": c.ImageID, | ||
"Labels": labels, | ||
"Mounts": mount, | ||
"State": c.State, | ||
} | ||
return containerContext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package formatter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewContainerContext(t *testing.T) { | ||
type TestCase struct { | ||
container *types.Container | ||
flagNoTrunc bool | ||
expected ContainerContext | ||
} | ||
|
||
testCases := []TestCase{ | ||
{ | ||
container: &types.Container{ | ||
Command: "bash", | ||
Created: 4, | ||
HostConfig: &types.HostConfig{Runtime: "runc"}, | ||
ID: "abcdelj8937", | ||
Image: "Image123", | ||
ImageID: "234567890", | ||
Labels: map[string]string{"a": "b", "c": "d"}, | ||
Names: []string{"nameA", "nameB"}, | ||
State: "StateA", | ||
Status: "StatusB", | ||
Mounts: []types.MountPoint{ | ||
types.MountPoint{Source: "/root/code"}, | ||
types.MountPoint{Source: "/test"}, | ||
}, | ||
}, | ||
flagNoTrunc: false, | ||
expected: ContainerContext{ | ||
"Name": "nameA", | ||
"ID": "abcdel", | ||
"Status": "StatusB", | ||
"Created": "49 years" + " ago", | ||
"Image": "Image123", | ||
"Runtime": "runc", | ||
"Command": "bash", | ||
"ImageID": "234567890", | ||
"Labels": "a = b;c = d;", | ||
"State": "StateA", | ||
"Mounts": "/root/code;/test;", | ||
}, | ||
}, | ||
{ | ||
container: &types.Container{ | ||
Command: "shell", | ||
Created: 5, | ||
HostConfig: &types.HostConfig{Runtime: "runv"}, | ||
ID: "1234567890", | ||
Image: "Image456", | ||
ImageID: "abcdefg", | ||
Labels: map[string]string{}, | ||
Names: []string{"nameB", "nameA"}, | ||
State: "StateB", | ||
Status: "StatusA", | ||
Mounts: []types.MountPoint{ | ||
types.MountPoint{Source: "/root/code"}, | ||
types.MountPoint{Source: "/test"}, | ||
}, | ||
}, | ||
flagNoTrunc: true, | ||
expected: ContainerContext{ | ||
"Name": "nameB", | ||
"ID": "1234567890", | ||
"Status": "StatusA", | ||
"Created": "49 years" + " ago", | ||
"Image": "Image456", | ||
"Runtime": "runv", | ||
"Command": "shell", | ||
"ImageID": "abcdefg", | ||
"Labels": "", | ||
"State": "StateB", | ||
"Mounts": "/root/code;/test;", | ||
}, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
result := NewContainerContext(testCase.container, testCase.flagNoTrunc) | ||
assert.Equal(t, testCase.expected, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package formatter | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
// Format key to output | ||
const ( | ||
TableFormat = "table" | ||
RawFormat = "raw" | ||
) | ||
|
||
// IsTable to verify if table or raw | ||
func IsTable(format string) bool { | ||
return strings.HasPrefix(format, TableFormat) | ||
} | ||
|
||
// PreFormat is to format the format option | ||
func PreFormat(format string) string { | ||
if IsTable(format) { | ||
format = format[len(TableFormat):] | ||
// cut the space | ||
format = strings.Trim(format, " ") | ||
// input by cmd of "\t" is "\\t" | ||
replace := strings.NewReplacer(`\t`, "\t", `\n`, "\n") | ||
format = replace.Replace(format) | ||
|
||
if format[len(format)-1:] != "\n" { | ||
format += "\n" | ||
} | ||
} else { | ||
format = "Name:{{.Name}}\nID:{{.ID}}\nStatus:{{.Status}}\nCreated:{{.Created}}\nImage:{{.Image}}\nRuntime:{{.Runtime}}\n\n" | ||
} | ||
return format | ||
} | ||
|
||
// LabelsToString is to transform the labels from map to string | ||
func LabelsToString(labels map[string]string) string { | ||
var labelstring string | ||
sortedkeys := make([]string, 0) | ||
for key := range labels { | ||
sortedkeys = append(sortedkeys, key) | ||
} | ||
sort.Strings(sortedkeys) | ||
for _, key := range sortedkeys { | ||
labelstring += fmt.Sprintf("%s = %s;", key, labels[key]) | ||
} | ||
return labelstring | ||
} | ||
|
||
// MountPointToString is to transform the MountPoint from array to string | ||
func MountPointToString(mount []types.MountPoint) string { | ||
var mountstring string | ||
for _, value := range mount { | ||
mountstring += fmt.Sprintf("%s;", value.Source) | ||
} | ||
return mountstring | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package formatter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPreFormat(t *testing.T) { | ||
type TestCase struct { | ||
input string | ||
output string | ||
} | ||
|
||
testCases := []TestCase{ | ||
{ | ||
input: "table {{.ID}}\t{{.Status}}\t{{.Created}}\t{{.Name}}\t{{.Image}}\t{{.State}}", | ||
output: "{{.ID}}\t{{.Status}}\t{{.Created}}\t{{.Name}}\t{{.Image}}\t{{.State}}\n", | ||
}, | ||
{ | ||
input: "table {{.ID}}\\t{{.Status}}\\t{{.Created}}\\t{{.Name}}\\t{{.Image}}\\t{{.State}}", | ||
output: "{{.ID}}\t{{.Status}}\t{{.Created}}\t{{.Name}}\t{{.Image}}\t{{.State}}\n", | ||
}, | ||
{ | ||
input: "table {{.ID}}\t{{.Status}}\t{{.Created}}\\t{{.Name}}\t{{.Image}}\t{{.State}}\n", | ||
output: "{{.ID}}\t{{.Status}}\t{{.Created}}\t{{.Name}}\t{{.Image}}\t{{.State}}\n", | ||
}, | ||
{ | ||
input: "raw", | ||
output: "Name:{{.Name}}\nID:{{.ID}}\nStatus:{{.Status}}\nCreated:{{.Created}}\nImage:{{.Image}}\nRuntime:{{.Runtime}}\n\n", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
result := PreFormat(testCase.input) | ||
assert.Equal(t, testCase.output, result) | ||
} | ||
} | ||
|
||
func TestLabelToString(t *testing.T) { | ||
type TestCase struct { | ||
input map[string]string | ||
output string | ||
} | ||
testCases := []TestCase{ | ||
{ | ||
input: map[string]string{ | ||
"a": "b", | ||
"c": "d", | ||
}, | ||
output: "a = b;c = d;", | ||
}, | ||
{ | ||
input: map[string]string{ | ||
"a": "b", | ||
}, | ||
output: "a = b;", | ||
}, | ||
{ | ||
input: map[string]string{}, | ||
output: "", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
result := LabelsToString(testCase.input) | ||
assert.Equal(t, testCase.output, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.