forked from bruin-data/bruin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-test.go
172 lines (149 loc) · 4.1 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
jd "github.com/josephburnett/jd/lib"
"github.com/pkg/errors"
)
var currentFolder string
func main() {
var err error
currentFolder = filepath.Join(currentFolder, "integration-tests")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
expectExitCode("validate happy-path", 0)
expectExitCode("run --use-uv happy-path", 0)
expectExitCode("run happy-path", 0)
expectJSONOutput("internal parse-pipeline happy-path", "happy-path/expectations/pipeline.yml.json")
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",
[]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",
[]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, contains []string) {
output, err := runCommand(command)
if err != nil {
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.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, err := runCommand(command)
if err != nil {
// Try to get the exit code
if errors.As(err, exec.ExitError{}) {
exitError := err.(*exec.ExitError) //nolint: errorlint
// Get the status code
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() != code {
fmt.Println(strconv.Itoa(code) + " Was expected but got:" + strconv.Itoa(status.ExitStatus()))
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:", command)
args := []string{"run", "../main.go"}
args = append(args, strings.Split(command, " ")...)
cmd := exec.Command("go", args...)
cmd.Dir = currentFolder
output, err := cmd.Output()
return string(output), err
}