-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentryEventFromWorkflowRun_test.go
157 lines (137 loc) · 4.11 KB
/
sentryEventFromWorkflowRun_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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v32/github"
)
var update = flag.Bool("update", false, "update .golden files")
type happyPathActionsClient struct {
jobs *github.Jobs
}
type mockableActionsService interface {
ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *github.ListWorkflowJobsOptions) (*github.Jobs, *github.Response, error)
MockWorkflowJobs(*github.Jobs)
}
type test struct {
name string
actionsService mockableActionsService
workflowRun *github.WorkflowRun
jobs []*github.WorkflowJob
err bool
}
var (
tests []test
http200 = &github.Response{
Response: &http.Response{StatusCode: 200},
}
)
func (c *happyPathActionsClient) ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *github.ListWorkflowJobsOptions) (*github.Jobs, *github.Response, error) {
return c.jobs, http200, nil
}
func (c *happyPathActionsClient) MockWorkflowJobs(j *github.Jobs) {
c.jobs = j
}
func init() {
tests = []test{
{
name: "example",
actionsService: &happyPathActionsClient{},
jobs: []*github.WorkflowJob{
{
ID: github.Int64(1234),
NodeID: github.String("test"),
Name: github.String("test"),
StartedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
CompletedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 06, 0, time.UTC)},
Steps: []*github.TaskStep{
{
Number: github.Int64(1234),
Name: github.String("test"),
StartedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
CompletedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 06, 0, time.UTC)},
},
},
},
},
err: false,
},
{
name: "failure",
actionsService: &happyPathActionsClient{},
jobs: []*github.WorkflowJob{
{
ID: github.Int64(1234),
NodeID: github.String("test"),
Name: github.String("test"),
StartedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
CompletedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 06, 0, time.UTC)},
Steps: []*github.TaskStep{
{
Number: github.Int64(1234),
Name: github.String("test"),
StartedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
CompletedAt: &github.Timestamp{time.Date(2020, time.January, 02, 15, 04, 06, 0, time.UTC)},
},
},
},
},
err: false,
}}
}
func TestTable(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fixture := filepath.Join(".", "testdata", fmt.Sprintf("workflow_run.%s.json", tt.name))
fixtureBytes, err := ioutil.ReadFile(fixture)
if err != nil {
t.Fatal(err)
}
var workflowRunEvent CompleteWorkflowRunEvent
jsonErr := json.Unmarshal(fixtureBytes, &workflowRunEvent)
if jsonErr != nil {
t.Fatal(jsonErr)
}
tt.actionsService.MockWorkflowJobs(&github.Jobs{Jobs: tt.jobs})
event, err := sentryEventFromWorkflowRun(context.Background(), &workflowRunEvent, tt.actionsService, strings.NewReader("random"))
if tt.err && err == nil {
t.Errorf("%s: expected an error, didn't get one", tt.name)
return
}
if !tt.err && err != nil {
t.Errorf("%s: %+v", tt.name, err)
return
}
if event == nil {
t.Errorf("%s: event is nil", tt.name)
return
}
got, err := json.MarshalIndent(event, "", " ")
if err != nil {
t.Error(err)
}
golden := filepath.Join(".", "testdata", fmt.Sprintf("sentry_event.%s.json", tt.name))
if *update {
err := ioutil.WriteFile(golden, got, 0600)
if err != nil {
t.Fatal(err)
}
}
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("struct %s mismatch (-want +got):\n%s", tt.name, diff)
}
})
}
}