forked from m4ksio/testingdock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
56 lines (46 loc) · 1.33 KB
/
helpers.go
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
package testingdock
import (
"fmt"
"net"
"strconv"
"testing"
"time"
)
// printf just wraps fmt.Printf.
func printf(format string, args ...interface{}) {
fmt.Printf("··· DOCK: %v %s\n", time.Now().UTC(), fmt.Sprintf(format, args...))
}
// RandomPort returns a random available port as a string.
func RandomPort(t testing.TB) string {
return strconv.FormatInt(int64(randomPort(t)), 10)
}
// randomPort returns random available port as an int.
func randomPort(t testing.TB) int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
t.Fatalf("testingdock: resolve failure: %s", err.Error())
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
t.Fatalf("testingdock: listen failure: %s", err.Error())
}
if err := l.Close(); err != nil {
t.Fatalf("testingdock: listener closing failure: %s", err.Error())
}
return l.Addr().(*net.TCPAddr).Port
}
// Check whether a map containing labels has the "owner=testingdock" label.
func isOwnedByTestingdock(labels map[string]string) bool {
for key, value := range labels {
if fmt.Sprintf("%s=%s", key, value) == "owner=testingdock" {
return true
}
}
return false
}
// Create a map of labels containting the "owner=testingdock" label.
func createTestingLabel() map[string]string {
labels := make(map[string]string)
labels["owner"] = "testingdock"
return labels
}