-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmx_test.go
119 lines (100 loc) · 2.89 KB
/
gmx_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
package main
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/m-lab/go/osx"
"github.com/m-lab/go/rtx"
)
func TestRootHandler(t *testing.T) {
expectedStatus := http.StatusOK
expectedPayload := "GitHub Maintenance Exporter"
req, err := http.NewRequest("POST", "/", strings.NewReader(""))
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
rootHandler(rec, req)
if rec.Code != expectedStatus {
t.Errorf("rootHandler(): test %s: wrong HTTP status: got %v; want %v",
"TestRootHandler", rec.Code, expectedStatus)
}
bytes, _ := io.ReadAll(rec.Body)
payload := string(bytes)
if string(payload) != expectedPayload {
t.Errorf("rootHandler(): test %s: unexpected return text: got %s; want %s",
"TestRootHandler", payload, expectedPayload)
}
}
func TestGithubSecretFromFile(t *testing.T) {
dir, err := os.MkdirTemp("", "TestGithubSecretFromFile")
rtx.Must(err, "Could not create tempdir")
defer os.RemoveAll(dir)
rtx.Must(os.WriteFile(dir+"/secret", []byte("test"), 0644), "Could not create test secret")
b := MustReadGithubSecret(dir + "/secret")
if !reflect.DeepEqual(b, []byte("test")) {
t.Errorf("%v != %v", b, "test")
}
}
func TestGithubSecretFromEnv(t *testing.T) {
revert := osx.MustSetenv("GITHUB_WEBHOOK_SECRET", "test")
defer revert()
b := MustReadGithubSecret("")
if !reflect.DeepEqual(b, []byte("test")) {
t.Errorf("%v != %v", b, "test")
}
}
func TestGithubSecretFromEmptyFile(t *testing.T) {
dir, err := os.MkdirTemp("", "TestGithubSecretFromEmptyFile")
rtx.Must(err, "Could not create tempdir")
defer os.RemoveAll(dir)
rtx.Must(os.WriteFile(dir+"/secret", []byte{}, 0644), "Could not create test secret")
logFatal = func(...interface{}) { panic("testerror") }
defer func() {
r := recover()
if r == nil {
t.Error("Should have had a panic but did not")
}
}()
MustReadGithubSecret(dir + "/secret")
}
func TestMainBadProject(t *testing.T) {
logFatal = func(...interface{}) { panic("testerror") }
defer func() {
r := recover()
if r == nil {
t.Error("Should have had a panic but did not")
}
}()
*fProject = "mlab-doesnotexist"
main()
}
func TestMainViaSmokeTest(t *testing.T) {
dir, err := os.MkdirTemp("", "TestMainViaSmokeTest")
rtx.Must(err, "Could not create tempdir")
defer os.RemoveAll(dir)
rtx.Must(os.WriteFile(dir+"/secret", []byte("test"), 0644), "Could not create test secret")
logFatal = func(...interface{}) { panic("testerror") }
defer func() {
r := recover()
if r == nil {
t.Error("Should have had a panic but did not")
}
}()
*fGitHubSecretPath = dir + "/secret"
*fStateFilePath = dir + "/state.json"
*fListenAddress = ":0"
*fProject = "mlab-sandbox"
mainCtx, mainCancel = context.WithCancel(context.Background())
go func() {
time.Sleep(500 * time.Millisecond)
mainCancel()
}()
main() // No crash and no freeze and full coverage of main() == success
}