summaryrefslogtreecommitdiff
path: root/integrationtests/commandutils.go
blob: fff168f2c1d087825cf89065b49e696182e5e127 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package integrationtests

import (
	"bufio"
	"context"
	"fmt"
	"io"
	"os"
	"os/exec"
	"strings"
	"syscall"
	"testing"
	"time"
)

func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string,
	args ...string) (int, error) {

	if _, err := os.Stat(cmdStr); err != nil {
		return 0, fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err)
	}

	t.Log("Creating stdout file", stdoutFile)
	fd, err := os.Create(stdoutFile)
	if err != nil {
		return 0, nil
	}
	defer fd.Close()

	t.Log("Running command", cmdStr, strings.Join(args, " "))
	cmd := exec.CommandContext(ctx, cmdStr, args...)
	out, err := cmd.CombinedOutput()
	t.Log("Done running command!", err)
	fd.Write(out)

	return exitCodeFromError(err), err
}

func runCommandRetry(ctx context.Context, t *testing.T, retries int, stdoutFile,
	cmd string, args ...string) (exitCode int, err error) {

	for i := 0; i < retries; i++ {
		time.Sleep(time.Second)
		if exitCode, err = runCommand(ctx, t, stdoutFile, cmd, args...); exitCode == 0 {
			return
		}
	}
	return
}

func startCommand(ctx context.Context, t *testing.T, inPipeFile,
	cmdStr string, args ...string) (<-chan string, <-chan string, <-chan error, error) {

	stdoutCh := make(chan string)
	stderrCh := make(chan string)

	if _, err := os.Stat(cmdStr); err != nil {
		return stdoutCh, stderrCh, nil,
			fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err)
	}

	t.Log(cmdStr, strings.Join(args, " "))
	cmd := exec.CommandContext(ctx, cmdStr, args...)

	var stdinPipe io.WriteCloser
	if inPipeFile != "" {
		var err error
		stdinPipe, err = cmd.StdinPipe()
		if err != nil {
			return stdoutCh, stderrCh, nil, err
		}
	}
	cmdStdout, err := cmd.StdoutPipe()
	if err != nil {
		return stdoutCh, stderrCh, nil, err
	}
	cmdStderr, err := cmd.StderrPipe()
	if err != nil {
		return stdoutCh, stderrCh, nil, err
	}
	err = cmd.Start()
	if err != nil {
		return stdoutCh, stderrCh, nil, err
	}

	// Read input file and send to stdin pipe?
	if inPipeFile != "" {
		t.Log(fmt.Sprintf("Piping %s to stdin pipe", inPipeFile))
		fd, err := os.Open(inPipeFile)
		if err != nil {
			return stdoutCh, stderrCh, nil, err
		}
		go func() {
			io.Copy(stdinPipe, bufio.NewReader(fd))
			stdinPipe.Close()
			fd.Close()
		}()
	}

	go func() {
		// Read raw bytes to preserve line endings, but filter out protocol messages
		buf := make([]byte, 4096)
		var accumulated []byte
		
		for {
			n, err := cmdStdout.Read(buf)
			if n > 0 {
				accumulated = append(accumulated, buf[:n]...)
				
				// Process accumulated data line by line, preserving original line endings
				data := accumulated
				var processedLines [][]byte
				var remaining []byte
				
				// Split on LF while preserving CRLF sequences
				start := 0
				for i := 0; i < len(data); i++ {
					if data[i] == '\n' {
						// Found a complete line (including the \n)
						line := data[start:i+1]
						processedLines = append(processedLines, line)
						start = i + 1
					}
				}
				
				// Keep any remaining partial line
				if start < len(data) {
					remaining = data[start:]
				}
				accumulated = remaining
				
				// Send complete lines, filtering out protocol messages
				for _, line := range processedLines {
					lineStr := string(line)
					lineContent := strings.TrimRight(lineStr, "\r\n")
					
					// Filter out protocol messages like ".syn close connection"
					if strings.HasPrefix(lineContent, ".syn ") || 
					   strings.HasPrefix(lineContent, "CLIENT|") ||
					   strings.HasPrefix(lineContent, "SERVER|") {
						continue
					}
					
					// Check for protocol messages appended to content (like "content.syn close connection")
					if strings.Contains(lineContent, ".syn close connection") {
						// Remove the protocol message from the content
						cleanContent := strings.Replace(lineContent, ".syn close connection", "", 1)
						// Preserve the original line ending
						lineEnding := lineStr[len(lineContent):]
						stdoutCh <- cleanContent + lineEnding
					} else {
						stdoutCh <- lineStr
					}
				}
			}
			if err != nil {
				// Send any remaining data in buffer
				if len(accumulated) > 0 {
					remaining := string(accumulated)
					remainingContent := strings.TrimRight(remaining, "\r\n")
					// Filter out protocol messages
					if strings.HasPrefix(remainingContent, ".syn ") || 
					   strings.HasPrefix(remainingContent, "CLIENT|") ||
					   strings.HasPrefix(remainingContent, "SERVER|") {
						// Skip protocol messages
					} else if strings.Contains(remainingContent, ".syn close connection") {
						// Remove the protocol message from the content
						cleanContent := strings.Replace(remainingContent, ".syn close connection", "", 1)
						// Preserve the original ending
						ending := remaining[len(remainingContent):]
						stdoutCh <- cleanContent + ending
					} else {
						stdoutCh <- remaining
					}
				}
				break
			}
		}
	}()
	go func() {
		scanner := bufio.NewScanner(cmdStderr)
		scanner.Split(bufio.ScanLines)
		for scanner.Scan() {
			stderrCh <- scanner.Text()
		}
		close(stderrCh)
	}()

	cmdErrCh := make(chan error)
	go func() {
		cmdErrCh <- cmd.Wait()
	}()

	return stdoutCh, stderrCh, cmdErrCh, nil
}

func waitForCommand(ctx context.Context, t *testing.T,
	stdoutCh, stderrCh <-chan string, cmdErrCh <-chan error) {

	for {
		select {
		case line, ok := <-stdoutCh:
			if ok {
				t.Log(line)
			}
		case line, ok := <-stderrCh:
			if ok {
				t.Log(line)
			}
		case cmdErr := <-cmdErrCh:
			t.Log(fmt.Sprintf("Command finished with with exit code %d: %v",
				exitCodeFromError(cmdErr), cmdErr))
			return
		}
	}
}

func exitCodeFromError(err error) int {
	if err == nil {
		return 0
	}
	if exitError, ok := err.(*exec.ExitError); ok {
		ws := exitError.Sys().(syscall.WaitStatus)
		return ws.ExitStatus()
	}
	panic(fmt.Sprintf("Unable to get process exit code from error: %v", err))
}