-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: watcher missconfiguration checks (#23)
* fix: watcher missconfiguration checks Fixes some of the checks for initialising the WatchConfig and adds test coverage. AB#9753 * use builtin time compare as it is clearer --------- Co-authored-by: Robin Bryce <[email protected]>
- Loading branch information
1 parent
0c1bd8e
commit 76b5a13
Showing
3 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.local | ||
**/test_results | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package watcher | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type checkWatchConfig func(t *testing.T, cfg WatchConfig) | ||
|
||
func TestConfigDefaults(t *testing.T) { | ||
hourSince := time.Now().Add(-time.Hour) | ||
|
||
type args struct { | ||
cfg *WatchConfig | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
errPrefix string | ||
check checkWatchConfig | ||
}{ | ||
{ | ||
name: "horizon or since options are required", | ||
args: args{ | ||
cfg: &WatchConfig{}, | ||
}, | ||
errPrefix: "provide horizon on its own or either of the since", | ||
}, | ||
|
||
{ | ||
name: "poll with since an hour in the past", | ||
args: args{ | ||
cfg: &WatchConfig{ | ||
Since: hourSince, | ||
}, | ||
}, | ||
check: func(t *testing.T, cfg WatchConfig) { | ||
assert.Equal(t, hourSince, cfg.Since) | ||
assert.Equal(t, time.Second, cfg.Interval) | ||
assert.NotEqual(t, "", cfg.IDSince) // should be set to IDTimeHex | ||
}, | ||
}, | ||
|
||
{ | ||
name: "bad hex string for idtimestamp errors", | ||
args: args{ | ||
cfg: &WatchConfig{ | ||
IDSince: "thisisnothex", | ||
}, | ||
}, | ||
errPrefix: "encoding/hex: invalid byte", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ConfigDefaults(tt.args.cfg) | ||
if err != nil { | ||
if tt.errPrefix == "" { | ||
t.Errorf("NewWatchConfig() unexpected error = %v", err) | ||
} | ||
if !strings.HasPrefix(err.Error(), tt.errPrefix) { | ||
t.Errorf("NewWatchConfig() unexpected error = %v, expected prefix: %s", err, tt.errPrefix) | ||
} | ||
} else { | ||
if tt.errPrefix != "" { | ||
t.Errorf("NewWatchConfig() expected error prefix = %s", tt.errPrefix) | ||
} | ||
} | ||
if tt.check != nil { | ||
tt.check(t, *tt.args.cfg) | ||
} | ||
}) | ||
} | ||
} |