github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_logs_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     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/internal/testutil"
    17  
    18  	"golang.org/x/net/context"
    19  )
    20  
    21  func TestContainerLogsError(t *testing.T) {
    22  	client := &Client{
    23  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    24  	}
    25  	_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
    26  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    27  		t.Fatalf("expected a Server Error, got %v", err)
    28  	}
    29  	_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
    30  		Since: "2006-01-02TZ",
    31  	})
    32  	testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
    33  	_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
    34  		Until: "2006-01-02TZ",
    35  	})
    36  	testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
    37  }
    38  
    39  func TestContainerLogs(t *testing.T) {
    40  	expectedURL := "/containers/container_id/logs"
    41  	cases := []struct {
    42  		options             types.ContainerLogsOptions
    43  		expectedQueryParams map[string]string
    44  	}{
    45  		{
    46  			expectedQueryParams: map[string]string{
    47  				"tail": "",
    48  			},
    49  		},
    50  		{
    51  			options: types.ContainerLogsOptions{
    52  				Tail: "any",
    53  			},
    54  			expectedQueryParams: map[string]string{
    55  				"tail": "any",
    56  			},
    57  		},
    58  		{
    59  			options: types.ContainerLogsOptions{
    60  				ShowStdout: true,
    61  				ShowStderr: true,
    62  				Timestamps: true,
    63  				Details:    true,
    64  				Follow:     true,
    65  			},
    66  			expectedQueryParams: map[string]string{
    67  				"tail":       "",
    68  				"stdout":     "1",
    69  				"stderr":     "1",
    70  				"timestamps": "1",
    71  				"details":    "1",
    72  				"follow":     "1",
    73  			},
    74  		},
    75  		{
    76  			options: types.ContainerLogsOptions{
    77  				// An complete invalid date, timestamp or go duration will be
    78  				// passed as is
    79  				Since: "invalid but valid",
    80  			},
    81  			expectedQueryParams: map[string]string{
    82  				"tail":  "",
    83  				"since": "invalid but valid",
    84  			},
    85  		},
    86  		{
    87  			options: types.ContainerLogsOptions{
    88  				// An complete invalid date, timestamp or go duration will be
    89  				// passed as is
    90  				Until: "invalid but valid",
    91  			},
    92  			expectedQueryParams: map[string]string{
    93  				"tail":  "",
    94  				"until": "invalid but valid",
    95  			},
    96  		},
    97  	}
    98  	for _, logCase := range cases {
    99  		client := &Client{
   100  			client: newMockClient(func(r *http.Request) (*http.Response, error) {
   101  				if !strings.HasPrefix(r.URL.Path, expectedURL) {
   102  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
   103  				}
   104  				// Check query parameters
   105  				query := r.URL.Query()
   106  				for key, expected := range logCase.expectedQueryParams {
   107  					actual := query.Get(key)
   108  					if actual != expected {
   109  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
   110  					}
   111  				}
   112  				return &http.Response{
   113  					StatusCode: http.StatusOK,
   114  					Body:       ioutil.NopCloser(bytes.NewReader([]byte("response"))),
   115  				}, nil
   116  			}),
   117  		}
   118  		body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  		defer body.Close()
   123  		content, err := ioutil.ReadAll(body)
   124  		if err != nil {
   125  			t.Fatal(err)
   126  		}
   127  		if string(content) != "response" {
   128  			t.Fatalf("expected response to contain 'response', got %s", string(content))
   129  		}
   130  	}
   131  }
   132  
   133  func ExampleClient_ContainerLogs_withTimeout() {
   134  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   135  	defer cancel()
   136  
   137  	client, _ := NewEnvClient()
   138  	reader, err := client.ContainerLogs(ctx, "container_id", types.ContainerLogsOptions{})
   139  	if err != nil {
   140  		log.Fatal(err)
   141  	}
   142  
   143  	_, err = io.Copy(os.Stdout, reader)
   144  	if err != nil && err != io.EOF {
   145  		log.Fatal(err)
   146  	}
   147  }