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