-
Notifications
You must be signed in to change notification settings - Fork 499
/
testing.go
142 lines (117 loc) · 3.76 KB
/
testing.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
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"testing"
"time"
"slices"
"github.com/PagerDuty/go-pagerduty"
"github.com/cloudquery/plugin-sdk/v4/plugin"
"github.com/cloudquery/plugin-sdk/v4/scheduler"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/cloudquery/plugin-sdk/v4/transformers"
"github.com/rs/zerolog"
)
type MockHttpClient struct {
// A map from request path to response object()
// e.g. "/users" -> []User
mockResponses map[string]any
}
func (mockHttpClient *MockHttpClient) AddMockResponse(url string, object any) {
if mockHttpClient.mockResponses == nil {
mockHttpClient.mockResponses = make(map[string]any)
}
mockHttpClient.mockResponses[url] = object
}
func PagerdutyMockTestHelper(t *testing.T, table *schema.Table, buildMockHttpClient func() *MockHttpClient) {
t.Helper()
l := zerolog.New(zerolog.NewTestWriter(t)).Output(
zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.StampMicro},
).Level(zerolog.DebugLevel).With().Timestamp().Logger()
spec := Spec{
TeamIds: nil,
MaxRequestsPerSecond: nil,
Concurrency: 0,
}
pagerdutyClient := pagerduty.NewClient("test_auth_token")
pagerdutyClient.HTTPClient = buildMockHttpClient()
c, err := New(l, spec, WithClient(pagerdutyClient))
if err != nil {
t.Fatal(err)
}
tables := schema.Tables{table}
if err := transformers.TransformTables(tables); err != nil {
t.Fatal(err)
}
sched := scheduler.NewScheduler(scheduler.WithLogger(l))
messages, err := sched.SyncAll(context.Background(), c, tables)
if err != nil {
t.Fatalf("failed to sync: %v", err)
}
plugin.ValidateNoEmptyColumns(t, tables, messages)
}
func (mockHttpClient *MockHttpClient) Do(req *http.Request) (*http.Response, error) {
mockResponseObject := mockHttpClient.mockResponses[strings.TrimRight(req.URL.Path, "/")]
marshaledMockResponse, err := json.Marshal(mockResponseObject)
if err != nil {
panic(err)
}
httpResponse := http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: io.NopCloser(bytes.NewReader(marshaledMockResponse)),
}
return &httpResponse, nil
}
// Timestamp fields such as `created_at` arrive as `string`s from the API.
// this function pusts valid `RFC3339` timestamps into fields like `DeletedAt`, `CreatedAt`...
// Mostly copy-paste from the `plugin-sdk` faker.
// Receives an interface that is a pointer to a struct, and only looks at fields one level deep.
// Pionter-to-pointer structs are supported.
func FakeStringTimestamps(ptrObj any) error {
timestampFieldNames := []string{
"CreateAt", "CreatedAt", "DeletedAt", "LastStatusChangeAt", "StartTime", "EndTime", "LastIncidentTimestamp",
}
ptrType := reflect.TypeOf(ptrObj) // reflection-type is a pointer
ptrKind := ptrType.Kind()
ptrValue := reflect.ValueOf(ptrObj) // a reflection-value that is a pointer
if ptrKind != reflect.Ptr {
return fmt.Errorf("object must be a pointer")
}
if ptrValue.IsNil() {
return fmt.Errorf("object must not be nil")
}
if ptrValue.Elem().Kind() == reflect.Ptr {
return FakeStringTimestamps(ptrValue.Elem().Interface())
}
if ptrValue.Elem().Kind() != reflect.Struct {
return fmt.Errorf("object must be a pointer to a struct")
}
structValue := ptrValue.Elem()
structType := structValue.Type()
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
fieldType := field.Type
fieldKind := fieldType.Kind()
fieldValue := structValue.Field(i)
if fieldKind == reflect.String {
if !fieldValue.CanSet() { // don't panic on unexported fields
continue
}
if slices.Contains(timestampFieldNames, field.Name) {
fieldValue.Set(reflect.ValueOf(time.Now().Format(time.RFC3339)))
}
}
}
return nil
}