| 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 testing contains helpers for testing. |
| 18 |
package testing |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"net/url" |
| 24 |
"os" |
| 25 |
"path" |
| 26 |
"path/filepath" |
| 27 |
"runtime" |
| 28 |
"strings" |
| 29 |
"sync" |
| 30 |
|
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 33 |
) |
| 34 |
|
| 35 |
type captureHelper struct { |
| 36 |
outputFunc func(int, string) error |
| 37 |
lock sync.Mutex |
| 38 |
logEvents []string |
| 39 |
logEventCb func(string) |
| 40 |
} |
| 41 |
|
| 42 |
func (h *captureHelper) Output(calldepth int, s string) error { |
| 43 |
err := h.outputFunc(calldepth+2, s) |
| 44 |
if err == nil { |
| 45 |
h.lock.Lock() |
| 46 |
defer h.lock.Unlock() |
| 47 |
if h.logEventCb != nil { |
| 48 |
h.logEventCb(s) |
| 49 |
} |
| 50 |
h.logEvents = append(h.logEvents, s+"\n") |
| 51 |
} |
| 52 |
return err |
| 53 |
} |
| 54 |
|
| 55 |
func (h *captureHelper) captured() string { |
| 56 |
h.lock.Lock() |
| 57 |
defer h.lock.Unlock() |
| 58 |
return strings.Join(h.logEvents, "") |
| 59 |
} |
| 60 |
|
| 61 |
func (h *captureHelper) reset() { |
| 62 |
h.lock.Lock() |
| 63 |
defer h.lock.Unlock() |
| 64 |
h.logEvents = nil |
| 65 |
} |
| 66 |
|
| 67 |
func (h *captureHelper) setLogEventCb(cb func(string)) { |
| 68 |
h.lock.Lock() |
| 69 |
defer h.lock.Unlock() |
| 70 |
h.logEventCb = cb |
| 71 |
} |
| 72 |
|
| 73 |
// TestLogger implements logger.Logger using gocheck.C and supporting |
| 74 |
// capturing log strings. |
| 75 |
type TestLogger struct { |
| 76 |
logger.Logger |
| 77 |
helper *captureHelper |
| 78 |
} |
| 79 |
|
| 80 |
// NewTestLogger can be used in tests instead of |
| 81 |
// NewSimpleLogger(FromMinimalLogger). |
| 82 |
func NewTestLogger(minLog logger.MinimalLogger, level string) *TestLogger { |
| 83 |
h := &captureHelper{outputFunc: minLog.Output} |
| 84 |
log := &TestLogger{ |
| 85 |
Logger: logger.NewSimpleLoggerFromMinimalLogger(h, level), |
| 86 |
helper: h, |
| 87 |
} |
| 88 |
return log |
| 89 |
} |
| 90 |
|
| 91 |
// Captured returns accumulated log events. |
| 92 |
func (tlog *TestLogger) Captured() string { |
| 93 |
return tlog.helper.captured() |
| 94 |
} |
| 95 |
|
| 96 |
// Reset resets accumulated log events. |
| 97 |
func (tlog *TestLogger) ResetCapture() { |
| 98 |
tlog.helper.reset() |
| 99 |
} |
| 100 |
|
| 101 |
// SetLogEventCb sets a callback invoked for log events. |
| 102 |
func (tlog *TestLogger) SetLogEventCb(cb func(string)) { |
| 103 |
tlog.helper.setLogEventCb(cb) |
| 104 |
} |
| 105 |
|
| 106 |
// SourceRelative produces a path relative to the source code, makes |
| 107 |
// sense only for tests when the code is available on disk. |
| 108 |
func SourceRelative(relativePath string) string { |
| 109 |
_, file, _, ok := runtime.Caller(1) |
| 110 |
if !ok { |
| 111 |
panic("failed to get source filename using Caller()") |
| 112 |
} |
| 113 |
dir := filepath.Dir(file) |
| 114 |
|
| 115 |
root := os.Getenv("LOMIRI_PUSH_TEST_RESOURCES_ROOT") |
| 116 |
if root != "" { |
| 117 |
const sep = "lomiri-push-service" |
| 118 |
|
| 119 |
idx := strings.LastIndex(dir, sep) |
| 120 |
if idx == -1 { |
| 121 |
panic(fmt.Errorf("unable to find %s in %#v", sep, dir)) |
| 122 |
} |
| 123 |
idx += len(sep) |
| 124 |
|
| 125 |
dir = filepath.Join(root, dir[idx:]) |
| 126 |
} |
| 127 |
return filepath.Join(dir, relativePath) |
| 128 |
} |
| 129 |
|
| 130 |
// ScriptAbsPath gets the absolute path to a script in the scripts directory |
| 131 |
// assuming we're in a subdirectory of the project |
| 132 |
func ScriptAbsPath(script string) string { |
| 133 |
cwd, err := os.Getwd() |
| 134 |
if err != nil { |
| 135 |
panic(fmt.Errorf("unable to get working directory: %v", err)) |
| 136 |
} |
| 137 |
|
| 138 |
if !path.IsAbs(cwd) { |
| 139 |
panic(fmt.Errorf("working directory not absolute? %v", cwd)) |
| 140 |
} |
| 141 |
|
| 142 |
for cwd != "/" { |
| 143 |
filename := path.Join(cwd, "scripts", script) |
| 144 |
_, err := os.Stat(filename) |
| 145 |
if err == nil { |
| 146 |
return filename |
| 147 |
} |
| 148 |
if !os.IsNotExist(err) { |
| 149 |
panic(fmt.Errorf("unable to stat %v: %v", filename, err)) |
| 150 |
} |
| 151 |
cwd = path.Dir(cwd) |
| 152 |
} |
| 153 |
panic(fmt.Errorf("unable to find script %v", script)) |
| 154 |
} |
| 155 |
|
| 156 |
// Ns makes a []Notification from just payloads. |
| 157 |
func Ns(payloads ...json.RawMessage) []protocol.Notification { |
| 158 |
res := make([]protocol.Notification, len(payloads)) |
| 159 |
for i := 0; i < len(payloads); i++ { |
| 160 |
res[i].Payload = payloads[i] |
| 161 |
} |
| 162 |
return res |
| 163 |
} |
| 164 |
|
| 165 |
// ParseURL parses a URL conveniently. |
| 166 |
func ParseURL(s string) *url.URL { |
| 167 |
purl, err := url.Parse(s) |
| 168 |
if err != nil { |
| 169 |
panic(err) |
| 170 |
} |
| 171 |
return purl |
| 172 |
} |
| 173 |
|
| 174 |
// DumpGoroutines dumps current goroutines. |
| 175 |
func DumpGoroutines() { |
| 176 |
var buf [64 * 1024]byte |
| 177 |
sz := runtime.Stack(buf[:], true) |
| 178 |
dump := string(buf[:sz]) |
| 179 |
fmt.Println(dump) |
| 180 |
fmt.Println("#goroutines#", strings.Count("\n"+dump, "\ngoroutine ")) |
| 181 |
} |