@@ -17,6 +17,7 @@ package upgradecheck
17
17
18
18
import (
19
19
"context"
20
+ "encoding/json"
20
21
"errors"
21
22
"fmt"
22
23
"io"
@@ -67,6 +68,16 @@ func TestCheckForUpgrades(t *testing.T) {
67
68
ctx := logging .NewContext (context .Background (), logging .Discard ())
68
69
cfg := & rest.Config {}
69
70
71
+ // Pass *testing.T to allows the correct messages from the assert package
72
+ // in the event of certain failures.
73
+ checkData := func (t * testing.T , header string ) {
74
+ data := clientUpgradeData {}
75
+ err := json .Unmarshal ([]byte (header ), & data )
76
+ assert .NilError (t , err )
77
+ assert .Assert (t , data .DeploymentID != "" )
78
+ assert .Equal (t , data .PGOVersion , "4.7.3" )
79
+ }
80
+
70
81
t .Run ("success" , func (t * testing.T ) {
71
82
// A successful call
72
83
funcFoo = func () (* http.Response , error ) {
@@ -77,10 +88,11 @@ func TestCheckForUpgrades(t *testing.T) {
77
88
}, nil
78
89
}
79
90
80
- res , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
91
+ res , header , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
81
92
fakeClient , cfg , false )
82
93
assert .NilError (t , err )
83
94
assert .Equal (t , res , `{"pgo_versions":[{"tag":"v5.0.4"},{"tag":"v5.0.3"},{"tag":"v5.0.2"},{"tag":"v5.0.1"},{"tag":"v5.0.0"}]}` )
95
+ checkData (t , header )
84
96
})
85
97
86
98
t .Run ("total failure, err sending" , func (t * testing.T ) {
@@ -91,12 +103,13 @@ func TestCheckForUpgrades(t *testing.T) {
91
103
return & http.Response {}, errors .New ("whoops" )
92
104
}
93
105
94
- res , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
106
+ res , header , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
95
107
fakeClient , cfg , false )
96
108
// Two failed calls because of env var
97
109
assert .Equal (t , counter , 2 )
98
110
assert .Equal (t , res , "" )
99
111
assert .Equal (t , err .Error (), `whoops` )
112
+ checkData (t , header )
100
113
})
101
114
102
115
t .Run ("recovers from panic" , func (t * testing.T ) {
@@ -107,12 +120,14 @@ func TestCheckForUpgrades(t *testing.T) {
107
120
panic (fmt .Errorf ("oh no!" ))
108
121
}
109
122
110
- res , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
123
+ res , header , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
111
124
fakeClient , cfg , false )
112
125
// One call because of panic
113
126
assert .Equal (t , counter , 1 )
114
127
assert .Equal (t , res , "" )
115
128
assert .Equal (t , err .Error (), `oh no!` )
129
+ // no http response returned, so don't perform full check
130
+ assert .Assert (t , header == "" )
116
131
})
117
132
118
133
t .Run ("total failure, bad StatusCode" , func (t * testing.T ) {
@@ -126,12 +141,13 @@ func TestCheckForUpgrades(t *testing.T) {
126
141
}, nil
127
142
}
128
143
129
- res , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
144
+ res , header , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
130
145
fakeClient , cfg , false )
131
146
assert .Equal (t , res , "" )
132
147
// Two failed calls because of env var
133
148
assert .Equal (t , counter , 2 )
134
149
assert .Equal (t , err .Error (), `received StatusCode 400` )
150
+ checkData (t , header )
135
151
})
136
152
137
153
t .Run ("one failure, then success" , func (t * testing.T ) {
@@ -154,11 +170,12 @@ func TestCheckForUpgrades(t *testing.T) {
154
170
}, nil
155
171
}
156
172
157
- res , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
173
+ res , header , err := checkForUpgrades (ctx , "4.7.3" , backoff ,
158
174
fakeClient , cfg , false )
159
175
assert .Equal (t , counter , 2 )
160
176
assert .NilError (t , err )
161
177
assert .Equal (t , res , `{"pgo_versions":[{"tag":"v5.0.4"},{"tag":"v5.0.3"},{"tag":"v5.0.2"},{"tag":"v5.0.1"},{"tag":"v5.0.0"}]}` )
178
+ checkData (t , header )
162
179
})
163
180
}
164
181
0 commit comments