mirror of https://github.com/docker/cli.git
Compare commits
8 Commits
c4fe413abe
...
25058a134e
Author | SHA1 | Date |
---|---|---|
Alano Terblanche | 25058a134e | |
Sebastiaan van Stijn | a5fb752ecf | |
Laura Brehm | 4e64c59d64 | |
Jonathan A. Sternberg | 3472bbc28a | |
Laura Brehm | 649e564ee0 | |
Sebastiaan van Stijn | e1213edcc6 | |
Jonathan A. Sternberg | b1956f5073 | |
Alano Terblanche | dddf757fb1 |
|
@ -67,7 +67,7 @@ jobs:
|
|||
name: Update Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.21'
|
||||
go-version: 1.22.7
|
||||
-
|
||||
name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
|
|
|
@ -180,7 +180,7 @@ func toWslPath(s string) string {
|
|||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("mnt/%s%s", drive, p)
|
||||
return fmt.Sprintf("mnt/%s%s", strings.ToLower(drive), p)
|
||||
}
|
||||
|
||||
func parseUNCPath(s string) (drive, p string, ok bool) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
@ -9,21 +10,48 @@ import (
|
|||
)
|
||||
|
||||
func TestWslSocketPath(t *testing.T) {
|
||||
u, err := url.Parse("unix:////./c:/my/file/path")
|
||||
assert.NilError(t, err)
|
||||
testCases := []struct {
|
||||
doc string
|
||||
fs fs.FS
|
||||
url string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
doc: "filesystem where WSL path does not exist",
|
||||
fs: fstest.MapFS{
|
||||
"my/file/path": {},
|
||||
},
|
||||
url: "unix:////./c:/my/file/path",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
doc: "filesystem where WSL path exists",
|
||||
fs: fstest.MapFS{
|
||||
"mnt/c/my/file/path": {},
|
||||
},
|
||||
url: "unix:////./c:/my/file/path",
|
||||
expected: "/mnt/c/my/file/path",
|
||||
},
|
||||
{
|
||||
doc: "filesystem where WSL path exists uppercase URL",
|
||||
fs: fstest.MapFS{
|
||||
"mnt/c/my/file/path": {},
|
||||
},
|
||||
url: "unix:////./C:/my/file/path",
|
||||
expected: "/mnt/c/my/file/path",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.doc, func(t *testing.T) {
|
||||
u, err := url.Parse(tc.url)
|
||||
assert.NilError(t, err)
|
||||
// Ensure host is empty.
|
||||
assert.Equal(t, u.Host, "")
|
||||
|
||||
// Use a filesystem where the WSL path exists.
|
||||
fs := fstest.MapFS{
|
||||
"mnt/c/my/file/path": {},
|
||||
}
|
||||
assert.Equal(t, wslSocketPath(u.Path, fs), "/mnt/c/my/file/path")
|
||||
result := wslSocketPath(u.Path, tc.fs)
|
||||
|
||||
// Use a filesystem where the WSL path doesn't exist.
|
||||
fs = fstest.MapFS{
|
||||
"my/file/path": {},
|
||||
assert.Equal(t, result, tc.expected)
|
||||
})
|
||||
}
|
||||
assert.Equal(t, wslSocketPath(u.Path, fs), "")
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/docker/cli/cli/version"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
@ -94,7 +95,9 @@ func startCobraCommandTimer(mp metric.MeterProvider, attrs []attribute.KeyValue)
|
|||
metric.WithAttributes(cmdStatusAttrs...),
|
||||
)
|
||||
if mp, ok := mp.(MeterProvider); ok {
|
||||
mp.ForceFlush(ctx)
|
||||
if err := mp.ForceFlush(ctx); err != nil {
|
||||
otel.Handle(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -358,7 +358,9 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
|
|||
|
||||
mp := dockerCli.MeterProvider()
|
||||
if mp, ok := mp.(command.MeterProvider); ok {
|
||||
defer mp.Shutdown(ctx)
|
||||
if err := mp.Shutdown(ctx); err != nil {
|
||||
otel.Handle(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Fprint(dockerCli.Err(), "Warning: Unexpected OTEL error, metrics may not be flushed")
|
||||
}
|
||||
|
|
|
@ -1,17 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/cli/cli/debug"
|
||||
"github.com/docker/cli/cli/streams"
|
||||
"github.com/docker/cli/cmd/docker/internal/signals"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
"gotest.tools/v3/poll"
|
||||
)
|
||||
|
||||
func TestClientDebugEnabled(t *testing.T) {
|
||||
|
@ -75,3 +86,92 @@ func TestVersion(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Contains(b.String(), "Docker version"))
|
||||
}
|
||||
|
||||
func TestFallbackForceExit(t *testing.T) {
|
||||
longRunningCommand := cobra.Command{
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
read, _, err := os.Pipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// wait until the parent process sends a signal to exit
|
||||
_, _, err = bufio.NewReader(read).ReadLine()
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
// This is the child process that will run the long running command
|
||||
if os.Getenv("TEST_FALLBACK_FORCE_EXIT") == "1" {
|
||||
fmt.Println("running long command")
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), signals.TerminationSignals...)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
longRunningCommand.SetErr(streams.NewOut(os.Stderr))
|
||||
longRunningCommand.SetOut(streams.NewOut(os.Stdout))
|
||||
|
||||
go forceExitAfter3TerminationSignals(ctx, streams.NewOut(os.Stderr))
|
||||
|
||||
err := longRunningCommand.ExecuteContext(ctx)
|
||||
if err != nil {
|
||||
os.Exit(0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
// spawn the child process
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=TestFallbackForceExit")
|
||||
cmd.Env = append(os.Environ(), "TEST_FALLBACK_FORCE_EXIT=1")
|
||||
|
||||
var buf strings.Builder
|
||||
cmd.Stderr = &buf
|
||||
cmd.Stdout = &buf
|
||||
|
||||
t.Cleanup(func() {
|
||||
_ = cmd.Process.Kill()
|
||||
})
|
||||
|
||||
assert.NilError(t, cmd.Start())
|
||||
|
||||
poll.WaitOn(t, func(t poll.LogT) poll.Result {
|
||||
if strings.Contains(buf.String(), "running long command") {
|
||||
return poll.Success()
|
||||
}
|
||||
return poll.Continue("waiting for child process to start")
|
||||
}, poll.WithTimeout(1*time.Second), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
cmd.Process.Signal(syscall.SIGINT)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
cmdErr := make(chan error, 1)
|
||||
go func() {
|
||||
cmdErr <- cmd.Wait()
|
||||
}()
|
||||
|
||||
poll.WaitOn(t, func(t poll.LogT) poll.Result {
|
||||
if strings.Contains(buf.String(), "got 3 SIGTERM/SIGINTs, forcefully exiting") {
|
||||
return poll.Success()
|
||||
}
|
||||
return poll.Continue("waiting for child process to exit")
|
||||
},
|
||||
poll.WithTimeout(1*time.Second), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
select {
|
||||
case cmdErr := <-cmdErr:
|
||||
assert.Error(t, cmdErr, "exit status 1")
|
||||
exitErr, ok := cmdErr.(*exec.ExitError)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected error type: %T", cmdErr)
|
||||
}
|
||||
if exitErr.Success() {
|
||||
t.Fatalf("unexpected exit status: %v", exitErr)
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("timed out waiting for child process to exit")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue