github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/integration/plugin/logging/read_test.go (about)

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