-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgithub_test.go
161 lines (144 loc) · 4.59 KB
/
github_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
package github
import (
"context"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/actions/actions-runner-controller/github/fake"
"github.com/google/go-github/v52/github"
)
var server *httptest.Server
func newTestClient() *Client {
c := Config{
Token: "token",
}
client, err := c.NewClient()
if err != nil {
panic(err)
}
baseURL, err := url.Parse(server.URL + "/")
if err != nil {
panic(err)
}
client.BaseURL = baseURL
return client
}
func TestMain(m *testing.M) {
res := &fake.FixedResponses{
ListRunners: fake.DefaultListRunnersHandler(),
}
server = fake.NewServer(fake.WithFixedResponses(res))
defer server.Close()
m.Run()
}
func TestGetRegistrationToken(t *testing.T) {
tests := []struct {
enterprise string
org string
repo string
token string
err bool
}{
{enterprise: "", org: "", repo: "test/valid", token: fake.RegistrationToken, err: false},
{enterprise: "", org: "", repo: "test/invalid", token: "", err: true},
{enterprise: "", org: "", repo: "test/error", token: "", err: true},
{enterprise: "", org: "test", repo: "", token: fake.RegistrationToken, err: false},
{enterprise: "", org: "invalid", repo: "", token: "", err: true},
{enterprise: "", org: "error", repo: "", token: "", err: true},
{enterprise: "test", org: "", repo: "", token: fake.RegistrationToken, err: false},
{enterprise: "invalid", org: "", repo: "", token: "", err: true},
{enterprise: "error", org: "", repo: "", token: "", err: true},
}
client := newTestClient()
for i, tt := range tests {
rt, err := client.GetRegistrationToken(context.Background(), tt.enterprise, tt.org, tt.repo, "test")
if !tt.err && err != nil {
t.Errorf("[%d] unexpected error: %v", i, err)
}
if tt.token != rt.GetToken() {
t.Errorf("[%d] unexpected token: %v", i, rt.GetToken())
}
}
}
func TestListRunners(t *testing.T) {
tests := []struct {
enterprise string
org string
repo string
length int
err bool
}{
{enterprise: "", org: "", repo: "test/valid", length: 2, err: false},
{enterprise: "", org: "", repo: "test/invalid", length: 0, err: true},
{enterprise: "", org: "", repo: "test/error", length: 0, err: true},
{enterprise: "", org: "test", repo: "", length: 2, err: false},
{enterprise: "", org: "invalid", repo: "", length: 0, err: true},
{enterprise: "", org: "error", repo: "", length: 0, err: true},
{enterprise: "test", org: "", repo: "", length: 2, err: false},
{enterprise: "invalid", org: "", repo: "", length: 0, err: true},
{enterprise: "error", org: "", repo: "", length: 0, err: true},
}
client := newTestClient()
for i, tt := range tests {
runners, err := client.ListRunners(context.Background(), tt.enterprise, tt.org, tt.repo)
if !tt.err && err != nil {
t.Errorf("[%d] unexpected error: %v", i, err)
}
if tt.length != len(runners) {
t.Errorf("[%d] unexpected runners list: %v", i, runners)
}
}
}
func TestRemoveRunner(t *testing.T) {
tests := []struct {
enterprise string
org string
repo string
err bool
}{
{enterprise: "", org: "", repo: "test/valid", err: false},
{enterprise: "", org: "", repo: "test/invalid", err: true},
{enterprise: "", org: "", repo: "test/error", err: true},
{enterprise: "", org: "test", repo: "", err: false},
{enterprise: "", org: "invalid", repo: "", err: true},
{enterprise: "", org: "error", repo: "", err: true},
{enterprise: "test", org: "", repo: "", err: false},
{enterprise: "invalid", org: "", repo: "", err: true},
{enterprise: "error", org: "", repo: "", err: true},
}
client := newTestClient()
for i, tt := range tests {
err := client.RemoveRunner(context.Background(), tt.enterprise, tt.org, tt.repo, int64(1))
if !tt.err && err != nil {
t.Errorf("[%d] unexpected error: %v", i, err)
}
}
}
func TestCleanup(t *testing.T) {
token := "token"
client := newTestClient()
client.regTokens = map[string]*github.RegistrationToken{
"active": &github.RegistrationToken{
Token: &token,
ExpiresAt: &github.Timestamp{Time: time.Now().Add(time.Hour * 1)},
},
"expired": &github.RegistrationToken{
Token: &token,
ExpiresAt: &github.Timestamp{Time: time.Now().Add(-time.Hour * 1)},
},
}
client.cleanup()
if _, ok := client.regTokens["active"]; !ok {
t.Errorf("active token was accidentally removed")
}
if _, ok := client.regTokens["expired"]; ok {
t.Errorf("expired token still exists")
}
}
func TestUserAgent(t *testing.T) {
client := newTestClient()
if client.UserAgent != "actions-runner-controller/NA" {
t.Errorf("UserAgent should be set to actions-runner-controller/NA")
}
}