Skip to content

Commit 6abe279

Browse files
authored
Merge pull request #3492 from apostasie/IGNORE
Test rework, part 4
2 parents 73ab789 + 33beb32 commit 6abe279

File tree

72 files changed

+5809
-3332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+5809
-3332
lines changed

cmd/nerdctl/completion/completion_linux_test.go

+26-33
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestCompletion(t *testing.T) {
2828
nerdtest.Setup()
2929

3030
testCase := &test.Case{
31-
Description: "Base completion",
32-
Require: test.Not(nerdtest.Docker),
31+
Require: test.Not(nerdtest.Docker),
3332
Setup: func(data test.Data, helpers test.Helpers) {
3433
helpers.Ensure("pull", testutil.AlpineImage)
3534
helpers.Ensure("network", "create", data.Identifier())
@@ -43,37 +42,37 @@ func TestCompletion(t *testing.T) {
4342
SubTests: []*test.Case{
4443
{
4544
Description: "--cgroup-manager",
46-
Command: test.RunCommand("__complete", "--cgroup-manager", ""),
45+
Command: test.Command("__complete", "--cgroup-manager", ""),
4746
Expected: test.Expects(0, nil, test.Contains("cgroupfs\n")),
4847
},
4948
{
5049
Description: "--snapshotter",
51-
Command: test.RunCommand("__complete", "--snapshotter", ""),
50+
Command: test.Command("__complete", "--snapshotter", ""),
5251
Expected: test.Expects(0, nil, test.Contains("native\n")),
5352
},
5453
{
5554
Description: "empty",
56-
Command: test.RunCommand("__complete", ""),
55+
Command: test.Command("__complete", ""),
5756
Expected: test.Expects(0, nil, test.Contains("run\t")),
5857
},
5958
{
6059
Description: "run -",
61-
Command: test.RunCommand("__complete", "run", "-"),
60+
Command: test.Command("__complete", "run", "-"),
6261
Expected: test.Expects(0, nil, test.Contains("--network\t")),
6362
},
6463
{
6564
Description: "run --n",
66-
Command: test.RunCommand("__complete", "run", "--n"),
65+
Command: test.Command("__complete", "run", "--n"),
6766
Expected: test.Expects(0, nil, test.Contains("--network\t")),
6867
},
6968
{
7069
Description: "run --ne",
71-
Command: test.RunCommand("__complete", "run", "--ne"),
70+
Command: test.Command("__complete", "run", "--ne"),
7271
Expected: test.Expects(0, nil, test.Contains("--network\t")),
7372
},
7473
{
7574
Description: "run --net",
76-
Command: test.RunCommand("__complete", "run", "--net", ""),
75+
Command: test.Command("__complete", "run", "--net", ""),
7776
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
7877
return &test.Expected{
7978
Output: test.All(
@@ -85,7 +84,7 @@ func TestCompletion(t *testing.T) {
8584
},
8685
{
8786
Description: "run -it --net",
88-
Command: test.RunCommand("__complete", "run", "-it", "--net", ""),
87+
Command: test.Command("__complete", "run", "-it", "--net", ""),
8988
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
9089
return &test.Expected{
9190
Output: test.All(
@@ -97,7 +96,7 @@ func TestCompletion(t *testing.T) {
9796
},
9897
{
9998
Description: "run -ti --rm --net",
100-
Command: test.RunCommand("__complete", "run", "-it", "--rm", "--net", ""),
99+
Command: test.Command("__complete", "run", "-it", "--rm", "--net", ""),
101100
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
102101
return &test.Expected{
103102
Output: test.All(
@@ -109,12 +108,12 @@ func TestCompletion(t *testing.T) {
109108
},
110109
{
111110
Description: "run --restart",
112-
Command: test.RunCommand("__complete", "run", "--restart", ""),
111+
Command: test.Command("__complete", "run", "--restart", ""),
113112
Expected: test.Expects(0, nil, test.Contains("always\n")),
114113
},
115114
{
116115
Description: "network --rm",
117-
Command: test.RunCommand("__complete", "network", "rm", ""),
116+
Command: test.Command("__complete", "network", "rm", ""),
118117
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
119118
return &test.Expected{
120119
Output: test.All(
@@ -126,15 +125,15 @@ func TestCompletion(t *testing.T) {
126125
},
127126
{
128127
Description: "run --cap-add",
129-
Command: test.RunCommand("__complete", "run", "--cap-add", ""),
128+
Command: test.Command("__complete", "run", "--cap-add", ""),
130129
Expected: test.Expects(0, nil, test.All(
131130
test.Contains("sys_admin\n"),
132131
test.DoesNotContain("CAP_SYS_ADMIN\n"),
133132
)),
134133
},
135134
{
136135
Description: "volume inspect",
137-
Command: test.RunCommand("__complete", "volume", "inspect", ""),
136+
Command: test.Command("__complete", "volume", "inspect", ""),
138137
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
139138
return &test.Expected{
140139
Output: test.Contains(data.Get("identifier") + "\n"),
@@ -143,7 +142,7 @@ func TestCompletion(t *testing.T) {
143142
},
144143
{
145144
Description: "volume rm",
146-
Command: test.RunCommand("__complete", "volume", "rm", ""),
145+
Command: test.Command("__complete", "volume", "rm", ""),
147146
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
148147
return &test.Expected{
149148
Output: test.Contains(data.Get("identifier") + "\n"),
@@ -152,52 +151,46 @@ func TestCompletion(t *testing.T) {
152151
},
153152
{
154153
Description: "no namespace --cgroup-manager",
155-
Command: func(data test.Data, helpers test.Helpers) test.Command {
156-
cmd := helpers.Command()
157-
cmd.Clear()
158-
cmd.WithBinary("nerdctl")
159-
cmd.WithArgs("__complete", "--cgroup-manager", "")
160-
return cmd
154+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
155+
return helpers.Custom("nerdctl", "__complete", "--cgroup-manager", "")
161156
},
162157
Expected: test.Expects(0, nil, test.Contains("cgroupfs\n")),
163158
},
164159
{
165160
Description: "no namespace empty",
166-
Command: func(data test.Data, helpers test.Helpers) test.Command {
167-
return helpers.Command().Clear().WithBinary("nerdctl").WithArgs("__complete", "")
161+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
162+
return helpers.Custom("nerdctl", "__complete", "")
168163
},
169164
Expected: test.Expects(0, nil, test.Contains("run\t")),
170165
},
171166
{
172167
Description: "namespace space empty",
173-
Command: func(data test.Data, helpers test.Helpers) test.Command {
168+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
174169
// mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
175-
return helpers.Command().Clear().WithBinary("nerdctl").
176-
WithArgs("__complete", "--namespace", testutil.Namespace, "")
170+
return helpers.Custom("nerdctl", "__complete", "--namespace", string(helpers.Read(nerdtest.Namespace)), "")
177171
},
178172
Expected: test.Expects(0, nil, test.Contains("run\t")),
179173
},
180174
{
181175
Description: "run -i",
182-
Command: test.RunCommand("__complete", "run", "-i", ""),
176+
Command: test.Command("__complete", "run", "-i", ""),
183177
Expected: test.Expects(0, nil, test.Contains(testutil.AlpineImage)),
184178
},
185179
{
186180
Description: "run -it",
187-
Command: test.RunCommand("__complete", "run", "-it", ""),
181+
Command: test.Command("__complete", "run", "-it", ""),
188182
Expected: test.Expects(0, nil, test.Contains(testutil.AlpineImage)),
189183
},
190184
{
191185
Description: "run -it --rm",
192-
Command: test.RunCommand("__complete", "run", "-it", "--rm", ""),
186+
Command: test.Command("__complete", "run", "-it", "--rm", ""),
193187
Expected: test.Expects(0, nil, test.Contains(testutil.AlpineImage)),
194188
},
195189
{
196190
Description: "namespace run -i",
197-
Command: func(data test.Data, helpers test.Helpers) test.Command {
191+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
198192
// mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
199-
return helpers.Command().Clear().WithBinary("nerdctl").
200-
WithArgs("__complete", "--namespace", testutil.Namespace, "run", "-i", "")
193+
return helpers.Custom("nerdctl", "__complete", "--namespace", string(helpers.Read(nerdtest.Namespace)), "run", "-i", "")
201194
},
202195
Expected: test.Expects(0, nil, test.Contains(testutil.AlpineImage+"\n")),
203196
},

cmd/nerdctl/container/container_create_linux_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ func TestCreateWithTty(t *testing.T) {
185185

186186
// TestIssue2993 tests https://github.com/containerd/nerdctl/issues/2993
187187
func TestIssue2993(t *testing.T) {
188-
testutil.DockerIncompatible(t)
188+
testCase := nerdtest.Setup()
189189

190-
nerdtest.Setup()
190+
testCase.Require = test.Not(nerdtest.Docker)
191191

192192
const (
193193
containersPathKey = "containersPath"
@@ -203,7 +203,7 @@ func TestIssue2993(t *testing.T) {
203203
return h
204204
}
205205

206-
testCase := &test.Group{
206+
testCase.SubTests = []*test.Case{
207207
{
208208
Description: "Issue #2993 - nerdctl no longer leaks containers and etchosts directories and files when container creation fails.",
209209
Setup: func(data test.Data, helpers test.Helpers) {
@@ -233,7 +233,7 @@ func TestIssue2993(t *testing.T) {
233233
Cleanup: func(data test.Data, helpers test.Helpers) {
234234
helpers.Anyhow("rm", "--data-root", data.TempDir(), "-f", data.Identifier())
235235
},
236-
Command: func(data test.Data, helpers test.Helpers) test.Command {
236+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
237237
return helpers.Command("run", "--data-root", data.TempDir(), "--name", data.Identifier(), "-d", testutil.AlpineImage, "sleep", "infinity")
238238
},
239239
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
@@ -281,7 +281,7 @@ func TestIssue2993(t *testing.T) {
281281
Cleanup: func(data test.Data, helpers test.Helpers) {
282282
helpers.Anyhow("--data-root", data.TempDir(), "rm", "-f", data.Identifier())
283283
},
284-
Command: func(data test.Data, helpers test.Helpers) test.Command {
284+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
285285
return helpers.Command("--data-root", data.TempDir(), "rm", "-f", data.Identifier())
286286
},
287287
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {

cmd/nerdctl/helpers/testing.go

+105
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@
1717
package helpers
1818

1919
import (
20+
"context"
21+
"encoding/json"
22+
"errors"
23+
"fmt"
2024
"os"
25+
"os/exec"
2126
"path/filepath"
2227
"testing"
2328

2429
"gotest.tools/v3/assert"
30+
31+
containerd "github.com/containerd/containerd/v2/client"
32+
"github.com/containerd/containerd/v2/core/content"
33+
34+
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
35+
"github.com/containerd/nerdctl/v2/pkg/testutil"
2536
)
2637

2738
func CreateBuildContext(t *testing.T, dockerfile string) string {
@@ -30,3 +41,97 @@ func CreateBuildContext(t *testing.T, dockerfile string) string {
3041
assert.NilError(t, err)
3142
return tmpDir
3243
}
44+
45+
func RmiAll(base *testutil.Base) {
46+
base.T.Logf("Pruning images")
47+
imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
48+
// remove empty output line at the end
49+
imageIDs = imageIDs[:len(imageIDs)-1]
50+
// use `Run` on purpose (same below) because `rmi all` may fail on individual
51+
// image id that has an expected running container (e.g. a registry)
52+
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()
53+
54+
base.T.Logf("Pruning build caches")
55+
if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil {
56+
base.Cmd("builder", "prune", "--force").AssertOK()
57+
}
58+
59+
// For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs
60+
// https://github.com/containerd/nerdctl/pull/1833
61+
if base.Target == testutil.Nerdctl {
62+
base.T.Logf("Pruning all content blobs")
63+
addr := base.ContainerdAddress()
64+
client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace))
65+
assert.NilError(base.T, err)
66+
cs := client.ContentStore()
67+
ctx := context.TODO()
68+
wf := func(info content.Info) error {
69+
base.T.Logf("Pruning blob %+v", info)
70+
if err := cs.Delete(ctx, info.Digest); err != nil {
71+
base.T.Log(err)
72+
}
73+
return nil
74+
}
75+
if err := cs.Walk(ctx, wf); err != nil {
76+
base.T.Log(err)
77+
}
78+
79+
base.T.Logf("Pruning all images (again?)")
80+
imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
81+
base.T.Logf("pruning following images: %+v", imageIDs)
82+
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()
83+
}
84+
}
85+
86+
func ExtractDockerArchive(archiveTarPath, rootfsPath string) error {
87+
if err := os.MkdirAll(rootfsPath, 0755); err != nil {
88+
return err
89+
}
90+
workDir, err := os.MkdirTemp("", "extract-docker-archive")
91+
if err != nil {
92+
return err
93+
}
94+
defer os.RemoveAll(workDir)
95+
if err := ExtractTarFile(workDir, archiveTarPath); err != nil {
96+
return err
97+
}
98+
manifestJSONPath := filepath.Join(workDir, "manifest.json")
99+
manifestJSONBytes, err := os.ReadFile(manifestJSONPath)
100+
if err != nil {
101+
return err
102+
}
103+
var mani DockerArchiveManifestJSON
104+
if err := json.Unmarshal(manifestJSONBytes, &mani); err != nil {
105+
return err
106+
}
107+
if len(mani) > 1 {
108+
return fmt.Errorf("multi-image archive cannot be extracted: contains %d images", len(mani))
109+
}
110+
if len(mani) < 1 {
111+
return errors.New("invalid archive")
112+
}
113+
ent := mani[0]
114+
for _, l := range ent.Layers {
115+
layerTarPath := filepath.Join(workDir, l)
116+
if err := ExtractTarFile(rootfsPath, layerTarPath); err != nil {
117+
return err
118+
}
119+
}
120+
return nil
121+
}
122+
123+
type DockerArchiveManifestJSON []DockerArchiveManifestJSONEntry
124+
125+
type DockerArchiveManifestJSONEntry struct {
126+
Config string
127+
RepoTags []string
128+
Layers []string
129+
}
130+
131+
func ExtractTarFile(dirPath, tarFilePath string) error {
132+
cmd := exec.Command("tar", "Cxf", dirPath, tarFilePath)
133+
if out, err := cmd.CombinedOutput(); err != nil {
134+
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
135+
}
136+
return nil
137+
}

0 commit comments

Comments
 (0)