MCPcopy
hub / github.com/docker/compose / TestRebuildOnDotEnvWithExternalNetwork

Function TestRebuildOnDotEnvWithExternalNetwork

pkg/e2e/watch_test.go:46–131  ·  pkg/e2e/watch_test.go::TestRebuildOnDotEnvWithExternalNetwork
(t *testing.T)

Source from the content-addressed store, hash-verified

44}
45
46func TestRebuildOnDotEnvWithExternalNetwork(t *testing.T) {
47 const projectName = "test_rebuild_on_dotenv_with_external_network"
48 const svcName = "ext-alpine"
49 containerName := strings.Join([]string{projectName, svcName, "1"}, "-")
50 const networkName = "e2e-watch-external_network_test"
51 const dotEnvFilepath = "./fixtures/watch/.env"
52
53 c := NewCLI(t, WithEnv(
54 "COMPOSE_PROJECT_NAME="+projectName,
55 "COMPOSE_FILE=./fixtures/watch/with-external-network.yaml",
56 ))
57
58 cleanup := func() {
59 c.RunDockerComposeCmdNoCheck(t, "down", "--remove-orphans", "--volumes", "--rmi=local")
60 c.RunDockerOrExitError(t, "network", "rm", networkName)
61 os.Remove(dotEnvFilepath) //nolint:errcheck
62 }
63 cleanup()
64
65 t.Log("create network that is referenced by the container we're testing")
66 c.RunDockerCmd(t, "network", "create", networkName)
67 res := c.RunDockerCmd(t, "network", "ls")
68 assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
69
70 t.Log("create a dotenv file that will be used to trigger the rebuild")
71 err := os.WriteFile(dotEnvFilepath, []byte("HELLO=WORLD"), 0o666)
72 assert.NilError(t, err)
73 _, err = os.ReadFile(dotEnvFilepath)
74 assert.NilError(t, err)
75
76 // TODO: refactor this duplicated code into frameworks? Maybe?
77 t.Log("starting docker compose watch")
78 cmd := c.NewDockerComposeCmd(t, "--verbose", "watch", svcName)
79 // stream output since watch runs in the background
80 cmd.Stdout = os.Stdout
81 cmd.Stderr = os.Stderr
82 r := icmd.StartCmd(cmd)
83 assert.NilError(t, r.Error)
84 var testComplete atomic.Bool
85 go func() {
86 // if the process exits abnormally before the test is done, fail the test
87 if err := r.Cmd.Wait(); err != nil && !t.Failed() && !testComplete.Load() {
88 assert.Check(t, cmp.Nil(err))
89 }
90 }()
91
92 t.Log("wait for watch to start watching")
93 c.WaitForCondition(t, func() (bool, string) {
94 out := r.String()
95 return strings.Contains(out, "Watch enabled"), "watch not started"
96 }, 30*time.Second, 1*time.Second)
97
98 pn := c.RunDockerCmd(t, "inspect", containerName, "-f", "{{ .HostConfig.NetworkMode }}")
99 assert.Equal(t, strings.TrimSpace(pn.Stdout()), networkName)
100
101 t.Log("create a dotenv file that will be used to trigger the rebuild")
102 err = os.WriteFile(dotEnvFilepath, []byte("HELLO=WORLD\nTEST=REBUILD"), 0o666)
103 assert.NilError(t, err)

Callers

nothing calls this directly

Calls 15

RunDockerOrExitErrorMethod · 0.95
RunDockerCmdMethod · 0.95
NewDockerComposeCmdMethod · 0.95
WaitForConditionMethod · 0.95
NewCLIFunction · 0.85
WithEnvFunction · 0.85
WriteFileMethod · 0.80
ReadFileMethod · 0.80
RemoveMethod · 0.65
LogMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected