-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test rework, part 4 #3492
Test rework, part 4 #3492
Changes from all commits
0e35a53
2d2afe0
0fafd1a
91f4eb4
7190bc6
157d183
4451d75
33beb32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,22 @@ | |
package helpers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
containerd "github.com/containerd/containerd/v2/client" | ||
"github.com/containerd/containerd/v2/core/content" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/buildkitutil" | ||
"github.com/containerd/nerdctl/v2/pkg/testutil" | ||
) | ||
|
||
func CreateBuildContext(t *testing.T, dockerfile string) string { | ||
|
@@ -30,3 +41,97 @@ func CreateBuildContext(t *testing.T, dockerfile string) string { | |
assert.NilError(t, err) | ||
return tmpDir | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As explained in the commit message, we do currently have a mix of platform specific test files (eg: _platform_test - along with dependencies that may be platform limited too) and tests that call These helpers here are moved around into the main helper file so that we can reduce a little bit the platform specific test files. Either way, all of that has nothing to do here - unless they are used in live code, they should be ultimately moved to |
||
func RmiAll(base *testutil.Base) { | ||
base.T.Logf("Pruning images") | ||
imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() | ||
// remove empty output line at the end | ||
imageIDs = imageIDs[:len(imageIDs)-1] | ||
// use `Run` on purpose (same below) because `rmi all` may fail on individual | ||
// image id that has an expected running container (e.g. a registry) | ||
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() | ||
|
||
base.T.Logf("Pruning build caches") | ||
if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil { | ||
base.Cmd("builder", "prune", "--force").AssertOK() | ||
} | ||
|
||
// For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs | ||
// https://github.com/containerd/nerdctl/pull/1833 | ||
if base.Target == testutil.Nerdctl { | ||
base.T.Logf("Pruning all content blobs") | ||
addr := base.ContainerdAddress() | ||
client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace)) | ||
assert.NilError(base.T, err) | ||
cs := client.ContentStore() | ||
ctx := context.TODO() | ||
wf := func(info content.Info) error { | ||
base.T.Logf("Pruning blob %+v", info) | ||
if err := cs.Delete(ctx, info.Digest); err != nil { | ||
base.T.Log(err) | ||
} | ||
return nil | ||
} | ||
if err := cs.Walk(ctx, wf); err != nil { | ||
base.T.Log(err) | ||
} | ||
|
||
base.T.Logf("Pruning all images (again?)") | ||
imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() | ||
base.T.Logf("pruning following images: %+v", imageIDs) | ||
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() | ||
} | ||
} | ||
|
||
func ExtractDockerArchive(archiveTarPath, rootfsPath string) error { | ||
if err := os.MkdirAll(rootfsPath, 0755); err != nil { | ||
return err | ||
} | ||
workDir, err := os.MkdirTemp("", "extract-docker-archive") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use t.TempDir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, all of that stuff inside |
||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(workDir) | ||
if err := ExtractTarFile(workDir, archiveTarPath); err != nil { | ||
return err | ||
} | ||
manifestJSONPath := filepath.Join(workDir, "manifest.json") | ||
manifestJSONBytes, err := os.ReadFile(manifestJSONPath) | ||
if err != nil { | ||
return err | ||
} | ||
var mani DockerArchiveManifestJSON | ||
if err := json.Unmarshal(manifestJSONBytes, &mani); err != nil { | ||
return err | ||
} | ||
if len(mani) > 1 { | ||
return fmt.Errorf("multi-image archive cannot be extracted: contains %d images", len(mani)) | ||
} | ||
if len(mani) < 1 { | ||
return errors.New("invalid archive") | ||
} | ||
ent := mani[0] | ||
for _, l := range ent.Layers { | ||
layerTarPath := filepath.Join(workDir, l) | ||
if err := ExtractTarFile(rootfsPath, layerTarPath); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type DockerArchiveManifestJSON []DockerArchiveManifestJSONEntry | ||
|
||
type DockerArchiveManifestJSONEntry struct { | ||
Config string | ||
RepoTags []string | ||
Layers []string | ||
} | ||
|
||
func ExtractTarFile(dirPath, tarFilePath string) error { | ||
cmd := exec.Command("tar", "Cxf", dirPath, tarFilePath) | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leverage new
Custom
for much simpler syntax.