forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatecheck_test.go
More file actions
158 lines (136 loc) · 3.66 KB
/
updatecheck_test.go
File metadata and controls
158 lines (136 loc) · 3.66 KB
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
package updatecheck_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/go-github/v43/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/coderd/updatecheck"
"github.com/coder/coder/testutil"
)
func TestChecker_Notify(t *testing.T) {
t.Parallel()
responses := []github.RepositoryRelease{
{TagName: github.String("v1.2.3"), HTMLURL: github.String("https://someurl.com")},
{TagName: github.String("v1.2.4"), HTMLURL: github.String("https://someurl.com")},
{TagName: github.String("v1.2.4"), HTMLURL: github.String("https://someurl.com")},
{TagName: github.String("v1.2.5"), HTMLURL: github.String("https://someurl.com")},
}
responseC := make(chan github.RepositoryRelease, len(responses))
for _, r := range responses {
responseC <- r
}
wantVersion := []string{"v1.2.3", "v1.2.4", "v1.2.5"}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-r.Context().Done():
case resp := <-responseC:
b, err := json.Marshal(resp)
assert.NoError(t, err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}
}))
defer srv.Close()
db := databasefake.New()
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Named(t.Name())
notify := make(chan updatecheck.Result, len(wantVersion))
c := updatecheck.New(db, logger, updatecheck.Options{
Interval: 1 * time.Nanosecond, // Zero means unset.
URL: srv.URL,
Notify: func(r updatecheck.Result) {
select {
case notify <- r:
default:
t.Error("unexpected notification")
}
},
})
defer c.Close()
ctx, _ := testutil.Context(t)
for i := 0; i < len(wantVersion); i++ {
select {
case <-ctx.Done():
t.Error("timed out waiting for notification")
case r := <-notify:
assert.Equal(t, wantVersion[i], r.Version)
}
}
}
func TestChecker_Latest(t *testing.T) {
t.Parallel()
rr := github.RepositoryRelease{
TagName: github.String("v1.2.3"),
HTMLURL: github.String("https://someurl.com"),
}
tests := []struct {
name string
release github.RepositoryRelease
wantR updatecheck.Result
wantErr bool
}{
{
name: "check latest",
release: rr,
wantR: updatecheck.Result{
Version: "v1.2.3",
URL: "https://someurl.com",
},
wantErr: false,
},
{
name: "missing release data",
release: github.RepositoryRelease{},
wantErr: true,
},
{
name: "error",
release: rr,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if tt.wantErr {
w.WriteHeader(http.StatusInternalServerError)
return
}
rrJSON, err := json.Marshal(rr)
assert.NoError(t, err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(rrJSON)
}))
defer srv.Close()
db := databasefake.New()
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Named(t.Name())
c := updatecheck.New(db, logger, updatecheck.Options{
URL: srv.URL,
})
defer c.Close()
ctx, _ := testutil.Context(t)
_ = ctx
gotR, err := c.Latest(ctx)
if tt.wantErr {
require.Error(t, err)
return
}
// Zero out the time so we can compare the rest of the struct.
gotR.Checked = time.Time{}
require.Equal(t, tt.wantR, gotR, "wrong version")
})
}
}
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}