github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/container_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  	"io/ioutil"
     9  	"log"
    10  	"net/http"
    11  	"os"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/docker/docker/api/types"
    17  	"gotest.tools/assert"
    18  	is "gotest.tools/assert/cmp"
    19  )
    20  
    21  func TestContainerLogsNotFoundError(t *testing.T) {
    22  	client := &Client{
    23  		client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
    24  	}
    25  	_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
    26  	if !IsErrNotFound(err) {
    27  		t.Fatalf("expected a not found error, got %v", err)
    28  	}
    29  }
    30  
    31  func TestContainerLogsError(t *testing.T) {
    32  	client := &Client{
    33  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    34  	}
    35  	_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
    36  	assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
    37  	_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
    38  		Since: "2006-01-02TZ",
    39  	})
    40  	assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
    41  	_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
    42  		Until: "2006-01-02TZ",
    43  	})
    44  	assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
    45  }
    46  
    47  func TestContainerLogs(t *testing.T) {
    48  	expectedURL := "/containers/container_id/logs"
    49  	cases := []struct {
    50  		options             types.ContainerLogsOptions
    51  		expectedQueryParams map[string]string
    52  		expectedError       string
    53  	}{
    54  		{
    55  			expectedQueryParams: map[string]string{
    56  				"tail": "",
    57  			},
    58  		},
    59  		{
    60  			options: types.ContainerLogsOptions{
    61  				Tail: "any",
    62  			},
    63  			expectedQueryParams: map[string]string{
    64  				"tail": "any",
    65  			},
    66  		},
    67  		{
    68  			options: types.ContainerLogsOptions{
    69  				ShowStdout: true,
    70  				ShowStderr: true,
    71  				Timestamps: true,
    72  				Details:    true,
    73  				Follow:     true,
    74  			},
    75  			expectedQueryParams: map[string]string{
    76  				"tail":       "",
    77  				"stdout":     "1",
    78  				"stderr":     "1",
    79  				"timestamps": "1",
    80  				"details":    "1",
    81  				"follow":     "1",
    82  			},
    83  		},
    84  		{
    85  			options: types.ContainerLogsOptions{
    86  				// timestamp will be passed as is
    87  				Since: "1136073600.000000001",
    88  			},
    89  			expectedQueryParams: map[string]string{
    90  				"tail":  "",
    91  				"since": "1136073600.000000001",
    92  			},
    93  		},
    94  		{
    95  			options: types.ContainerLogsOptions{
    96  				// timestamp will be passed as is
    97  				Until: "1136073600.000000001",
    98  			},
    99  			expectedQueryParams: map[string]string{
   100  				"tail":  "",
   101  				"until": "1136073600.000000001",
   102  			},
   103  		},
   104  		{
   105  			options: types.ContainerLogsOptions{
   106  				// An complete invalid date will not be passed
   107  				Since: "invalid value",
   108  			},
   109  			expectedError: `invalid value for "since": failed to parse value as time or duration: "invalid value"`,
   110  		},
   111  		{
   112  			options: types.ContainerLogsOptions{
   113  				// An complete invalid date will not be passed
   114  				Until: "invalid value",
   115  			},
   116  			expectedError: `invalid value for "until": failed to parse value as time or duration: "invalid value"`,
   117  		},
   118  	}
   119  	for _, logCase := range cases {
   120  		client := &Client{
   121  			client: newMockClient(func(r *http.Request) (*http.Response, error) {
   122  				if !strings.HasPrefix(r.URL.Path, expectedURL) {
   123  					return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, r.URL)
   124  				}
   125  				// Check query parameters
   126  				query := r.URL.Query()
   127  				for key, expected := range logCase.expectedQueryParams {
   128  					actual := query.Get(key)
   129  					if actual != expected {
   130  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
   131  					}
   132  				}
   133  				return &http.Response{
   134  					StatusCode: http.StatusOK,
   135  					Body:       ioutil.NopCloser(bytes.NewReader([]byte("response"))),
   136  				}, nil
   137  			}),
   138  		}
   139  		body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
   140  		if logCase.expectedError != "" {
   141  			assert.Check(t, is.Error(err, logCase.expectedError))
   142  			continue
   143  		}
   144  		assert.NilError(t, err)
   145  		defer body.Close()
   146  		content, err := ioutil.ReadAll(body)
   147  		assert.NilError(t, err)
   148  		assert.Check(t, is.Contains(string(content), "response"))
   149  	}
   150  }
   151  
   152  func ExampleClient_ContainerLogs_withTimeout() {
   153  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   154  	defer cancel()
   155  
   156  	client, _ := NewClientWithOpts(FromEnv)
   157  	reader, err := client.ContainerLogs(ctx, "container_id", types.ContainerLogsOptions{})
   158  	if err != nil {
   159  		log.Fatal(err)
   160  	}
   161  
   162  	_, err = io.Copy(os.Stdout, reader)
   163  	if err != nil && err != io.EOF {
   164  		log.Fatal(err)
   165  	}
   166  }