-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_engine_test.go
135 lines (110 loc) · 3.27 KB
/
update_engine_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
package main
import (
"fmt"
"net/url"
"os"
"testing"
"time"
"github.com/godbus/dbus/v5"
"github.com/stretchr/testify/assert"
)
type dbusConnRecorder struct {
Path dbus.ObjectPath
Name string
Values []interface{}
}
// Emit implements dbusConn and records the arguments passed to Emit
func (d *dbusConnRecorder) Emit(path dbus.ObjectPath, name string, values ...interface{}) error {
d.Path = path
d.Name = name
d.Values = values
return nil
}
func mockNewVersionFuncFactory(version string) newVersionFunc {
return func(string) (string, error) {
return version, nil
}
}
func TestUpdateEngineAttemptUpdate(t *testing.T) {
ue := &updateEngine{updateCh: make(chan bool)}
var err error
go func() {
err = ue.AttemptUpdate()
}()
select {
case ret := <-ue.updateCh:
assert.True(t, ret)
case <-time.After(3 * time.Second):
t.Fatal("expected to receive update request on channel")
}
assert.Nil(t, err)
}
func TestUpdateEngineCheckForUpdate(t *testing.T) {
versionURL, _ := url.Parse(defaultVersionURL)
s := newStatus()
d := &dbusConnRecorder{}
// Test path for the kured release loaction
kuredReleasePath = fmt.Sprintf("%s/reboot-required", t.TempDir())
ue := updateEngine{
conn: d,
status: s,
osVersion: "2605.11.0",
newVersion: mockNewVersionFuncFactory("2605.11.0"),
versionURL: versionURL,
}
// The os version and new version match so there shouldn't be any update
// statuses emitted
if err := ue.checkForUpdate(); err != nil {
t.Fatal("expected nil error")
}
assert.Equal(t, &dbusConnRecorder{}, ue.conn)
assert.Greater(t, ue.status.lastCheckedTime, int64(0))
// Now that the version is different the status should be updated and a
// signal should have been emitted with the expected values
ue.newVersion = mockNewVersionFuncFactory("2605.12.0")
err := ue.checkForUpdate()
assert.Nil(t, err)
assert.Equal(t, d.Path, dbus.ObjectPath(dbusPath))
assert.Equal(t, d.Name, dbusInterface+".StatusUpdate")
assert.Equal(t, d.Values[2], updateStatusUpdatedNeedReboot)
assert.Equal(t, d.Values[3], "2605.12.0")
assert.Equal(t, ue.status.currentOperation, updateStatusUpdatedNeedReboot)
assert.Equal(t, ue.status.newVersion, "2605.12.0")
// Check that we have the flag file for kured
if _, err := os.Stat(kuredReleasePath); os.IsNotExist(err) {
t.Fatal("Missing release flag file for kured")
}
}
func TestUpdateEngineResetStatus(t *testing.T) {
ue := &updateEngine{
status: &status{
lastCheckedTime: 12345,
progress: 12.6,
currentOperation: updateStatusUpdatedNeedReboot,
newVersion: "2605.12.0",
newSize: 10,
},
}
err := ue.ResetStatus()
assert.Nil(t, err)
assert.Equal(t, ue.status, newStatus())
}
func TestUpdateEngineGetStatus(t *testing.T) {
s := &status{
lastCheckedTime: 12345,
progress: 12.6,
currentOperation: updateStatusUpdatedNeedReboot,
newVersion: "2605.12.0",
newSize: 10,
}
ue := &updateEngine{
status: s,
}
lastCheckedTime, progress, currentOperation, newVersion, newSize, err := ue.GetStatus()
assert.Nil(t, err)
assert.Equal(t, s.lastCheckedTime, lastCheckedTime)
assert.Equal(t, s.progress, progress)
assert.Equal(t, s.currentOperation, currentOperation)
assert.Equal(t, s.newVersion, newVersion)
assert.Equal(t, s.newSize, newSize)
}