forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutil.go
136 lines (121 loc) · 3.02 KB
/
testutil.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
124
125
126
127
128
129
130
131
132
133
134
135
136
package test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"os"
"testing"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/yaml"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)
// StartInformer is a helper to start an informer, wait for its cache to sync and return a cancel func
func StartInformer(informer cache.SharedIndexInformer) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
go informer.Run(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
log.Fatal("Timed out waiting for informer cache to sync")
}
return cancel
}
// GetFreePort finds an available free port on the OS
func GetFreePort() (int, error) {
ln, err := net.Listen("tcp", "[::]:0")
if err != nil {
return 0, err
}
return ln.Addr().(*net.TCPAddr).Port, ln.Close()
}
// WaitForPortListen waits until the given address is listening on the port
func WaitForPortListen(addr string, timeout time.Duration) error {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
timer := time.NewTimer(timeout)
if timeout == 0 {
timer.Stop()
} else {
defer timer.Stop()
}
for {
select {
case <-ticker.C:
if portIsOpen(addr) {
return nil
}
case <-timer.C:
return fmt.Errorf("timeout after %s", timeout.String())
}
}
}
func portIsOpen(addr string) bool {
conn, err := net.Dial("tcp", addr)
if err != nil {
return false
}
_ = conn.Close()
return true
}
// Read the contents of a file and returns it as string. Panics on error.
func MustLoadFileToString(path string) string {
o, err := os.ReadFile(path)
if err != nil {
panic(err.Error())
}
return string(o)
}
func YamlToUnstructured(yamlStr string) *unstructured.Unstructured {
obj := make(map[string]interface{})
err := yaml.Unmarshal([]byte(yamlStr), &obj)
if err != nil {
panic(err)
}
return &unstructured.Unstructured{Object: obj}
}
func YamlToApplication(yamlStr string) *v1alpha1.Application {
app := v1alpha1.Application{}
err := yaml.Unmarshal([]byte(yamlStr), &app)
if err != nil {
panic(err)
}
return &app
}
// ToMap converts any object to a map[string]interface{}
func ToMap(obj interface{}) map[string]interface{} {
data, err := json.Marshal(obj)
if err != nil {
panic(err)
}
var res map[string]interface{}
err = json.Unmarshal(data, &res)
if err != nil {
panic(err)
}
return res
}
// GetTestDir will return the full directory path of the
// calling test file.
func GetTestDir(t *testing.T) string {
t.Helper()
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return cwd
}
// CaptureLogEntries captures log entries generated by the logger and return it as string
func CaptureLogEntries(run func()) string {
f := log.StandardLogger().Formatter
log.SetFormatter(&log.TextFormatter{DisableColors: true})
defer log.SetFormatter(f)
output := bytes.NewBuffer(nil)
log.SetOutput(output)
log.SetLevel(log.DebugLevel)
defer log.SetOutput(os.Stdout)
run()
return output.String()
}