Skip to content

Commit e4bfab7

Browse files
Merge pull request #3417 from dmcgowan/testing-log-hook
Add unit test logging hook
2 parents d6be45e + 63ceaf8 commit e4bfab7

17 files changed

Lines changed: 178 additions & 77 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ GO_GCFLAGS=$(shell \
111111
BINARIES=$(addprefix bin/,$(COMMANDS))
112112

113113
# Flags passed to `go test`
114-
TESTFLAGS ?= -v $(TESTFLAGS_RACE)
114+
TESTFLAGS ?= $(TESTFLAGS_RACE)
115115
TESTFLAGS_PARALLEL ?= 8
116116

117117
.PHONY: clean all AUTHORS build binaries test integration generate protos checkprotos coverage ci check help install uninstall vendor release mandir install-man

benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func BenchmarkContainerCreate(b *testing.B) {
3131
}
3232
defer client.Close()
3333

34-
ctx, cancel := testContext()
34+
ctx, cancel := testContext(b)
3535
defer cancel()
3636

3737
image, err := client.GetImage(ctx, testImage)
@@ -74,7 +74,7 @@ func BenchmarkContainerStart(b *testing.B) {
7474
}
7575
defer client.Close()
7676

77-
ctx, cancel := testContext()
77+
ctx, cancel := testContext(b)
7878
defer cancel()
7979

8080
image, err := client.GetImage(ctx, testImage)

client_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/containerd/defaults"
3232
"github.com/containerd/containerd/images"
3333
"github.com/containerd/containerd/log"
34+
"github.com/containerd/containerd/log/logtest"
3435
"github.com/containerd/containerd/namespaces"
3536
"github.com/containerd/containerd/pkg/testutil"
3637
"github.com/containerd/containerd/platforms"
@@ -56,9 +57,12 @@ func init() {
5657
flag.Parse()
5758
}
5859

59-
func testContext() (context.Context, context.CancelFunc) {
60+
func testContext(t testing.TB) (context.Context, context.CancelFunc) {
6061
ctx, cancel := context.WithCancel(context.Background())
6162
ctx = namespaces.WithNamespace(ctx, testNamespace)
63+
if t != nil {
64+
ctx = logtest.WithT(ctx, t)
65+
}
6266
return ctx, cancel
6367
}
6468

@@ -73,7 +77,7 @@ func TestMain(m *testing.M) {
7377

7478
var (
7579
buf = bytes.NewBuffer(nil)
76-
ctx, cancel = testContext()
80+
ctx, cancel = testContext(nil)
7781
)
7882
defer cancel()
7983

@@ -203,7 +207,7 @@ func TestImagePull(t *testing.T) {
203207
}
204208
defer client.Close()
205209

206-
ctx, cancel := testContext()
210+
ctx, cancel := testContext(t)
207211
defer cancel()
208212
_, err = client.Pull(ctx, testImage, WithPlatformMatcher(platforms.Default()))
209213
if err != nil {
@@ -217,7 +221,7 @@ func TestImagePullAllPlatforms(t *testing.T) {
217221
t.Fatal(err)
218222
}
219223
defer client.Close()
220-
ctx, cancel := testContext()
224+
ctx, cancel := testContext(t)
221225
defer cancel()
222226

223227
cs := client.ContentStore()
@@ -252,7 +256,7 @@ func TestImagePullSomePlatforms(t *testing.T) {
252256
t.Fatal(err)
253257
}
254258
defer client.Close()
255-
ctx, cancel := testContext()
259+
ctx, cancel := testContext(t)
256260
defer cancel()
257261

258262
cs := client.ContentStore()
@@ -323,7 +327,7 @@ func TestImagePullSchema1(t *testing.T) {
323327
}
324328
defer client.Close()
325329

326-
ctx, cancel := testContext()
330+
ctx, cancel := testContext(t)
327331
defer cancel()
328332
schema1TestImage := "gcr.io/google_containers/pause:3.0@sha256:0d093c962a6c2dd8bb8727b661e2b5f13e9df884af9945b4cc7088d9350cd3ee"
329333
_, err = client.Pull(ctx, schema1TestImage, WithPlatform(platforms.DefaultString()), WithSchema1Conversion)
@@ -339,7 +343,7 @@ func TestImagePullWithConcurrencyLimit(t *testing.T) {
339343
}
340344
defer client.Close()
341345

342-
ctx, cancel := testContext()
346+
ctx, cancel := testContext(t)
343347
defer cancel()
344348
_, err = client.Pull(ctx, testImage,
345349
WithPlatformMatcher(platforms.Default()),
@@ -352,7 +356,7 @@ func TestImagePullWithConcurrencyLimit(t *testing.T) {
352356
func TestClientReconnect(t *testing.T) {
353357
t.Parallel()
354358

355-
ctx, cancel := testContext()
359+
ctx, cancel := testContext(t)
356360
defer cancel()
357361

358362
client, err := newClient(t, address)
@@ -410,7 +414,7 @@ func TestDefaultRuntimeWithNamespaceLabels(t *testing.T) {
410414
}
411415
defer client.Close()
412416

413-
ctx, cancel := testContext()
417+
ctx, cancel := testContext(t)
414418
defer cancel()
415419
namespaces := client.NamespaceService()
416420
testRuntime := "testRuntime"

container_checkpoint_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestCheckpointRestorePTY(t *testing.T) {
5353
}
5454

5555
var (
56-
ctx, cancel = testContext()
56+
ctx, cancel = testContext(t)
5757
id = t.Name()
5858
)
5959
defer cancel()
@@ -179,7 +179,7 @@ func TestCheckpointRestore(t *testing.T) {
179179
}
180180

181181
var (
182-
ctx, cancel = testContext()
182+
ctx, cancel = testContext(t)
183183
id = t.Name()
184184
)
185185
defer cancel()
@@ -269,7 +269,7 @@ func TestCheckpointRestoreNewContainer(t *testing.T) {
269269
}
270270

271271
id := t.Name()
272-
ctx, cancel := testContext()
272+
ctx, cancel := testContext(t)
273273
defer cancel()
274274

275275
image, err := client.GetImage(ctx, testImage)
@@ -359,7 +359,7 @@ func TestCheckpointLeaveRunning(t *testing.T) {
359359
}
360360

361361
var (
362-
ctx, cancel = testContext()
362+
ctx, cancel = testContext(t)
363363
id = t.Name()
364364
)
365365
defer cancel()
@@ -425,7 +425,7 @@ func TestCRWithImagePath(t *testing.T) {
425425
defer client.Close()
426426

427427
var (
428-
ctx, cancel = testContext()
428+
ctx, cancel = testContext(t)
429429
id = t.Name() + "-checkpoint"
430430
)
431431
defer cancel()

container_linux_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestTaskUpdate(t *testing.T) {
5858
defer client.Close()
5959

6060
var (
61-
ctx, cancel = testContext()
61+
ctx, cancel = testContext(t)
6262
id = t.Name()
6363
)
6464
defer cancel()
@@ -135,7 +135,7 @@ func TestShimInCgroup(t *testing.T) {
135135
}
136136
defer client.Close()
137137
var (
138-
ctx, cancel = testContext()
138+
ctx, cancel = testContext(t)
139139
id = t.Name()
140140
)
141141
defer cancel()
@@ -192,7 +192,7 @@ func TestDaemonRestart(t *testing.T) {
192192

193193
var (
194194
image Image
195-
ctx, cancel = testContext()
195+
ctx, cancel = testContext(t)
196196
id = t.Name()
197197
)
198198
defer cancel()
@@ -268,7 +268,7 @@ func TestShimDoesNotLeakPipes(t *testing.T) {
268268

269269
var (
270270
image Image
271-
ctx, cancel = testContext()
271+
ctx, cancel = testContext(t)
272272
id = t.Name()
273273
)
274274
defer cancel()
@@ -341,7 +341,7 @@ func TestDaemonReconnectsToShimIOPipesOnRestart(t *testing.T) {
341341

342342
var (
343343
image Image
344-
ctx, cancel = testContext()
344+
ctx, cancel = testContext(t)
345345
id = t.Name()
346346
)
347347
defer cancel()
@@ -471,7 +471,7 @@ func TestContainerPTY(t *testing.T) {
471471

472472
var (
473473
image Image
474-
ctx, cancel = testContext()
474+
ctx, cancel = testContext(t)
475475
id = t.Name()
476476
)
477477
defer cancel()
@@ -548,7 +548,7 @@ func TestContainerAttach(t *testing.T) {
548548

549549
var (
550550
image Image
551-
ctx, cancel = testContext()
551+
ctx, cancel = testContext(t)
552552
id = t.Name()
553553
)
554554
defer cancel()
@@ -693,7 +693,7 @@ func TestContainerUsername(t *testing.T) {
693693

694694
var (
695695
image Image
696-
ctx, cancel = testContext()
696+
ctx, cancel = testContext(t)
697697
id = t.Name()
698698
)
699699
defer cancel()
@@ -768,7 +768,7 @@ func testContainerUser(t *testing.T, userstr, expectedOutput string) {
768768

769769
var (
770770
image Image
771-
ctx, cancel = testContext()
771+
ctx, cancel = testContext(t)
772772
id = strings.Replace(t.Name(), "/", "_", -1)
773773
)
774774
defer cancel()
@@ -843,7 +843,7 @@ func TestContainerAttachProcess(t *testing.T) {
843843

844844
var (
845845
image Image
846-
ctx, cancel = testContext()
846+
ctx, cancel = testContext(t)
847847
id = t.Name()
848848
)
849849
defer cancel()
@@ -960,7 +960,7 @@ func TestContainerUserID(t *testing.T) {
960960

961961
var (
962962
image Image
963-
ctx, cancel = testContext()
963+
ctx, cancel = testContext(t)
964964
id = t.Name()
965965
)
966966
defer cancel()
@@ -1029,7 +1029,7 @@ func TestContainerKillAll(t *testing.T) {
10291029

10301030
var (
10311031
image Image
1032-
ctx, cancel = testContext()
1032+
ctx, cancel = testContext(t)
10331033
id = t.Name()
10341034
)
10351035
defer cancel()
@@ -1086,7 +1086,7 @@ func TestDaemonRestartWithRunningShim(t *testing.T) {
10861086

10871087
var (
10881088
image Image
1089-
ctx, cancel = testContext()
1089+
ctx, cancel = testContext(t)
10901090
id = t.Name()
10911091
)
10921092
defer cancel()
@@ -1167,7 +1167,7 @@ func TestContainerRuntimeOptionsv1(t *testing.T) {
11671167

11681168
var (
11691169
image Image
1170-
ctx, cancel = testContext()
1170+
ctx, cancel = testContext(t)
11711171
id = t.Name()
11721172
)
11731173
defer cancel()
@@ -1210,7 +1210,7 @@ func TestContainerRuntimeOptionsv2(t *testing.T) {
12101210

12111211
var (
12121212
image Image
1213-
ctx, cancel = testContext()
1213+
ctx, cancel = testContext(t)
12141214
id = t.Name()
12151215
)
12161216
defer cancel()
@@ -1251,7 +1251,7 @@ func initContainerAndCheckChildrenDieOnKill(t *testing.T, opts ...oci.SpecOpts)
12511251

12521252
var (
12531253
image Image
1254-
ctx, cancel = testContext()
1254+
ctx, cancel = testContext(t)
12551255
id = t.Name()
12561256
)
12571257
defer cancel()
@@ -1350,7 +1350,7 @@ func testUserNamespaces(t *testing.T, readonlyRootFS bool) {
13501350

13511351
var (
13521352
image Image
1353-
ctx, cancel = testContext()
1353+
ctx, cancel = testContext(t)
13541354
id = strings.Replace(t.Name(), "/", "-", -1)
13551355
)
13561356
defer cancel()
@@ -1439,7 +1439,7 @@ func TestTaskResize(t *testing.T) {
14391439

14401440
var (
14411441
image Image
1442-
ctx, cancel = testContext()
1442+
ctx, cancel = testContext(t)
14431443
id = t.Name()
14441444
)
14451445
defer cancel()
@@ -1474,7 +1474,7 @@ func TestTaskResize(t *testing.T) {
14741474
func TestContainerImage(t *testing.T) {
14751475
t.Parallel()
14761476

1477-
ctx, cancel := testContext()
1477+
ctx, cancel := testContext(t)
14781478
defer cancel()
14791479
id := t.Name()
14801480

@@ -1507,7 +1507,7 @@ func TestContainerImage(t *testing.T) {
15071507
func TestContainerNoImage(t *testing.T) {
15081508
t.Parallel()
15091509

1510-
ctx, cancel := testContext()
1510+
ctx, cancel := testContext(t)
15111511
defer cancel()
15121512
id := t.Name()
15131513

@@ -1535,7 +1535,7 @@ func TestContainerNoImage(t *testing.T) {
15351535
func TestUIDNoGID(t *testing.T) {
15361536
t.Parallel()
15371537

1538-
ctx, cancel := testContext()
1538+
ctx, cancel := testContext(t)
15391539
defer cancel()
15401540
id := t.Name()
15411541

@@ -1578,7 +1578,7 @@ func TestBindLowPortNonRoot(t *testing.T) {
15781578

15791579
var (
15801580
image Image
1581-
ctx, cancel = testContext()
1581+
ctx, cancel = testContext(t)
15821582
id = t.Name()
15831583
)
15841584
defer cancel()
@@ -1634,7 +1634,7 @@ func TestBindLowPortNonOpt(t *testing.T) {
16341634

16351635
var (
16361636
image Image
1637-
ctx, cancel = testContext()
1637+
ctx, cancel = testContext(t)
16381638
id = t.Name()
16391639
)
16401640
defer cancel()
@@ -1695,7 +1695,7 @@ func TestContainerNoSTDIN(t *testing.T) {
16951695

16961696
var (
16971697
image Image
1698-
ctx, cancel = testContext()
1698+
ctx, cancel = testContext(t)
16991699
id = t.Name()
17001700
)
17011701
defer cancel()
@@ -1748,7 +1748,7 @@ func TestShimOOMScore(t *testing.T) {
17481748

17491749
var (
17501750
image Image
1751-
ctx, cancel = testContext()
1751+
ctx, cancel = testContext(t)
17521752
id = t.Name()
17531753
)
17541754
defer cancel()

0 commit comments

Comments
 (0)