/
util.go
176 lines (148 loc) · 3.71 KB
/
util.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package testutil
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"reflect"
"regexp"
"strings"
"testing"
"text/template"
"ecsdeployer.com/ecsdeployer/internal/util"
"github.com/jmespath/go-jmespath"
"github.com/webdestroya/awsmocker"
"github.com/webdestroya/go-log"
"golang.org/x/exp/maps"
)
func DisableLoggingForTest(t *testing.T) {
orig := log.Log
t.Cleanup(func() {
log.Log = orig
})
log.Log = log.New(io.Discard)
}
func DisableLogging() {
log.Log = log.New(io.Discard)
}
func TemplateApply(tpl string, fields interface{}) string {
tplate, err := template.New("testutil").Parse(tpl)
if err != nil {
panic(err)
}
var buffer bytes.Buffer
err = tplate.Execute(&buffer, fields)
if err != nil {
panic(err)
}
return buffer.String()
}
func jsonify(obj interface{}) string {
return util.Must(util.Jsonify(obj))
}
func JmesPathSearch(obj interface{}, searchPath string) interface{} {
result, err := jmespath.Search(searchPath, obj)
if err != nil {
panic(fmt.Errorf("Failed to find '%s': %w", searchPath, err))
}
return result
}
func JmesSearchOrNil(obj interface{}, searchPath string) interface{} {
result, err := jmespath.Search(searchPath, obj)
if err != nil {
return nil
}
return result
}
func JmesRequestMatcher(jmesMap map[string]interface{}) func(*awsmocker.ReceivedRequest) bool {
cleanMap := make(map[string]interface{}, len(jmesMap))
searchPaths := maps.Keys(jmesMap)
// searchPaths := make([]string, 0, len(jmesMap))
for k, v := range jmesMap {
// searchPaths = append(searchPaths, k)
switch val := v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32:
cleanMap[k] = numberToFloat64(val)
case string, bool, float64, nil:
cleanMap[k] = v
default:
panic("jmes expressions should evaluate to a string/bool/number/nil")
}
}
return func(rr *awsmocker.ReceivedRequest) bool {
newMap := make(map[string]interface{}, len(cleanMap))
for _, k := range searchPaths {
newMap[k] = JmesSearchOrNil(rr.JsonPayload, k)
// fmt.Printf("COMPARING: %s (%v, %v) ? [%T, %T]\n", k, newMap[k], cleanMap[k], newMap[k], cleanMap[k])
}
return reflect.DeepEqual(newMap, cleanMap)
}
}
func numberToFloat64(value any) any {
if value == nil {
return nil
}
switch v := value.(type) {
case int:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case uint:
return float64(v)
case uint8:
return float64(v)
case uint16:
return float64(v)
case uint32:
return float64(v)
case uint64:
return float64(v)
case float32:
return float64(v)
}
panic(fmt.Errorf("bad type: %T is not a numerical type", value))
}
// Go doesnt have nice heredocs or <<~EOF things.
// this lets me not write ugly yaml blocks for test cases
func CleanTestYaml(value string) string {
return strings.ReplaceAll(StripIndentation(value), "\t", " ")
}
var (
leadingWhitespace = regexp.MustCompile("^[ \t]+")
)
func StripIndentation(value string) string {
var indent string
scanner := bufio.NewScanner(strings.NewReader(value))
for scanner.Scan() {
line := scanner.Text()
// empty lines are fine
if len(line) == 0 {
continue
}
matches := leadingWhitespace.FindStringSubmatch(line)
// we found no matches, that means that part of this string is at the root. abort
if len(matches) == 0 {
return value
}
prefix := matches[0]
if indent == "" || (len(prefix) < len(indent)) {
indent = prefix
}
}
return regexp.MustCompile("(?m)^"+indent).ReplaceAllString(value, "")
}
func RandomHex(n int) string {
bytes := make([]byte, (n+1)/2)
if _, err := rand.Read(bytes); err != nil {
panic(err)
}
return hex.EncodeToString(bytes)
}