libcontainer: avoid using t.Cleanup
t.Cleanup is not present in Go 1.13.
Dockre/Moby still builds runc with Go 1.13, so we should still support
Go 1.13.
@@ -13,6 +13,7 @@ import (
func TestLoggingToFile(t *testing.T) {
l := runLogForwarding(t)
+ defer l.cleanup()
logToLogWriter(t, l, `{"level": "info","msg":"kitten"}`)
finish(t, l)
@@ -21,6 +22,7 @@ func TestLoggingToFile(t *testing.T) {
func TestLogForwardingDoesNotStopOnJsonDecodeErr(t *testing.T) {
l := runLogForwarding(t)
+ defer l.cleanup()
logToLogWriter(t, l, "invalid-json-with-kitten")
checkWait(t, l, "failed to decode")
@@ -34,6 +36,7 @@ func TestLogForwardingDoesNotStopOnJsonDecodeErr(t *testing.T) {
func TestLogForwardingDoesNotStopOnLogLevelParsingErr(t *testing.T) {
l := runLogForwarding(t)
+ defer l.cleanup()
logToLogWriter(t, l, `{"level": "alert","msg":"puppy"}`)
checkWait(t, l, "failed to parse log level")
@@ -47,6 +50,7 @@ func TestLogForwardingDoesNotStopOnLogLevelParsingErr(t *testing.T) {
func TestLogForwardingStopsAfterClosingTheWriter(t *testing.T) {
l := runLogForwarding(t)
+ defer l.cleanup()
logToLogWriter(t, l, `{"level": "info","msg":"sync"}`)
@@ -73,6 +77,9 @@ type log struct {
w io.WriteCloser
file string
done chan error
+
+ // TODO: use t.Cleanup after dropping support for Go 1.13
+ cleanup func()
}
func runLogForwarding(t *testing.T) *log {
@@ -81,10 +88,6 @@ func runLogForwarding(t *testing.T) *log {
if err != nil {
t.Fatal(err)
}
- t.Cleanup(func() {
- logR.Close()
- logW.Close()
- })
tempFile, err := ioutil.TempFile("", "")
if err != nil {
@@ -92,7 +95,6 @@ func runLogForwarding(t *testing.T) *log {
}
tempFile.Close()
logFile := tempFile.Name()
- t.Cleanup(func() { os.Remove(logFile) })
logConfig := Config{LogLevel: logrus.InfoLevel, LogFormat: "json", LogFilePath: logFile}
loggingConfigured = false
@@ -102,7 +104,13 @@ func runLogForwarding(t *testing.T) *log {
doneForwarding := ForwardLogs(logR)
- return &log{w: logW, done: doneForwarding, file: logFile}
+ cleanup := func() {
+ os.Remove(logFile)
+ logR.Close()
+ logW.Close()
+ }
+
+ return &log{w: logW, done: doneForwarding, file: logFile, cleanup: cleanup}
}
func finish(t *testing.T, l *log) {
