Skip to content

Commit bd64f3d

Browse files
fix: Add Buildctl cache prune (#1367)
Signed-off-by: Shubharanshu Mahapatra <shubhum@amazon.com>
1 parent 28fbd58 commit bd64f3d

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

e2e/vm/vm_cache_cleanup_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:build darwin
5+
6+
package vm
7+
8+
import (
9+
"fmt"
10+
"strings"
11+
"time"
12+
13+
"github.com/onsi/ginkgo/v2"
14+
"github.com/onsi/gomega"
15+
"github.com/runfinch/common-tests/command"
16+
"github.com/runfinch/common-tests/ffs"
17+
"github.com/runfinch/common-tests/option"
18+
)
19+
20+
var testVMPrune = func(o *option.Option, installed bool) {
21+
ginkgo.Describe("Test System Prune Cleans Up Build Cache, Images, Containers", func() {
22+
ginkgo.It("check prune cleans up build cache, images and containers", func() {
23+
limaCtlO, err := limaCtlOpt(installed)
24+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
25+
26+
command.Stdout(limaCtlO, "shell", "finch", "mount")
27+
setupTestContainer(o)
28+
verifyResourcesExist(o, limaCtlO)
29+
restartVM(o)
30+
verifyResourcesExist(o, limaCtlO)
31+
32+
// Run system prune
33+
pruneOutput := command.StdoutStr(o, "system", "prune", "--all", "--force")
34+
gomega.Expect(strings.TrimSpace(pruneOutput)).ToNot(gomega.Equal(""),
35+
"System prune should produce output indicating resources were cleaned")
36+
37+
// Verify all resources are cleaned up after prune
38+
verifyResourcesCleaned(o, limaCtlO)
39+
})
40+
})
41+
}
42+
43+
func setupTestContainer(o *option.Option) {
44+
alpineImage := "public.ecr.aws/docker/library/alpine:latest"
45+
buildContext := ffs.CreateBuildContext(fmt.Sprintf(`FROM %s
46+
CMD ["echo", "finch-test-dummy-output"]
47+
`, alpineImage))
48+
49+
command.Run(o, "build", "-t", "test:tag", buildContext)
50+
command.Run(o, "run", "--name", "test-container", "test:tag")
51+
// TODO: Fix Issue with nerdctl namestore when we try to delete with prune
52+
// name-store error\ncannot release name \"test-4c8e9\" (used by ID \"\", not by <>
53+
command.Run(o, "container", "rm", "-f", "test-container")
54+
}
55+
56+
func verifyResourcesExist(o *option.Option, limaCtlO *option.Option) {
57+
gomega.Expect(command.StdoutStr(o, "images", "-q")).ToNot(gomega.BeEmpty(),
58+
"Expected at least one image to exist")
59+
buildCacheOutput := command.Stdout(limaCtlO, "shell", "finch", "sudo", "buildctl", "du", "--format=json")
60+
gomega.Expect(strings.TrimSpace(string(buildCacheOutput))).ToNot(gomega.Equal("null"),
61+
"Expected build cache to exist")
62+
}
63+
64+
func restartVM(o *option.Option) {
65+
command.New(o, "vm", "remove", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(20).Run()
66+
time.Sleep(5 * time.Second)
67+
command.New(o, "vm", "init").WithTimeoutInSeconds(160).Run()
68+
}
69+
70+
func verifyResourcesCleaned(o *option.Option, limaCtlO *option.Option) {
71+
gomega.Expect(command.StdoutStr(o, "images", "ls", "-q")).To(gomega.Equal(""),
72+
"Expected no images after prune")
73+
gomega.Expect(command.StdoutStr(o, "container", "ls", "-a", "-q")).To(gomega.Equal(""),
74+
"Expected no containers after prune")
75+
buildCacheOutput := command.Stdout(limaCtlO, "shell", "finch", "sudo", "buildctl", "du", "--format=json")
76+
gomega.Expect(strings.TrimSpace(string(buildCacheOutput))).To(gomega.Equal("null"),
77+
"Expected build cache to be cleaned up after prune")
78+
}

e2e/vm/vm_darwin_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func TestVM(t *testing.T) {
6161
})
6262

6363
ginkgo.Describe("", func() {
64+
testVMPrune(o, *e2e.Installed)
6465
testVMLifecycle(o)
6566
testAdditionalDisk(o, *e2e.Installed)
6667
testConfig(o, *e2e.Installed)

e2e/vm/vm_windows_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func TestVM(t *testing.T) {
4646
}, func() {})
4747

4848
ginkgo.Describe("", func() {
49+
/*
50+
TODO: Fix issue with prune in windows. Error:
51+
failed marshalling json out of network configuration file \"/etc/cni/net.d/nerdctl-bridge.conflist\":
52+
error parsing configuration list:unexpected end of JSON input\nFor details on the schema"
53+
*/
54+
// testVMPrune(o, *e2e.Installed)
4955
testVMLifecycle(o)
5056
testAdditionalDisk(o, *e2e.Installed)
5157
testFinchConfigFile(o)

finch.yaml.d/common.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ provision:
7171
sudo mkdir -p /mnt/lima-finch/soci-snapshotter-grpc/snapshotter/snapshots /var/lib/soci-snapshotter-grpc
7272
sudo mount --bind /mnt/lima-finch/soci-snapshotter-grpc /var/lib/soci-snapshotter-grpc
7373
74+
# Mounting buildkit dir to make the metadata persistent
75+
sudo mkdir -p /mnt/lima-finch/buildkit /var/lib/buildkit
76+
sudo mount --bind /mnt/lima-finch/buildkit /var/lib/buildkit
77+
7478
# Make sure stargz and buildkit are restarted with containerd
7579
sudo mkdir -p /usr/local/lib/systemd/system/buildkit.service.d/
7680
printf '[Unit]\nPartOf=containerd.service\n' | sudo tee /usr/local/lib/systemd/system/buildkit.service.d/finch.conf

0 commit comments

Comments
 (0)