github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/service_logs_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "log" 9 "net/http" 10 "os" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/docker/docker/api/types" 16 "github.com/docker/docker/errdefs" 17 "gotest.tools/v3/assert" 18 is "gotest.tools/v3/assert/cmp" 19 ) 20 21 func TestServiceLogsError(t *testing.T) { 22 client := &Client{ 23 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 24 } 25 _, err := client.ServiceLogs(context.Background(), "service_id", types.ContainerLogsOptions{}) 26 if !errdefs.IsSystem(err) { 27 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 28 } 29 _, err = client.ServiceLogs(context.Background(), "service_id", types.ContainerLogsOptions{ 30 Since: "2006-01-02TZ", 31 }) 32 assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`)) 33 } 34 35 func TestServiceLogs(t *testing.T) { 36 expectedURL := "/services/service_id/logs" 37 cases := []struct { 38 options types.ContainerLogsOptions 39 expectedQueryParams map[string]string 40 expectedError string 41 }{ 42 { 43 expectedQueryParams: map[string]string{ 44 "tail": "", 45 }, 46 }, 47 { 48 options: types.ContainerLogsOptions{ 49 Tail: "any", 50 }, 51 expectedQueryParams: map[string]string{ 52 "tail": "any", 53 }, 54 }, 55 { 56 options: types.ContainerLogsOptions{ 57 ShowStdout: true, 58 ShowStderr: true, 59 Timestamps: true, 60 Details: true, 61 Follow: true, 62 }, 63 expectedQueryParams: map[string]string{ 64 "tail": "", 65 "stdout": "1", 66 "stderr": "1", 67 "timestamps": "1", 68 "details": "1", 69 "follow": "1", 70 }, 71 }, 72 { 73 options: types.ContainerLogsOptions{ 74 // timestamp will be passed as is 75 Since: "1136073600.000000001", 76 }, 77 expectedQueryParams: map[string]string{ 78 "tail": "", 79 "since": "1136073600.000000001", 80 }, 81 }, 82 { 83 options: types.ContainerLogsOptions{ 84 // An complete invalid date will not be passed 85 Since: "invalid value", 86 }, 87 expectedError: `invalid value for "since": failed to parse value as time or duration: "invalid value"`, 88 }, 89 } 90 for _, logCase := range cases { 91 client := &Client{ 92 client: newMockClient(func(r *http.Request) (*http.Response, error) { 93 if !strings.HasPrefix(r.URL.Path, expectedURL) { 94 return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, r.URL) 95 } 96 // Check query parameters 97 query := r.URL.Query() 98 for key, expected := range logCase.expectedQueryParams { 99 actual := query.Get(key) 100 if actual != expected { 101 return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual) 102 } 103 } 104 return &http.Response{ 105 StatusCode: http.StatusOK, 106 Body: io.NopCloser(bytes.NewReader([]byte("response"))), 107 }, nil 108 }), 109 } 110 body, err := client.ServiceLogs(context.Background(), "service_id", logCase.options) 111 if logCase.expectedError != "" { 112 assert.Check(t, is.Error(err, logCase.expectedError)) 113 continue 114 } 115 assert.NilError(t, err) 116 defer body.Close() 117 content, err := io.ReadAll(body) 118 assert.NilError(t, err) 119 assert.Check(t, is.Contains(string(content), "response")) 120 } 121 } 122 123 func ExampleClient_ServiceLogs_withTimeout() { 124 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 125 defer cancel() 126 127 client, _ := NewClientWithOpts(FromEnv) 128 reader, err := client.ServiceLogs(ctx, "service_id", types.ContainerLogsOptions{}) 129 if err != nil { 130 log.Fatal(err) 131 } 132 133 _, err = io.Copy(os.Stdout, reader) 134 if err != nil && err != io.EOF { 135 log.Fatal(err) 136 } 137 }