github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration/plugin/logging/read_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/container"
    13  	"github.com/docker/docker/pkg/stdcopy"
    14  	"github.com/docker/docker/testutil/daemon"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  // TestReadPluginNoRead tests that reads are supported even if the plugin isn't capable.
    19  func TestReadPluginNoRead(t *testing.T) {
    20  	if runtime.GOOS == "windows" {
    21  		t.Skip("no unix domain sockets on Windows")
    22  	}
    23  	t.Parallel()
    24  	d := daemon.New(t)
    25  	d.StartWithBusybox(t, "--iptables=false")
    26  	defer d.Stop(t)
    27  
    28  	client, err := d.NewClient()
    29  	assert.Assert(t, err)
    30  	createPlugin(t, client, "test", "discard", asLogDriver)
    31  
    32  	ctx := context.Background()
    33  
    34  	err = client.PluginEnable(ctx, "test", types.PluginEnableOptions{Timeout: 30})
    35  	assert.Check(t, err)
    36  	d.Stop(t)
    37  
    38  	cfg := &container.Config{
    39  		Image: "busybox",
    40  		Cmd:   []string{"/bin/echo", "hello world"},
    41  	}
    42  	for desc, test := range map[string]struct {
    43  		dOpts         []string
    44  		logsSupported bool
    45  	}{
    46  		"default":                    {logsSupported: true},
    47  		"disabled caching":           {[]string{"--log-opt=cache-disabled=true"}, false},
    48  		"explicitly enabled caching": {[]string{"--log-opt=cache-disabled=false"}, true},
    49  	} {
    50  		t.Run(desc, func(t *testing.T) {
    51  			d.Start(t, append([]string{"--iptables=false"}, test.dOpts...)...)
    52  			defer d.Stop(t)
    53  			c, err := client.ContainerCreate(ctx,
    54  				cfg,
    55  				&container.HostConfig{LogConfig: container.LogConfig{Type: "test"}},
    56  				nil,
    57  				nil,
    58  				"",
    59  			)
    60  			assert.Assert(t, err)
    61  			defer client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true})
    62  
    63  			err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
    64  			assert.Assert(t, err)
    65  
    66  			logs, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ShowStdout: true})
    67  			if !test.logsSupported {
    68  				assert.Assert(t, err != nil)
    69  				return
    70  			}
    71  			assert.Assert(t, err)
    72  			defer logs.Close()
    73  
    74  			buf := bytes.NewBuffer(nil)
    75  
    76  			errCh := make(chan error, 1)
    77  			go func() {
    78  				_, err := stdcopy.StdCopy(buf, buf, logs)
    79  				errCh <- err
    80  			}()
    81  
    82  			select {
    83  			case <-time.After(60 * time.Second):
    84  				t.Fatal("timeout waiting for IO to complete")
    85  			case err := <-errCh:
    86  				assert.Assert(t, err)
    87  			}
    88  			assert.Assert(t, strings.TrimSpace(buf.String()) == "hello world", buf.Bytes())
    89  		})
    90  	}
    91  
    92  }