@@ -11,7 +11,6 @@ import (
11
11
"github.com/stretchr/testify/require"
12
12
"golang.org/x/xerrors"
13
13
14
- "github.com/coder/coder/v2/coderd/coderdtest"
15
14
"github.com/coder/coder/v2/coderd/healthcheck"
16
15
"github.com/coder/coder/v2/coderd/healthcheck/health"
17
16
)
@@ -25,12 +24,17 @@ func TestAccessURL(t *testing.T) {
25
24
var (
26
25
ctx , cancel = context .WithCancel (context .Background ())
27
26
report healthcheck.AccessURLReport
28
- client = coderdtest .New (t , nil )
27
+ resp = []byte ("OK" )
28
+ srv = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
29
+ w .WriteHeader (http .StatusOK )
30
+ _ , _ = w .Write (resp )
31
+ }))
29
32
)
30
33
defer cancel ()
31
34
32
35
report .Run (ctx , & healthcheck.AccessURLReportOptions {
33
- AccessURL : client .URL ,
36
+ Client : srv .Client (),
37
+ AccessURL : mustURL (t , srv .URL ),
34
38
})
35
39
36
40
assert .True (t , report .Healthy )
@@ -41,35 +45,27 @@ func TestAccessURL(t *testing.T) {
41
45
assert .Nil (t , report .Error )
42
46
})
43
47
44
- t .Run ("404 " , func (t * testing.T ) {
48
+ t .Run ("NotSet " , func (t * testing.T ) {
45
49
t .Parallel ()
46
50
47
51
var (
48
52
ctx , cancel = context .WithCancel (context .Background ())
49
53
report healthcheck.AccessURLReport
50
- resp = []byte ("NOT OK" )
51
- srv = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
52
- w .WriteHeader (http .StatusNotFound )
53
- w .Write (resp )
54
- }))
55
54
)
56
55
defer cancel ()
57
- defer srv .Close ()
58
-
59
- u , err := url .Parse (srv .URL )
60
- require .NoError (t , err )
61
56
62
57
report .Run (ctx , & healthcheck.AccessURLReportOptions {
63
- Client : srv . Client (),
64
- AccessURL : u ,
58
+ Client : nil , // defaults to http.DefaultClient
59
+ AccessURL : nil ,
65
60
})
66
61
67
62
assert .False (t , report .Healthy )
68
- assert .True (t , report .Reachable )
69
- assert .Equal (t , health .SeverityWarning , report .Severity )
70
- assert .Equal (t , http .StatusNotFound , report .StatusCode )
71
- assert .Equal (t , string (resp ), report .HealthzResponse )
72
- assert .Nil (t , report .Error )
63
+ assert .False (t , report .Reachable )
64
+ assert .Equal (t , health .SeverityError , report .Severity )
65
+ assert .Equal (t , 0 , report .StatusCode )
66
+ assert .Equal (t , "" , report .HealthzResponse )
67
+ require .NotNil (t , report .Error )
68
+ assert .Contains (t , * report .Error , healthcheck .ErrAccessURLNotSet )
73
69
})
74
70
75
71
t .Run ("ClientErr" , func (t * testing.T ) {
@@ -81,7 +77,7 @@ func TestAccessURL(t *testing.T) {
81
77
resp = []byte ("OK" )
82
78
srv = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
83
79
w .WriteHeader (http .StatusOK )
84
- w .Write (resp )
80
+ _ , _ = w .Write (resp )
85
81
}))
86
82
client = srv .Client ()
87
83
)
@@ -93,12 +89,9 @@ func TestAccessURL(t *testing.T) {
93
89
return nil , expErr
94
90
})
95
91
96
- u , err := url .Parse (srv .URL )
97
- require .NoError (t , err )
98
-
99
92
report .Run (ctx , & healthcheck.AccessURLReportOptions {
100
93
Client : client ,
101
- AccessURL : u ,
94
+ AccessURL : mustURL ( t , srv . URL ) ,
102
95
})
103
96
104
97
assert .False (t , report .Healthy )
@@ -108,6 +101,38 @@ func TestAccessURL(t *testing.T) {
108
101
assert .Equal (t , "" , report .HealthzResponse )
109
102
require .NotNil (t , report .Error )
110
103
assert .Contains (t , * report .Error , expErr .Error ())
104
+ assert .Contains (t , * report .Error , healthcheck .ErrAccessURLFetch )
105
+ })
106
+
107
+ t .Run ("404" , func (t * testing.T ) {
108
+ t .Parallel ()
109
+
110
+ var (
111
+ ctx , cancel = context .WithCancel (context .Background ())
112
+ report healthcheck.AccessURLReport
113
+ resp = []byte ("NOT OK" )
114
+ srv = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
115
+ w .WriteHeader (http .StatusNotFound )
116
+ _ , _ = w .Write (resp )
117
+ }))
118
+ )
119
+ defer cancel ()
120
+ defer srv .Close ()
121
+
122
+ report .Run (ctx , & healthcheck.AccessURLReportOptions {
123
+ Client : srv .Client (),
124
+ AccessURL : mustURL (t , srv .URL ),
125
+ })
126
+
127
+ assert .False (t , report .Healthy )
128
+ assert .True (t , report .Reachable )
129
+ assert .Equal (t , health .SeverityWarning , report .Severity )
130
+ assert .Equal (t , http .StatusNotFound , report .StatusCode )
131
+ assert .Equal (t , string (resp ), report .HealthzResponse )
132
+ assert .Nil (t , report .Error )
133
+ if assert .NotEmpty (t , report .Warnings ) {
134
+ assert .Contains (t , report .Warnings [0 ], healthcheck .ErrAccessURLNotOK )
135
+ }
111
136
})
112
137
113
138
t .Run ("DismissedError" , func (t * testing.T ) {
@@ -133,3 +158,10 @@ type roundTripFunc func(r *http.Request) (*http.Response, error)
133
158
func (rt roundTripFunc ) RoundTrip (r * http.Request ) (* http.Response , error ) {
134
159
return rt (r )
135
160
}
161
+
162
+ func mustURL (t testing.TB , s string ) * url.URL {
163
+ t .Helper ()
164
+ u , err := url .Parse (s )
165
+ require .NoError (t , err )
166
+ return u
167
+ }
0 commit comments