-
Notifications
You must be signed in to change notification settings - Fork 40
/
env.go
104 lines (93 loc) · 2.18 KB
/
env.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package testutils
import (
"net"
"os"
"testing"
"time"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.viam.com/test"
)
var (
logger = golog.Global().Named("test")
noSkip = false
internetConnected *bool
)
func skipWithError(tb testing.TB, err error) {
tb.Helper()
if noSkip {
tb.Fatal(err)
return
}
tb.Skip(err)
}
// SkipUnlessInternet verifies there is an internet connection.
func SkipUnlessInternet(tb testing.TB) {
tb.Helper()
if internetConnected == nil {
var connected bool
conn, err := net.DialTimeout("tcp", "mozilla.org:80", 5*time.Second)
if err == nil {
test.That(tb, conn.Close(), test.ShouldBeNil)
connected = true
}
internetConnected = &connected
}
if *internetConnected {
return
}
skipWithError(tb, errors.New("internet not connected"))
}
func artifactGoogleCreds() (string, error) {
creds, ok := os.LookupEnv("ARTIFACT_GOOGLE_APPLICATION_CREDENTIALS")
if !ok || creds == "" {
return "", errors.New("no artifact google credentials found")
}
return creds, nil
}
// SkipUnlessArtifactGoogleCreds verifies google credentials are available for artifact.
func SkipUnlessArtifactGoogleCreds(tb testing.TB) {
tb.Helper()
_, err := artifactGoogleCreds()
if err == nil {
return
}
skipWithError(tb, err)
}
// ArtifactGoogleCreds returns the google credentials for artifact.
func ArtifactGoogleCreds(tb testing.TB) string {
tb.Helper()
creds, err := artifactGoogleCreds()
if err != nil {
skipWithError(tb, err)
return ""
}
return creds
}
func backingMongoDBURI() (string, error) {
mongoURI, ok := os.LookupEnv("TEST_MONGODB_URI")
if !ok || mongoURI == "" {
return "", errors.New("no MongoDB URI found")
}
setupMongoDBForTests()
return mongoURI, nil
}
// SkipUnlessBackingMongoDBURI verifies there is a backing MongoDB URI to use.
func SkipUnlessBackingMongoDBURI(tb testing.TB) {
tb.Helper()
_, err := backingMongoDBURI()
if err == nil {
return
}
skipWithError(tb, err)
}
// BackingMongoDBURI returns the backing MongoDB URI to use.
func BackingMongoDBURI(tb testing.TB) string {
tb.Helper()
mongoURI, err := backingMongoDBURI()
if err != nil {
skipWithError(tb, err)
return ""
}
return mongoURI
}