TestComposeWithStreams tests execution of docker-compose commands with streams
(t *testing.T)
| 120 | |
| 121 | // TestComposeWithStreams tests execution of docker-compose commands with streams |
| 122 | func TestComposeWithStreams(t *testing.T) { |
| 123 | assert := asrt.New(t) |
| 124 | |
| 125 | container, _ := dockerutil.FindContainerByName(t.Name()) |
| 126 | if container != nil { |
| 127 | _ = dockerutil.RemoveContainer(container.ID) |
| 128 | } |
| 129 | |
| 130 | // Use the current actual web container for this, so replace in base docker-compose file |
| 131 | composeBase := filepath.Join("testdata", "TestComposeWithStreams", "test-compose-with-streams.yaml") |
| 132 | tmpDir := testcommon.CreateTmpDir(t.Name()) |
| 133 | realComposeFile := filepath.Join(tmpDir, "replaced-compose-with-streams.yaml") |
| 134 | |
| 135 | err := fileutil.ReplaceStringInFile("TEST-COMPOSE-WITH-STREAMS-IMAGE", versionconstants.WebImg+":"+versionconstants.WebTag, composeBase, realComposeFile) |
| 136 | assert.NoError(err) |
| 137 | |
| 138 | composeFiles := []string{realComposeFile} |
| 139 | |
| 140 | t.Cleanup(func() { |
| 141 | _, _, err = dockerutil.ComposeCmd(&dockerutil.ComposeCmdOpts{ |
| 142 | ComposeFiles: composeFiles, |
| 143 | Action: []string{"down"}, |
| 144 | }) |
| 145 | assert.NoError(err) |
| 146 | }) |
| 147 | |
| 148 | _, _, err = dockerutil.ComposeCmd(&dockerutil.ComposeCmdOpts{ |
| 149 | ComposeFiles: composeFiles, |
| 150 | Action: []string{"up", "-d"}, |
| 151 | }) |
| 152 | require.NoError(t, err) |
| 153 | |
| 154 | _, err = dockerutil.ContainerWait(60, map[string]string{ |
| 155 | "com.ddev.site-name": t.Name(), |
| 156 | "com.docker.compose.oneoff": "False", |
| 157 | }) |
| 158 | if err != nil { |
| 159 | logout, _ := exec.RunCommand("docker", []string{"logs", t.Name()}) |
| 160 | inspectOut, _ := exec.RunCommandPipe("sh", []string{"-c", fmt.Sprintf("docker inspect %s|jq -r '.[0].State.Health.Log'", t.Name())}) |
| 161 | t.Fatalf("FAIL: TestComposeWithStreams failed to ContainerWait for container: %v, logs\n========= container logs ======\n%s\n======= end logs =======\n==== health log =====\ninspectOut\n%s\n========", err, logout, inspectOut) |
| 162 | } |
| 163 | |
| 164 | // Point stdout to os.Stdout and do simple ps -ef in web container |
| 165 | stdout := util.CaptureStdOut() |
| 166 | err = dockerutil.ComposeWithStreams(&dockerutil.ComposeCmdOpts{ |
| 167 | ComposeFiles: composeFiles, |
| 168 | Action: []string{"exec", "-T", "web", "ps", "-ef"}, |
| 169 | }, os.Stdin, os.Stdout, os.Stderr) |
| 170 | assert.NoError(err) |
| 171 | output := stdout() |
| 172 | assert.Contains(output, "supervisord") |
| 173 | |
| 174 | // Reverse stdout and stderr and create an error and normal stdout. We should see only the error captured in stdout |
| 175 | stdout = util.CaptureStdOut() |
| 176 | err = dockerutil.ComposeWithStreams(&dockerutil.ComposeCmdOpts{ |
| 177 | ComposeFiles: composeFiles, |
| 178 | Action: []string{"exec", "-T", "web", "ls", "-d", "xx", "/var/run/apache2"}, |
| 179 | }, os.Stdin, os.Stderr, os.Stdout) |
nothing calls this directly
no test coverage detected