Skip to content

Commit 09f3b20

Browse files
committed
image: add unit tests for image import command
add unit tests for image import command Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 0930112 commit 09f3b20

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package image
18+
19+
import (
20+
"archive/tar"
21+
"bytes"
22+
"errors"
23+
"net/http"
24+
"net/http/httptest"
25+
"os"
26+
"path/filepath"
27+
"strings"
28+
"testing"
29+
30+
"gotest.tools/v3/assert"
31+
32+
"github.com/containerd/nerdctl/mod/tigron/expect"
33+
"github.com/containerd/nerdctl/mod/tigron/require"
34+
"github.com/containerd/nerdctl/mod/tigron/test"
35+
"github.com/containerd/nerdctl/mod/tigron/tig"
36+
37+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
38+
)
39+
40+
// minimalRootfsTar returns a valid tar archive with no files.
41+
func minimalRootfsTar(t *testing.T) *bytes.Buffer {
42+
t.Helper()
43+
buf := new(bytes.Buffer)
44+
tw := tar.NewWriter(buf)
45+
assert.NilError(t, tw.Close())
46+
return buf
47+
}
48+
49+
func TestImageImportErrors(t *testing.T) {
50+
nerdtest.Setup()
51+
52+
testCase := &test.Case{
53+
Description: "TestImageImportErrors",
54+
Require: require.Linux,
55+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
56+
return helpers.Command("import", "", "image:tag")
57+
},
58+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
59+
return &test.Expected{
60+
ExitCode: 1,
61+
Errors: []error{errors.New(data.Labels().Get("error"))},
62+
}
63+
},
64+
Data: test.WithLabels(map[string]string{
65+
"error": "no such file or directory",
66+
}),
67+
}
68+
69+
testCase.Run(t)
70+
}
71+
72+
func TestImageImport(t *testing.T) {
73+
testCase := nerdtest.Setup()
74+
75+
var urlServer *httptest.Server
76+
77+
testCase.SubTests = []*test.Case{
78+
{
79+
Description: "image import from stdin",
80+
Cleanup: func(data test.Data, helpers test.Helpers) {
81+
helpers.Anyhow("rmi", "-f", data.Identifier())
82+
},
83+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
84+
cmd := helpers.Command("import", "-", data.Identifier())
85+
cmd.Feed(bytes.NewReader(minimalRootfsTar(t).Bytes()))
86+
return cmd
87+
},
88+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
89+
identifier := data.Identifier()
90+
return &test.Expected{
91+
Output: expect.All(
92+
func(stdout string, t tig.T) {
93+
imgs := helpers.Capture("images")
94+
assert.Assert(t, strings.Contains(imgs, identifier))
95+
},
96+
),
97+
}
98+
},
99+
},
100+
{
101+
Description: "image import from file",
102+
Cleanup: func(data test.Data, helpers test.Helpers) {
103+
helpers.Anyhow("rmi", "-f", data.Identifier())
104+
},
105+
Setup: func(data test.Data, helpers test.Helpers) {
106+
p := filepath.Join(data.Temp().Path(), "rootfs.tar")
107+
assert.NilError(t, os.WriteFile(p, minimalRootfsTar(t).Bytes(), 0644))
108+
data.Labels().Set("tar", p)
109+
},
110+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
111+
return helpers.Command("import", data.Labels().Get("tar"), data.Identifier())
112+
},
113+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
114+
identifier := data.Identifier()
115+
return &test.Expected{
116+
Output: expect.All(
117+
func(stdout string, t tig.T) {
118+
imgs := helpers.Capture("images")
119+
assert.Assert(t, strings.Contains(imgs, identifier))
120+
},
121+
),
122+
}
123+
},
124+
},
125+
{
126+
Description: "image import with message",
127+
Cleanup: func(data test.Data, helpers test.Helpers) {
128+
helpers.Anyhow("rmi", "-f", data.Identifier())
129+
},
130+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
131+
cmd := helpers.Command("import", "-m", "A message", "-", data.Identifier())
132+
cmd.Feed(bytes.NewReader(minimalRootfsTar(t).Bytes()))
133+
return cmd
134+
},
135+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
136+
identifier := data.Identifier() + ":latest"
137+
return &test.Expected{
138+
Output: expect.All(
139+
func(stdout string, t tig.T) {
140+
img := nerdtest.InspectImage(helpers, identifier)
141+
assert.Equal(t, img.Comment, "A message")
142+
},
143+
),
144+
}
145+
},
146+
},
147+
{
148+
Description: "image import with platform",
149+
Cleanup: func(data test.Data, helpers test.Helpers) {
150+
helpers.Anyhow("rmi", "-f", data.Identifier())
151+
},
152+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
153+
cmd := helpers.Command("import", "--platform", "linux/amd64", "-", data.Identifier())
154+
cmd.Feed(bytes.NewReader(minimalRootfsTar(t).Bytes()))
155+
return cmd
156+
},
157+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
158+
identifier := data.Identifier() + ":latest"
159+
return &test.Expected{
160+
Output: expect.All(
161+
func(stdout string, t tig.T) {
162+
img := nerdtest.InspectImage(helpers, identifier)
163+
assert.Equal(t, img.Architecture, "amd64")
164+
assert.Equal(t, img.Os, "linux")
165+
},
166+
),
167+
}
168+
},
169+
},
170+
{
171+
Description: "image import from URL",
172+
Cleanup: func(data test.Data, helpers test.Helpers) {
173+
if urlServer != nil {
174+
urlServer.Close()
175+
}
176+
helpers.Anyhow("rmi", "-f", data.Identifier())
177+
},
178+
Setup: func(data test.Data, helpers test.Helpers) {
179+
urlServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
180+
w.Header().Set("Content-Type", "application/x-tar")
181+
_, _ = w.Write(minimalRootfsTar(t).Bytes())
182+
}))
183+
data.Labels().Set("url", urlServer.URL)
184+
},
185+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
186+
return helpers.Command("import", data.Labels().Get("url"), data.Identifier())
187+
},
188+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
189+
identifier := data.Identifier()
190+
return &test.Expected{
191+
Output: expect.All(
192+
func(stdout string, t tig.T) {
193+
imgs := helpers.Capture("images")
194+
assert.Assert(t, strings.Contains(imgs, identifier))
195+
},
196+
),
197+
}
198+
},
199+
},
200+
}
201+
testCase.Run(t)
202+
}

0 commit comments

Comments
 (0)