(ctx context.Context, t *testctx.T)
| 18 | var echoSocketSrc string |
| 19 | |
| 20 | func (ContainerSuite) TestWithUnixSocket(ctx context.Context, t *testctx.T) { |
| 21 | c := connect(ctx, t) |
| 22 | |
| 23 | tmp := t.TempDir() |
| 24 | sock := filepath.Join(tmp, "test.sock") |
| 25 | |
| 26 | l, err := net.Listen("unix", sock) |
| 27 | require.NoError(t, err) |
| 28 | |
| 29 | defer l.Close() |
| 30 | |
| 31 | go func() { |
| 32 | for { |
| 33 | c, err := l.Accept() |
| 34 | if err != nil { |
| 35 | if !errors.Is(err, net.ErrClosed) { |
| 36 | t.Logf("accept: %s", err) |
| 37 | panic(err) |
| 38 | } |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | n, err := io.Copy(c, c) |
| 43 | if err != nil { |
| 44 | t.Logf("hello: %s", err) |
| 45 | panic(err) |
| 46 | } |
| 47 | |
| 48 | t.Logf("copied %d bytes", n) |
| 49 | |
| 50 | err = c.Close() |
| 51 | if err != nil { |
| 52 | t.Logf("close: %s", err) |
| 53 | panic(err) |
| 54 | } |
| 55 | } |
| 56 | }() |
| 57 | |
| 58 | echo := c.Directory().WithNewFile("main.go", echoSocketSrc).File("main.go") |
| 59 | |
| 60 | ContainerSuite{}.withHostSocket(c, sock, func(hostSock *dagger.Socket) { |
| 61 | ctr := c.Container(). |
| 62 | From(golangImage). |
| 63 | WithMountedFile("/src/main.go", echo). |
| 64 | WithUnixSocket("/tmp/test.sock", hostSock). |
| 65 | WithExec([]string{"go", "run", "/src/main.go", "/tmp/test.sock", "hello"}) |
| 66 | stdout, err := ctr.Stdout(ctx) |
| 67 | require.NoError(t, err) |
| 68 | require.Equal(t, "hello\n", stdout) |
| 69 | |
| 70 | t.Run("socket can be removed", func(ctx context.Context, t *testctx.T) { |
| 71 | without := ctr.WithoutUnixSocket("/tmp/test.sock"). |
| 72 | WithExec([]string{"ls", "/tmp"}) |
| 73 | |
| 74 | stdout, err = without.Stdout(ctx) |
| 75 | require.NoError(t, err) |
| 76 | require.Empty(t, stdout) |
| 77 | }) |
nothing calls this directly
no test coverage detected