Skip to content

add equals/hashCode and toString to MountableFile#2144

Closed
t1 wants to merge 8 commits into
testcontainers:masterfrom
t1:master
Closed

add equals/hashCode and toString to MountableFile#2144
t1 wants to merge 8 commits into
testcontainers:masterfrom
t1:master

Conversation

@t1

@t1 t1 commented Dec 3, 2019

Copy link
Copy Markdown

MountableFile is used as a key in a HashMap but it doesn't override equals/hashcode.
I didn't experience any problems, but it definitely has potential for screwing up.

And because the change was so trivial, I also added a toString.

@t1
t1 requested review from bsideup, kiview and rnorth as code owners December 3, 2019 13:50
Comment thread core/src/main/java/org/testcontainers/utility/MountableFile.java Outdated
* An abstraction over files and classpath resources aimed at encapsulating all the complexity of generating
* a path that the Docker daemon is about to create a volume mount for.
*/
@EqualsAndHashCode(of = {"path", "forcedFileMode"})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
@EqualsAndHashCode(of = {"path", "forcedFileMode"})
@EqualsAndHashCode(of = "path")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, but wouldn't that be confusing. They are different, after all, if their file mode is different. Otherwise, only the path should be the key.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

They are different, after all, if their file mode is different

they represent the same path. The forced file mode is only a hint that is used to override the one from the file system (but the target object is the same)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't agree, but it's more your code than mine ;-)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I just pushed another update

@bsideup bsideup changed the title add equals/hashCode and toString add equals/hashCode and toString to MountableFile Dec 3, 2019
@t1

t1 commented Dec 3, 2019

Copy link
Copy Markdown
Author

A lot of checks fail, but they don't seem to be related to my change.

Comment thread core/src/test/java/org/testcontainers/utility/MountableFileTest.java Outdated
@rnorth

rnorth commented Dec 5, 2019

Copy link
Copy Markdown
Member

It looks like we've got a real test failure:

    @Test
    public void shouldUseCopyOnlyWithReadOnlyClasspathResources() {
        String resource = "/test_copy_to_container.txt";
        GenericContainer<?> container = new GenericContainer<>()
            .withClasspathResourceMapping(resource, "/readOnly", BindMode.READ_ONLY)
            .withClasspathResourceMapping(resource, "/readOnlyNoSelinux", BindMode.READ_ONLY)

            .withClasspathResourceMapping(resource, "/readOnlyShared", BindMode.READ_ONLY, SelinuxContext.SHARED)
            .withClasspathResourceMapping(resource, "/readWrite", BindMode.READ_WRITE);

        Map<MountableFile, String> copyMap = container.getCopyToFileContainerPathMap();
FAILS-->assertTrue("uses copy for read-only", copyMap.containsValue("/readOnly"));
        assertTrue("uses copy for read-only and no Selinux", copyMap.containsValue("/readOnlyNoSelinux"));

        assertFalse("uses mount for read-only with Selinux", copyMap.containsValue("/readOnlyShared"));
        assertFalse("uses mount for read-write", copyMap.containsValue("/readWrite"));
    }

The copyToFileContainerPathMap is of type Map<MountableFile, String>. Now that it has equals/hashCode it behaves as a key should, and unfortunately copyToFileContainerPathMap ends up containing just one entry.

This doesn't seem right - it should be valid for one file to be copied to many places, so the original code seems to be wrong. It really feels like copyToFileContainerPathMap should be flipped around to be Map<String, MountableFile>. WDYT @bsideup ?

@t1

t1 commented Dec 7, 2019

Copy link
Copy Markdown
Author

@rnorth: ... or it could be a Map<MountableFile, Set<String>>, which would be more clear about the intent.

Actually also the file mode should probably be part of the target, as the same file can end up at multiple locations with different modes, but I guess that would mean a breaking API change.

@rnorth

rnorth commented Dec 14, 2019

Copy link
Copy Markdown
Member

Perhaps we don’t actually need the container to be a map at all..? For our own usage we only care about iterating over the mount points, so this really just needs to be a list internally. De-duplication is not something we have to do; indeed currently we’ll just be letting Docker figure it out.

We don’t have a Triple class yet, but it would essentially be a List<Triple<MountableFile, String, BindMode>> (where the String is the destination path).

To avoid this being a breaking change we could keep the signature of getCopyToFileContainerPathMap as it is, but return an IdentityHashMap representation of the underlying List. I’m a bit sceptical that this method is useful for anything other than our unit tests, but we should avoid accidentally breaking it.

@rnorth

rnorth commented Mar 5, 2020

Copy link
Copy Markdown
Member

@t1 are you interested in continuing with this PR?

@t1

t1 commented Mar 6, 2020

Copy link
Copy Markdown
Author

@rnorth : time is — as always — scarce... and this ticket has exploded. It's grown from implementing equals/hasCode to a redesign (even when it's just a small one). I don't think that I'll be able to squeeze out more time for this. My pain with the issue is just too small. It was more of a remove-some-cruft-issue to me.

Maybe it would be better to merge what we have than to wait for the full change?

@rnorth

rnorth commented May 7, 2020

Copy link
Copy Markdown
Member

@t1 sorry, but I think I'll close: as it is, there's a real test failure, so we can't merge it.

Similarly we're also very time constrained, so I too think that the effort of addressing the comments is not really worth it for a theoretical issue.

Thanks for your attempt to improve things, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants