Skip to content
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

e2e: Allow ImageCacheDir to be changed with an environment variable #17089

Closed
wants to merge 1 commit into from

Conversation

sstosh
Copy link
Contributor

@sstosh sstosh commented Jan 12, 2023

  • We can change the ImageCacheDir with TMPDIR.

  • Reduce dependency on /tmp for e2e tests.

  • When a rootless user, the cache directory is failed to
    remove due to a permission. Therefore, we use
    podman unshare rm to remove the cache.

Signed-off-by: Toshiki Sonoda [email protected]

Does this PR introduce a user-facing change?

None

@openshift-ci openshift-ci bot added release-note-none do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jan 12, 2023
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jan 12, 2023

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sstosh
Once this PR has been reviewed and has the lgtm label, please assign flouthoc for approval by writing /assign @flouthoc in a comment. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sstosh sstosh force-pushed the e2e-image-cache branch 7 times, most recently from 70f62ed to b197be5 Compare January 16, 2023 00:26
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Changes LGTM with one question.

@edsantiago PTAL

@@ -188,6 +188,7 @@ var _ = Describe("Podman save", func() {

cmd = exec.Command("cp", "sign/key.gpg", "/tmp/key.gpg")
Expect(cmd.Run()).To(Succeed())
defer os.Remove("/tmp/key.gpg")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that also be under cachedir?

Copy link
Contributor Author

@sstosh sstosh Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to fix the keyPath value from /tmp/key.gpg to cachePath("key.gpg") in e2e/sign/policy.json.

"keyPath": "/tmp/key.gpg"

I haven't come up with a best way, so I remove the key.gpg by defer.

Copy link
Member

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of maybe see what problem this is trying to solve, but I think this might not be the best way to do it. Blocking because of two concerns.

@@ -844,9 +847,27 @@ func populateCache(podman *PodmanTestIntegration) {
fmt.Printf("-----------------------------\n")
}

func cachePath(path string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm failing to see how this will use $PODMAN_TEST_IMAGE_CACHE_DIR. Should it?

Copy link
Contributor Author

@sstosh sstosh Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm bit a confused.
ImageCacheDir is hard corded (/tmp/podman/imagecachedir).

ImageCacheDir = "/tmp/podman/imagecachedir"

On the other hand, $PODMAN_TEST_IMAGE_CACHE_DIR also exists and used.

func imageTarPath(image string) string {
cacheDir := os.Getenv("PODMAN_TEST_IMAGE_CACHE_DIR")
if cacheDir == "" {
cacheDir = os.Getenv("TMPDIR")
if cacheDir == "" {
cacheDir = "/tmp"
}
}
// e.g., registry.com/fubar:latest -> registry.com-fubar-latest.tar
imageCacheName := strings.ReplaceAll(strings.ReplaceAll(image, ":", "-"), "/", "-") + ".tar"
return filepath.Join(cacheDir, imageCacheName)
}

I don't know if it is necessary to separate ImageCacheDir and $PODMAN_TEST_IMAGE_CACHE_DIR.
If unnecessary, I think that $PODMAN_TEST_IMAGE_CACHE_DIR can be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long to respond. I think you've hit on the main issue: $PODMAN_TEST_IMAGE_CACHE_DIR has nothing whatsoever to do with ImageCacheDir. It's a confusing similarity. I would suggest leaving the existing $PODMAN_TEST_IMAGE_CACHE_DIR code untouched. The reason that exists is that ginkgo tests pull enormous images, and that takes half an hour on my slow network. I need to cache those locally.

The rest of your PR, though, still needs touchup. Here's what I suggest:

  • leave imageTarPath() untouched
  • Remove lines 845-849, so cachePath() always returns $TMPDIR/arg (defaulting to /tmp) (as you do now)
    • Bonus points if you can get rid of "" requirement, so callers can do cachePath() instead of cachePath(""). The former is more readable.
  • in removeCache(), always remove cachePath("imagedir"). Right now you don't remove it if $TMPDIR is set, and that seems like a logic bug.
    • bonus points if you find a way to remove using podman unshare or some magic namespace code, because that code always always always fails rootless.

Thanks again for your patience and for your contribution.

Copy link
Contributor Author

@sstosh sstosh Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked that the cache directory is failed to remove due to a permission.
When a rootless user, remove the cache with podman unshare rm -rf <cacheDir>.

// Nuke tempdir
if isRootless() {
cmd := exec.Command(p.PodmanBinary, "unshare", "rm", "-rf", p.TempDir)
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
} else {
if err := os.RemoveAll(p.TempDir); err != nil {
fmt.Printf("%q\n", err)
}
}

// Remove cache dirs
imageDir := cachePath("imagedir")
if isRootless() {
cmd := exec.Command(p.PodmanBinary, "unshare", "rm", "-rf", imageDir)
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
} else {
if err := os.RemoveAll(imageDir); err != nil {
fmt.Printf("%q\n", err)
}
}

imageCacheDir = os.Getenv("PODMAN_TEST_IMAGE_CACHE_DIR")
}

if err := os.RemoveAll(imageCacheDir); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too happy with the possibility of someone (inadvertently or not) setting PODMAN_TEST_IMAGE_CACHE_DIR=/tmp (or /etc or even /) and having that be wiped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed a condition for remove a cache directory.

@sstosh sstosh force-pushed the e2e-image-cache branch 3 times, most recently from c34b1e9 to 647fc4a Compare January 18, 2023 02:58
Comment on lines 335 to 336
nonExistentFile := cachePath("nonexistent")
session := podmanTest.Podman([]string{"create", "--authfile", nonExistentFile, "--name=foo", ALPINE})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this, and the other /tmp/nonexistent changes, are overkill.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted cachePath("noexistent") to /tmp/noexistent.

@sstosh sstosh force-pushed the e2e-image-cache branch 4 times, most recently from 3b265fd to ab0e361 Compare January 20, 2023 06:37
@sstosh sstosh marked this pull request as ready for review January 20, 2023 07:53
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 20, 2023
- We can change the ImageCacheDir with `TMPDIR`.

- Reduce dependency on `/tmp` for e2e tests.

- When a rootless user, the cache directory is failed to
remove due to a permission. Therefore, we use
`podman unshare rm` to remove the cache.

Signed-off-by: Toshiki Sonoda <[email protected]>
@edsantiago
Copy link
Member

I'm really sorry, but I can't review this. I've tried three times, Friday and today, and it's too convoluted. This is not your fault -- our test setup overloads too many things in and too many names (image, cache, tmp) -- but the result is, I can't understand this, can't confirm that it has no unintended consequences, and can't approve it.

May I request that you break this down into smaller PRs? Something like:

1) temp/scratch (not cache) files

Add a new function, tempFilePath() or scratchFilePath() or anything that does not say "cache":

func tempFilePath(path string) string {
	return filepath.Join(os.TempDir(), path)
        // even better would be creating a one-time `os.MkdirTemp("", "podman-e2e-tests.*")
        // but that would again lead to a too-complicated PR. Once the code is cleaned up,
        // we can look into that as a future improvement.
}

...and use that new function in checkpoint_test.go and push_test.go. I find it really important to get rid of the word "cache" in these contexts.

(In case someone suggests os.CreateTemp(): no go. That creates the file; we want a path to an uncreated file. The randomization/security/race aspects of CreateTemp() are not important in this test context).

2) image cache

Leave ImageCacheDir as a global, but make it start as nil, and initialize it at runtime to filepath.join(os.TempDir(), "imagecachedir"), Then, in mount_rootless_test.go, do the substring check against ImageCacheDir.

That leaves the delete code, which still scares the heck out of me, but I think with these two changes the delete code will be easier to understand.

Alternatively, perhaps someone else in @containers/podman-maintainers can understand and review and approve this PR as it is. But again, I'm sorry, I cannot.

@sstosh
Copy link
Contributor Author

sstosh commented Jan 24, 2023

Thank you for your review and suggestions.
I'll open the other three PRs later such as below:

  • e2e: Add tempFilePath() for refactoring

  • e2e: Refactoring ImageCacheDir

  • e2e: Remove the cache with "podman unshare rm" when a rootless user

This PR become longer, so I'll close it once.

@sstosh sstosh deleted the e2e-image-cache branch January 30, 2023 14:22
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 13, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants