forked from treeverse/lakeFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlakectl_util.go
285 lines (245 loc) · 10.5 KB
/
lakectl_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package esti
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
// lakectl tests utility functions
var update = flag.Bool("update", false, "update golden files with results")
var (
errValueNotUnique = errors.New("value not unique in mapping")
errVariableNotFound = errors.New("variable not found")
)
var (
reTimestamp = regexp.MustCompile(`timestamp: \d+\n`)
reTime = regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4} \w{1,3}`)
reCommitID = regexp.MustCompile(`[\d|a-f]{64}`)
reShortCommitID = regexp.MustCompile(`[\d|a-f]{16}`)
reChecksum = regexp.MustCompile(`[\d|a-f]{32}`)
reEndpoint = regexp.MustCompile(`https?://\w+(:\d+)?/api/v\d+/`)
rePhysicalAddress = regexp.MustCompile(`/data/[0-9a-v]{20}/[0-9a-v]{20}`)
reVariable = regexp.MustCompile(`\$\{([^${}]+)}`)
rePreSignURL = regexp.MustCompile(`https://\S+data\S+`)
)
func lakectlLocation() string {
return viper.GetString("lakectl_dir") + "/lakectl"
}
func LakectlWithParams(accessKeyID, secretAccessKey, endPointURL string) string {
lakectlCmdline := "LAKECTL_CREDENTIALS_ACCESS_KEY_ID=" + accessKeyID +
" LAKECTL_CREDENTIALS_SECRET_ACCESS_KEY=" + secretAccessKey +
" LAKECTL_SERVER_ENDPOINT_URL=" + endPointURL +
" " + lakectlLocation()
return lakectlCmdline
}
func Lakectl() string {
return LakectlWithParams(viper.GetString("access_key_id"), viper.GetString("secret_access_key"), viper.GetString("endpoint_url"))
}
func runShellCommand(t *testing.T, command string, isTerminal bool) ([]byte, error) {
t.Helper()
t.Logf("Run shell command '%s'", command)
// Assuming linux. Not sure if this is correct
cmd := exec.Command("/bin/sh", "-c", command)
cmd.Env = append(os.Environ(),
"LAKECTL_INTERACTIVE="+strconv.FormatBool(isTerminal),
)
return cmd.CombinedOutput()
}
func runShellCommandWithTimeout(t *testing.T, command string, isTerminal bool, timeout time.Duration) ([]byte, error) {
t.Helper()
t.Logf("Run shell command '%s' with timeout of '%d'", command, timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// Assuming linux. Not sure if this is correct
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", command)
cmd.Env = append(os.Environ(),
"LAKECTL_INTERACTIVE="+strconv.FormatBool(isTerminal),
)
return cmd.CombinedOutput()
}
// expandVariables receives a string with (possibly) variables in the form of {VAR_NAME}, and
// a var-name -> value mapping. It attempts to replace all occurrences of all variables with
// the corresponding values from the map. If all variables in the string has their mapping
// expandVariables is successful and returns the result string. If at least one variable does
// not have a mapping, expandVariables fails and returns error
func expandVariables(s string, vars map[string]string) (string, error) {
s = reVariable.ReplaceAllStringFunc(s, func(varReference string) string {
if val, ok := vars[varReference[2:len(varReference)-1]]; ok {
return val
}
return varReference
})
if missingVar := reVariable.FindString(s); missingVar != "" {
return "", fmt.Errorf("%w, %s", errVariableNotFound, missingVar)
}
return s, nil
}
// embedVariables replaces run-specific values from a string with generic, normalized
// variables, that can later be expanded by expandVariables.
// It receives a string that may contain some run-specific data (e.g. repo-name), and
// a mapping of variable names to values. It then replaces all the values found in the original
// string with the corresponding variable name, in the format of {VAR_NAME}. This string can later
// be consumed by expandVariables.
//
// Notes:
// - embedVariables will fail if 2 different variables maps to the same value. While this is a possible
// scenario (e.g. a file named 'master' in 'master' branch) it cannot be processed by embedVariables
// - Values are processed from longest to shortest, and so, if certain var value contains another (e.g.
// VAR1 -> "xy", VAR2 -> "xyz"), the longest option will be considered first. As an example, the string
// "wxyz contains xy which is a prefix of xyz" will be embedded as
// "w{VAR2} contains {VAR1} which is a prefix of {VAR2}")
func embedVariables(s string, vars map[string]string) (string, error) {
revMap := make(map[string]string)
vals := make([]string, 0, len(vars)) // collecting all vals, which will be used as keys, in order to control iteration order
for k, v := range vars {
if _, exist := revMap[v]; exist {
return "", fmt.Errorf("%w, %s", errValueNotUnique, v)
}
revMap[v] = k
vals = append(vals, v)
}
// Sorting the reversed keys (variable values) by descending length in order to handle longer names first
// This will diminish replacing partial names that were used to construct longer names
sort.Slice(vals, func(i, j int) bool {
return len(vals[i]) > len(vals[j])
})
for _, val := range vals {
s = strings.ReplaceAll(s, val, "${"+revMap[val]+"}")
}
return s, nil
}
func sanitize(output string, vars map[string]string) string {
// The order of execution below is important as certain expression can contain others
// and so, should be handled first
s := strings.ReplaceAll(output, "\r\n", "\n")
if _, ok := vars["DATE"]; !ok {
s = normalizeProgramTimestamp(s)
}
s = normalizeRandomObjectKey(s, vars["STORAGE"])
s = normalizeCommitID(s)
s = normalizeChecksum(s)
s = normalizeShortCommitID(s)
s = normalizeEndpoint(s, vars["LAKEFS_ENDPOINT"])
s = normalizePreSignURL(s) // should be after storage and endpoint to enable non pre-sign url on azure
return s
}
func RunCmdAndVerifySuccessWithFile(t *testing.T, cmd string, isTerminal bool, goldenFile string, vars map[string]string) {
t.Helper()
runCmdAndVerifyWithFile(t, cmd, goldenFile, false, isTerminal, vars)
}
func RunCmdAndVerifyContainsText(t *testing.T, cmd string, isTerminal bool, expectedRaw string, vars map[string]string) {
t.Helper()
s := sanitize(expectedRaw, vars)
expected, err := expandVariables(s, vars)
require.NoErrorf(t, err, "Variable embed failed - %s", err)
sanitizedResult := runCmd(t, cmd, false, isTerminal, vars)
require.Contains(t, sanitizedResult, expected)
}
func RunCmdAndVerifyContainsTextWithTimeout(t *testing.T, cmd string, expectFail bool, isTerminal bool, expectedRaw string, vars map[string]string, timeout time.Duration) {
t.Helper()
s := sanitize(expectedRaw, vars)
expected, err := expandVariables(s, vars)
require.NoErrorf(t, err, "Variable embed failed - %s", err)
sanitizedResult := runCmdWithTimeout(t, cmd, expectFail, isTerminal, vars, timeout)
require.Contains(t, sanitizedResult, expected)
}
func RunCmdAndVerifyFailureWithFile(t *testing.T, cmd string, isTerminal bool, goldenFile string, vars map[string]string) {
t.Helper()
runCmdAndVerifyWithFile(t, cmd, goldenFile, true, isTerminal, vars)
}
func runCmdAndVerifyWithFile(t *testing.T, cmd, goldenFile string, expectFail, isTerminal bool, vars map[string]string) {
t.Helper()
goldenFile = "golden/" + goldenFile + ".golden"
if *update {
updateGoldenFile(t, cmd, isTerminal, goldenFile, vars)
} else {
content, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal("Failed to read", goldenFile, "-", err)
}
expected := sanitize(string(content), vars)
runCmdAndVerifyResult(t, cmd, expectFail, isTerminal, expected, vars)
}
}
func updateGoldenFile(t *testing.T, cmd string, isTerminal bool, goldenFile string, vars map[string]string) {
t.Helper()
result, _ := runShellCommand(t, cmd, isTerminal)
s := sanitize(string(result), vars)
s, err := embedVariables(s, vars)
require.NoError(t, err, "Variable embed failed")
err = os.WriteFile(goldenFile, []byte(s), 0o600) //nolint: gomnd
require.NoErrorf(t, err, "Failed to write file %s", goldenFile)
}
func RunCmdAndVerifySuccess(t *testing.T, cmd string, isTerminal bool, expected string, vars map[string]string) {
t.Helper()
runCmdAndVerifyResult(t, cmd, false, isTerminal, expected, vars)
}
func RunCmdAndVerifyFailure(t *testing.T, cmd string, isTerminal bool, expected string, vars map[string]string) {
t.Helper()
runCmdAndVerifyResult(t, cmd, true, isTerminal, expected, vars)
}
func runCmd(t *testing.T, cmd string, expectFail bool, isTerminal bool, vars map[string]string) string {
t.Helper()
result, err := runShellCommand(t, cmd, isTerminal)
if expectFail {
require.Errorf(t, err, "Expected error in '%s' command did not occur. Output: %s", cmd, string(result))
} else {
require.NoErrorf(t, err, "Failed to run '%s' command - %s", cmd, string(result))
}
return sanitize(string(result), vars)
}
func runCmdWithTimeout(t *testing.T, cmd string, expectFail bool, isTerminal bool, vars map[string]string, timeout time.Duration) string {
t.Helper()
result, err := runShellCommandWithTimeout(t, cmd, isTerminal, timeout)
if expectFail {
require.Errorf(t, err, "Expected error in '%s' command did not occur. Output: %s", cmd, string(result))
} else {
require.NoErrorf(t, err, "Failed to run '%s' command - %s", cmd, string(result))
}
return sanitize(string(result), vars)
}
func runCmdAndVerifyResult(t *testing.T, cmd string, expectFail bool, isTerminal bool, expected string, vars map[string]string) {
t.Helper()
expanded, err := expandVariables(expected, vars)
if err != nil {
t.Fatal("Failed to extract variables for:", cmd)
}
sanitizedResult := runCmd(t, cmd, expectFail, isTerminal, vars)
require.Equalf(t, expanded, sanitizedResult, "Unexpected output for %s command", cmd)
}
func normalizeProgramTimestamp(output string) string {
s := reTimestamp.ReplaceAllString(output, "timestamp: <TIMESTAMP>")
return reTime.ReplaceAllString(s, "<DATE> <TIME> <TZ>")
}
func normalizeRandomObjectKey(output string, objectPrefix string) string {
objectPrefix = strings.TrimPrefix(objectPrefix, "/")
for _, match := range rePhysicalAddress.FindAllString(output, -1) {
output = strings.Replace(output, objectPrefix+match, objectPrefix+"/<OBJECT_KEY>", 1)
}
return output
}
func normalizeCommitID(output string) string {
return reCommitID.ReplaceAllString(output, "<COMMIT_ID>")
}
func normalizeShortCommitID(output string) string {
return reShortCommitID.ReplaceAllString(output, "<COMMIT_ID_16>")
}
func normalizeChecksum(output string) string {
return reChecksum.ReplaceAllString(output, "<CHECKSUM>")
}
func normalizeEndpoint(output string, endpoint string) string {
return reEndpoint.ReplaceAllString(output, endpoint)
}
func normalizePreSignURL(output string) string {
return rePreSignURL.ReplaceAllString(output, "<PRE_SIGN_URL>")
}