Skip to content

Commit f364f24

Browse files
authored
Merge pull request #3910 from apostasie/sub-project-testing-3
CI: test framework, part III
2 parents e08cb35 + 62c1565 commit f364f24

22 files changed

+290
-234
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ require (
8181
github.com/containerd/plugin v1.0.0 // indirect
8282
github.com/containerd/ttrpc v1.2.7 // indirect
8383
github.com/containers/ocicrypt v1.2.1 // indirect
84+
github.com/creack/pty v1.1.24 // indirect
8485
github.com/djherbis/times v1.6.0 // indirect
8586
github.com/docker/docker-credential-helpers v0.8.2 // indirect
8687
github.com/felixge/httpsnoop v1.0.4 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ github.com/coreos/go-iptables v0.8.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFE
7676
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
7777
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
7878
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
79-
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
80-
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
79+
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
80+
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
8181
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
8282
github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
8383
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

mod/tigron/.golangci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
output:
3+
sort-results: true
4+
5+
issues:
6+
max-issues-per-linter: 0
7+
max-same-issues: 0
8+
9+
run:
10+
concurrency: 0
11+
timeout: 5m
12+
issues-exit-code: 2
13+
tests: true
14+
modules-download-mode: readonly
15+
allow-parallel-runners: true
16+
allow-serial-runners: true
17+
18+
linters:
19+
disable-all: false
20+
enable-all: true
21+
disable:
22+
# Opting-out
23+
- nonamedreturns # named returns are occasionally useful
24+
- exhaustruct # does not serve much of a purpose
25+
- funlen # not interested
26+
- cyclop # not interested much
27+
- godox # having these are useful
28+
# Duplicating
29+
- gci # we use go-imports instead
30+
# Deprecated
31+
- tenv
32+
# TODO: Temporarily out until we wrap up all of them
33+
# - wrapcheck
34+
35+
linters-settings:
36+
staticcheck:
37+
checks:
38+
- "all"
39+
40+
depguard:
41+
rules:
42+
main:
43+
files:
44+
- "$all"
45+
allow:
46+
- $gostd
47+
- "github.com/containerd/nerdctl/mod/tigron"
48+
# WATCHOUT! https://github.com/OpenPeeDeeP/depguard/issues/108
49+
# Currently, depguard will fail detecting any dependency starting with a standard package name as third-party.
50+
# Thus, the following three are allowed provisionally, though currently not "necessary".
51+
- "golang.org/x/sync"
52+
- "golang.org/x/term"
53+
- "gotest.tools"
54+
- "github.com/creack/pty"

mod/tigron/.yamllint

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
3+
extends: default
4+
5+
rules:
6+
indentation:
7+
spaces: 2
8+
indent-sequences: consistent
9+
truthy:
10+
allowed-values: ['true', 'false', 'on', 'off']
11+
comments-indentation: disable
12+
document-start: disable
13+
line-length: disable

mod/tigron/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ lint-go-all:
7575
$(call title, $@)
7676
@cd $(MAKEFILE_DIR) \
7777
&& GOOS=darwin make lint-go \
78+
&& GOOS=freebsd make lint-go \
7879
&& GOOS=linux make lint-go \
7980
&& GOOS=windows make lint-go
8081
$(call footer, $@)
8182

8283
lint-imports:
8384
$(call title, $@)
8485
@cd $(MAKEFILE_DIR) \
85-
&& goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes "github.com/containerd" ./...
86+
&& goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes "$(ORG_PREFIXES)" ./...
8687
$(call footer, $@)
8788

8889
lint-yaml:
@@ -124,6 +125,7 @@ lint-licenses-all:
124125
$(call title, $@)
125126
@cd $(MAKEFILE_DIR) \
126127
&& GOOS=darwin make lint-licenses \
128+
&& GOOS=freebsd make lint-licenses \
127129
&& GOOS=linux make lint-licenses \
128130
&& GOOS=windows make lint-licenses
129131
$(call footer, $@)
@@ -141,6 +143,7 @@ fix-go-all:
141143
$(call title, $@)
142144
@cd $(MAKEFILE_DIR) \
143145
&& GOOS=darwin make fix-go \
146+
&& GOOS=freebsd make fix-go \
144147
&& GOOS=linux make fix-go \
145148
&& GOOS=windows make fix-go
146149
$(call footer, $@)

mod/tigron/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/containerd/nerdctl/mod/tigron
33
go 1.23
44

55
require (
6+
github.com/creack/pty v1.1.24
67
golang.org/x/sync v0.11.0
78
golang.org/x/term v0.29.0
89
gotest.tools/v3 v3.5.2

mod/tigron/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
2+
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
13
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
24
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
35
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=

mod/tigron/require/requirement.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func Binary(name string) *test.Requirement {
2828
return &test.Requirement{
29-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
29+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
3030
mess := fmt.Sprintf("executable %q has been found in PATH", name)
3131
ret := true
3232
if _, err := exec.LookPath(name); err != nil {
@@ -41,7 +41,7 @@ func Binary(name string) *test.Requirement {
4141

4242
func OS(os string) *test.Requirement {
4343
return &test.Requirement{
44-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
44+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
4545
mess := fmt.Sprintf("current operating system is %q", runtime.GOOS)
4646
ret := true
4747
if runtime.GOOS != os {
@@ -55,7 +55,7 @@ func OS(os string) *test.Requirement {
5555

5656
func Arch(arch string) *test.Requirement {
5757
return &test.Requirement{
58-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
58+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
5959
mess := fmt.Sprintf("current architecture is %q", runtime.GOARCH)
6060
ret := true
6161
if runtime.GOARCH != arch {
@@ -67,18 +67,22 @@ func Arch(arch string) *test.Requirement {
6767
}
6868
}
6969

70-
var Amd64 = Arch("amd64")
71-
var Arm64 = Arch("arm64")
72-
var Windows = OS("windows")
73-
var Linux = OS("linux")
74-
var Darwin = OS("darwin")
70+
//nolint:gochecknoglobals
71+
var (
72+
Amd64 = Arch("amd64")
73+
Arm64 = Arch("arm64")
74+
Windows = OS("windows")
75+
Linux = OS("linux")
76+
Darwin = OS("darwin")
77+
)
7578

76-
// NOTE: Not will always lose setups and cleanups...
79+
// NOTE: Not will always ignore any setup and cleanup inside the wrapped requirement.
7780

7881
func Not(requirement *test.Requirement) *test.Requirement {
7982
return &test.Requirement{
8083
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
8184
ret, mess := requirement.Check(data, helpers)
85+
8286
return !ret, mess
8387
},
8488
}
@@ -93,10 +97,12 @@ func All(requirements ...*test.Requirement) *test.Requirement {
9397
for _, requirement := range requirements {
9498
ret, subMess = requirement.Check(data, helpers)
9599
mess += "\n" + subMess
100+
96101
if !ret {
97102
return ret, mess
98103
}
99104
}
105+
100106
return ret, mess
101107
},
102108
Setup: func(data test.Data, helpers test.Helpers) {

mod/tigron/test/case.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ type Case struct {
5959
parent *Case
6060
}
6161

62-
// Run prepares and executes the test, and any possible subtests
62+
// Run prepares and executes the test, and any possible subtests.
63+
//
64+
//nolint:gocognit
6365
func (test *Case) Run(t *testing.T) {
6466
t.Helper()
6567
// Run the test
68+
//nolint:thelper
6669
testRun := func(subT *testing.T) {
6770
subT.Helper()
6871

@@ -81,10 +84,13 @@ func (test *Case) Run(t *testing.T) {
8184

8285
// If we have a parent, get parent env, data and config
8386
var parentData Data
87+
8488
var parentConfig Config
89+
8590
if test.parent != nil {
8691
parentData = test.parent.Data
8792
parentConfig = test.parent.Config
93+
8894
for k, v := range test.parent.Env {
8995
if _, ok := test.Env[k]; !ok {
9096
test.Env[k] = v
@@ -96,23 +102,22 @@ func (test *Case) Run(t *testing.T) {
96102
test.Data = configureData(test.t, test.Data, parentData)
97103
test.Config = configureConfig(test.Config, parentConfig)
98104

99-
var b CustomizableCommand
105+
var custCom CustomizableCommand
100106
if registeredTestable == nil {
101-
b = &GenericCommand{}
107+
custCom = &GenericCommand{}
102108
} else {
103-
b = registeredTestable.CustomCommand(test, test.t)
109+
custCom = registeredTestable.CustomCommand(test, test.t)
104110
}
105111

106-
b.WithCwd(test.Data.TempDir())
107-
108-
b.withT(test.t)
109-
b.withTempDir(test.Data.TempDir())
110-
b.withEnv(test.Env)
111-
b.withConfig(test.Config)
112+
custCom.WithCwd(test.Data.TempDir())
113+
custCom.withT(test.t)
114+
custCom.withTempDir(test.Data.TempDir())
115+
custCom.withEnv(test.Env)
116+
custCom.withConfig(test.Config)
112117

113118
// Attach the base command, and t
114119
test.helpers = &helpersInternal{
115-
cmdInternal: b,
120+
cmdInternal: custCom,
116121
t: test.t,
117122
}
118123

@@ -125,9 +130,11 @@ func (test *Case) Run(t *testing.T) {
125130
if !shouldRun {
126131
test.t.Skipf("test skipped as: %s", message)
127132
}
133+
128134
if test.Require.Setup != nil {
129135
setups = append(setups, test.Require.Setup)
130136
}
137+
131138
if test.Require.Cleanup != nil {
132139
cleanups = append(cleanups, test.Require.Cleanup)
133140
}
@@ -154,35 +161,46 @@ func (test *Case) Run(t *testing.T) {
154161
}
155162

156163
// Execute cleanups now
164+
test.t.Log("")
157165
test.t.Log("======================== Pre-test cleanup ========================")
166+
158167
for _, cleanup := range cleanups {
159168
cleanup(test.Data, test.helpers)
160169
}
161170

162171
// Register the cleanups, in reverse
163172
test.t.Cleanup(func() {
173+
test.t.Log("")
164174
test.t.Log("======================== Post-test cleanup ========================")
175+
165176
slices.Reverse(cleanups)
177+
166178
for _, cleanup := range cleanups {
167179
cleanup(test.Data, test.helpers)
168180
}
169181
})
170182

171183
// Run the setups
184+
test.t.Log("")
172185
test.t.Log("======================== Test setup ========================")
186+
173187
for _, setup := range setups {
174188
setup(test.Data, test.helpers)
175189
}
176190

177191
// Run the command if any, with expectations
178192
// Note: if we have a command, we already know we DO have Expected
193+
test.t.Log("")
179194
test.t.Log("======================== Test Run ========================")
195+
180196
if test.Command != nil {
181197
test.Command(test.Data, test.helpers).Run(test.Expected(test.Data, test.helpers))
182198
}
183199

184200
// Now go for the subtests
201+
test.t.Log("")
185202
test.t.Log("======================== Processing subtests ========================")
203+
186204
for _, subTest := range test.SubTests {
187205
subTest.parent = test
188206
subTest.Run(test.t)

0 commit comments

Comments
 (0)