From 63ceaf877d82af6ada381191f6a837f6e92f4717 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 15 Jul 2019 15:29:09 -0700 Subject: [PATCH] Add testing log context Enables showing debug logs in testing output. For integration tests the client log output will show in addition to daemon output, with timestamps for better correlation. Signed-off-by: Derek McGowan --- benchmark_test.go | 4 +-- client_test.go | 22 ++++++++------ container_checkpoint_test.go | 10 +++--- container_linux_test.go | 50 +++++++++++++++--------------- container_test.go | 52 ++++++++++++++++---------------- content/testsuite/testsuite.go | 2 ++ daemon_config_linux_test.go | 2 +- export_test.go | 2 +- image_test.go | 4 +-- import_test.go | 4 +-- lease_test.go | 2 +- metadata/containers_test.go | 2 ++ metadata/db_test.go | 3 +- snapshots/testsuite/testsuite.go | 3 +- 14 files changed, 86 insertions(+), 76 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 7a0904c55..bc4d93e44 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -31,7 +31,7 @@ func BenchmarkContainerCreate(b *testing.B) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(b) defer cancel() image, err := client.GetImage(ctx, testImage) @@ -74,7 +74,7 @@ func BenchmarkContainerStart(b *testing.B) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(b) defer cancel() image, err := client.GetImage(ctx, testImage) diff --git a/client_test.go b/client_test.go index 30ed5b107..f100f9c19 100644 --- a/client_test.go +++ b/client_test.go @@ -31,6 +31,7 @@ import ( "github.com/containerd/containerd/defaults" "github.com/containerd/containerd/images" "github.com/containerd/containerd/log" + "github.com/containerd/containerd/log/logtest" "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/pkg/testutil" "github.com/containerd/containerd/platforms" @@ -56,9 +57,12 @@ func init() { flag.Parse() } -func testContext() (context.Context, context.CancelFunc) { +func testContext(t testing.TB) (context.Context, context.CancelFunc) { ctx, cancel := context.WithCancel(context.Background()) ctx = namespaces.WithNamespace(ctx, testNamespace) + if t != nil { + ctx = logtest.WithT(ctx, t) + } return ctx, cancel } @@ -73,7 +77,7 @@ func TestMain(m *testing.M) { var ( buf = bytes.NewBuffer(nil) - ctx, cancel = testContext() + ctx, cancel = testContext(nil) ) defer cancel() @@ -203,7 +207,7 @@ func TestImagePull(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() _, err = client.Pull(ctx, testImage, WithPlatformMatcher(platforms.Default())) if err != nil { @@ -217,7 +221,7 @@ func TestImagePullAllPlatforms(t *testing.T) { t.Fatal(err) } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() cs := client.ContentStore() @@ -252,7 +256,7 @@ func TestImagePullSomePlatforms(t *testing.T) { t.Fatal(err) } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() cs := client.ContentStore() @@ -323,7 +327,7 @@ func TestImagePullSchema1(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() schema1TestImage := "gcr.io/google_containers/pause:3.0@sha256:0d093c962a6c2dd8bb8727b661e2b5f13e9df884af9945b4cc7088d9350cd3ee" _, err = client.Pull(ctx, schema1TestImage, WithPlatform(platforms.DefaultString()), WithSchema1Conversion) @@ -339,7 +343,7 @@ func TestImagePullWithConcurrencyLimit(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() _, err = client.Pull(ctx, testImage, WithPlatformMatcher(platforms.Default()), @@ -352,7 +356,7 @@ func TestImagePullWithConcurrencyLimit(t *testing.T) { func TestClientReconnect(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := newClient(t, address) @@ -410,7 +414,7 @@ func TestDefaultRuntimeWithNamespaceLabels(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() namespaces := client.NamespaceService() testRuntime := "testRuntime" diff --git a/container_checkpoint_test.go b/container_checkpoint_test.go index 68d3493f3..5e037fb2f 100644 --- a/container_checkpoint_test.go +++ b/container_checkpoint_test.go @@ -53,7 +53,7 @@ func TestCheckpointRestorePTY(t *testing.T) { } var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -179,7 +179,7 @@ func TestCheckpointRestore(t *testing.T) { } var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -269,7 +269,7 @@ func TestCheckpointRestoreNewContainer(t *testing.T) { } id := t.Name() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() image, err := client.GetImage(ctx, testImage) @@ -359,7 +359,7 @@ func TestCheckpointLeaveRunning(t *testing.T) { } var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -425,7 +425,7 @@ func TestCRWithImagePath(t *testing.T) { defer client.Close() var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() + "-checkpoint" ) defer cancel() diff --git a/container_linux_test.go b/container_linux_test.go index d8e423b53..0e1769eb6 100644 --- a/container_linux_test.go +++ b/container_linux_test.go @@ -58,7 +58,7 @@ func TestTaskUpdate(t *testing.T) { defer client.Close() var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -135,7 +135,7 @@ func TestShimInCgroup(t *testing.T) { } defer client.Close() var ( - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -192,7 +192,7 @@ func TestDaemonRestart(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -268,7 +268,7 @@ func TestShimDoesNotLeakPipes(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -341,7 +341,7 @@ func TestDaemonReconnectsToShimIOPipesOnRestart(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -471,7 +471,7 @@ func TestContainerPTY(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -548,7 +548,7 @@ func TestContainerAttach(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -693,7 +693,7 @@ func TestContainerUsername(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -768,7 +768,7 @@ func testContainerUser(t *testing.T, userstr, expectedOutput string) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = strings.Replace(t.Name(), "/", "_", -1) ) defer cancel() @@ -843,7 +843,7 @@ func TestContainerAttachProcess(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -960,7 +960,7 @@ func TestContainerUserID(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1029,7 +1029,7 @@ func TestContainerKillAll(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1086,7 +1086,7 @@ func TestDaemonRestartWithRunningShim(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1167,7 +1167,7 @@ func TestContainerRuntimeOptionsv1(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1210,7 +1210,7 @@ func TestContainerRuntimeOptionsv2(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1251,7 +1251,7 @@ func initContainerAndCheckChildrenDieOnKill(t *testing.T, opts ...oci.SpecOpts) var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1350,7 +1350,7 @@ func testUserNamespaces(t *testing.T, readonlyRootFS bool) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = strings.Replace(t.Name(), "/", "-", -1) ) defer cancel() @@ -1439,7 +1439,7 @@ func TestTaskResize(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1474,7 +1474,7 @@ func TestTaskResize(t *testing.T) { func TestContainerImage(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1507,7 +1507,7 @@ func TestContainerImage(t *testing.T) { func TestContainerNoImage(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1535,7 +1535,7 @@ func TestContainerNoImage(t *testing.T) { func TestUIDNoGID(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1578,7 +1578,7 @@ func TestBindLowPortNonRoot(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1634,7 +1634,7 @@ func TestBindLowPortNonOpt(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1695,7 +1695,7 @@ func TestContainerNoSTDIN(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1748,7 +1748,7 @@ func TestShimOOMScore(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() diff --git a/container_test.go b/container_test.go index 52b1ee71b..c905a389f 100644 --- a/container_test.go +++ b/container_test.go @@ -60,7 +60,7 @@ func TestContainerList(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() containers, err := client.Containers(ctx) @@ -82,7 +82,7 @@ func TestNewContainer(t *testing.T) { } defer client.Close() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() container, err := client.NewContainer(ctx, id, WithNewSpec()) @@ -112,7 +112,7 @@ func TestContainerStart(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -175,7 +175,7 @@ func TestContainerOutput(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() expected = "kingkoye" ) @@ -244,7 +244,7 @@ func TestContainerExec(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -329,7 +329,7 @@ func TestContainerLargeExecArgs(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -405,7 +405,7 @@ func TestContainerPids(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -482,7 +482,7 @@ func TestContainerCloseIO(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -538,7 +538,7 @@ func TestDeleteRunningContainer(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -593,7 +593,7 @@ func TestContainerKill(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -648,7 +648,7 @@ func TestContainerNoBinaryExists(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -695,7 +695,7 @@ func TestContainerExecNoBinaryExists(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -760,7 +760,7 @@ func TestWaitStoppedTask(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -823,7 +823,7 @@ func TestWaitStoppedProcess(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -911,7 +911,7 @@ func TestTaskForceDelete(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -952,7 +952,7 @@ func TestProcessForceDelete(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1020,7 +1020,7 @@ func TestContainerHostname(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() expected = "myhostname" ) @@ -1089,7 +1089,7 @@ func TestContainerExitedAtSet(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1149,7 +1149,7 @@ func TestDeleteContainerExecCreated(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1219,7 +1219,7 @@ func TestContainerMetrics(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1276,7 +1276,7 @@ func TestDeletedContainerMetrics(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() @@ -1321,7 +1321,7 @@ func TestDeletedContainerMetrics(t *testing.T) { func TestContainerExtensions(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1366,7 +1366,7 @@ func TestContainerExtensions(t *testing.T) { func TestContainerUpdate(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1411,7 +1411,7 @@ func TestContainerUpdate(t *testing.T) { func TestContainerInfo(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1439,7 +1439,7 @@ func TestContainerInfo(t *testing.T) { func TestContainerLabels(t *testing.T) { t.Parallel() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() id := t.Name() @@ -1484,7 +1484,7 @@ func TestContainerHook(t *testing.T) { var ( image Image - ctx, cancel = testContext() + ctx, cancel = testContext(t) id = t.Name() ) defer cancel() diff --git a/content/testsuite/testsuite.go b/content/testsuite/testsuite.go index dd3f1d36f..3e1a90ddf 100644 --- a/content/testsuite/testsuite.go +++ b/content/testsuite/testsuite.go @@ -31,6 +31,7 @@ import ( "github.com/containerd/containerd/content" "github.com/containerd/containerd/errdefs" + "github.com/containerd/containerd/log/logtest" "github.com/containerd/containerd/pkg/testutil" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -101,6 +102,7 @@ func Name(ctx context.Context) string { func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root string) (context.Context, content.Store, func() error, error), fn func(ctx context.Context, t *testing.T, cs content.Store)) func(t *testing.T) { return func(t *testing.T) { ctx := context.WithValue(context.Background(), nameKey{}, name) + ctx = logtest.WithT(ctx, t) tmpDir, err := ioutil.TempDir("", "content-suite-"+name+"-") if err != nil { diff --git a/daemon_config_linux_test.go b/daemon_config_linux_test.go index d11d1d9f8..e78c5f7cb 100644 --- a/daemon_config_linux_test.go +++ b/daemon_config_linux_test.go @@ -136,7 +136,7 @@ version = 1 client, _, cleanup := newDaemonWithConfig(t, configTOML) defer cleanup() - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() // FIXME(AkihiroSuda): import locally frozen image? image, err := client.Pull(ctx, testImage, WithPullUnpack) diff --git a/export_test.go b/export_test.go index 6b6dc98f8..ab2caa2d0 100644 --- a/export_test.go +++ b/export_test.go @@ -33,7 +33,7 @@ func TestExport(t *testing.T) { if testing.Short() || runtime.GOOS == "windows" { t.Skip() } - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := New(address) diff --git a/image_test.go b/image_test.go index beb55fa14..cae4694eb 100644 --- a/image_test.go +++ b/image_test.go @@ -35,7 +35,7 @@ func TestImageIsUnpacked(t *testing.T) { } const imageName = "docker.io/library/busybox:latest" - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := newClient(t, address) @@ -86,7 +86,7 @@ func TestImagePullWithDistSourceLabel(t *testing.T) { tag = "latest" ) - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := newClient(t, address) diff --git a/import_test.go b/import_test.go index 621dcde4a..0d7e1859a 100644 --- a/import_test.go +++ b/import_test.go @@ -43,7 +43,7 @@ func TestExportAndImport(t *testing.T) { if testing.Short() || runtime.GOOS == "windows" { t.Skip() } - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := New(address) @@ -83,7 +83,7 @@ func TestExportAndImport(t *testing.T) { } func TestImport(t *testing.T) { - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := New(address) diff --git a/lease_test.go b/lease_test.go index 092766ad5..4f6ec9e19 100644 --- a/lease_test.go +++ b/lease_test.go @@ -32,7 +32,7 @@ func TestLeaseResources(t *testing.T) { t.Skip() } - ctx, cancel := testContext() + ctx, cancel := testContext(t) defer cancel() client, err := newClient(t, address) diff --git a/metadata/containers_test.go b/metadata/containers_test.go index 14540d43a..efa6ce68e 100644 --- a/metadata/containers_test.go +++ b/metadata/containers_test.go @@ -30,6 +30,7 @@ import ( "github.com/containerd/containerd/containers" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/filters" + "github.com/containerd/containerd/log/logtest" "github.com/containerd/containerd/namespaces" "github.com/containerd/typeurl" "github.com/gogo/protobuf/types" @@ -748,6 +749,7 @@ func checkContainersEqual(t *testing.T, a, b *containers.Container, format strin func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) { ctx, cancel := context.WithCancel(context.Background()) ctx = namespaces.WithNamespace(ctx, "testing") + ctx = logtest.WithT(ctx, t) dirname, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", "_", -1)+"-") if err != nil { diff --git a/metadata/db_test.go b/metadata/db_test.go index a0243cb85..64632db02 100644 --- a/metadata/db_test.go +++ b/metadata/db_test.go @@ -36,6 +36,7 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/gc" "github.com/containerd/containerd/images" + "github.com/containerd/containerd/log/logtest" "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/snapshots" "github.com/containerd/containerd/snapshots/native" @@ -330,7 +331,7 @@ func TestMetadataCollector(t *testing.T) { defer cleanup() var ( - ctx = context.Background() + ctx = logtest.WithT(context.Background(), t) objects = []object{ blob(bytesFor(1), true), diff --git a/snapshots/testsuite/testsuite.go b/snapshots/testsuite/testsuite.go index ea2a88b6a..50e0232ed 100644 --- a/snapshots/testsuite/testsuite.go +++ b/snapshots/testsuite/testsuite.go @@ -27,6 +27,7 @@ import ( "time" "github.com/containerd/containerd/errdefs" + "github.com/containerd/containerd/log/logtest" "github.com/containerd/containerd/mount" "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/pkg/testutil" @@ -71,7 +72,7 @@ func makeTest(name string, snapshotterFn func(ctx context.Context, root string) return func(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := logtest.WithT(context.Background(), t) ctx = namespaces.WithNamespace(ctx, "testsuite") // Make two directories: a snapshotter root and a play area for the tests: //