| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package suites |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"io/ioutil" |
| 24 |
"os" |
| 25 |
"os/exec" |
| 26 |
"path/filepath" |
| 27 |
"regexp" |
| 28 |
"strings" |
| 29 |
"time" |
| 30 |
|
| 31 |
. "gopkg.in/check.v1" |
| 32 |
|
| 33 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 34 |
) |
| 35 |
|
| 36 |
// FillConfig fills cfg from values. |
| 37 |
func FillConfig(cfg, values map[string]interface{}) { |
| 38 |
for k, v := range values { |
| 39 |
cfg[k] = v |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// FillServerConfig fills cfg with default server values and "addr": addr. |
| 44 |
func FillServerConfig(cfg map[string]interface{}, addr string) { |
| 45 |
FillConfig(cfg, map[string]interface{}{ |
| 46 |
"exchange_timeout": "0.1s", |
| 47 |
"ping_interval": "0.5s", |
| 48 |
"session_queue_size": 10, |
| 49 |
"broker_queue_size": 100, |
| 50 |
"addr": addr, |
| 51 |
"key_pem_file": helpers.SourceRelative("../ssl/testing.key"), |
| 52 |
"cert_pem_file": helpers.SourceRelative("../ssl/testing.cert"), |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
const MaxNotificationsPerApplication = 45 |
| 57 |
|
| 58 |
// FillHttpServerConfig fills cfg with default http server values and |
| 59 |
// "http_addr": httpAddr. |
| 60 |
func FillHTTPServerConfig(cfg map[string]interface{}, httpAddr string) { |
| 61 |
FillConfig(cfg, map[string]interface{}{ |
| 62 |
"http_addr": httpAddr, |
| 63 |
"http_read_timeout": "1s", |
| 64 |
"http_write_timeout": "1s", |
| 65 |
"max_notifications_per_app": MaxNotificationsPerApplication, |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
// WriteConfig writes out a config and returns the written filepath. |
| 70 |
func WriteConfig(c *C, dir, filename string, cfg map[string]interface{}) string { |
| 71 |
cfgFpath := filepath.Join(dir, filename) |
| 72 |
cfgJson, err := json.Marshal(cfg) |
| 73 |
if err != nil { |
| 74 |
c.Fatal(err) |
| 75 |
} |
| 76 |
err = ioutil.WriteFile(cfgFpath, cfgJson, os.ModePerm) |
| 77 |
if err != nil { |
| 78 |
c.Fatal(err) |
| 79 |
} |
| 80 |
return cfgFpath |
| 81 |
} |
| 82 |
|
| 83 |
var rxLineInfo = regexp.MustCompile("^.*?(?: .+\\.go:\\d+:)? ([[:alpha:]].*)\n") |
| 84 |
|
| 85 |
// RunAndObserve runs cmdName and returns a channel that will receive |
| 86 |
// cmdName stderr logging and a function to kill the process. |
| 87 |
func RunAndObserve(c *C, cmdName string, arg ...string) (<-chan string, func(os.Signal)) { |
| 88 |
cmd := exec.Command(cmdName, arg...) |
| 89 |
stderr, err := cmd.StderrPipe() |
| 90 |
if err != nil { |
| 91 |
c.Fatal(err) |
| 92 |
} |
| 93 |
err = cmd.Start() |
| 94 |
if err != nil { |
| 95 |
c.Fatal(err) |
| 96 |
} |
| 97 |
bufErr := bufio.NewReaderSize(stderr, 5000) |
| 98 |
getLineInfo := func(full bool) (string, error) { |
| 99 |
for { |
| 100 |
line, err := bufErr.ReadString('\n') |
| 101 |
if err != nil { |
| 102 |
return "", err |
| 103 |
} |
| 104 |
if full { |
| 105 |
return strings.TrimRight(line, "\n"), nil |
| 106 |
} |
| 107 |
extracted := rxLineInfo.FindStringSubmatch(line) |
| 108 |
if extracted == nil { |
| 109 |
return "", fmt.Errorf("unexpected line: %#v", line) |
| 110 |
} |
| 111 |
info := extracted[1] |
| 112 |
return info, nil |
| 113 |
} |
| 114 |
} |
| 115 |
logs := make(chan string, 10) |
| 116 |
go func() { |
| 117 |
panicked := false |
| 118 |
for { |
| 119 |
info, err := getLineInfo(panicked) |
| 120 |
if err != nil { |
| 121 |
logs <- fmt.Sprintf("%s capture: %v", cmdName, err) |
| 122 |
close(logs) |
| 123 |
return |
| 124 |
} |
| 125 |
if panicked || strings.HasPrefix(info, "ERROR(PANIC") { |
| 126 |
panicked = true |
| 127 |
c.Log(info) |
| 128 |
continue |
| 129 |
} |
| 130 |
if strings.HasPrefix(info, "DEBUG ") && !strings.HasPrefix(info, "DEBUG session(") { |
| 131 |
// skip non session DEBUG logs |
| 132 |
c.Log(info) |
| 133 |
continue |
| 134 |
} |
| 135 |
logs <- info |
| 136 |
} |
| 137 |
}() |
| 138 |
return logs, func(sig os.Signal) { cmd.Process.Signal(sig) } |
| 139 |
} |
| 140 |
|
| 141 |
const ( |
| 142 |
DevListeningOnPat = "INFO listening for devices on " |
| 143 |
HTTPListeningOnPat = "INFO listening for http on " |
| 144 |
) |
| 145 |
|
| 146 |
// ExtractListeningAddr goes over logs until a line starting with pat |
| 147 |
// and returns the rest of that line. |
| 148 |
func ExtractListeningAddr(c *C, logs <-chan string, pat string) string { |
| 149 |
for line := range logs { |
| 150 |
if !strings.HasPrefix(line, pat) { |
| 151 |
c.Fatalf("matching %v: %v", pat, line) |
| 152 |
} |
| 153 |
return line[len(pat):] |
| 154 |
} |
| 155 |
panic(fmt.Errorf("logs closed unexpectedly marching %v", pat)) |
| 156 |
} |
| 157 |
|
| 158 |
// NextEvent receives an event from given string channel with a 5s timeout, |
| 159 |
// or from a channel for errors. |
| 160 |
func NextEvent(events <-chan string, errCh <-chan error) string { |
| 161 |
select { |
| 162 |
case <-time.After(5 * time.Second): |
| 163 |
panic("too long stuck waiting for next event") |
| 164 |
case err := <-errCh: |
| 165 |
return err.Error() // will fail comparison typically |
| 166 |
case evStr := <-events: |
| 167 |
return evStr |
| 168 |
} |
| 169 |
} |