-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathupdateinfo_test.go
154 lines (145 loc) · 4.05 KB
/
updateinfo_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
package updater
import (
"encoding/xml"
"testing"
"github.com/kinvolk/go-omaha/omaha"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
updateExistsResponse = `<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="nebraska">
<daystart elapsed_seconds="0" />
<app appid="e96281a6-d1af-4bde-9a0a-97b76e56dc57" status="ok">
<updatecheck status="ok">
<urls>
<url codebase="https://kinvolk.io/test/response" />
</urls>
<manifest version="2191.5.0">
<packages>
<package name="flatcar_production_update.gz" hash="test+x2zIoeClk=" size="465881871" required="true" />
</packages>
<actions>
<action event="postinstall" sha256="test/FodbjVgqkyF/y8=" DisablePayloadBackoff="true" />
</actions>
</manifest>
</updatecheck>
</app>
</response>
`
noUpdateResponse = `<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="nebraska">
<daystart elapsed_seconds="0" />
<app appid="e96281a6-d1af-4bde-9a0a-97b76e56dc57" status="ok">
<updatecheck status="noupdate">
<urls />
</updatecheck>
</app>
</response>`
errorResponse = `<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="nebraska">
<daystart elapsed_seconds="0" />
<app appid="h96281a6-d1af-4bde-9a0a-97b76e56dc57" status="error-failedToRetrieveUpdatePackageInfo">
<updatecheck status="error-internal">
<urls />
</updatecheck>
</app>
</response>`
nonUpdateCheckResponse = `<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="nebraska">
<daystart elapsed_seconds="0" />
<app appid="e96281a6-d1af-4bde-9a0a-97b76e56dc57" status="error-internal">
</app>
</response>`
appID = "e96281a6-d1af-4bde-9a0a-97b76e56dc57"
errorAppID = "h96281a6-d1af-4bde-9a0a-97b76e56dc57"
)
func TestUpdateInfo(t *testing.T) {
tests := []struct {
name string
response string
appID string
isNil bool
hasUpdate bool
updateStatus string
packagesCount int
urlCount int
version string
}{
{
name: "update_exists",
response: updateExistsResponse,
appID: appID,
isNil: false,
hasUpdate: true,
updateStatus: "ok",
packagesCount: 1,
urlCount: 1,
version: "2191.5.0",
},
{
name: "no_update_exists",
response: noUpdateResponse,
appID: appID,
isNil: false,
hasUpdate: false,
updateStatus: "noupdate",
packagesCount: 0,
urlCount: 0,
},
{
name: "error_response",
response: errorResponse,
appID: errorAppID,
isNil: false,
hasUpdate: false,
updateStatus: "error-internal",
packagesCount: 0,
urlCount: 0,
},
{
name: "non_update_check_response",
response: nonUpdateCheckResponse,
appID: appID,
isNil: true,
},
{
name: "invalid_app_id",
response: updateExistsResponse,
appID: errorAppID,
isNil: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var omahaResponse omaha.Response
err := xml.Unmarshal([]byte(tc.response), &omahaResponse)
require.NoError(t, err)
updateInfo, err := newUpdateInfo(&omahaResponse, tc.appID)
if tc.isNil {
assert.Nil(t, updateInfo)
assert.Error(t, err)
} else {
assert.NotNil(t, updateInfo)
assert.Equal(t, tc.hasUpdate, updateInfo.HasUpdate)
assert.Equal(t, tc.updateStatus, updateInfo.UpdateStatus)
assert.Equal(t, tc.version, updateInfo.Version)
assert.Equal(t, tc.urlCount, len(updateInfo.URLs))
if tc.urlCount > 0 {
assert.NotEqual(t, "", updateInfo.URL())
} else {
assert.Equal(t, "", updateInfo.URL())
}
assert.Equal(t, tc.packagesCount, len(updateInfo.Packages))
if tc.packagesCount > 0 {
assert.NotNil(t, updateInfo.Package())
} else {
assert.Nil(t, updateInfo.Package())
}
assert.Equal(t, &omahaResponse, updateInfo.OmahaResponse())
}
})
}
}