1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package docker
import (
"encoding/json"
"net/http"
"reflect"
"strings"
"testing"
)
func TestTopContainer(t *testing.T) {
t.Parallel()
jsonTop := `{
"Processes": [
[
"ubuntu",
"3087",
"815",
"0",
"01:44",
"?",
"00:00:00",
"cmd1"
],
[
"root",
"3158",
"3087",
"0",
"01:44",
"?",
"00:00:01",
"cmd2"
]
],
"Titles": [
"UID",
"PID",
"PPID",
"C",
"STIME",
"TTY",
"TIME",
"CMD"
]
}`
var expected TopResult
err := json.Unmarshal([]byte(jsonTop), &expected)
if err != nil {
t.Fatal(err)
}
id := "4fa6e0f0"
fakeRT := &FakeRoundTripper{message: jsonTop, status: http.StatusOK}
client := newTestClient(fakeRT)
processes, err := client.TopContainer(id, "")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(processes, expected) {
t.Errorf("TopContainer: Expected %#v. Got %#v.", expected, processes)
}
if len(processes.Processes) != 2 || len(processes.Processes[0]) != 8 ||
processes.Processes[0][7] != "cmd1" {
t.Errorf("TopContainer: Process list to include cmd1. Got %#v.", processes)
}
expectedURI := "/containers/" + id + "/top"
if !strings.HasSuffix(fakeRT.requests[0].URL.String(), expectedURI) {
t.Errorf("TopContainer: Expected URI to have %q. Got %q.", expectedURI, fakeRT.requests[0].URL.String())
}
}
func TestTopContainerNotFound(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "no such container", status: http.StatusNotFound})
_, err := client.TopContainer("abef348", "")
expectNoSuchContainer(t, "abef348", err)
}
func TestTopContainerWithPsArgs(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "no such container", status: http.StatusNotFound}
client := newTestClient(fakeRT)
_, err := client.TopContainer("abef348", "aux")
expectNoSuchContainer(t, "abef348", err)
expectedURI := "/containers/abef348/top?ps_args=aux"
if !strings.HasSuffix(fakeRT.requests[0].URL.String(), expectedURI) {
t.Errorf("TopContainer: Expected URI to have %q. Got %q.", expectedURI, fakeRT.requests[0].URL.String())
}
}
|