forked from bruin-data/bruin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-test.go
218 lines (193 loc) · 5.55 KB
/
integration-test.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
jd "github.com/josephburnett/jd/lib"
"github.com/pkg/errors"
)
var currentFolder string
func main() {
path, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
currentFolder = filepath.Join(path, "integration-tests")
if runtime.GOOS == "windows" {
out, err := exec.Command("mv", "bin/bruin", "bin/bruin.exe").Output()
if err != nil {
fmt.Printf("failed to rename binary for execution on windows: %s\n", out)
panic(err)
}
}
expectJSONOutput("internal parse-pipeline happy-path", "happy-path/expectations/pipeline.yml.json")
expectExitCode("run --tag include --exclude-tag exclude chess-extended", 0)
expectExitCode("run --tag include --exclude-tag exclude chess-extended/expectations/chess_games.asset.yml", 1)
expectOutputIncludes(
"run --tag include --exclude-tag exclude --only checks chess-extended",
0,
[]string{"Executed 1 tasks", "total_games:positive"},
)
expectOutputIncludes(
"run --tag include --exclude-tag exclude --only main chess-extended",
0,
[]string{"Executed 3 tasks", " Finished: chess_playground.games", "Finished: chess_playground.profiles", "Finished: chess_playground.game_outcome_summary"},
)
expectOutputIncludes(
"run --push-metadata --only push-metadata bigquery-metadata",
0,
[]string{" Starting: shopify_raw.products:metadata-push", "Starting: shopify_raw.inventory_items:metadata-push"},
)
expectExitCode("validate happy-path", 0)
expectExitCode("run --use-uv happy-path", 0)
// expectExitCode("run happy-path", 0)
expectJSONOutput(
"internal parse-asset happy-path/assets/asset.py",
"happy-path/expectations/asset.py.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/chess_games.asset.yml",
"happy-path/expectations/chess_games.asset.yml.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/chess_profiles.asset.yml",
"happy-path/expectations/chess_profiles.asset.yml.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/player_summary.sql",
"happy-path/expectations/player_summary.sql.json",
)
expectOutputIncludes(
"internal parse-asset faulty-pipeline/assets/error.sql",
1,
[]string{"error creating asset from file", "unmarshal errors"},
)
expectJSONOutput(
"validate -o json missing-upstream/assets/nonexistent.sql",
"missing-upstream/expectations/missing_upstream.json",
)
expectOutputIncludes(
"run malformed/assets/malformed.sql",
1,
[]string{"Parser Error: syntax error at or near \"S_ELECT_\"", "Failed assets 1"},
)
expectJSONOutput(
"internal connections",
"expected_connections_schema.json",
)
expectJSONOutput(
"connections list -o json",
"expected_connections.json",
)
expectJSONOutput(
"internal parse-pipeline -c lineage",
"lineage/expectations/lineage.json",
)
expectJSONOutput(
"internal parse-asset -c lineage/assets/example.sql",
"lineage/expectations/lineage-asset.json",
)
}
func expectOutputIncludes(command string, code int, contains []string) {
output, exitCode, err := runCommandWithExitCode(command)
if err != nil {
if exitCode != code {
fmt.Println("Failed:", err)
os.Exit(1)
}
}
for _, c := range contains {
if !strings.Contains(output, c) {
fmt.Printf("Failed:, output of '%s does not contain '%s'", command, c)
os.Exit(1)
}
}
fmt.Println("Passed")
}
func expectJSONOutput(command string, jsonFilePath string) {
output, err := runCommand(command)
if err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
expectation, err := jd.ReadJsonFile(filepath.Join(currentFolder, jsonFilePath))
if err != nil {
fmt.Println("Failed to read expectation:", err)
os.Exit(1)
}
// normalize linebreaks
parsedOutput, err := jd.ReadJsonString(strings.ReplaceAll(output, "\\r\\n", "\\n"))
if err != nil {
fmt.Println("Failed to parse output:", err)
os.Exit(1)
}
diff := expectation.Diff(parsedOutput)
if len(diff) != 0 {
var path jd.JsonNode
for _, d := range diff {
// Paths are hell to normalize, we skip
path = d.Path[len(d.Path)-1]
if path.Json() == "\"path\"" {
continue
}
fmt.Println("Mismatch at: ", d.Path)
fmt.Print("Expected json: ")
fmt.Println(d.NewValues)
fmt.Print("Not Matching found json: ")
fmt.Println(d.OldValues)
os.Exit(1)
}
}
fmt.Println("Passed")
}
func expectExitCode(command string, code int) {
output, exitCode, err := runCommandWithExitCode(command)
if err != nil {
if exitCode != code {
fmt.Println(strconv.Itoa(code) + " Was expected but got:" + strconv.Itoa(exitCode))
fmt.Printf("Error: %v\n", err)
fmt.Println(output)
os.Exit(1)
}
// Handle other errors
fmt.Printf("Error running command: %v\n", err)
fmt.Println(output)
return
}
fmt.Println("Passed")
}
func runCommand(command string) (string, error) {
fmt.Println("Running command: bruin", command)
args := strings.Split(command, " ")
executable := "bruin"
if runtime.GOOS == "windows" {
executable = "bruin.exe"
}
wd, _ := os.Getwd()
binary := filepath.Join(wd, "bin", executable)
cmd := exec.Command(binary, args...)
cmd.Dir = currentFolder
output, err := cmd.Output()
return string(output), err
}
func runCommandWithExitCode(command string) (string, int, error) {
output, err := runCommand(command)
if err != nil {
// Try to get the exit code
var exitError *exec.ExitError
if errors.As(err, &exitError) {
// Get the status code
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
return output, status.ExitStatus(), nil
}
}
return output, 1, err
}
return output, 0, nil
}