-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.go
190 lines (162 loc) · 4.57 KB
/
e2e.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/segmentio/analytics-go"
"gopkg.in/yaml.v2"
)
const unifiedCiAppID = "48fa8fbee698622c"
const (
defaultBitriseSecretsName = ".bitrise.secrets.yml"
)
type partialBitriseModel struct {
Workflows yaml.MapSlice `json:"workflows,omitempty" yaml:"workflows,omitempty"`
}
func runE2E(commandFactory command.Factory, workDir string, shouldFailOnFirstError bool, segmentKey string, parentURL string) error {
e2eBitriseYMLPath := filepath.Join(workDir, "e2e", "bitrise.yml")
if exists, err := pathutil.IsPathExists(e2eBitriseYMLPath); err != nil {
return err
} else if !exists {
return fmt.Errorf("looking for bitrise.yml in e2e directory, path (%s) does not exists", e2eBitriseYMLPath)
}
log.Infof("Using bitrise.yml from: %s", e2eBitriseYMLPath)
secrets, err := lookupSecrets(workDir)
if err != nil {
return err
}
if secrets == "" {
log.Errorf("No %s found", defaultBitriseSecretsName)
} else {
log.Infof("Using secrets from: %s", secrets)
}
workflows, err := readE2EWorkflows(e2eBitriseYMLPath)
if err != nil {
return err
}
shouldSendAnalytics := parentURL != "" && segmentKey != ""
var client analytics.Client
if shouldSendAnalytics {
client = analytics.New(segmentKey)
defer client.Close()
}
var result string
success := true
for _, workflow := range workflows {
start := time.Now()
err = runE2EWorkflow(commandFactory, workDir, e2eBitriseYMLPath, secrets, workflow)
elapsed := time.Since(start).Milliseconds()
if shouldSendAnalytics {
if err := sendAnalytics(client, workflow, err == nil, parentURL, elapsed); err != nil {
return err
}
}
if err != nil {
if shouldFailOnFirstError {
return fmt.Errorf("'%s' E2E test failed: %w", workflow, err)
}
success = false
result += fmt.Sprintf("- %s (FAIL): %s \n", colorstring.Red(workflow), err)
continue
}
result += fmt.Sprintf("- %s (OK) \n", colorstring.Green(workflow))
}
log.Infof("Step E2E summary:")
log.Printf("%s", result)
if !success {
return fmt.Errorf("E2E tests failed")
}
return nil
}
func sendAnalytics(client analytics.Client, workflow string, success bool, parentURL string, duration int64) error {
var status string
if success {
status = "success"
} else {
status = "error"
}
if err := client.Enqueue(analytics.Track{
UserId: unifiedCiAppID,
Event: "ci_e2e_finished",
Properties: map[string]interface{}{
"workflow": workflow,
"status": status,
"parent_url": parentURL,
"stack_id": os.Getenv("BITRISEIO_STACK_ID"),
"duration": duration,
},
}); err != nil {
return err
}
return nil
}
func readE2EWorkflows(configPath string) ([]string, error) {
configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
return readE2EWorkflowsFromBytes(configBytes)
}
func readE2EWorkflowsFromBytes(configBytes []byte) ([]string, error) {
model := partialBitriseModel{}
if err := yaml.Unmarshal(configBytes, &model); err != nil {
return nil, err
}
var result []string
for _, workflow := range model.Workflows {
key, ok := workflow.Key.(string)
if !ok {
return nil, fmt.Errorf("failed to cast workflow name to string")
}
if strings.HasPrefix(key, "test_") {
result = append(result, key)
}
}
return result, nil
}
func runE2EWorkflow(commandFactory command.Factory, workDir string, configPath string, secretsPath string, workflow string) error {
e2eCmdArgs := []string{"run", "--config", configPath}
if secretsPath != "" {
e2eCmdArgs = append(e2eCmdArgs, "--inventory", secretsPath)
}
e2eCmdArgs = append(e2eCmdArgs, workflow)
e2eCmd := commandFactory.Create(
"bitrise",
e2eCmdArgs,
&command.Opts{
Dir: workDir,
Stdin: os.Stdin,
Stdout: os.Stdout,
})
fmt.Println()
log.Donef("$ %s", e2eCmd.PrintableCommandArgs())
if err := e2eCmd.Run(); err != nil {
if errorutil.IsExitStatusError(err) {
return err
}
return fmt.Errorf("failed to run command: %v", err)
}
return nil
}
func lookupSecrets(workDir string) (string, error) {
secretLookupPaths := []string{
filepath.Join(workDir, "e2e", defaultBitriseSecretsName),
filepath.Join(workDir, defaultBitriseSecretsName),
}
for _, secretPath := range secretLookupPaths {
if exists, err := pathutil.IsPathExists(secretPath); err != nil {
return "", err
} else if exists {
return secretPath, nil
}
}
return "", nil
}