|
| 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 | +} |
0 commit comments