-
Notifications
You must be signed in to change notification settings - Fork 18
/
auto_update_test.go
102 lines (96 loc) · 3.67 KB
/
auto_update_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
package main
import (
"bytes"
"fmt"
"os"
"regexp"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
// We don't want detailed logs in trace (time will always be different)
log.SetFormat("%level:upper%: %message%")
log.SetDefaultConsoleHookLevel(logrus.DebugLevel)
os.Exit(m.Run())
}
func setupUpdaterMock(localVersion string, latestVersion string) *RunnerUpdaterMock {
version = localVersion
return &RunnerUpdaterMock{
GetUpdateVersionFunc: func() (string, error) { return latestVersion, nil }, // Remote version
GetLastRefreshFunc: func(string) time.Duration { return 0 * time.Hour }, // Force update
SetLastRefreshFunc: func(string) {},
ShouldUpdateFunc: func() bool { return true },
RunFunc: func() int { return 0 },
RestartFunc: func() int { return 0 },
DoUpdateFunc: func(url string) (err error) { return nil },
}
}
func TestRunWithUpdateCheck(t *testing.T) {
tests := []struct {
name string
local string
latest string
runCount int
restartCount int
expectedLogPattern []string
}{
{"lower", "1.20.0", "1.21.0", 0, 1, []string{
`WARNING: Updating .*tgf.test(?:\.exe)? from 1.20.0 ==> 1.21.0`,
`INFO: TGF updated to 1.21.0`,
`WARNING: TGF is restarting...`,
}},
{"equal", "1.21.0", "1.21.0", 1, 0, []string{
`DEBUG: Your current version \(1.21.0\) is up to date.`,
}},
{"higher", "1.21.0", "1.20.0", 1, 0, []string{
`DEBUG: Your current version \(1.21.0\) is up to date.`,
}},
{"error on latest", "1.20.0", "not a number", 1, 0, []string{
`ERROR: Semver error on retrieved version "not a number" : No Major.Minor.Patch elements found`,
}},
{"error on local", "not a number", "1.20.0", 1, 0, []string{
`WARNING: Semver error on current version "not a number": No Major.Minor.Patch elements found`,
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buffer bytes.Buffer
log.SetOut(&buffer)
mockUpdater := setupUpdaterMock(tt.local, tt.latest)
RunWithUpdateCheck(mockUpdater)
fmt.Println(buffer.String())
for _, logPattern := range tt.expectedLogPattern {
match, err := regexp.MatchString(logPattern, buffer.String())
assert.NoError(t, err)
assert.Truef(t, match, "Output doesn't contains %s", logPattern)
}
assert.Equal(t, len(mockUpdater.RunCalls()), tt.runCount, "Run calls")
assert.Equal(t, len(mockUpdater.RestartCalls()), tt.restartCount, "Restart calls")
})
}
}
func TestShouldUpdate(t *testing.T) {
tests := []struct {
name string
version string
config TGFConfig
log string
}{
{"bypass", "", TGFConfig{tgf: &TGFApplication{}, AutoUpdate: true}, "DEBUG: Running locally. Bypassing update version check.\n"},
{"forced", "", TGFConfig{tgf: &TGFApplication{AutoUpdateSet: true, AutoUpdate: true}}, "DEBUG: Auto update is forced locally. Checking version...\n"},
{"disabled", "", TGFConfig{tgf: &TGFApplication{AutoUpdateSet: true, AutoUpdate: false}}, "DEBUG: Auto update is force disabled. Bypassing update version check.\n"},
{"due", "1.1.1", TGFConfig{tgf: &TGFApplication{AutoUpdateSet: false}, AutoUpdate: true, AutoUpdateDelay: 0 * time.Hour}, "DEBUG: An update is due. Checking version...\n"},
{"config disabled", "", TGFConfig{tgf: &TGFApplication{AutoUpdateSet: false}, AutoUpdate: false}, "DEBUG: Auto update is disabled in the config. Bypassing update version check.\n"},
}
version = locallyBuilt
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buffer bytes.Buffer
log.SetOut(&buffer)
tt.config.ShouldUpdate()
assert.Equal(t, tt.log, buffer.String())
})
}
}