1// +build ignore 2// 3// Build all of the tests. 4// 5// Run as: 6// 7// go run test-compile.go -out name.tar 8 9package main 10 11import ( 12 "archive/zip" 13 "flag" 14 "fmt" 15 "io" 16 "log" 17 "os" 18 "os/exec" 19 "path" 20 21 "github.com/mcu-tools/mcuboot/samples/zephyr/mcutests" 22) 23 24var outFile = flag.String("out", "test-images.zip", "Name of zip file to put built tests into") 25 26func main() { 27 err := run() 28 if err != nil { 29 log.Fatal(err) 30 } 31} 32 33func run() error { 34 flag.Parse() 35 36 zipper, err := NewBuilds() 37 if err != nil { 38 return err 39 } 40 defer zipper.Close() 41 42 for _, group := range mcutests.Tests { 43 fmt.Printf("Compiling %q\n", group.ShortName) 44 c := exec.Command("make", 45 fmt.Sprintf("test-%s", group.ShortName)) 46 // TODO: Should capture the output and show it if 47 // there is an error. 48 err = c.Run() 49 if err != nil { 50 return err 51 } 52 53 err = zipper.Capture(group.ShortName) 54 if err != nil { 55 return err 56 } 57 } 58 59 return nil 60} 61 62// A Builds create a zipfile of the contents of various builds. The 63// names will be constructed. 64type Builds struct { 65 // The file being written to. 66 file *os.File 67 68 // The zip writer writing the data. 69 zip *zip.Writer 70} 71 72func NewBuilds() (*Builds, error) { 73 name := *outFile 74 file, err := os.OpenFile(name, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644) 75 if err != nil { 76 return nil, err 77 } 78 79 z := zip.NewWriter(file) 80 81 return &Builds{ 82 file: file, 83 zip: z, 84 }, nil 85} 86 87func (b *Builds) Close() error { 88 return b.zip.Close() 89} 90 91func (b *Builds) Capture(testName string) error { 92 // Collect stat information from the test directory, which 93 // should be close enough to make the zip file meaningful. 94 info, err := os.Stat(".") 95 if err != nil { 96 return err 97 } 98 99 header, err := zip.FileInfoHeader(info) 100 if err != nil { 101 return err 102 } 103 104 header.Name = testName + "/" 105 106 _, err = b.zip.CreateHeader(header) 107 if err != nil { 108 return err 109 } 110 111 for _, name := range []string{ 112 "mcuboot.bin", 113 "signed-hello1.bin", 114 "signed-hello2.bin", 115 } { 116 err = b.add(testName, name, name) 117 if err != nil { 118 return err 119 } 120 } 121 122 return nil 123} 124 125func (b *Builds) add(baseName, zipName, fileName string) error { 126 inp, err := os.Open(fileName) 127 if err != nil { 128 return err 129 } 130 defer inp.Close() 131 132 info, err := inp.Stat() 133 if err != nil { 134 return err 135 } 136 137 header, err := zip.FileInfoHeader(info) 138 if err != nil { 139 return err 140 } 141 142 header.Name = path.Join(baseName, zipName) 143 header.Method = zip.Deflate 144 145 wr, err := b.zip.CreateHeader(header) 146 if err != nil { 147 return err 148 } 149 150 _, err = io.Copy(wr, inp) 151 if err != nil { 152 return err 153 } 154 155 return nil 156} 157