Skip to content

Commit 358f060

Browse files
Fix for #88 with unit tests
1 parent e2bd2cf commit 358f060

File tree

4 files changed

+173
-11
lines changed

4 files changed

+173
-11
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func runProfile(
358358

359359
// add progress receivers if necessary
360360
if profile.StatusFile != "" {
361-
wrapper.addProgress(status.NewProgress(profile))
361+
wrapper.addProgress(status.NewProgress(profile, status.NewStatus(profile.StatusFile)))
362362
}
363363
if profile.PrometheusPush != "" || profile.PrometheusSaveToFile != "" {
364364
wrapper.addProgress(prom.NewProgress(profile, prom.NewMetrics(group, version, profile.PrometheusLabels)))

progress/error.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,21 @@ func IsWarning(err error) bool {
1717
if errors.As(err, &exitErr) {
1818
return exitErr.ExitCode() == 3
1919
}
20+
// so far, internal warning is only used in unit tests
21+
warn := &InternalWarning{}
22+
if errors.As(err, &warn) {
23+
return true
24+
}
2025
return false
2126
}
2227

2328
func IsError(err error) bool {
2429
return err != nil && !IsWarning(err)
2530
}
31+
32+
type InternalWarning struct {
33+
}
34+
35+
func (w InternalWarning) Error() string {
36+
return "internal warning"
37+
}

status/progress.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ import (
88
)
99

1010
type Progress struct {
11-
profile *config.Profile
11+
profile *config.Profile
12+
generator *Status
1213
}
1314

14-
func NewProgress(profile *config.Profile) *Progress {
15+
func NewProgress(profile *config.Profile, statusGenerator *Status) *Progress {
1516
return &Progress{
16-
profile: profile,
17+
profile: profile,
18+
generator: statusGenerator,
1719
}
1820
}
1921

22+
// getGenerator returns the default status file generator.
23+
// Please note the status file is loaded every time you call this function.
24+
func (p *Progress) getGenerator() *Status {
25+
if p.generator == nil {
26+
p.generator = NewStatus(p.profile.StatusFile)
27+
}
28+
return p.generator.Load()
29+
}
30+
2031
func (p *Progress) Status(status progress.Status) {
2132
// we don't report any progress here
2233
}
@@ -30,7 +41,11 @@ func (p *Progress) Summary(command string, summary progress.Summary, stderr stri
3041
p.success(command, summary, stderr)
3142

3243
case progress.IsWarning(result):
33-
p.success(command, summary, stderr)
44+
if command == constants.CommandBackup && p.profile.Backup.NoErrorOnWarning {
45+
p.success(command, summary, stderr)
46+
} else {
47+
p.error(command, summary, stderr, result)
48+
}
3449

3550
case progress.IsError(result):
3651
p.error(command, summary, stderr, result)
@@ -41,15 +56,15 @@ func (p *Progress) success(command string, summary progress.Summary, stderr stri
4156
var err error
4257
switch command {
4358
case constants.CommandBackup:
44-
status := NewStatus(p.profile.StatusFile).Load()
59+
status := p.getGenerator()
4560
status.Profile(p.profile.Name).BackupSuccess(summary, stderr)
4661
err = status.Save()
4762
case constants.CommandCheck:
48-
status := NewStatus(p.profile.StatusFile).Load()
63+
status := p.getGenerator()
4964
status.Profile(p.profile.Name).CheckSuccess(summary, stderr)
5065
err = status.Save()
5166
case constants.SectionConfigurationRetention, constants.CommandForget:
52-
status := NewStatus(p.profile.StatusFile).Load()
67+
status := p.getGenerator()
5368
status.Profile(p.profile.Name).RetentionSuccess(summary, stderr)
5469
err = status.Save()
5570
}
@@ -63,15 +78,15 @@ func (p *Progress) error(command string, summary progress.Summary, stderr string
6378
var err error
6479
switch command {
6580
case constants.CommandBackup:
66-
status := NewStatus(p.profile.StatusFile).Load()
81+
status := p.getGenerator()
6782
status.Profile(p.profile.Name).BackupError(fail, summary, stderr)
6883
err = status.Save()
6984
case constants.CommandCheck:
70-
status := NewStatus(p.profile.StatusFile).Load()
85+
status := p.getGenerator()
7186
status.Profile(p.profile.Name).CheckError(fail, summary, stderr)
7287
err = status.Save()
7388
case constants.SectionConfigurationRetention, constants.CommandForget:
74-
status := NewStatus(p.profile.StatusFile).Load()
89+
status := p.getGenerator()
7590
status.Profile(p.profile.Name).RetentionError(fail, summary, stderr)
7691
err = status.Save()
7792
}

status/progress_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package status
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/creativeprojects/resticprofile/config"
8+
"github.com/creativeprojects/resticprofile/constants"
9+
"github.com/creativeprojects/resticprofile/progress"
10+
"github.com/spf13/afero"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestProgressNoStatusFile(t *testing.T) {
16+
filename := "TestProgressNoStatusFile.json"
17+
profileName := "profileName"
18+
19+
fs := afero.NewMemMapFs()
20+
status := newAferoStatus(fs, filename)
21+
profile := &config.Profile{
22+
Name: profileName,
23+
Backup: &config.BackupSection{},
24+
}
25+
p := NewProgress(profile, status)
26+
p.Summary(constants.CommandBackup, progress.Summary{}, "", nil)
27+
28+
exists, err := afero.Exists(fs, filename)
29+
require.NoError(t, err)
30+
assert.False(t, exists)
31+
}
32+
33+
func TestProgressSuccess(t *testing.T) {
34+
filename := "TestProgressSuccess.json"
35+
profileName := "profileName"
36+
37+
fs := afero.NewMemMapFs()
38+
status := newAferoStatus(fs, filename)
39+
profile := &config.Profile{
40+
Name: profileName,
41+
StatusFile: filename,
42+
Backup: &config.BackupSection{},
43+
}
44+
p := NewProgress(profile, status)
45+
p.Summary(constants.CommandBackup, progress.Summary{}, "", nil)
46+
47+
exists, err := afero.Exists(fs, filename)
48+
require.NoError(t, err)
49+
assert.True(t, exists)
50+
51+
status = newAferoStatus(fs, filename).Load()
52+
assert.True(t, status.Profiles[profileName].Backup.Success)
53+
assert.Empty(t, status.Profiles[profileName].Backup.Error)
54+
assert.Empty(t, status.Profiles[profileName].Backup.Stderr)
55+
}
56+
57+
func TestProgressError(t *testing.T) {
58+
filename := "TestProgressError.json"
59+
profileName := "profileName"
60+
message := "something unexpected happened"
61+
stderr := "blah blah blah"
62+
63+
fs := afero.NewMemMapFs()
64+
status := newAferoStatus(fs, filename)
65+
profile := &config.Profile{
66+
Name: profileName,
67+
StatusFile: filename,
68+
Backup: &config.BackupSection{},
69+
}
70+
p := NewProgress(profile, status)
71+
p.Summary(constants.CommandBackup, progress.Summary{}, stderr, errors.New(message))
72+
73+
exists, err := afero.Exists(fs, filename)
74+
require.NoError(t, err)
75+
assert.True(t, exists)
76+
77+
status = newAferoStatus(fs, filename).Load()
78+
assert.False(t, status.Profiles[profileName].Backup.Success)
79+
assert.Equal(t, message, status.Profiles[profileName].Backup.Error)
80+
assert.Equal(t, stderr, status.Profiles[profileName].Backup.Stderr)
81+
}
82+
83+
func TestProgressWarningAsSuccess(t *testing.T) {
84+
filename := "TestProgressWarningAsSuccess.json"
85+
profileName := "profileName"
86+
stderr := "blah blah blah"
87+
88+
fs := afero.NewMemMapFs()
89+
status := newAferoStatus(fs, filename)
90+
profile := &config.Profile{
91+
Name: profileName,
92+
StatusFile: filename,
93+
Backup: &config.BackupSection{
94+
NoErrorOnWarning: true,
95+
},
96+
}
97+
p := NewProgress(profile, status)
98+
p.Summary(constants.CommandBackup, progress.Summary{}, stderr, &progress.InternalWarning{})
99+
100+
exists, err := afero.Exists(fs, filename)
101+
require.NoError(t, err)
102+
assert.True(t, exists)
103+
104+
status = newAferoStatus(fs, filename).Load()
105+
assert.True(t, status.Profiles[profileName].Backup.Success)
106+
assert.Empty(t, status.Profiles[profileName].Backup.Error)
107+
assert.Equal(t, stderr, status.Profiles[profileName].Backup.Stderr)
108+
}
109+
110+
func TestProgressWarningAsError(t *testing.T) {
111+
filename := "TestProgressWarningAsError.json"
112+
profileName := "profileName"
113+
stderr := "blah blah blah"
114+
115+
fs := afero.NewMemMapFs()
116+
status := newAferoStatus(fs, filename)
117+
profile := &config.Profile{
118+
Name: profileName,
119+
StatusFile: filename,
120+
Backup: &config.BackupSection{
121+
NoErrorOnWarning: false,
122+
},
123+
}
124+
p := NewProgress(profile, status)
125+
p.Summary(constants.CommandBackup, progress.Summary{}, stderr, &progress.InternalWarning{})
126+
127+
exists, err := afero.Exists(fs, filename)
128+
require.NoError(t, err)
129+
assert.True(t, exists)
130+
131+
status = newAferoStatus(fs, filename).Load()
132+
assert.False(t, status.Profiles[profileName].Backup.Success)
133+
assert.Equal(t, "internal warning", status.Profiles[profileName].Backup.Error)
134+
assert.Equal(t, stderr, status.Profiles[profileName].Backup.Stderr)
135+
}

0 commit comments

Comments
 (0)