-
Notifications
You must be signed in to change notification settings - Fork 15
/
tests.go
123 lines (103 loc) · 2.32 KB
/
tests.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2018-2022, CS Systemes d'Information, http://csgroup.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tests
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
"github.com/sirupsen/logrus"
)
var preservedOutput = os.Stdout
// Used for tests to capture logrus output
func LogrusCapture(routine func()) string {
log := ""
r, w, _ := os.Pipe()
logrus.SetOutput(w)
os.Stdout = w
routine()
os.Stdout = preservedOutput
logrus.SetOutput(preservedOutput)
err := w.Close()
if err != nil {
return ""
}
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err == nil {
log = buf.String()
}
fmt.Println(log)
return log
}
// Used for tests to run code segment with timelimit
// return status of (true: in time, false: timeouted)
func TimelimitCapture(routine func(), timeout time.Duration) bool {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
routine()
}()
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return true
case <-time.After(timeout):
logrus.Warnf("Routine exceed timelimit of %d sec", timeout/1000000000)
return false
}
}
// Check if Minio is locally online
/*
Fragment TOML
[[tenants]]
[tenants.objectstorage]
Type = "s3"
Region = "stub"
Endpoint = "http://localhost:9000"
AccessKey = "accessKey"
SecretKey = "secretKey"
*/
func MinioOnline() bool {
endpoint := func() string {
value, ok := os.LookupEnv("MINIO_HEALTH_ENDPOINT")
if !ok {
value = "http://localhost:9000/"
}
return value
}()
return func() (status bool) {
resp, err := http.Get(endpoint)
if err != nil {
return false
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
status = false
}
}(resp.Body)
return true
}()
}