run-tests.go 5.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// +build ignore
//
// Build multiple configurations of MCUboot for Zephyr, making sure
// that they run properly.
//
// Run as:
//
//    go run run-tests.go [flags]
//
// Add -help as a flag to get help.  See comment below for logIn on
// how to configure terminal output to a file so this program can see
// the output of the Zephyr device.

package main

import (
17
	"archive/zip"
18
19
20
21
22
23
24
25
26
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"strings"
	"time"
27

28
	"github.com/mcu-tools/mcuboot/samples/zephyr/mcutests"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
)

// logIn gives the pathname of the log output from the Zephyr device.
// In order to see the serial output, but still be useful for human
// debugging, the output of the terminal emulator should be teed to a
// file that this program will read from.  This can be done with
// something like:
//
//     picocom -b 115200 /dev/ttyACM0 | tee /tmp/zephyr.out
//
// Other terminal programs should also have logging options.
var logIn = flag.String("login", "/tmp/zephyr.out", "File name of terminal log from Zephyr device")

// Output from this test run is written to the given log file.
var logOut = flag.String("logout", "tests.log", "Log file to write to")

45
46
var preBuilt = flag.String("prebuilt", "", "Name of file with prebuilt tests")

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
func main() {
	err := run()
	if err != nil {
		log.Fatal(err)
	}
}

func run() error {
	flag.Parse()

	lines := make(chan string, 30)
	go readLog(lines)

	// Write output to a log file
	logFile, err := os.Create(*logOut)
	if err != nil {
		return err
	}
	defer logFile.Close()
	lg := bufio.NewWriter(logFile)
	defer lg.Flush()

69
70
71
72
73
74
75
76
77
78
79
	var extractor *Extractor

	if *preBuilt != "" {
		// If there are pre-built images, open them.
		extractor, err = NewExtractor(*preBuilt)
		if err != nil {
			return err
		}
		defer extractor.Close()
	}

80
81
	for _, group := range mcutests.Tests {
		fmt.Printf("Running %q\n", group.Name)
82
		fmt.Fprintf(lg, "-------------------------------------\n")
83
		fmt.Fprintf(lg, "---- Running %q\n", group.Name)
84

85
		for _, test := range group.Tests {
86
87
88
89
			if *preBuilt == "" {
				// No prebuilt, build the tests
				// ourselves.
				err = runCommands(test.Build, lg)
90
91
92
				if err != nil {
					return err
				}
93
			} else {
94
95
96
97
98
99
				// Extract the build artifacts from
				// the zip file.
				err = extractor.Extract(group.ShortName)
				if err != nil {
					return err
				}
100
101
102
103
104
			}

			err = runCommands(test.Commands, lg)
			if err != nil {
				return err
105
106
			}

107
			err = expect(lg, lines, test.Expect)
108
109
110
111
112
113
114
115
116
117
118
119
			if err != nil {
				return err
			}

			fmt.Fprintf(lg, "---- Passed\n")
		}
		fmt.Printf("    Passed!\n")
	}

	return nil
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Run a set of commands
func runCommands(cmds [][]string, lg io.Writer) error {
	for _, cmd := range cmds {
		fmt.Printf("    %s\n", cmd)
		fmt.Fprintf(lg, "---- Run: %s\n", cmd)
		err := runCommand(cmd, lg)
		if err != nil {
			return err
		}
	}

	return nil
}

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
// Run a single command.
func runCommand(cmd []string, lg io.Writer) error {
	c := exec.Command(cmd[0], cmd[1:]...)
	c.Stdout = lg
	c.Stderr = lg
	return c.Run()
}

// Expect the given string.
func expect(lg io.Writer, lines <-chan string, exp string) error {
	// Read lines, and if we hit a timeout before seeing our
	// expected line, then consider that an error.
	fmt.Fprintf(lg, "---- expect: %q\n", exp)

	stopper := time.NewTimer(10 * time.Second)
	defer stopper.Stop()
outer:
	for {
		select {
		case line := <-lines:
			fmt.Fprintf(lg, "---- target: %q\n", line)
			if strings.Contains(line, exp) {
				break outer
			}
		case <-stopper.C:
			fmt.Fprintf(lg, "timeout, didn't receive output\n")
			return fmt.Errorf("timeout, didn't receive expected string: %q", exp)
		}
	}

	return nil
}

// Read things from the log file, discarding everything already there.
func readLog(sink chan<- string) {
	file, err := os.Open(*logIn)
	if err != nil {
		log.Fatal(err)
	}

	_, err = file.Seek(0, 2)
	if err != nil {
		log.Fatal(err)
	}

	prefix := ""
	for {
		// Read lines until EOF, then delay a bit, and do it
		// all again.
		rd := bufio.NewReader(file)

		for {
			line, err := rd.ReadString('\n')
			if err == io.EOF {
				// A partial line can happen because
				// we are racing with the writer.
				if line != "" {
					prefix = line
				}
				break
			}
			if err != nil {
				log.Fatal(err)
			}

			line = prefix + line
			prefix = ""
			sink <- line
			// fmt.Printf("line: %q\n", line)
		}

		// Pause a little
		time.Sleep(250 * time.Millisecond)
	}
}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

// An Extractor holds an opened Zip file, and is able to extract files
// based on the directory name.
type Extractor struct {
	file *os.File
	zip  *zip.Reader
}

// NewExtractor returns an Extractor based on the contents of a zip
// file.
func NewExtractor(name string) (*Extractor, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	size, err := f.Seek(0, 2)
	if err != nil {
		f.Close()
		return nil, err
	}

	rd, err := zip.NewReader(f, size)
	if err != nil {
		f.Close()
		return nil, err
	}

	return &Extractor{
		file: f,
		zip:  rd,
	}, nil
}

func (e *Extractor) Close() error {
	return e.file.Close()
}

// Extract extracts the files of the given directory name into the
// current directory.  These files will overwrite any files of these
// names that already exist (presumably from previous extractions).
func (e *Extractor) Extract(dir string) error {
	prefix := dir + "/"

	count := 0
	for _, file := range e.zip.File {
		if len(file.Name) > len(prefix) && strings.HasPrefix(file.Name, prefix) {
			outName := file.Name[len(prefix):len(file.Name)]
			fmt.Printf("->%q\n", outName)

			err := e.single(file, outName)
			if err != nil {
				return err
			}

			count += 1
		}
	}

	if count == 0 {
		return fmt.Errorf("File for %s missing from archive", dir)
	}

	return nil
}

// single extracts a single file from the zip archive, writing the
// results to a file 'outName'.
func (e *Extractor) single(file *zip.File, outName string) error {
	inf, err := file.Open()
	if err != nil {
		return err
	}

	outf, err := os.Create(outName)
	if err != nil {
		return err
	}
	defer outf.Close()

	_, err = io.Copy(outf, inf)
	return err
}