github.com/kinvolk/docker@v1.13.1/client/container_top_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "reflect" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "golang.org/x/net/context" 15 ) 16 17 func TestContainerTopError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 _, err := client.ContainerTop(context.Background(), "nothing", []string{}) 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestContainerTop(t *testing.T) { 28 expectedURL := "/containers/container_id/top" 29 expectedProcesses := [][]string{ 30 {"p1", "p2"}, 31 {"p3"}, 32 } 33 expectedTitles := []string{"title1", "title2"} 34 35 client := &Client{ 36 client: newMockClient(func(req *http.Request) (*http.Response, error) { 37 if !strings.HasPrefix(req.URL.Path, expectedURL) { 38 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 39 } 40 query := req.URL.Query() 41 args := query.Get("ps_args") 42 if args != "arg1 arg2" { 43 return nil, fmt.Errorf("args not set in URL query properly. Expected 'arg1 arg2', got %v", args) 44 } 45 46 b, err := json.Marshal(types.ContainerProcessList{ 47 Processes: [][]string{ 48 {"p1", "p2"}, 49 {"p3"}, 50 }, 51 Titles: []string{"title1", "title2"}, 52 }) 53 if err != nil { 54 return nil, err 55 } 56 57 return &http.Response{ 58 StatusCode: http.StatusOK, 59 Body: ioutil.NopCloser(bytes.NewReader(b)), 60 }, nil 61 }), 62 } 63 64 processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"}) 65 if err != nil { 66 t.Fatal(err) 67 } 68 if !reflect.DeepEqual(expectedProcesses, processList.Processes) { 69 t.Fatalf("Processes: expected %v, got %v", expectedProcesses, processList.Processes) 70 } 71 if !reflect.DeepEqual(expectedTitles, processList.Titles) { 72 t.Fatalf("Titles: expected %v, got %v", expectedTitles, processList.Titles) 73 } 74 }