1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
package container // import "github.com/docker/docker/integration/container"
import (
"bytes"
"io"
"strings"
"testing"
"time"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/daemon/logger/jsonfilelog"
"github.com/docker/docker/daemon/logger/local"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/integration/internal/termtest"
"github.com/docker/docker/pkg/stdcopy"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip"
)
// Regression test for #35370
// Makes sure that when following we don't get an EOF error when there are no logs
func TestLogsFollowTailEmpty(t *testing.T) {
// FIXME(vdemeester) fails on a e2e run on linux...
skip.If(t, testEnv.IsRemoteDaemon)
ctx := setupTest(t)
apiClient := testEnv.APIClient()
id := container.Run(ctx, t, apiClient, container.WithCmd("sleep", "100000"))
logs, err := apiClient.ContainerLogs(ctx, id, containertypes.LogsOptions{ShowStdout: true, Tail: "2"})
if logs != nil {
defer logs.Close()
}
assert.Check(t, err)
_, err = stdcopy.StdCopy(io.Discard, io.Discard, logs)
assert.Check(t, err)
}
func TestLogs(t *testing.T) {
drivers := []string{local.Name, jsonfilelog.Name}
for _, logDriver := range drivers {
t.Run("driver "+logDriver, func(t *testing.T) {
testLogs(t, logDriver)
})
}
}
func testLogs(t *testing.T, logDriver string) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
testCases := []struct {
desc string
logOps containertypes.LogsOptions
expectedOut string
expectedErr string
tty bool
}{
// TTY, only one output stream
{
desc: "tty/stdout and stderr",
tty: true,
logOps: containertypes.LogsOptions{
ShowStdout: true,
ShowStderr: true,
},
expectedOut: "this is fineaccidents happen",
},
{
desc: "tty/only stdout",
tty: true,
logOps: containertypes.LogsOptions{
ShowStdout: true,
ShowStderr: false,
},
expectedOut: "this is fineaccidents happen",
},
{
desc: "tty/only stderr",
tty: true,
logOps: containertypes.LogsOptions{
ShowStdout: false,
ShowStderr: true,
},
expectedOut: "",
},
// Without TTY, both stdout and stderr
{
desc: "without tty/stdout and stderr",
tty: false,
logOps: containertypes.LogsOptions{
ShowStdout: true,
ShowStderr: true,
},
expectedOut: "this is fine",
expectedErr: "accidents happen",
},
{
desc: "without tty/only stdout",
tty: false,
logOps: containertypes.LogsOptions{
ShowStdout: true,
ShowStderr: false,
},
expectedOut: "this is fine",
expectedErr: "",
},
{
desc: "without tty/only stderr",
tty: false,
logOps: containertypes.LogsOptions{
ShowStdout: false,
ShowStderr: true,
},
expectedOut: "",
expectedErr: "accidents happen",
},
}
pollTimeout := time.Second * 10
if testEnv.DaemonInfo.OSType == "windows" {
pollTimeout = StopContainerWindowsPollTimeout
}
for _, tC := range testCases {
tC := tC
t.Run(tC.desc, func(t *testing.T) {
t.Parallel()
tty := tC.tty
id := container.Run(ctx, t, apiClient,
container.WithCmd("sh", "-c", "echo -n this is fine; echo -n accidents happen >&2"),
container.WithTty(tty),
container.WithLogDriver(logDriver),
)
defer apiClient.ContainerRemove(ctx, id, containertypes.RemoveOptions{Force: true})
poll.WaitOn(t, container.IsStopped(ctx, apiClient, id),
poll.WithDelay(time.Millisecond*100),
poll.WithTimeout(pollTimeout))
logs, err := apiClient.ContainerLogs(ctx, id, tC.logOps)
assert.NilError(t, err)
defer logs.Close()
var stdout, stderr bytes.Buffer
if tty {
// TTY, only one output stream
_, err = io.Copy(&stdout, logs)
} else {
_, err = stdcopy.StdCopy(&stdout, &stderr, logs)
}
assert.NilError(t, err)
stdoutStr := stdout.String()
if tty && testEnv.DaemonInfo.OSType == "windows" {
stdoutStr = stripEscapeCodes(t, stdoutStr)
// Special case for Windows Server 2019
// Check only that the raw output stream contains strings
// that were printed to container's stdout and stderr.
// This is a workaround for the backspace being outputted in an unexpected place
// which breaks the parsed output: https://github.com/moby/moby/issues/43710
if strings.Contains(testEnv.DaemonInfo.OperatingSystem, "Windows Server Version 1809") {
if tC.logOps.ShowStdout {
assert.Check(t, cmp.Contains(stdout.String(), "this is fine"))
assert.Check(t, cmp.Contains(stdout.String(), "accidents happen"))
} else {
assert.DeepEqual(t, stdoutStr, "")
}
return
}
}
assert.DeepEqual(t, stdoutStr, tC.expectedOut)
assert.DeepEqual(t, stderr.String(), tC.expectedErr)
})
}
}
// This hack strips the escape codes that appear in the Windows TTY output and don't have
// any effect on the text content.
// This doesn't handle all escape sequences, only ones that were encountered during testing.
func stripEscapeCodes(t *testing.T, input string) string {
t.Logf("Stripping: %q\n", input)
output, err := termtest.StripANSICommands(input)
assert.NilError(t, err)
return output
}
|