mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-22 15:27:02 +00:00
test: Use testlib.RemoveIfOk where appropriate
Some tests did not make use of testlib.RemoveIfOk, which resulted in some duplication; this patch fixes that. While at it, userdb tests have its own simpler variant, so add some safety checks to it.
This commit is contained in:
@@ -252,6 +252,8 @@ func expvarMustEq(t *testing.T, name string, v *expvar.Int, expected int64) {
|
|||||||
|
|
||||||
func TestCacheBasics(t *testing.T) {
|
func TestCacheBasics(t *testing.T) {
|
||||||
dir := testlib.MustTempDir(t)
|
dir := testlib.MustTempDir(t)
|
||||||
|
defer testlib.RemoveIfOk(t, dir)
|
||||||
|
|
||||||
c, err := NewCache(dir)
|
c, err := NewCache(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -308,15 +310,13 @@ func TestCacheBasics(t *testing.T) {
|
|||||||
expvarMustEq(t, "cacheFetches", cacheFetches, 4)
|
expvarMustEq(t, "cacheFetches", cacheFetches, 4)
|
||||||
expvarMustEq(t, "cacheHits", cacheHits, 1)
|
expvarMustEq(t, "cacheHits", cacheHits, 1)
|
||||||
expvarMustEq(t, "cacheFailedFetch", cacheFailedFetch, 1)
|
expvarMustEq(t, "cacheFailedFetch", cacheFailedFetch, 1)
|
||||||
|
|
||||||
if !t.Failed() {
|
|
||||||
os.RemoveAll(dir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test how the cache behaves when the files are corrupt.
|
// Test how the cache behaves when the files are corrupt.
|
||||||
func TestCacheBadData(t *testing.T) {
|
func TestCacheBadData(t *testing.T) {
|
||||||
dir := testlib.MustTempDir(t)
|
dir := testlib.MustTempDir(t)
|
||||||
|
defer testlib.RemoveIfOk(t, dir)
|
||||||
|
|
||||||
c, err := NewCache(dir)
|
c, err := NewCache(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -379,10 +379,6 @@ func TestCacheBadData(t *testing.T) {
|
|||||||
|
|
||||||
expvarMustEq(t, "cacheUnmarshalErrors", cacheUnmarshalErrors, 1)
|
expvarMustEq(t, "cacheUnmarshalErrors", cacheUnmarshalErrors, 1)
|
||||||
expvarMustEq(t, "cacheInvalid", cacheInvalid, 1)
|
expvarMustEq(t, "cacheInvalid", cacheInvalid, 1)
|
||||||
|
|
||||||
if !t.Failed() {
|
|
||||||
os.RemoveAll(dir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PolicyCache) mustFetch(ctx context.Context, t *testing.T, d string) *Policy {
|
func (c *PolicyCache) mustFetch(ctx context.Context, t *testing.T, d string) *Policy {
|
||||||
@@ -408,6 +404,8 @@ func mustRewriteAndChtime(t *testing.T, fname, content string) {
|
|||||||
|
|
||||||
func TestCacheRefresh(t *testing.T) {
|
func TestCacheRefresh(t *testing.T) {
|
||||||
dir := testlib.MustTempDir(t)
|
dir := testlib.MustTempDir(t)
|
||||||
|
defer testlib.RemoveIfOk(t, dir)
|
||||||
|
|
||||||
c, err := NewCache(dir)
|
c, err := NewCache(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -452,10 +450,6 @@ func TestCacheRefresh(t *testing.T) {
|
|||||||
if p.MaxAge != 200*time.Second {
|
if p.MaxAge != 200*time.Second {
|
||||||
t.Fatalf("policy.MaxAge is %v, expected 200s", p.MaxAge)
|
t.Fatalf("policy.MaxAge is %v, expected 200s", p.MaxAge)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !t.Failed() {
|
|
||||||
os.RemoveAll(dir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheSlashSafe(t *testing.T) {
|
func TestCacheSlashSafe(t *testing.T) {
|
||||||
|
|||||||
@@ -5,12 +5,19 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Remove the file if the test was successful. Used in defer statements, to
|
// Remove the file if the test was successful. Used in defer statements, to
|
||||||
// leave files around for inspection when the tests failed.
|
// leave files around for inspection when the tests failed.
|
||||||
func removeIfSuccessful(t *testing.T, fname string) {
|
func removeIfSuccessful(t *testing.T, fname string) {
|
||||||
|
// Safeguard, to make sure we only remove test files.
|
||||||
|
// This should help prevent accidental deletions.
|
||||||
|
if !strings.Contains(fname, "userdb_test") {
|
||||||
|
panic("invalid/dangerous directory")
|
||||||
|
}
|
||||||
|
|
||||||
if !t.Failed() {
|
if !t.Failed() {
|
||||||
os.Remove(fname)
|
os.Remove(fname)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user