Merge pull request #6889 from kzys/pull-restart-test

Share container images between TestRestartMonitor and TestRestartMonitorWithOnFailurePolicy
This commit is contained in:
Derek McGowan 2022-05-04 10:35:39 -07:00 committed by GitHub
commit 885468815e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"testing" "testing"
"time" "time"
@ -122,10 +123,9 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
// with the restart monitor service plugin // with the restart monitor service plugin
func TestRestartMonitor(t *testing.T) { func TestRestartMonitor(t *testing.T) {
const ( const (
interval = 10 * time.Second interval = 5 * time.Second
epsilon = 1 * time.Second
count = 20
) )
configTOML := fmt.Sprintf(` configTOML := fmt.Sprintf(`
version = 2 version = 2
[plugins] [plugins]
@ -135,13 +135,36 @@ version = 2
client, _, cleanup := newDaemonWithConfig(t, configTOML) client, _, cleanup := newDaemonWithConfig(t, configTOML)
defer cleanup() defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
_, err := client.Pull(ctx, testImage, WithPullUnpack)
if err != nil {
t.Fatal(err)
}
t.Run("Always", func(t *testing.T) {
testRestartMonitorAlways(t, client, interval)
})
t.Run("Failure Policy", func(t *testing.T) {
testRestartMonitorWithOnFailurePolicy(t, client, interval)
})
}
// testRestartMonitorAlways restarts its container always.
func testRestartMonitorAlways(t *testing.T, client *Client, interval time.Duration) {
const (
epsilon = 1 * time.Second
count = 20
)
var ( var (
ctx, cancel = testContext(t) ctx, cancel = testContext(t)
id = t.Name() id = strings.ReplaceAll(t.Name(), "/", "_")
) )
defer cancel() defer cancel()
image, err := client.Pull(ctx, testImage, WithPullUnpack) image, err := client.GetImage(ctx, testImage)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -233,26 +256,15 @@ version = 2
t.Logf("%v: the task was restarted since %v", time.Now(), lastCheck) t.Logf("%v: the task was restarted since %v", time.Now(), lastCheck)
} }
func TestRestartMonitorWithOnFailurePolicy(t *testing.T) { // testRestartMonitorWithOnFailurePolicy restarts its container with `on-failure:1`
const ( func testRestartMonitorWithOnFailurePolicy(t *testing.T, client *Client, interval time.Duration) {
interval = 5 * time.Second
)
configTOML := fmt.Sprintf(`
version = 2
[plugins]
[plugins."io.containerd.internal.v1.restart"]
interval = "%s"
`, interval.String())
client, _, cleanup := newDaemonWithConfig(t, configTOML)
defer cleanup()
var ( var (
ctx, cancel = testContext(t) ctx, cancel = testContext(t)
id = t.Name() id = strings.ReplaceAll(t.Name(), "/", "_")
) )
defer cancel() defer cancel()
image, err := client.Pull(ctx, testImage, WithPullUnpack) image, err := client.GetImage(ctx, testImage)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }