test: use T.TempDir to create temporary test directory

The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-03-07 16:55:12 +08:00
committed by Ramkumar Chinchani
parent 4be2652085
commit 0d77b60de7
19 changed files with 238 additions and 759 deletions
+3 -15
View File
@@ -5,9 +5,7 @@ package monitoring_test
import (
"context"
"io/ioutil"
"net/http"
"os"
"path"
"testing"
"time"
@@ -28,11 +26,7 @@ func TestExtensionMetrics(t *testing.T) {
conf := config.New()
conf.HTTP.Port = port
rootDir, err := ioutil.TempDir("", "metrics-test")
if err != nil {
panic(err)
}
defer os.RemoveAll(rootDir)
rootDir := t.TempDir()
conf.Storage.RootDirectory = rootDir
conf.Extensions = &extconf.ExtensionConfig{}
@@ -61,7 +55,7 @@ func TestExtensionMetrics(t *testing.T) {
monitoring.IncDownloadCounter(ctlr.Metrics, "alpine")
monitoring.IncUploadCounter(ctlr.Metrics, "alpine")
err = test.CopyFiles("../../../test/data/zot-test", path.Join(rootDir, "alpine"))
err := test.CopyFiles("../../../test/data/zot-test", path.Join(rootDir, "alpine"))
if err != nil {
panic(err)
}
@@ -88,13 +82,7 @@ func TestExtensionMetrics(t *testing.T) {
conf := config.New()
conf.HTTP.Port = port
rootDir, err := ioutil.TempDir("", "metrics-test")
if err != nil {
panic(err)
}
defer os.RemoveAll(rootDir)
conf.Storage.RootDirectory = rootDir
conf.Storage.RootDirectory = t.TempDir()
conf.Extensions = &extconf.ExtensionConfig{}
var disabled bool
conf.Extensions.Metrics = &extconf.MetricsConfig{Enable: &disabled}